2FAuth/tests/Feature/Services/TwoFAccountServiceTest.php

340 lines
11 KiB
PHP
Raw Normal View History

2021-11-22 01:09:54 +01:00
<?php
2021-11-30 17:39:33 +01:00
namespace Tests\Feature\Services;
2021-11-22 01:09:54 +01:00
2022-11-22 15:15:52 +01:00
use App\Facades\TwoFAccounts;
2021-12-02 13:15:53 +01:00
use App\Models\Group;
use App\Models\TwoFAccount;
use App\Models\User;
2023-08-01 11:28:27 +02:00
use App\Services\TwoFAccountService;
use PHPUnit\Framework\Attributes\CoversClass;
2022-12-13 12:07:29 +01:00
use Tests\Data\MigrationTestData;
2022-12-09 10:52:17 +01:00
use Tests\Data\OtpTestData;
2022-11-22 15:15:52 +01:00
use Tests\FeatureTestCase;
2021-11-22 01:09:54 +01:00
/**
2023-08-01 11:28:27 +02:00
* TwoFAccountServiceTest test class
2021-11-22 01:09:54 +01:00
*/
2023-08-01 11:28:27 +02:00
#[CoversClass(TwoFAccountService::class)]
#[CoversClass(TwoFAccounts::class)]
2021-11-22 01:09:54 +01:00
class TwoFAccountServiceTest extends FeatureTestCase
{
/**
* @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
2021-11-22 01:09:54 +01:00
*/
protected $user;
2021-11-22 01:09:54 +01:00
2021-11-30 17:39:33 +01:00
/**
* @var \App\Models\TwoFAccount
2021-11-30 17:39:33 +01:00
*/
2023-03-10 22:59:46 +01:00
protected $customTotpTwofaccount;
protected $customHotpTwofaccount;
2021-11-30 17:39:33 +01:00
2021-11-22 01:09:54 +01:00
/**
* @var \App\Models\Group
2021-11-22 01:09:54 +01:00
*/
2023-03-10 22:59:46 +01:00
protected $userGroupA;
protected $userGroupB;
2021-11-22 01:09:54 +01:00
/**
* @test
*/
2022-12-13 12:07:29 +01:00
public function setUp() : void
2021-11-22 01:09:54 +01:00
{
parent::setUp();
2023-03-10 22:59:46 +01:00
$this->user = User::factory()->create();
$this->userGroupA = Group::factory()->for($this->user)->create();
$this->userGroupB = Group::factory()->for($this->user)->create();
$this->customTotpTwofaccount = TwoFAccount::factory()->for($this->user)->create([
'legacy_uri' => OtpTestData::TOTP_FULL_CUSTOM_URI,
'service' => OtpTestData::SERVICE,
'account' => OtpTestData::ACCOUNT,
'icon' => OtpTestData::ICON_PNG,
'otp_type' => 'totp',
'secret' => OtpTestData::SECRET,
'digits' => OtpTestData::DIGITS_CUSTOM,
'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
'period' => OtpTestData::PERIOD_CUSTOM,
'counter' => null,
]);
2021-11-22 01:09:54 +01:00
$this->customHotpTwofaccount = TwoFAccount::factory()->for($this->user)->create([
'legacy_uri' => OtpTestData::HOTP_FULL_CUSTOM_URI,
'service' => OtpTestData::SERVICE,
'account' => OtpTestData::ACCOUNT,
'icon' => OtpTestData::ICON_PNG,
'otp_type' => 'hotp',
'secret' => OtpTestData::SECRET,
'digits' => OtpTestData::DIGITS_CUSTOM,
'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
'period' => null,
'counter' => OtpTestData::COUNTER_CUSTOM,
]);
2021-11-22 01:09:54 +01:00
}
2021-11-30 17:39:33 +01:00
/**
* @test
*/
public function test_withdraw_comma_separated_ids_deletes_relation()
{
$twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
$this->userGroupA->twofaccounts()->saveMany($twofaccounts);
$this->assertDatabaseHas('twofaccounts', [
'id' => $this->customHotpTwofaccount->id,
'group_id' => $this->userGroupA->id,
]);
$this->assertDatabaseHas('twofaccounts', [
'id' => $this->customTotpTwofaccount->id,
'group_id' => $this->userGroupA->id,
]);
2022-11-22 15:15:52 +01:00
TwoFAccounts::withdraw($this->customHotpTwofaccount->id . ',' . $this->customTotpTwofaccount->id);
2021-11-30 17:39:33 +01:00
$this->assertDatabaseHas('twofaccounts', [
2022-11-22 15:15:52 +01:00
'id' => $this->customTotpTwofaccount->id,
'group_id' => null,
2021-11-30 17:39:33 +01:00
]);
$this->assertDatabaseHas('twofaccounts', [
2022-11-22 15:15:52 +01:00
'id' => $this->customHotpTwofaccount->id,
'group_id' => null,
2021-11-30 17:39:33 +01:00
]);
}
/**
* @test
*/
public function test_withdraw_array_of_ids_deletes_relation()
2021-11-30 17:39:33 +01:00
{
$twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
$this->userGroupA->twofaccounts()->saveMany($twofaccounts);
$this->assertDatabaseHas('twofaccounts', [
'id' => $this->customHotpTwofaccount->id,
'group_id' => $this->userGroupA->id,
]);
2022-11-22 15:15:52 +01:00
$this->assertDatabaseHas('twofaccounts', [
'id' => $this->customTotpTwofaccount->id,
'group_id' => $this->userGroupA->id,
]);
TwoFAccounts::withdraw([$this->customHotpTwofaccount->id, $this->customTotpTwofaccount->id]);
2021-11-30 17:39:33 +01:00
$this->assertDatabaseHas('twofaccounts', [
2022-11-22 15:15:52 +01:00
'id' => $this->customTotpTwofaccount->id,
'group_id' => null,
2021-11-30 17:39:33 +01:00
]);
$this->assertDatabaseHas('twofaccounts', [
2022-11-22 15:15:52 +01:00
'id' => $this->customHotpTwofaccount->id,
'group_id' => null,
2021-11-30 17:39:33 +01:00
]);
}
/**
* @test
*/
public function test_withdraw_single_id_deletes_relation()
{
$twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
$this->userGroupA->twofaccounts()->saveMany($twofaccounts);
$this->assertDatabaseHas('twofaccounts', [
'id' => $this->customTotpTwofaccount->id,
'group_id' => $this->userGroupA->id,
]);
2022-11-22 15:15:52 +01:00
TwoFAccounts::withdraw($this->customTotpTwofaccount->id);
2021-11-30 17:39:33 +01:00
$this->assertDatabaseHas('twofaccounts', [
2022-11-22 15:15:52 +01:00
'id' => $this->customTotpTwofaccount->id,
'group_id' => null,
2021-11-30 17:39:33 +01:00
]);
}
/**
* @test
*/
public function test_withdraw_missing_ids_returns_void()
{
$this->assertNull(TwoFAccounts::withdraw(null));
2021-11-30 17:39:33 +01:00
}
/**
* @test
*/
public function test_migrate_from_gauth_returns_correct_accounts()
2022-11-22 15:15:52 +01:00
{
$this->actingAs($this->user);
2021-11-30 17:39:33 +01:00
2022-12-09 10:52:17 +01:00
$twofaccounts = TwoFAccounts::migrate(MigrationTestData::GOOGLE_AUTH_MIGRATION_URI);
$this->assertCount(2, $twofaccounts);
$this->assertEquals('totp', $twofaccounts->first()->otp_type);
$this->assertEquals(OtpTestData::SERVICE, $twofaccounts->first()->service);
$this->assertEquals(OtpTestData::ACCOUNT, $twofaccounts->first()->account);
$this->assertEquals(OtpTestData::SECRET, $twofaccounts->first()->secret);
$this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccounts->first()->digits);
$this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccounts->first()->period);
$this->assertEquals(null, $twofaccounts->first()->counter);
$this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccounts->first()->algorithm);
$this->assertEquals('totp', $twofaccounts->last()->otp_type);
2022-11-22 15:15:52 +01:00
$this->assertEquals(OtpTestData::SERVICE . '_bis', $twofaccounts->last()->service);
$this->assertEquals(OtpTestData::ACCOUNT . '_bis', $twofaccounts->last()->account);
$this->assertEquals(OtpTestData::SECRET, $twofaccounts->last()->secret);
$this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccounts->last()->digits);
$this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccounts->last()->period);
$this->assertEquals(null, $twofaccounts->last()->counter);
$this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccounts->last()->algorithm);
}
/**
* @test
*/
public function test_migrate_from_gauth_returns_flagged_duplicates()
{
2023-03-25 12:24:12 +01:00
$this->actingAs($this->user, 'api-guard');
2023-03-10 22:59:46 +01:00
$parameters = [
'service' => OtpTestData::SERVICE,
'account' => OtpTestData::ACCOUNT,
'icon' => OtpTestData::ICON_PNG,
'otp_type' => 'totp',
'secret' => OtpTestData::SECRET,
'digits' => OtpTestData::DIGITS_DEFAULT,
'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
'period' => OtpTestData::PERIOD_DEFAULT,
];
2023-03-25 12:24:12 +01:00
TwoFAccount::factory()->for($this->user)->create($parameters);
2022-11-22 15:15:52 +01:00
$parameters['service'] = OtpTestData::SERVICE . '_bis';
$parameters['account'] = OtpTestData::ACCOUNT . '_bis';
2023-03-25 12:24:12 +01:00
TwoFAccount::factory()->for($this->user)->create($parameters);
2022-12-09 10:52:17 +01:00
$twofaccounts = TwoFAccounts::migrate(MigrationTestData::GOOGLE_AUTH_MIGRATION_URI);
$this->assertEquals(-1, $twofaccounts->first()->id);
$this->assertEquals(-1, $twofaccounts->last()->id);
}
/**
* @test
*/
public function test_migrate_invalid_migration_from_gauth_returns_InvalidMigrationData_exception()
{
$this->expectException(\App\Exceptions\InvalidMigrationDataException::class);
2022-12-09 10:52:17 +01:00
$twofaccounts = TwoFAccounts::migrate(MigrationTestData::GOOGLE_AUTH_MIGRATION_URI_WITH_INVALID_DATA);
}
/**
* @test
*/
public function test_export_single_id_returns_collection()
{
$twofaccounts = TwoFAccounts::export($this->customTotpTwofaccount->id);
$this->assertContainsOnlyInstancesOf(TwoFAccount::class, $twofaccounts);
$this->assertObjectEquals($this->customTotpTwofaccount, $twofaccounts->first());
}
/**
* @test
*/
public function test_export_comma_separated_ids_returns_collection()
{
$twofaccounts = TwoFAccounts::export($this->customTotpTwofaccount->id . ',' . $this->customHotpTwofaccount->id);
$this->assertContainsOnlyInstancesOf(TwoFAccount::class, $twofaccounts);
$this->assertObjectEquals($this->customTotpTwofaccount, $twofaccounts->first());
$this->assertObjectEquals($this->customHotpTwofaccount, $twofaccounts->last());
}
/**
* @test
*/
public function test_export_array_of_ids_returns_collection()
{
$twofaccounts = TwoFAccounts::export([$this->customTotpTwofaccount->id, $this->customHotpTwofaccount->id]);
$this->assertContainsOnlyInstancesOf(TwoFAccount::class, $twofaccounts);
$this->assertObjectEquals($this->customTotpTwofaccount, $twofaccounts->first());
$this->assertObjectEquals($this->customHotpTwofaccount, $twofaccounts->last());
}
/**
* @test
*/
public function test_delete_comma_separated_ids()
{
$twofaccounts = TwoFAccount::factory()->count(2)->for($this->user)->create();
$this->assertDatabaseHas('twofaccounts', [
'id' => $twofaccounts[0]->id,
]);
$this->assertDatabaseHas('twofaccounts', [
'id' => $twofaccounts[1]->id,
]);
TwoFAccounts::delete($twofaccounts[0]->id . ',' . $twofaccounts[1]->id);
$this->assertDatabaseMissing('twofaccounts', [
'id' => $twofaccounts[0]->id,
]);
$this->assertDatabaseMissing('twofaccounts', [
'id' => $twofaccounts[1]->id,
]);
}
/**
* @test
*/
public function test_delete_array_of_ids()
{
$twofaccounts = TwoFAccount::factory()->count(2)->for($this->user)->create();
$this->assertDatabaseHas('twofaccounts', [
'id' => $twofaccounts[0]->id,
]);
$this->assertDatabaseHas('twofaccounts', [
'id' => $twofaccounts[1]->id,
]);
TwoFAccounts::delete([$twofaccounts[0]->id, $twofaccounts[1]->id]);
$this->assertDatabaseMissing('twofaccounts', [
'id' => $twofaccounts[0]->id,
]);
$this->assertDatabaseMissing('twofaccounts', [
'id' => $twofaccounts[1]->id,
]);
}
/**
* @test
*/
public function test_delete_single_id()
{
$twofaccount = TwoFAccount::factory()->for($this->user)->create();
$this->assertDatabaseHas('twofaccounts', [
'id' => $twofaccount->id,
]);
2023-03-10 22:59:46 +01:00
TwoFAccounts::delete($twofaccount->id);
$this->assertDatabaseMissing('twofaccounts', [
'id' => $twofaccount->id,
]);
}
2022-11-22 15:15:52 +01:00
}