Skip to content

Commit

Permalink
more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ildyria committed Oct 10, 2024
1 parent b44ca2b commit f8d49a6
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 39 deletions.
30 changes: 0 additions & 30 deletions app/Assets/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use App\Exceptions\Internal\ZeroModuloException;
use Illuminate\Support\Facades\File;
use function Safe\ini_get;
use function Safe\parse_url;

class Helpers
{
Expand Down Expand Up @@ -54,35 +53,6 @@ public function trancateIf32(string $id, int $prevShortId = 0, int $phpMax = PHP
return (string) $shortId;
}

/**
* Returns the extension of the filename (path or URI) or an empty string.
*
* @param string $filename
* @param bool $isURI
*
* @return string extension of the filename starting with a dot
*/
public function getExtension(string $filename, bool $isURI = false): string
{
// If $filename is an URI, get only the path component
if ($isURI === true) {
/** @var string $filename this is true because PHP_URL_PATH is specified */
$filename = parse_url($filename, PHP_URL_PATH);
}

$extension = pathinfo($filename, PATHINFO_EXTENSION);

// Special cases
// https://github.com/electerious/Lychee/issues/482
list($extension) = explode(':', $extension, 2);

if ($extension !== '') {
$extension = '.' . $extension;
}

return $extension;
}

/**
* Check if $path has readable and writable permissions.
*
Expand Down
4 changes: 1 addition & 3 deletions app/Http/Requests/WebAuthn/EditCredentialRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\Http\Requests\BaseApiRequest;
use App\Models\User;
use App\Policies\UserPolicy;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Gate;
use Laragear\WebAuthn\Models\WebAuthnCredential;

Expand All @@ -20,8 +19,7 @@ class EditCredentialRequest extends BaseApiRequest
*/
public function authorize(): bool
{
return Gate::check(UserPolicy::CAN_EDIT, [User::class]) &&
($this->credential->user_id === Auth::id());
return Gate::check(UserPolicy::CAN_EDIT, [User::class]);
}

public function rules(): array
Expand Down
2 changes: 1 addition & 1 deletion config/filesystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ function renv_cond(string $cst): string
'dist' => [
'driver' => 'local',
'root' => env('LYCHEE_DIST', public_path('dist/')),
'url' => env('LYCHEE_DIST_URL', 'dist/'),
'url' => env('LYCHEE_DIST_URL', '/dist/'),
'visibility' => 'public',
],

Expand Down
3 changes: 2 additions & 1 deletion routes/api_v2.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
*/
Route::get('/WebAuthn', [WebAuthn\WebAuthnManageController::class, 'list']);
Route::patch('/WebAuthn', [WebAuthn\WebAuthnManageController::class, 'edit']);
Route::post('/WebAuthn::delete', [WebAuthn\WebAuthnManageController::class, 'delete']);
Route::delete('/WebAuthn', [WebAuthn\WebAuthnManageController::class, 'delete']);

// Special Webauthn operations
Route::post('/WebAuthn::register/options', [WebAuthn\WebAuthnRegisterController::class, 'options'])
Expand All @@ -164,6 +164,7 @@
// If Authenticated: list of the registrated Oauth providers
// If not Authenticated: list of the available Oauth providers
Route::get('/Oauth', [OauthController::class, 'list']);
Route::delete('/Oauth', [OauthController::class, 'clear']);

/**
* DIAGNOSTICS.
Expand Down
6 changes: 6 additions & 0 deletions tests/Feature_v2/Oauth/OauthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,17 @@ public function testGetAnonymous(): void
$this->assertOk($response);
$response->assertJson(['github']);

$response = $this->deleteJson('Oauth', ['provider' => 'github']);
$this->assertUnauthorized($response);

$response = $this->actingAs($this->userMayUpload1)->getJson('Oauth');
$this->assertOk($response);
$response->assertJson([[
'providerType' => 'github',
'isEnabled' => false,
]]);

$response = $this->actingAs($this->userMayUpload1)->deleteJson('Oauth', ['provider' => 'github']);
$this->assertNoContent($response);
}
}
19 changes: 15 additions & 4 deletions tests/Feature_v2/WebAuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -388,11 +388,14 @@ public function testWebAuthListingUnautorized(): void
*
* @return void
*/
public function testWebAuthDeleteUnautorized(): void
public function testWebAuthEditDeleteUnautorized(): void
{
$this->createCredentials();

$responseDelete = $this->postJson('WebAuthn::delete', ['id' => '_Xlz-khgFhDdkvOWyy_YqC54ExkYyp1o6HAQiybqLST-9RGBndpgI06TQygIYI7ZL2dayCMYm6J1-bXyl72obA']);
$responseEdit = $this->patchJson('WebAuthn', ['id' => '_Xlz-khgFhDdkvOWyy_YqC54ExkYyp1o6HAQiybqLST-9RGBndpgI06TQygIYI7ZL2dayCMYm6J1-bXyl72obA', 'alias' => 'something']);
$this->assertUnauthorized($responseEdit);

$responseDelete = $this->deleteJson('WebAuthn', ['id' => '_Xlz-khgFhDdkvOWyy_YqC54ExkYyp1o6HAQiybqLST-9RGBndpgI06TQygIYI7ZL2dayCMYm6J1-bXyl72obA']);
$this->assertUnauthorized($responseDelete);
}

Expand All @@ -401,12 +404,20 @@ public function testWebAuthDeleteUnautorized(): void
*
* @return void
*/
public function testWebAuthDeleteAutorized(): void
public function testWebAuthEditDeleteAutorized(): void
{
$this->createCredentials();

Auth::loginUsingId($this->admin->id);
$responseDelete = $this->postJson('WebAuthn::delete', ['id' => '_Xlz-khgFhDdkvOWyy_YqC54ExkYyp1o6HAQiybqLST-9RGBndpgI06TQygIYI7ZL2dayCMYm6J1-bXyl72obA']);

$responseEdit = $this->patchJson('WebAuthn', ['id' => '_Xlz-khgFhDdkvOWyy_YqC54ExkYyp1o6HAQiybqLST-9RGBndpgI06TQygIYI7ZL2dayCMYm6J1-bXyl72obA', 'alias' => 'something']);
$this->assertNoContent($responseEdit);

$responseList = $this->getJson('WebAuthn');
$this->assertOk($responseList);
$this->assertEquals('something', $responseList->json()[0]['alias']);

$responseDelete = $this->deleteJson('WebAuthn', ['id' => '_Xlz-khgFhDdkvOWyy_YqC54ExkYyp1o6HAQiybqLST-9RGBndpgI06TQygIYI7ZL2dayCMYm6J1-bXyl72obA']);
$this->assertNoContent($responseDelete);
Auth::logout();
Session::flush();
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/FeaturesUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

/**
* We don't care for unhandled exceptions in tests.
* It is the nature of a test to throw an exception.
* Without this suppression we had 100+ Linter warning in this file which
* don't help anything.
*
* @noinspection PhpDocMissingThrowsInspection
* @noinspection PhpUnhandledExceptionInspection
*/

namespace Tests\Unit;

use App\Assets\Features;
use App\Exceptions\Internal\FeaturesDoesNotExistsException;
use Tests\AbstractTestCase;

class FeaturesUnitTest extends AbstractTestCase
{
public function testAllAreActive(): void
{
$this->assertFalse(Features::allAreActive(['use-s3']));
$this->assertTrue(Features::allAreActive(['vuejs']));
}

public function testSomeAreActive(): void
{
$this->assertFalse(Features::someAreActive(['use-s3']));
$this->assertTrue(Features::someAreActive(['vuejs', 'use-s3']));
}

public function testAllAreInactive(): void
{
$this->assertTrue(Features::allAreInactive(['use-s3']));
$this->assertFalse(Features::allAreInactive(['vuejs']));
}

public function testSomeAreInactive(): void
{
$this->assertFalse(Features::someAreInactive(['vuejs']));
$this->assertTrue(Features::someAreInactive(['vuejs', 'use-s3']));
}

public function testWhenArray(): void
{
$this->assertTrue(Features::when(['vuejs'], fn () => true, fn () => false));
}

public function testThrow(): void
{
$this->expectException(FeaturesDoesNotExistsException::class);
Features::active('livewire');
}
}

0 comments on commit f8d49a6

Please sign in to comment.