Browse Source

Add & validate MySQL tests

Bubka 3 năm trước cách đây
mục cha
commit
b0b648cf80

+ 12 - 3
.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

+ 10 - 10
app/Console/Commands/ResetDemo.php

@@ -74,16 +74,16 @@ class ResetDemo extends Command
             $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', [

+ 5 - 0
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/"

+ 41 - 0
phpunit-mysql.xml

@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    backupGlobals="false"
+    backupStaticAttributes="false"
+    bootstrap="vendor/autoload.php"
+    colors="true"
+    convertErrorsToExceptions="true"
+    convertNoticesToExceptions="true"
+    convertWarningsToExceptions="true"
+    processIsolation="false"
+    stopOnFailure="false"
+    stopOnError="false"
+    beStrictAboutTestsThatDoNotTestAnything="false"
+    xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd">
+    <coverage processUncoveredFiles="true">
+        <include>
+            <directory suffix=".php">./app</directory>
+        </include>
+    </coverage>
+    <testsuites>
+        <testsuite name="Unit">
+            <directory suffix="Test.php">./tests/Unit</directory>
+        </testsuite>
+        <testsuite name="Feature">
+            <directory suffix="Test.php">./tests/Feature</directory>
+        </testsuite>
+        <testsuite name="Api.v1">
+            <directory suffix="Test.php">./tests/Api/v1</directory>
+        </testsuite>
+    </testsuites>
+    <php>
+        <env name="APP_ENV" value="testing"/>
+        <!-- following values override .env.testing vars -->
+        <env name="DB_CONNECTION" value="mysql"/>
+        <env name="DB_HOST" value="localhost"/>
+        <env name="DB_PORT" value="3306"/>
+        <env name="DB_DATABASE" value="2fauth_test"/>
+        <env name="DB_USERNAME" value="test_user"/>
+        <env name="DB_PASSWORD" value="password"/>
+    </php>
+</phpunit>

+ 3 - 8
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">
     <coverage processUncoveredFiles="true">
@@ -28,13 +29,7 @@
         </testsuite>
     </testsuites>
     <php>
-        <server name="APP_ENV" value="testing"/>
-        <server name="DB_CONNECTION" value="sqlite"/>
-        <server name="DB_DATABASE" value=":memory:"/>
-        <server name="BCRYPT_ROUNDS" value="4"/>
-        <server name="CACHE_DRIVER" value="array"/>
-        <server name="MAIL_DRIVER" value="array"/>
-        <server name="QUEUE_CONNECTION" value="sync"/>
-        <server name="SESSION_DRIVER" value="array"/>
+        <env name="APP_ENV" value="testing"/>
+        <!-- Use .env.testing to customize other testing vars -->
     </php>
 </phpunit>

+ 7 - 10
tests/Api/v1/Controllers/GroupControllerTest.php

@@ -67,8 +67,7 @@ class GroupControllerTest extends FeatureTestCase
                 'name' => 'My second group',
             ])
             ->assertCreated()
-            ->assertExactJson([
-                'id' => 1,
+            ->assertJsonFragment([
                 'name' => 'My second group',
                 'twofaccounts_count' => 0,
             ]);
@@ -100,8 +99,7 @@ class GroupControllerTest extends FeatureTestCase
         $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 @@ class GroupControllerTest extends FeatureTestCase
                 'name' => 'name updated',
             ])
             ->assertOk()
-            ->assertExactJson([
-                'id' => 1,
+            ->assertJsonFragment([
                 'name' => 'name updated',
                 'twofaccounts_count' => 0,
             ]);
@@ -183,7 +180,7 @@ class GroupControllerTest extends FeatureTestCase
 
         $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 @@ class GroupControllerTest extends FeatureTestCase
 
         $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 @@ class GroupControllerTest extends FeatureTestCase
 
         $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 @@ class GroupControllerTest extends FeatureTestCase
 
         $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')

+ 2 - 2
tests/Api/v1/Controllers/TwoFAccountControllerTest.php

@@ -461,14 +461,14 @@ class TwoFAccountControllerTest extends FeatureTestCase
         // 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
             ]);
     }
 

+ 0 - 2
tests/Feature/Console/ResetDemoTest.php

@@ -32,8 +32,6 @@ class ResetDemoTest extends FeatureTestCase
              ->expectsOutput('Demo app refreshed')
              ->assertExitCode(0);
 
-        $user = User::find(1);
-
         $this->assertDatabaseCount('twofaccounts', 9);
 
         $this->assertDatabaseHas('twofaccounts', [

+ 22 - 0
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 @@ class RegisterControllerTest extends FeatureTestCase
      */
     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 @@ class RegisterControllerTest extends FeatureTestCase
             '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');
+    }
 
 
     /**

+ 4 - 2
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 @@ class WebAuthnLoginControllerTest extends FeatureTestCase
 
         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 @@ class WebAuthnLoginControllerTest extends FeatureTestCase
 
         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',