Browse Source

Fix tests after setting PHP 8.2 as default

Bubka 1 year ago
parent
commit
96978accb3

+ 8 - 1
app/Services/Migrators/GoogleAuthMigrator.php

@@ -45,7 +45,7 @@ class GoogleAuthMigrator extends Migrator
                 $parameters['otp_type']  = GAuthValueMapping::OTP_TYPE[OtpType::name($otp_parameters->getType())];
                 $parameters['service']   = $otp_parameters->getIssuer();
                 $parameters['account']   = str_replace($parameters['service'] . ':', '', $otp_parameters->getName());
-                $parameters['secret']    = Base32::encodeUpper($otp_parameters->getSecret());
+                $parameters['secret']    = $this->toBase32($otp_parameters->getSecret());
                 $parameters['algorithm'] = GAuthValueMapping::ALGORITHM[Algorithm::name($otp_parameters->getAlgorithm())];
                 $parameters['digits']    = GAuthValueMapping::DIGIT_COUNT[DigitCount::name($otp_parameters->getDigits())];
                 $parameters['counter']   = $parameters['otp_type'] === TwoFAccount::HOTP ? $otp_parameters->getCounter() : null;
@@ -73,4 +73,11 @@ class GoogleAuthMigrator extends Migrator
 
         return collect($twofaccounts);
     }
+
+    /**
+     * Encode into uppercase Base32
+     */
+    protected function toBase32(string $str) {
+        return Base32::encodeUpper($str);
+    }
 }

+ 1 - 1
app/Services/QrCodeService.php

@@ -29,7 +29,7 @@ class QrCodeService
 
         Log::info('data encoded to QR code');
 
-        return $qrcode->render($data);
+        return $qrcode->render('stringToEncode');
     }
 
     /**

+ 1 - 0
phpunit.xml

@@ -34,6 +34,7 @@
         </testsuite>
     </testsuites>
     <php>
+        <ini name="memory_limit" value="256M" />
         <env name="APP_ENV" value="testing"/>
         <!-- following values override .env.testing vars -->
     </php>

+ 16 - 0
tests/Api/v1/Controllers/GroupControllerTest.php

@@ -473,4 +473,20 @@ class GroupControllerTest extends FeatureTestCase
         $this->assertEquals(0, $this->user->preferences['defaultGroup']);
         $this->assertEquals(0, $this->user->preferences['activeGroup']);
     }
+
+    /**
+     * @test
+     */
+    public function test_twofaccount_is_released_on_group_destroy()
+    {
+        $this->actingAs($this->user, 'api-guard')
+            ->json('DELETE', '/api/v1/groups/' . $this->userGroupA->id)
+            ->assertNoContent();
+
+        $this->twofaccountA->refresh();
+        $this->twofaccountB->refresh();
+
+        $this->assertNull($this->twofaccountA->group_id);
+        $this->assertNull($this->twofaccountB->group_id);
+    }
 }

+ 1 - 1
tests/Api/v1/Controllers/QrCodeControllerTest.php

@@ -61,7 +61,7 @@ class QrCodeControllerTest extends FeatureTestCase
             ])
             ->assertOk();
 
-        $this->assertStringStartsWith('data:image/png;base64', $response->getData()->qrcode);
+        $this->assertStringStartsWith('data:image/svg+xml;base64', $response->getData()->qrcode);
     }
 
     /**

File diff suppressed because it is too large
+ 0 - 1
tests/Feature/Services/QrCodeServiceTest.php


+ 0 - 27
tests/Unit/Listeners/DissociateTwofaccountFromGroupTest.php

@@ -4,14 +4,8 @@ namespace Tests\Unit\Listeners;
 
 use App\Events\GroupDeleting;
 use App\Listeners\DissociateTwofaccountFromGroup;
-use App\Models\Group;
-use App\Models\TwoFAccount;
 use Illuminate\Support\Facades\Event;
-use Mockery\MockInterface;
 use PHPUnit\Framework\Attributes\CoversClass;
-use PHPUnit\Framework\Attributes\PreserveGlobalState;
-use PHPUnit\Framework\Attributes\RequiresPhp;
-use PHPUnit\Framework\Attributes\RunInSeparateProcess;
 use Tests\TestCase;
 
 /**
@@ -20,27 +14,6 @@ use Tests\TestCase;
 #[CoversClass(DissociateTwofaccountFromGroup::class)]
 class DissociateTwofaccountFromGroupTest extends TestCase
 {
-    /**
-     * @test
-     */
-    #[RunInSeparateProcess]
-    #[PreserveGlobalState(false)]
-    #[RequiresPhp('< 8.3.0')]
-    public function test_twofaccount_is_released_on_group_deletion()
-    {
-        $this->mock('alias:' . TwoFAccount::class, function (MockInterface $twoFAccount) {
-            $twoFAccount->shouldReceive('where->update')
-                ->once()
-                ->andReturn(1);
-        });
-
-        $group    = Group::factory()->make();
-        $event    = new GroupDeleting($group);
-        $listener = new DissociateTwofaccountFromGroup();
-
-        $this->assertNull($listener->handle($event));
-    }
-
     /**
      * @test
      */

+ 19 - 15
tests/Unit/MigratorTest.php

@@ -18,12 +18,8 @@ use App\Services\SettingService;
 use Illuminate\Support\Facades\Storage;
 use Mockery;
 use Mockery\MockInterface;
-use ParagonIE\ConstantTime\Base32;
 use PHPUnit\Framework\Attributes\CoversClass;
 use PHPUnit\Framework\Attributes\DataProvider;
-use PHPUnit\Framework\Attributes\PreserveGlobalState;
-use PHPUnit\Framework\Attributes\RequiresPhp;
-use PHPUnit\Framework\Attributes\RunInSeparateProcess;
 use PHPUnit\Framework\Attributes\UsesClass;
 use Tests\Data\MigrationTestData;
 use Tests\Data\OtpTestData;
@@ -142,6 +138,20 @@ class MigratorTest extends TestCase
         $this->fakeTwofaccount->id = TwoFAccount::FAKE_ID;
     }
 
+    /**
+     * Clean up the testing environment before the next test.
+     *
+     * @return void
+     *
+     * @throws \Mockery\Exception\InvalidCountException
+     */
+    protected function tearDown() : void
+    {
+        $this->forgetMock(SettingService::class);
+
+        parent::tearDown();
+    }
+
     /**
      * @test
      */
@@ -332,17 +342,14 @@ class MigratorTest extends TestCase
     /**
      * @test
      */
-    #[RunInSeparateProcess]
-    #[PreserveGlobalState(false)]
-    #[RequiresPhp('< 8.3.0')]
     public function test_migrate_gauth_returns_fake_accounts()
     {
-        $this->mock('alias:' . Base32::class, function (MockInterface $baseEncoder) {
-            $baseEncoder->shouldReceive('encodeUpper')
+        $migrator = $this->partialMock(GoogleAuthMigrator::class, function (MockInterface $migrator) {
+            $migrator->shouldAllowMockingProtectedMethods()->shouldReceive('toBase32')
                 ->andThrow(new \Exception());
         });
 
-        $migrator = new GoogleAuthMigrator();
+        /** @disregard Undefined function */
         $accounts = $migrator->migrate(MigrationTestData::GOOGLE_AUTH_MIGRATION_URI);
 
         $this->assertContainsOnlyInstancesOf(TwoFAccount::class, $accounts);
@@ -352,6 +359,8 @@ class MigratorTest extends TestCase
         // in the migration payload) so we do not use get() to retrieve items
         $this->assertEquals($this->fakeTwofaccount->id, $accounts->first()->id);
         $this->assertEquals($this->fakeTwofaccount->id, $accounts->last()->id);
+        
+        $this->forgetMock(GoogleAuthMigrator::class);
     }
 
     /**
@@ -548,9 +557,4 @@ class MigratorTest extends TestCase
             ],
         ];
     }
-
-    protected function tearDown() : void
-    {
-        Mockery::close();
-    }
 }

+ 5 - 7
tests/Unit/TwoFAccountModelTest.php

@@ -11,9 +11,6 @@ use Illuminate\Support\Facades\Crypt;
 use Mockery\MockInterface;
 use PHPUnit\Framework\Attributes\CoversClass;
 use PHPUnit\Framework\Attributes\DataProvider;
-use PHPUnit\Framework\Attributes\PreserveGlobalState;
-use PHPUnit\Framework\Attributes\RequiresPhp;
-use PHPUnit\Framework\Attributes\RunInSeparateProcess;
 use Tests\ModelTestCase;
 
 /**
@@ -63,6 +60,7 @@ class TwoFAccountModelTest extends ModelTestCase
         ]);
 
         $this->assertEquals('STRING==', Crypt::decryptString($twofaccount->getAttributes()[$attribute]));
+        $this->forgetMock(SettingService::class);
     }
 
     /**
@@ -98,6 +96,7 @@ class TwoFAccountModelTest extends ModelTestCase
         $twofaccount = TwoFAccount::factory()->make();
 
         $this->assertEquals($twofaccount->getAttributes()[$attribute], $twofaccount->$attribute);
+        $this->forgetMock(SettingService::class);
     }
 
     /**
@@ -118,14 +117,12 @@ class TwoFAccountModelTest extends ModelTestCase
         $twofaccount = TwoFAccount::factory()->make();
 
         $this->assertEquals(__('errors.indecipherable'), $twofaccount->$attribute);
+        $this->forgetMock(SettingService::class);
     }
 
     /**
      * @test
      */
-    #[RunInSeparateProcess]
-    #[PreserveGlobalState(false)]
-    #[RequiresPhp('< 8.3.0')]
     public function test_secret_is_uppercased_and_padded_at_setup()
     {
         $settingService = $this->mock(SettingService::class, function (MockInterface $settingService) {
@@ -134,7 +131,7 @@ class TwoFAccountModelTest extends ModelTestCase
                 ->andReturn(false);
         });
 
-        $helpers = $this->mock('alias:' . Helpers::class, function (MockInterface $helpers) {
+        $helpers = $this->mock(Helpers::class, function (MockInterface $helpers) {
             $helpers->shouldReceive('PadToBase32Format')
                 ->andReturn('YYYY====');
         });
@@ -144,6 +141,7 @@ class TwoFAccountModelTest extends ModelTestCase
         ]);
 
         $this->assertEquals('YYYY====', $twofaccount->secret);
+        $this->forgetMock(SettingService::class);
     }
 
     /**

Some files were not shown because too many files changed in this diff