From a6d4b6c5f6abc12b9191d7637f5e5d6bce2762cc Mon Sep 17 00:00:00 2001
From: Bubka <858858+Bubka@users.noreply.github.com>
Date: Tue, 16 Nov 2021 23:03:22 +0100
Subject: [PATCH] Fix reset-demo artisan command and its test

---
 app/Console/Commands/ResetDemo.php |   7 +-
 database/seeds/DemoSeeder.php      | 112 +++++++++++-----
 tests/Feature/ConsoleTest.php      | 200 ++++++++++++++++-------------
 3 files changed, 194 insertions(+), 125 deletions(-)

diff --git a/app/Console/Commands/ResetDemo.php b/app/Console/Commands/ResetDemo.php
index 54e66e2e..f2d91081 100644
--- a/app/Console/Commands/ResetDemo.php
+++ b/app/Console/Commands/ResetDemo.php
@@ -75,9 +75,13 @@ public function handle()
             
             // 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('twofaccounts')->truncate();
             DB::table('options')->truncate();
+            DB::table('groups')->truncate();
             
             // Seed the db
             $this->callSilent('db:seed', [
@@ -93,9 +97,6 @@ public function handle()
     }
 
     private static function generateIcon($serviceName, $base64icon) {
-        // $image = str_replace('data:image/png;base64,', '', $image);
-        // $image = str_replace(' ', '+', $image);
-
         Storage::put('public/icons/' . $serviceName . '.png', base64_decode($base64icon));
     }
 
diff --git a/database/seeds/DemoSeeder.php b/database/seeds/DemoSeeder.php
index 19154347..1882e5d8 100644
--- a/database/seeds/DemoSeeder.php
+++ b/database/seeds/DemoSeeder.php
@@ -1,6 +1,7 @@
 <?php
 
 use App\User;
+use App\Group;
 use App\TwoFAccount;
 use Illuminate\Database\Seeder;
 
@@ -19,12 +20,71 @@ public function run()
             'password' => bcrypt('demo'),
         ]);
 
-        TwoFAccount::create([
+        $groupSocialNetwork = Group::create([
+            'name' => 'Social Networks',
+        ]);
+
+        $groupSocialNetwork->twofaccounts()->createMany([
+            [
+                'otp_type' => 'totp',
+                'account' => 'johndoe@facebook.com',
+                'service' => 'Facebook',
+                'secret' => 'A4GRFTVVRBGY7UIW',
+                'algorithm' => 'sha1',
+                'digits' => 6,
+                'period' => 30,
+                'icon' => 'facebook.png',
+                'legacy_uri' => 'otpauth://totp/Facebook:johndoe@facebook.com?secret=A4GRFTVVRBGY7UIW',
+            ],
+            [
+                'otp_type' => 'totp',
+                'service' => 'Twitter',
+                'account' => '@john',
+                'secret' => 'A2GRFTVVRBGY7UIW',
+                'algorithm' => 'sha1',
+                'digits' => 6,
+                'period' => 30,
+                'icon' => 'twitter.png',
+                'legacy_uri' => 'otpauth://totp/Twitter:@john?secret=A2GRFTVVRBGY7UIW',
+            ],
+            [
+                'otp_type' => 'totp',
+                'service' => 'Instagram',
+                'account' => '@johndoe',
+                'secret' => 'A6GRFTVVRBGY7UIW',
+                'algorithm' => 'sha1',
+                'digits' => 6,
+                'period' => 30,
+                'icon' => 'instagram.png',
+                'legacy_uri' => 'otpauth://totp/Instagram:@johndoe?secret=A6GRFTVVRBGY7UIW',
+            ],
+            [
+                'otp_type' => 'totp',
+                'service' => 'LinkedIn',
+                'account' => '@johndoe',
+                'secret' => 'A7GRFTVVRBGY7UIW',
+                'algorithm' => 'sha1',
+                'digits' => 6,
+                'period' => 30,
+                'icon' => 'linkedin.png',
+                'legacy_uri' => 'otpauth://totp/LinkedIn:@johndoe?secret=A7GRFTVVRBGY7UIW',
+            ]
+        ]);
+
+        $groupECommerce = Group::create([
+            'name' => 'eCommerce',
+        ]);
+
+        $groupSocialNetwork->twofaccounts()->create([
             'otp_type' => 'totp',
             'account' => 'johndoe',
             'service' => 'Amazon',
             'secret' => 'A7GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
             'icon' => 'amazon.png',
+            'legacy_uri' => 'otpauth://totp/Amazon:johndoe?secret=A7GRFTVVRBGY7UIW',
         ]);
 
         TwoFAccount::create([
@@ -32,7 +92,11 @@ public function run()
             'account' => 'john.doe@icloud.com',
             'service' => 'Apple',
             'secret' => 'A2GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
             'icon' => 'apple.png',
+            'legacy_uri' => 'otpauth://totp/Apple:john.doe@icloud.com?secret=A2GRFTVVRBGY7UIW',
         ]);
 
         TwoFAccount::create([
@@ -40,15 +104,11 @@ public function run()
             'account' => 'john.doe',
             'service' => 'Dropbox',
             'secret' => 'A3GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
             'icon' => 'dropbox.png',
-        ]);
-
-        TwoFAccount::create([
-            'otp_type' => 'totp',
-            'account' => 'johndoe@facebook.com',
-            'service' => 'Facebook',
-            'secret' => 'A4GRFTVVRBGY7UIW',
-            'icon' => 'facebook.png',
+            'legacy_uri' => 'otpauth://totp/Dropbox:john.doe?secret=A3GRFTVVRBGY7UIW',
         ]);
 
         TwoFAccount::create([
@@ -56,7 +116,11 @@ public function run()
             'account' => '@john',
             'service' => 'Github',
             'secret' => 'A2GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
             'icon' => 'github.png',
+            'legacy_uri' => 'otpauth://totp/Github:@john?secret=A2GRFTVVRBGY7UIW',
         ]);
 
         TwoFAccount::create([
@@ -64,31 +128,11 @@ public function run()
             'service' => 'Google',
             'account' => 'john.doe@gmail.com',
             'secret' => 'A5GRFTVVRBGY7UIW',
-            'icon' => 'google.png'
-        ]);
-
-        TwoFAccount::create([
-            'otp_type' => 'totp',
-            'service' => 'Instagram',
-            'account' => '@johndoe',
-            'secret' => 'A6GRFTVVRBGY7UIW',
-            'icon' => 'instagram.png'
-        ]);
-
-        TwoFAccount::create([
-            'otp_type' => 'totp',
-            'service' => 'LinkedIn',
-            'account' => '@johndoe',
-            'secret' => 'A7GRFTVVRBGY7UIW',
-            'icon' => 'linkedin.png'
-        ]);
-
-        TwoFAccount::create([
-            'otp_type' => 'totp',
-            'service' => 'Twitter',
-            'account' => '@john',
-            'secret' => 'A2GRFTVVRBGY7UIW',
-            'icon' => 'twitter.png'
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
+            'icon' => 'google.png',
+            'legacy_uri' => 'otpauth://totp/Google:john.doe@gmail.com?secret=A5GRFTVVRBGY7UIW',
         ]);
     }
 }
diff --git a/tests/Feature/ConsoleTest.php b/tests/Feature/ConsoleTest.php
index 0baa926e..45018027 100644
--- a/tests/Feature/ConsoleTest.php
+++ b/tests/Feature/ConsoleTest.php
@@ -3,19 +3,17 @@
 namespace Tests\Feature;
 
 use App\User;
-use Tests\TestCase;
+use Tests\FeatureTestCase;
 use Illuminate\Support\Facades\Config;
-use Illuminate\Support\Facades\Artisan;
+use Illuminate\Support\Facades\DB;
 
-class ConsoleTest extends TestCase
+class ConsoleTest extends FeatureTestCase
 {
 
     /**
-     * Test 2fauth:reset-demo console command.
-     *
-     * @return void
+     * @test
      */
-    public function test2fauthResetDemowithoutDemoModeConsoleCommand()
+    public function test_reset_demo_without_demo_mode_succeeded()
     {
         $this->artisan('2fauth:reset-demo')
              ->expectsOutput('2fauth:reset-demo can only run when isDemoApp option is On')
@@ -23,11 +21,9 @@ public function test2fauthResetDemowithoutDemoModeConsoleCommand()
     }
 
     /**
-     * Test 2fauth:reset-demo console command.
-     *
-     * @return void
+     * @test
      */
-    public function test2fauthResetDemowithConfirmConsoleCommand()
+    public function test_reset_demo_succeeded()
     {
         Config::set('2fauth.config.isDemoApp', true);
 
@@ -39,85 +35,115 @@ public function test2fauthResetDemowithConfirmConsoleCommand()
 
         $user = User::find(1);
 
-        $response = $this->actingAs($user, 'api')
-            ->json('GET', '/api/twofaccounts/1')
-            ->assertStatus(200)
-            ->assertJson([
-                'service' => 'Amazon',
-                'icon' => 'amazon.png',
-            ]);
+        $this->assertDatabaseCount('twofaccounts', 9);
 
-        $response = $this->actingAs($user, 'api')
-            ->json('GET', '/api/twofaccounts/2')
-            ->assertStatus(200)
-            ->assertJson([
-                'service' => 'Apple',
-                'icon' => 'apple.png',
-            ]);
+        $this->assertDatabaseHas('twofaccounts', [
+            'otp_type' => 'totp',
+            'account' => 'johndoe@facebook.com',
+            'service' => 'Facebook',
+            'secret' => 'A4GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
+            'icon' => 'facebook.png',
+            'legacy_uri' => 'otpauth://totp/Facebook:johndoe@facebook.com?secret=A4GRFTVVRBGY7UIW',
+        ]);
+        $this->assertDatabaseHas('twofaccounts', [
+            'otp_type' => 'totp',
+            'service' => 'Twitter',
+            'account' => '@john',
+            'secret' => 'A2GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
+            'icon' => 'twitter.png',
+            'legacy_uri' => 'otpauth://totp/Twitter:@john?secret=A2GRFTVVRBGY7UIW',
+        ]);
+        $this->assertDatabaseHas('twofaccounts', [
+            'otp_type' => 'totp',
+            'service' => 'Instagram',
+            'account' => '@johndoe',
+            'secret' => 'A6GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
+            'icon' => 'instagram.png',
+            'legacy_uri' => 'otpauth://totp/Instagram:@johndoe?secret=A6GRFTVVRBGY7UIW',
+        ]);
+        $this->assertDatabaseHas('twofaccounts', [
+            'otp_type' => 'totp',
+            'service' => 'LinkedIn',
+            'account' => '@johndoe',
+            'secret' => 'A7GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
+            'icon' => 'linkedin.png',
+            'legacy_uri' => 'otpauth://totp/LinkedIn:@johndoe?secret=A7GRFTVVRBGY7UIW',
+        ]);
+        $this->assertDatabaseHas('twofaccounts', [
+            'otp_type' => 'totp',
+            'account' => 'johndoe',
+            'service' => 'Amazon',
+            'secret' => 'A7GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
+            'icon' => 'amazon.png',
+            'legacy_uri' => 'otpauth://totp/Amazon:johndoe?secret=A7GRFTVVRBGY7UIW',
+        ]);
+        $this->assertDatabaseHas('twofaccounts', [
+            'otp_type' => 'totp',
+            'account' => 'john.doe@icloud.com',
+            'service' => 'Apple',
+            'secret' => 'A2GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
+            'icon' => 'apple.png',
+            'legacy_uri' => 'otpauth://totp/Apple:john.doe@icloud.com?secret=A2GRFTVVRBGY7UIW',
+        ]);
+        $this->assertDatabaseHas('twofaccounts', [
+            'otp_type' => 'totp',
+            'account' => 'john.doe',
+            'service' => 'Dropbox',
+            'secret' => 'A3GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
+            'icon' => 'dropbox.png',
+            'legacy_uri' => 'otpauth://totp/Dropbox:john.doe?secret=A3GRFTVVRBGY7UIW',
+        ]);
+        $this->assertDatabaseHas('twofaccounts', [
+            'otp_type' => 'totp',
+            'account' => '@john',
+            'service' => 'Github',
+            'secret' => 'A2GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
+            'icon' => 'github.png',
+            'legacy_uri' => 'otpauth://totp/Github:@john?secret=A2GRFTVVRBGY7UIW',
+        ]);
+        $this->assertDatabaseHas('twofaccounts', [
+            'otp_type' => 'totp',
+            'service' => 'Google',
+            'account' => 'john.doe@gmail.com',
+            'secret' => 'A5GRFTVVRBGY7UIW',
+            'algorithm' => 'sha1',
+            'digits' => 6,
+            'period' => 30,
+            'icon' => 'google.png',
+            'legacy_uri' => 'otpauth://totp/Google:john.doe@gmail.com?secret=A5GRFTVVRBGY7UIW',
+        ]);
 
-        $response = $this->actingAs($user, 'api')
-            ->json('GET', '/api/twofaccounts/3')
-            ->assertStatus(200)
-            ->assertJson([
-                'service' => 'Dropbox',
-                'icon' => 'dropbox.png',
-            ]);
-
-        $response = $this->actingAs($user, 'api')
-            ->json('GET', '/api/twofaccounts/4')
-            ->assertStatus(200)
-            ->assertJson([
-                'service' => 'Facebook',
-                'icon' => 'facebook.png',
-            ]);
-
-        $response = $this->actingAs($user, 'api')
-            ->json('GET', '/api/twofaccounts/5')
-            ->assertStatus(200)
-            ->assertJson([
-                'service' => 'Github',
-                'icon' => 'github.png',
-            ]);
-
-        $response = $this->actingAs($user, 'api')
-            ->json('GET', '/api/twofaccounts/6')
-            ->assertStatus(200)
-            ->assertJson([
-                'service' => 'Google',
-                'icon' => 'google.png',
-            ]);
-
-        $response = $this->actingAs($user, 'api')
-            ->json('GET', '/api/twofaccounts/7')
-            ->assertStatus(200)
-            ->assertJson([
-                'service' => 'Instagram',
-                'icon' => 'instagram.png',
-            ]);
-
-        $response = $this->actingAs($user, 'api')
-            ->json('GET', '/api/twofaccounts/8')
-            ->assertStatus(200)
-            ->assertJson([
-                'service' => 'LinkedIn',
-                'icon' => 'linkedin.png',
-            ]);
-
-        $response = $this->actingAs($user, 'api')
-            ->json('GET', '/api/twofaccounts/9')
-            ->assertStatus(200)
-            ->assertJson([
-                'service' => 'Twitter',
-                'icon' => 'twitter.png',
-            ]);
     }
 
+
     /**
-     * Test 2fauth:reset-demo console command.
-     *
-     * @return void
+     * @test
      */
-    public function test2fauthResetDemowithBadConfirmationConsoleCommand()
+    public function test_reset_demo_with_invalid_confirmation_succeeded()
     {
         Config::set('2fauth.config.isDemoApp', true);
 
@@ -129,11 +155,9 @@ public function test2fauthResetDemowithBadConfirmationConsoleCommand()
 
 
     /**
-     * Test 2fauth:reset-demo console command.
-     *
-     * @return void
+     * @test
      */
-    public function test2fauthResetDemowithoutConfirmationConsoleCommand()
+    public function test_reset_demo_with_no_confirm_option_succeeded()
     {
         Config::set('2fauth.config.isDemoApp', true);