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;
|
2022-12-09 10:52:17 +01:00
|
|
|
use Tests\Data\OtpTestData;
|
2022-11-22 15:15:52 +01:00
|
|
|
use Tests\FeatureTestCase;
|
2022-12-09 10:52:17 +01:00
|
|
|
use Tests\Data\MigrationTestData;
|
2021-11-22 01:09:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @covers \App\Services\TwoFAccountService
|
2022-12-09 10:52:17 +01:00
|
|
|
* @covers \App\Facades\TwoFAccounts
|
2021-11-22 01:09:54 +01:00
|
|
|
*/
|
|
|
|
class TwoFAccountServiceTest extends FeatureTestCase
|
|
|
|
{
|
|
|
|
/**
|
2021-12-02 13:15:53 +01:00
|
|
|
* App\Models\TwoFAccount $customTotpTwofaccount
|
2021-11-22 01:09:54 +01:00
|
|
|
*/
|
|
|
|
protected $customTotpTwofaccount;
|
|
|
|
|
2021-11-30 17:39:33 +01:00
|
|
|
/**
|
2021-12-02 13:15:53 +01:00
|
|
|
* App\Models\Group $group
|
2021-11-30 17:39:33 +01:00
|
|
|
*/
|
|
|
|
protected $group;
|
|
|
|
|
2021-11-22 01:09:54 +01:00
|
|
|
/**
|
2021-12-02 13:15:53 +01:00
|
|
|
* App\Models\TwoFAccount $customTotpTwofaccount
|
2021-11-22 01:09:54 +01:00
|
|
|
*/
|
|
|
|
protected $customHotpTwofaccount;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2022-12-09 10:52:17 +01:00
|
|
|
public function setUp(): void
|
2021-11-22 01:09:54 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
$this->customTotpTwofaccount = new TwoFAccount;
|
2022-07-05 10:10:24 +02:00
|
|
|
$this->customTotpTwofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI;
|
2022-11-22 15:15:52 +01:00
|
|
|
$this->customTotpTwofaccount->service = OtpTestData::SERVICE;
|
|
|
|
$this->customTotpTwofaccount->account = OtpTestData::ACCOUNT;
|
2022-12-13 09:08:22 +01:00
|
|
|
$this->customTotpTwofaccount->icon = OtpTestData::ICON_PNG;
|
2022-11-22 15:15:52 +01:00
|
|
|
$this->customTotpTwofaccount->otp_type = 'totp';
|
|
|
|
$this->customTotpTwofaccount->secret = OtpTestData::SECRET;
|
|
|
|
$this->customTotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM;
|
|
|
|
$this->customTotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
|
|
|
|
$this->customTotpTwofaccount->period = OtpTestData::PERIOD_CUSTOM;
|
|
|
|
$this->customTotpTwofaccount->counter = null;
|
2021-11-22 01:09:54 +01:00
|
|
|
$this->customTotpTwofaccount->save();
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
$this->customHotpTwofaccount = new TwoFAccount;
|
2022-07-05 10:10:24 +02:00
|
|
|
$this->customHotpTwofaccount->legacy_uri = OtpTestData::HOTP_FULL_CUSTOM_URI;
|
2022-11-22 15:15:52 +01:00
|
|
|
$this->customHotpTwofaccount->service = OtpTestData::SERVICE;
|
|
|
|
$this->customHotpTwofaccount->account = OtpTestData::ACCOUNT;
|
2022-12-13 09:08:22 +01:00
|
|
|
$this->customHotpTwofaccount->icon = OtpTestData::ICON_PNG;
|
2022-11-22 15:15:52 +01:00
|
|
|
$this->customHotpTwofaccount->otp_type = 'hotp';
|
|
|
|
$this->customHotpTwofaccount->secret = OtpTestData::SECRET;
|
|
|
|
$this->customHotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM;
|
|
|
|
$this->customHotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
|
|
|
|
$this->customHotpTwofaccount->period = null;
|
|
|
|
$this->customHotpTwofaccount->counter = OtpTestData::COUNTER_CUSTOM;
|
2021-11-22 01:09:54 +01:00
|
|
|
$this->customHotpTwofaccount->save();
|
2021-11-30 17:39:33 +01:00
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
$this->group = new Group;
|
2021-11-30 17:39:33 +01:00
|
|
|
$this->group->name = 'MyGroup';
|
|
|
|
$this->group->save();
|
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->group->twofaccounts()->saveMany($twofaccounts);
|
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_model_ids_deletes_relation()
|
|
|
|
{
|
|
|
|
$twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
|
|
|
|
$this->group->twofaccounts()->saveMany($twofaccounts);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2022-07-30 11:38:20 +02: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_single_id_deletes_relation()
|
|
|
|
{
|
|
|
|
$twofaccounts = collect([$this->customHotpTwofaccount, $this->customTotpTwofaccount]);
|
|
|
|
$this->group->twofaccounts()->saveMany($twofaccounts);
|
2022-11-22 15:15:52 +01:00
|
|
|
|
2022-07-30 11:38:20 +02: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()
|
|
|
|
{
|
2022-07-30 11:38:20 +02:00
|
|
|
$this->assertNull(TwoFAccounts::withdraw(null));
|
2021-11-30 17:39:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_delete_comma_separated_ids()
|
2022-11-22 15:15:52 +01:00
|
|
|
{
|
|
|
|
TwoFAccounts::delete($this->customHotpTwofaccount->id . ',' . $this->customTotpTwofaccount->id);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
$this->assertDatabaseMissing('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->customTotpTwofaccount->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
$this->assertDatabaseMissing('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->customHotpTwofaccount->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_delete_array_of_ids()
|
2022-11-22 15:15:52 +01:00
|
|
|
{
|
2022-07-30 11:38:20 +02:00
|
|
|
TwoFAccounts::delete([$this->customTotpTwofaccount->id, $this->customHotpTwofaccount->id]);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
$this->assertDatabaseMissing('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->customTotpTwofaccount->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
$this->assertDatabaseMissing('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->customHotpTwofaccount->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_delete_single_id()
|
2022-11-22 15:15:52 +01:00
|
|
|
{
|
2022-07-30 11:38:20 +02:00
|
|
|
TwoFAccounts::delete($this->customTotpTwofaccount->id);
|
2021-11-30 17:39:33 +01:00
|
|
|
|
|
|
|
$this->assertDatabaseMissing('twofaccounts', [
|
2022-11-22 15:15:52 +01:00
|
|
|
'id' => $this->customTotpTwofaccount->id,
|
2021-11-30 17:39:33 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2022-06-21 17:27:47 +02:00
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_convert_migration_from_gauth_returns_correct_accounts()
|
2022-11-22 15:15:52 +01:00
|
|
|
{
|
2022-12-09 10:52:17 +01:00
|
|
|
$twofaccounts = TwoFAccounts::migrate(MigrationTestData::GOOGLE_AUTH_MIGRATION_URI);
|
2022-06-21 17:27:47 +02:00
|
|
|
|
|
|
|
$this->assertCount(2, $twofaccounts);
|
|
|
|
|
|
|
|
$this->assertEquals('totp', $twofaccounts->first()->otp_type);
|
2022-07-05 10:10:24 +02:00
|
|
|
$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);
|
2022-06-21 17:27:47 +02:00
|
|
|
$this->assertEquals(null, $twofaccounts->first()->counter);
|
2022-07-05 10:10:24 +02:00
|
|
|
$this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccounts->first()->algorithm);
|
2022-06-21 17:27:47 +02:00
|
|
|
|
|
|
|
$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);
|
2022-07-05 10:10:24 +02:00
|
|
|
$this->assertEquals(OtpTestData::SECRET, $twofaccounts->last()->secret);
|
|
|
|
$this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccounts->last()->digits);
|
|
|
|
$this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccounts->last()->period);
|
2022-06-21 17:27:47 +02:00
|
|
|
$this->assertEquals(null, $twofaccounts->last()->counter);
|
2022-07-05 10:10:24 +02:00
|
|
|
$this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccounts->last()->algorithm);
|
2022-06-21 17:27:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
|
|
|
public function test_convert_migration_from_gauth_returns_flagged_duplicates()
|
|
|
|
{
|
|
|
|
$parameters = [
|
2022-07-05 10:10:24 +02:00
|
|
|
'service' => OtpTestData::SERVICE,
|
|
|
|
'account' => OtpTestData::ACCOUNT,
|
2022-12-13 09:08:22 +01:00
|
|
|
'icon' => OtpTestData::ICON_PNG,
|
2022-06-21 17:27:47 +02:00
|
|
|
'otp_type' => 'totp',
|
2022-07-05 10:10:24 +02:00
|
|
|
'secret' => OtpTestData::SECRET,
|
|
|
|
'digits' => OtpTestData::DIGITS_DEFAULT,
|
|
|
|
'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
|
|
|
|
'period' => OtpTestData::PERIOD_DEFAULT,
|
2022-06-21 17:27:47 +02:00
|
|
|
];
|
2022-07-05 10:10:24 +02:00
|
|
|
$twofaccount = new TwoFAccount;
|
|
|
|
$twofaccount->fillWithOtpParameters($parameters)->save();
|
|
|
|
|
2022-11-22 15:15:52 +01:00
|
|
|
$parameters['service'] = OtpTestData::SERVICE . '_bis';
|
|
|
|
$parameters['account'] = OtpTestData::ACCOUNT . '_bis';
|
2022-06-21 17:27:47 +02:00
|
|
|
|
2022-07-05 10:10:24 +02:00
|
|
|
$twofaccount = new TwoFAccount;
|
|
|
|
$twofaccount->fillWithOtpParameters($parameters)->save();
|
2022-06-21 17:27:47 +02:00
|
|
|
|
2022-12-09 10:52:17 +01:00
|
|
|
$twofaccounts = TwoFAccounts::migrate(MigrationTestData::GOOGLE_AUTH_MIGRATION_URI);
|
2022-06-21 17:27:47 +02:00
|
|
|
|
|
|
|
$this->assertEquals(-1, $twofaccounts->first()->id);
|
|
|
|
$this->assertEquals(-1, $twofaccounts->last()->id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @test
|
|
|
|
*/
|
2022-10-07 18:58:48 +02:00
|
|
|
public function test_convert_invalid_migration_from_gauth_returns_InvalidMigrationData_exception()
|
2022-06-21 17:27:47 +02:00
|
|
|
{
|
2022-10-07 18:58:48 +02:00
|
|
|
$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);
|
2022-06-21 17:27:47 +02:00
|
|
|
}
|
2022-11-22 15:15:52 +01:00
|
|
|
}
|