diff --git a/.env.testing b/.env.testing index 58ff9652..eda7ec8b 100644 --- a/.env.testing +++ b/.env.testing @@ -1,10 +1,10 @@ -APP_NAME=2FAuth +APP_NAME=TestApp APP_ENV=testing APP_KEY=base64:pdjaSqs7dDu8SdYCQzsayBAqeMC+85fmo3bpeBCCT94= APP_DEBUG=true APP_URL=http://localhost -WEBAUTHN_NAME=2FAuth +WEBAUTHN_NAME=TestApp WEBAUTHN_ID=localhost WEBAUTHN_USER_VERIFICATION=discouraged @@ -18,6 +18,15 @@ LOG_CHANNEL=stack DB_CONNECTION=testing DB_DATABASE=:memory: +# DB_CONNECTION=mysql +# DB_HOST=localhost +# DB_PORT=3306 +# DB_DATABASE=2fauth_test +# DB_USERNAME=test_user +# DB_PASSWORD=password + +BCRYPT_ROUNDS=4 + BROADCAST_DRIVER=log CACHE_DRIVER=array QUEUE_CONNECTION=sync @@ -28,7 +37,7 @@ REDIS_HOST=127.0.0.1 REDIS_PASSWORD=null REDIS_PORT=6379 -MAIL_DRIVER=smtp +MAIL_DRIVER=array MAIL_HOST=smtp.mailtrap.io MAIL_PORT=2525 MAIL_USERNAME=null diff --git a/app/Console/Commands/ResetDemo.php b/app/Console/Commands/ResetDemo.php index f76ecdc3..8eb526c2 100644 --- a/app/Console/Commands/ResetDemo.php +++ b/app/Console/Commands/ResetDemo.php @@ -74,16 +74,16 @@ public function handle() $this->line('Icons regenerated'); // Reset the db - DB::table('users')->truncate(); - DB::table('password_resets')->truncate(); - DB::table('oauth_access_tokens')->truncate(); - DB::table('oauth_personal_access_clients')->truncate(); - DB::table('oauth_refresh_tokens')->truncate(); - DB::table('web_authn_credentials')->truncate(); - DB::table('web_authn_recoveries')->truncate(); - DB::table('twofaccounts')->truncate(); - DB::table('options')->truncate(); - DB::table('groups')->truncate(); + DB::table('users')->delete(); + DB::table('password_resets')->delete(); + DB::table('oauth_access_tokens')->delete(); + DB::table('oauth_personal_access_clients')->delete(); + DB::table('oauth_refresh_tokens')->delete(); + DB::table('web_authn_credentials')->delete(); + DB::table('web_authn_recoveries')->delete(); + DB::table('twofaccounts')->delete(); + DB::table('options')->delete(); + DB::table('groups')->delete(); // Seed the db $this->callSilent('db:seed', [ diff --git a/composer.json b/composer.json index 33ba7a1d..ffdfe27a 100644 --- a/composer.json +++ b/composer.json @@ -66,8 +66,13 @@ "@php artisan key:generate --ansi" ], "test" : [ + "php artisan config:clear", "vendor/bin/phpunit" ], + "test-mysql" : [ + "php artisan config:clear", + "vendor/bin/phpunit -c phpunit-mysql.xml" + ], "test-coverage-html" : [ "@putenv XDEBUG_MODE=coverage", "vendor/bin/phpunit --coverage-html tests/Coverage/" diff --git a/phpunit-mysql.xml b/phpunit-mysql.xml new file mode 100644 index 00000000..bf32a736 --- /dev/null +++ b/phpunit-mysql.xml @@ -0,0 +1,41 @@ + + + + + ./app + + + + + ./tests/Unit + + + ./tests/Feature + + + ./tests/Api/v1 + + + + + + + + + + + + + diff --git a/phpunit.xml b/phpunit.xml index 3e8d67fb..3cc86b10 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -9,6 +9,7 @@ convertWarningsToExceptions="true" processIsolation="false" stopOnFailure="false" + stopOnError="false" beStrictAboutTestsThatDoNotTestAnything="false" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"> @@ -28,13 +29,7 @@ - - - - - - - - + + diff --git a/tests/Api/v1/Controllers/GroupControllerTest.php b/tests/Api/v1/Controllers/GroupControllerTest.php index c3e4b114..bb3edfb1 100644 --- a/tests/Api/v1/Controllers/GroupControllerTest.php +++ b/tests/Api/v1/Controllers/GroupControllerTest.php @@ -67,8 +67,7 @@ public function test_store_returns_created_group_resource() 'name' => 'My second group', ]) ->assertCreated() - ->assertExactJson([ - 'id' => 1, + ->assertJsonFragment([ 'name' => 'My second group', 'twofaccounts_count' => 0, ]); @@ -100,8 +99,7 @@ public function test_show_returns_group_resource() $response = $this->actingAs($this->user, 'api-guard') ->json('GET', '/api/v1/groups/' . $group->id) ->assertOk() - ->assertExactJson([ - 'id' => 1, + ->assertJsonFragment([ 'name' => 'My group', 'twofaccounts_count' => 0, ]); @@ -134,8 +132,7 @@ public function test_update_returns_updated_group_resource() 'name' => 'name updated', ]) ->assertOk() - ->assertExactJson([ - 'id' => 1, + ->assertJsonFragment([ 'name' => 'name updated', 'twofaccounts_count' => 0, ]); @@ -183,7 +180,7 @@ public function test_assign_accounts_returns_updated_group_resource() $response = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/groups/' . $group->id . '/assign', [ - 'ids' => [1,2], + 'ids' => [$accounts[0]->id, $accounts[1]->id], ]) ->assertOk() ->assertExactJson([ @@ -203,7 +200,7 @@ public function test_assign_accounts_to_missing_group_returns_not_found() $response = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/groups/1000/assign', [ - 'ids' => [1,2], + 'ids' => [$accounts[0]->id, $accounts[1]->id], ]) ->assertNotFound() ->assertJsonStructure([ @@ -238,7 +235,7 @@ public function test_get_assigned_accounts_returns_twofaccounts_collection() $assign = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/groups/' . $group->id . '/assign', [ - 'ids' => [1,2], + 'ids' => [$accounts[0]->id, $accounts[1]->id], ]); $response = $this->actingAs($this->user, 'api-guard') @@ -271,7 +268,7 @@ public function test_get_assigned_accounts_returns_twofaccounts_collection_with_ $assign = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/groups/' . $group->id . '/assign', [ - 'ids' => [1,2], + 'ids' => [$accounts[0]->id, $accounts[1]->id], ]); $response = $this->actingAs($this->user, 'api-guard') diff --git a/tests/Api/v1/Controllers/TwoFAccountControllerTest.php b/tests/Api/v1/Controllers/TwoFAccountControllerTest.php index ba234200..e29c794f 100644 --- a/tests/Api/v1/Controllers/TwoFAccountControllerTest.php +++ b/tests/Api/v1/Controllers/TwoFAccountControllerTest.php @@ -461,14 +461,14 @@ public function test_store_assigns_created_account_when_default_group_is_the_act // Set the default group to be the active one $settingService->set('defaultGroup', -1); // Set the active group - $settingService->set('activeGroup', 1); + $settingService->set('activeGroup', $this->group->id); $response = $this->actingAs($this->user, 'api-guard') ->json('POST', '/api/v1/twofaccounts', [ 'uri' => self::TOTP_SHORT_URI, ]) ->assertJsonFragment([ - 'group_id' => 1 + 'group_id' => $this->group->id ]); } diff --git a/tests/Feature/Console/ResetDemoTest.php b/tests/Feature/Console/ResetDemoTest.php index fd998364..fcff3333 100644 --- a/tests/Feature/Console/ResetDemoTest.php +++ b/tests/Feature/Console/ResetDemoTest.php @@ -32,8 +32,6 @@ public function test_reset_demo_succeeded() ->expectsOutput('Demo app refreshed') ->assertExitCode(0); - $user = User::find(1); - $this->assertDatabaseCount('twofaccounts', 9); $this->assertDatabaseHas('twofaccounts', [ diff --git a/tests/Feature/Http/Auth/RegisterControllerTest.php b/tests/Feature/Http/Auth/RegisterControllerTest.php index adc355b5..a8ed3e49 100644 --- a/tests/Feature/Http/Auth/RegisterControllerTest.php +++ b/tests/Feature/Http/Auth/RegisterControllerTest.php @@ -2,7 +2,9 @@ namespace Tests\Feature\Http\Auth; +use \App\Models\User; use Tests\FeatureTestCase; +use Illuminate\Support\Facades\DB; class RegisterControllerTest extends FeatureTestCase { @@ -25,6 +27,8 @@ public function setUp(): void */ public function test_register_returns_success() { + DB::table('users')->delete(); + $response = $this->json('POST', '/user', [ 'name' => self::USERNAME, 'email' => self::EMAIL, @@ -40,6 +44,24 @@ public function test_register_returns_success() 'name' => self::USERNAME, ]); } + + + /** + * @test + */ + public function test_register_returns_already_an_existing_user() + { + DB::table('users')->delete(); + $user = User::factory()->create(); + + $response = $this->json('POST', '/user', [ + 'name' => self::USERNAME, + 'email' => self::EMAIL, + 'password' => self::PASSWORD, + 'password_confirmation' => self::PASSWORD, + ]) + ->assertJsonValidationErrorFor('name'); + } /** diff --git a/tests/Feature/Http/Auth/WebAuthnLoginControllerTest.php b/tests/Feature/Http/Auth/WebAuthnLoginControllerTest.php index e46d5aca..d092cc14 100644 --- a/tests/Feature/Http/Auth/WebAuthnLoginControllerTest.php +++ b/tests/Feature/Http/Auth/WebAuthnLoginControllerTest.php @@ -24,6 +24,8 @@ class WebAuthnLoginControllerTest extends FeatureTestCase public function setUp(): void { parent::setUp(); + + DB::table('users')->delete(); } @@ -41,7 +43,7 @@ public function test_user_login_returns_success() DB::table('web_authn_credentials')->insert([ 'id' => 'dGVzdF9jcmVkZW50aWFsX2lk', - 'user_id' => 1, + 'user_id' => $this->user->id, 'type' => 'public_key', 'transports' => json_encode([]), 'attestation_type' => 'none', @@ -98,7 +100,7 @@ public function test_user_login_without_userhandle_returns_success() DB::table('web_authn_credentials')->insert([ 'id' => 'dGVzdF9jcmVkZW50aWFsX2lk', - 'user_id' => 1, + 'user_id' => $this->user->id, 'type' => 'public_key', 'transports' => json_encode([]), 'attestation_type' => 'none',