Fix & Complete tests

This commit is contained in:
Bubka 2024-07-06 16:05:04 +02:00
parent 17256c6788
commit 4e463a781b
6 changed files with 68 additions and 8 deletions

View File

@ -47,11 +47,12 @@ public function user()
$remoteUserHeader = config('auth.auth_proxy_headers.user');
$remoteUserHeader = $remoteUserHeader ?: 'REMOTE_USER';
$identifier = [];
$identifier['id'] = null;
try {
$identifier['id'] = request()->server($remoteUserHeader) ?? apache_request_headers()[$remoteUserHeader] ?? null;
} catch (\Throwable $e) {
$identifier['id'] = null;
} catch (\Throwable $e) { //@codeCoverageIgnore
// Do nothing
}
if (! $identifier['id'] || is_array($identifier['id'])) {

View File

@ -590,7 +590,7 @@ class MigrationTestData
"account": "' . OtpTestData::ACCOUNT . '",
"service": "' . OtpTestData::SERVICE . '",
"icon": "' . OtpTestData::ICON_JPEG . '",
"icon_mime": "image\/svg+xml",
"icon_mime": "image\/jpeg",
"icon_file": "' . OtpTestData::ICON_JPEG_DATA . '",
"secret": "' . OtpTestData::SECRET . '",
"digits": ' . OtpTestData::DIGITS_CUSTOM . ',
@ -614,7 +614,7 @@ class MigrationTestData
"account": "' . OtpTestData::ACCOUNT . '",
"service": "' . OtpTestData::SERVICE . '",
"icon": "' . OtpTestData::ICON_PNG . '",
"icon_mime": "image\/svg+xml",
"icon_mime": "image\/png",
"icon_file": "' . OtpTestData::ICON_PNG_DATA . '",
"secret": "' . OtpTestData::SECRET . '",
"digits": ' . OtpTestData::DIGITS_CUSTOM . ',
@ -638,7 +638,31 @@ class MigrationTestData
"account": "' . OtpTestData::ACCOUNT . '",
"service": "' . OtpTestData::SERVICE . '",
"icon": "' . OtpTestData::ICON_BMP . '",
"icon_mime": "image\/svg+xml",
"icon_mime": "image\/bmp",
"icon_file": "' . OtpTestData::ICON_BMP_DATA . '",
"secret": "' . OtpTestData::SECRET . '",
"digits": ' . OtpTestData::DIGITS_CUSTOM . ',
"algorithm": "' . OtpTestData::ALGORITHM_CUSTOM . '",
"period": ' . OtpTestData::PERIOD_CUSTOM . ',
"counter": null,
"legacy_uri": "' . OtpTestData::TOTP_FULL_CUSTOM_URI_NO_IMG . '"
}
]
}';
const VALID_2FAUTH_JSON_MIGRATION_PAYLOAD_WITH_XBMP_ICON = '
{
"app": "2fauth_v3.4.1",
"schema": 1,
"datetime": "2022-12-14T14:53:06.173939Z",
"data":
[
{
"otp_type": "totp",
"account": "' . OtpTestData::ACCOUNT . '",
"service": "' . OtpTestData::SERVICE . '",
"icon": "' . OtpTestData::ICON_BMP . '",
"icon_mime": "image\/x-ms-bmp",
"icon_file": "' . OtpTestData::ICON_BMP_DATA . '",
"secret": "' . OtpTestData::SECRET . '",
"digits": ' . OtpTestData::DIGITS_CUSTOM . ',
@ -662,7 +686,7 @@ class MigrationTestData
"account": "' . OtpTestData::ACCOUNT . '",
"service": "' . OtpTestData::SERVICE . '",
"icon": "' . OtpTestData::ICON_WEBP . '",
"icon_mime": "image\/svg+xml",
"icon_mime": "image\/webp",
"icon_file": "' . OtpTestData::ICON_WEBP_DATA . '",
"secret": "' . OtpTestData::SECRET . '",
"digits": ' . OtpTestData::DIGITS_CUSTOM . ',

View File

@ -3,6 +3,7 @@
namespace Tests\Feature;
use App\Providers\RouteServiceProvider;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Route;
use PHPUnit\Framework\Attributes\CoversClass;
@ -27,10 +28,24 @@ public function test_exception_handler_with_web_route()
#[Test]
public function test_all_api_routes_are_behind_apiv1_middleware()
{
Artisan::call('route:clear');
Artisan::call('route:cache');
$this->get('/');
$routes = Route::getRoutes();
foreach ($routes as $route) {
$middlewares = Route::gatherRouteMiddleware($route);
$middlewares = [];
try {
$uri = $route->uri;
$middlewares = Route::gatherRouteMiddleware($route);
}
catch (\Exception $ex)
{
$uri = $route->uri;
//return;
}
if (Str::startsWith($route->uri(), self::API_ROUTE_PREFIX)) {
$this->assertEquals(self::API_ROUTE_PREFIX, $route->getPrefix());

View File

@ -163,7 +163,7 @@ public function test_user_can_assign_multiple_accounts()
}
#[Test]
public function test_setUser_sets_twfaccounts_user()
public function test_setUser_sets_groups_user()
{
$this->groupOne = Group::factory()->create();
$this->groupTwo = Group::factory()->create();

View File

@ -308,4 +308,19 @@ public function test_delete_single_id()
'id' => $twofaccount->id,
]);
}
#[Test]
public function test_setUser_sets_twofaccounts_user()
{
$twofaccountA = TwoFAccount::factory()->create();
$twofaccountB = TwoFAccount::factory()->create();
$this->assertEquals(null, $twofaccountA->user_id);
$this->assertEquals(null, $twofaccountB->user_id);
TwoFAccounts::setUser(TwoFAccount::all(), $this->user);
$this->assertEquals($this->user->id, $twofaccountA->refresh()->user_id);
$this->assertEquals($this->user->id, $twofaccountB->refresh()->user_id);
}
}

View File

@ -8,6 +8,7 @@
use App\Factories\MigratorFactory;
use App\Models\TwoFAccount;
use App\Providers\MigrationServiceProvider;
use App\Providers\TwoFAuthServiceProvider;
use App\Services\Migrators\AegisMigrator;
use App\Services\Migrators\GoogleAuthMigrator;
use App\Services\Migrators\Migrator;
@ -29,6 +30,7 @@
* MigratorTest test class
*/
#[CoversClass(MigrationServiceProvider::class)]
#[CoversClass(TwoFAuthServiceProvider::class)]
#[CoversClass(MigratorFactory::class)]
#[CoversClass(Migrator::class)]
#[CoversClass(AegisMigrator::class)]
@ -435,6 +437,9 @@ public static function TwoFAuthWithIconMigrationProvider()
'BMP' => [
MigrationTestData::VALID_2FAUTH_JSON_MIGRATION_PAYLOAD_WITH_BMP_ICON,
],
'XBMP' => [
MigrationTestData::VALID_2FAUTH_JSON_MIGRATION_PAYLOAD_WITH_XBMP_ICON,
],
'WEBP' => [
MigrationTestData::VALID_2FAUTH_JSON_MIGRATION_PAYLOAD_WITH_WEBP_ICON,
],