Fix tests after setting PHP 8.2 as default

This commit is contained in:
Bubka 2024-04-29 16:19:16 +02:00
parent a1ca1ec9bf
commit 96978accb3
9 changed files with 52 additions and 53 deletions

View File

@ -45,7 +45,7 @@ public function migrate(mixed $migrationPayload) : Collection
$parameters['otp_type'] = GAuthValueMapping::OTP_TYPE[OtpType::name($otp_parameters->getType())]; $parameters['otp_type'] = GAuthValueMapping::OTP_TYPE[OtpType::name($otp_parameters->getType())];
$parameters['service'] = $otp_parameters->getIssuer(); $parameters['service'] = $otp_parameters->getIssuer();
$parameters['account'] = str_replace($parameters['service'] . ':', '', $otp_parameters->getName()); $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['algorithm'] = GAuthValueMapping::ALGORITHM[Algorithm::name($otp_parameters->getAlgorithm())];
$parameters['digits'] = GAuthValueMapping::DIGIT_COUNT[DigitCount::name($otp_parameters->getDigits())]; $parameters['digits'] = GAuthValueMapping::DIGIT_COUNT[DigitCount::name($otp_parameters->getDigits())];
$parameters['counter'] = $parameters['otp_type'] === TwoFAccount::HOTP ? $otp_parameters->getCounter() : null; $parameters['counter'] = $parameters['otp_type'] === TwoFAccount::HOTP ? $otp_parameters->getCounter() : null;
@ -73,4 +73,11 @@ public function migrate(mixed $migrationPayload) : Collection
return collect($twofaccounts); return collect($twofaccounts);
} }
/**
* Encode into uppercase Base32
*/
protected function toBase32(string $str) {
return Base32::encodeUpper($str);
}
} }

View File

@ -29,7 +29,7 @@ public static function encode(string $data)
Log::info('data encoded to QR code'); Log::info('data encoded to QR code');
return $qrcode->render($data); return $qrcode->render('stringToEncode');
} }
/** /**

View File

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

View File

@ -473,4 +473,20 @@ public function test_destroy_group_resets_user_preferences()
$this->assertEquals(0, $this->user->preferences['defaultGroup']); $this->assertEquals(0, $this->user->preferences['defaultGroup']);
$this->assertEquals(0, $this->user->preferences['activeGroup']); $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);
}
} }

View File

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

File diff suppressed because one or more lines are too long

View File

@ -4,14 +4,8 @@
use App\Events\GroupDeleting; use App\Events\GroupDeleting;
use App\Listeners\DissociateTwofaccountFromGroup; use App\Listeners\DissociateTwofaccountFromGroup;
use App\Models\Group;
use App\Models\TwoFAccount;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Mockery\MockInterface;
use PHPUnit\Framework\Attributes\CoversClass; use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\PreserveGlobalState;
use PHPUnit\Framework\Attributes\RequiresPhp;
use PHPUnit\Framework\Attributes\RunInSeparateProcess;
use Tests\TestCase; use Tests\TestCase;
/** /**
@ -20,27 +14,6 @@
#[CoversClass(DissociateTwofaccountFromGroup::class)] #[CoversClass(DissociateTwofaccountFromGroup::class)]
class DissociateTwofaccountFromGroupTest extends TestCase 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 * @test
*/ */

View File

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

View File

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