12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202 |
- <?php
- namespace Tests\Api\v1\Controllers;
- use App\Facades\Settings;
- use App\Models\Group;
- use App\Models\TwoFAccount;
- use App\Models\User;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Storage;
- use Tests\Classes\LocalFile;
- use Tests\Data\MigrationTestData;
- use Tests\Data\OtpTestData;
- use Tests\FeatureTestCase;
- /**
- * @covers \App\Api\v1\Controllers\TwoFAccountController
- * @covers \App\Api\v1\Resources\TwoFAccountReadResource
- * @covers \App\Api\v1\Resources\TwoFAccountStoreResource
- * @covers \App\Providers\MigrationServiceProvider
- * @covers \App\Providers\TwoFAuthServiceProvider
- */
- class TwoFAccountControllerTest extends FeatureTestCase
- {
- /**
- * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
- */
- protected $user;
- protected $anotherUser;
- /**
- * @var App\Models\Group
- */
- protected $userGroupA;
- protected $userGroupB;
- protected $anotherUserGroupA;
- protected $anotherUserGroupB;
- /**
- * @var App\Models\TwoFAccount
- */
- protected $twofaccountA;
- protected $twofaccountB;
- protected $twofaccountC;
- protected $twofaccountD;
- private const VALID_RESOURCE_STRUCTURE_WITHOUT_SECRET = [
- 'id',
- 'group_id',
- 'service',
- 'account',
- 'icon',
- 'otp_type',
- 'digits',
- 'algorithm',
- 'period',
- 'counter',
- ];
- private const VALID_RESOURCE_STRUCTURE_WITH_SECRET = [
- 'id',
- 'group_id',
- 'service',
- 'account',
- 'icon',
- 'otp_type',
- 'secret',
- 'digits',
- 'algorithm',
- 'period',
- 'counter',
- ];
- private const VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP = [
- 'generated_at',
- 'otp_type',
- 'password',
- 'period',
- ];
- private const VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP = [
- 'otp_type',
- 'password',
- 'counter',
- ];
- private const JSON_FRAGMENTS_FOR_CUSTOM_TOTP = [
- 'service' => OtpTestData::SERVICE,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'totp',
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_CUSTOM,
- 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
- 'period' => OtpTestData::PERIOD_CUSTOM,
- 'counter' => null,
- ];
- private const JSON_FRAGMENTS_FOR_DEFAULT_TOTP = [
- 'service' => null,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'totp',
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_DEFAULT,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'period' => OtpTestData::PERIOD_DEFAULT,
- 'counter' => null,
- ];
- private const JSON_FRAGMENTS_FOR_CUSTOM_HOTP = [
- 'service' => OtpTestData::SERVICE,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'hotp',
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_CUSTOM,
- 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
- 'period' => null,
- 'counter' => OtpTestData::COUNTER_CUSTOM,
- ];
- private const JSON_FRAGMENTS_FOR_DEFAULT_HOTP = [
- 'service' => null,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'hotp',
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_DEFAULT,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'period' => null,
- 'counter' => OtpTestData::COUNTER_DEFAULT,
- ];
- private const ARRAY_OF_INVALID_PARAMETERS = [
- 'account' => null,
- 'otp_type' => 'totp',
- 'secret' => OtpTestData::SECRET,
- ];
- /**
- * @test
- */
- public function setUp() : void
- {
- parent::setUp();
- $this->user = User::factory()->create();
- $this->userGroupA = Group::factory()->for($this->user)->create();
- $this->userGroupB = Group::factory()->for($this->user)->create();
- $this->twofaccountA = TwoFAccount::factory()->for($this->user)->create([
- 'group_id' => $this->userGroupA->id,
- ]);
- $this->twofaccountB = TwoFAccount::factory()->for($this->user)->create([
- 'group_id' => $this->userGroupA->id,
- ]);
- $this->anotherUser = User::factory()->create();
- $this->anotherUserGroupA = Group::factory()->for($this->anotherUser)->create();
- $this->anotherUserGroupB = Group::factory()->for($this->anotherUser)->create();
- $this->twofaccountC = TwoFAccount::factory()->for($this->anotherUser)->create([
- 'group_id' => $this->anotherUserGroupA->id,
- ]);
- $this->twofaccountD = TwoFAccount::factory()->for($this->anotherUser)->create([
- 'group_id' => $this->anotherUserGroupB->id,
- ]);
- }
- /**
- * @test
- *
- * @dataProvider indexUrlParameterProvider
- */
- public function test_index_returns_user_twofaccounts_only($urlParameter, $expected)
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts' . $urlParameter)
- ->assertOk()
- ->assertJsonCount(2, $key = null)
- ->assertJsonStructure([
- '*' => $expected,
- ])
- ->assertJsonFragment([
- 'id' => $this->twofaccountA->id,
- ])
- ->assertJsonFragment([
- 'id' => $this->twofaccountB->id,
- ])
- ->assertJsonMissing([
- 'id' => $this->twofaccountC->id,
- ])
- ->assertJsonMissing([
- 'id' => $this->twofaccountD->id,
- ]);
- }
- /**
- * Provide data for index tests
- */
- public function indexUrlParameterProvider()
- {
- return [
- 'VALID_RESOURCE_STRUCTURE_WITHOUT_SECRET' => [
- '',
- self::VALID_RESOURCE_STRUCTURE_WITHOUT_SECRET,
- ],
- 'VALID_RESOURCE_STRUCTURE_WITH_SECRET' => [
- '?withSecret=1',
- self::VALID_RESOURCE_STRUCTURE_WITH_SECRET,
- ],
- ];
- }
- /**
- * @test
- */
- public function test_show_returns_twofaccount_resource_with_secret()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts/' . $this->twofaccountA->id)
- ->assertOk()
- ->assertJsonStructure(self::VALID_RESOURCE_STRUCTURE_WITH_SECRET);
- }
- /**
- * @test
- */
- public function test_show_returns_twofaccount_resource_without_secret()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts/' . $this->twofaccountA->id . '?withSecret=0')
- ->assertOk()
- ->assertJsonStructure(self::VALID_RESOURCE_STRUCTURE_WITHOUT_SECRET);
- }
- /**
- * @test
- */
- // public function test_show_twofaccount_with_indeciphered_data_returns_replaced_data()
- // {
- // $dbEncryptionService = resolve('App\Services\DbEncryptionService');
- // $dbEncryptionService->setTo(true);
- // $twofaccount = TwoFAccount::factory()->create();
- // DB::table('twofaccounts')
- // ->where('id', $twofaccount->id)
- // ->update([
- // 'secret' => '**encrypted**',
- // 'account' => '**encrypted**',
- // ]);
- // $response = $this->actingAs($this->user, 'api-guard')
- // ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id)
- // ->assertJsonFragment([
- // 'secret' => '*indecipherable*',
- // 'account' => '*indecipherable*',
- // ]);
- // }
- /**
- * @test
- */
- public function test_show_missing_twofaccount_returns_not_found()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts/1000')
- ->assertNotFound()
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @test
- */
- public function test_show_twofaccount_of_another_user_is_forbidden()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts/' . $this->twofaccountC->id)
- ->assertForbidden()
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @dataProvider accountCreationProvider
- * @test
- */
- public function test_store_without_encryption_returns_success_with_consistent_resource_structure($payload, $expected)
- {
- Settings::set('useEncryption', false);
- Storage::put('test.png', 'emptied to prevent missing resource replaced by null by the model getter');
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts', $payload)
- ->assertCreated()
- ->assertJsonStructure(self::VALID_RESOURCE_STRUCTURE_WITH_SECRET)
- ->assertJsonFragment($expected);
- }
- /**
- * @dataProvider accountCreationProvider
- * @test
- */
- public function test_store_with_encryption_returns_success_with_consistent_resource_structure($payload, $expected)
- {
- Settings::set('useEncryption', true);
- Storage::put('test.png', 'emptied to prevent missing resource replaced by null by the model getter');
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts', $payload)
- ->assertCreated()
- ->assertJsonStructure(self::VALID_RESOURCE_STRUCTURE_WITH_SECRET)
- ->assertJsonFragment($expected);
- }
- /**
- * Provide data for TwoFAccount store tests
- */
- public function accountCreationProvider()
- {
- return [
- 'TOTP_FULL_CUSTOM_URI' => [
- [
- 'uri' => OtpTestData::TOTP_FULL_CUSTOM_URI,
- ],
- self::JSON_FRAGMENTS_FOR_CUSTOM_TOTP,
- ],
- 'TOTP_SHORT_URI' => [
- [
- 'uri' => OtpTestData::TOTP_SHORT_URI,
- ],
- self::JSON_FRAGMENTS_FOR_DEFAULT_TOTP,
- ],
- 'ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP' => [
- OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP,
- self::JSON_FRAGMENTS_FOR_CUSTOM_TOTP,
- ],
- 'ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP' => [
- OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP,
- self::JSON_FRAGMENTS_FOR_DEFAULT_TOTP,
- ],
- 'HOTP_FULL_CUSTOM_URI' => [
- [
- 'uri' => OtpTestData::HOTP_FULL_CUSTOM_URI,
- ],
- self::JSON_FRAGMENTS_FOR_CUSTOM_HOTP,
- ],
- 'HOTP_SHORT_URI' => [
- [
- 'uri' => OtpTestData::HOTP_SHORT_URI,
- ],
- self::JSON_FRAGMENTS_FOR_DEFAULT_HOTP,
- ],
- 'ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP' => [
- OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP,
- self::JSON_FRAGMENTS_FOR_CUSTOM_HOTP,
- ],
- 'ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP' => [
- OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP,
- self::JSON_FRAGMENTS_FOR_DEFAULT_HOTP,
- ],
- ];
- }
- /**
- * @test
- */
- public function test_store_with_invalid_uri_returns_validation_error()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => OtpTestData::INVALID_OTPAUTH_URI,
- ])
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_store_assigns_created_account_when_default_group_is_a_specific_one()
- {
- // Set the default group to a specific one
- $this->user['preferences->defaultGroup'] = $this->userGroupA->id;
- $this->user->save();
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => OtpTestData::TOTP_SHORT_URI,
- ])
- ->assertJsonFragment([
- 'group_id' => $this->userGroupA->id,
- ]);
- }
- /**
- * @test
- */
- public function test_store_assigns_created_account_when_default_group_is_the_active_one()
- {
- // Set the default group to be the active one
- $this->user['preferences->defaultGroup'] = -1;
- // Set the active group
- $this->user['preferences->activeGroup'] = $this->userGroupA->id;
- $this->user->save();
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => OtpTestData::TOTP_SHORT_URI,
- ])
- ->assertJsonFragment([
- 'group_id' => $this->userGroupA->id,
- ]);
- }
- /**
- * @test
- */
- public function test_store_assigns_created_account_when_default_group_is_no_group()
- {
- // Set the default group to No group
- $this->user['preferences->defaultGroup'] = 0;
- $this->user->save();
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => OtpTestData::TOTP_SHORT_URI,
- ])
- ->assertJsonFragment([
- 'group_id' => null,
- ]);
- }
- /**
- * @test
- */
- public function test_store_assigns_created_account_when_default_group_does_not_exist()
- {
- // Set the default group to a non-existing one
- $this->user['preferences->defaultGroup'] = 1000;
- $this->user->save();
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => OtpTestData::TOTP_SHORT_URI,
- ])
- ->assertJsonFragment([
- 'group_id' => null,
- ]);
- }
- /**
- * @test
- */
- public function test_update_totp_returns_success_with_updated_resource()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('PUT', '/api/v1/twofaccounts/' . $this->twofaccountA->id, OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)
- ->assertOk()
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_CUSTOM_TOTP);
- }
- /**
- * @test
- */
- public function test_update_hotp_returns_success_with_updated_resource()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('PUT', '/api/v1/twofaccounts/' . $this->twofaccountA->id, OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)
- ->assertOk()
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_CUSTOM_HOTP);
- }
- /**
- * @test
- */
- public function test_update_missing_twofaccount_returns_not_found()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('PUT', '/api/v1/twofaccounts/1000', OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)
- ->assertNotFound();
- }
- /**
- * @test
- */
- public function test_update_twofaccount_with_invalid_data_returns_validation_error()
- {
- $twofaccount = TwoFAccount::factory()->create();
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('PUT', '/api/v1/twofaccounts/' . $this->twofaccountA->id, self::ARRAY_OF_INVALID_PARAMETERS)
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_update_twofaccount_of_another_user_is_forbidden()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('PUT', '/api/v1/twofaccounts/' . $this->twofaccountC->id, OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)
- ->assertForbidden()
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @test
- */
- public function test_import_valid_gauth_payload_returns_success_with_consistent_resources()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/migration', [
- 'payload' => MigrationTestData::GOOGLE_AUTH_MIGRATION_URI,
- 'withSecret' => 1,
- ])
- ->assertOk()
- ->assertJsonCount(2, $key = null)
- ->assertJsonFragment([
- 'id' => 0,
- 'service' => OtpTestData::SERVICE,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'totp',
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_DEFAULT,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'period' => OtpTestData::PERIOD_DEFAULT,
- 'counter' => null,
- ])
- ->assertJsonFragment([
- 'id' => 0,
- 'service' => OtpTestData::SERVICE . '_bis',
- 'account' => OtpTestData::ACCOUNT . '_bis',
- 'otp_type' => 'totp',
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_DEFAULT,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'period' => OtpTestData::PERIOD_DEFAULT,
- 'counter' => null,
- ]);
- }
- /**
- * @test
- */
- public function test_import_with_invalid_gauth_payload_returns_validation_error()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/migration', [
- 'uri' => MigrationTestData::INVALID_GOOGLE_AUTH_MIGRATION_URI,
- ])
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_import_gauth_payload_with_duplicates_returns_negative_ids()
- {
- $twofaccount = TwoFAccount::factory()->create([
- 'otp_type' => 'totp',
- 'account' => OtpTestData::ACCOUNT,
- 'service' => OtpTestData::SERVICE,
- 'secret' => OtpTestData::SECRET,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'digits' => OtpTestData::DIGITS_DEFAULT,
- 'period' => OtpTestData::PERIOD_DEFAULT,
- 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
- 'icon' => '',
- ]);
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/migration', [
- 'payload' => MigrationTestData::GOOGLE_AUTH_MIGRATION_URI,
- ])
- ->assertOk()
- ->assertJsonFragment([
- 'id' => -1,
- 'service' => OtpTestData::SERVICE,
- 'account' => OtpTestData::ACCOUNT,
- ]);
- }
- /**
- * @test
- */
- public function test_import_invalid_gauth_payload_returns_bad_request()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/migration', [
- 'payload' => MigrationTestData::GOOGLE_AUTH_MIGRATION_URI_WITH_INVALID_DATA,
- ])
- ->assertStatus(400)
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @test
- */
- public function test_import_valid_aegis_json_file_returns_success()
- {
- $file = LocalFile::fake()->validAegisJsonFile();
- $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
- ->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/migration', [
- 'file' => $file,
- 'withSecret' => 1,
- ])
- ->assertOk()
- ->assertJsonCount(3, $key = null)
- ->assertJsonFragment([
- 'id' => 0,
- 'service' => OtpTestData::SERVICE,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'totp',
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_CUSTOM,
- 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
- 'period' => OtpTestData::PERIOD_CUSTOM,
- 'counter' => null,
- ])
- ->assertJsonFragment([
- 'id' => 0,
- 'service' => OtpTestData::SERVICE,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'hotp',
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_CUSTOM,
- 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
- 'period' => null,
- 'counter' => OtpTestData::COUNTER_CUSTOM,
- ])
- ->assertJsonFragment([
- 'id' => 0,
- 'service' => OtpTestData::STEAM,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'steamtotp',
- 'secret' => OtpTestData::STEAM_SECRET,
- 'digits' => OtpTestData::DIGITS_STEAM,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'period' => OtpTestData::PERIOD_DEFAULT,
- 'counter' => null,
- ]);
- }
- /**
- * @test
- *
- * @dataProvider invalidAegisJsonFileProvider
- */
- public function test_import_invalid_aegis_json_file_returns_bad_request($file)
- {
- $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
- ->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/migration', [
- 'file' => $file,
- ])
- ->assertStatus(400);
- }
- /**
- * Provide invalid Aegis JSON files for import tests
- */
- public function invalidAegisJsonFileProvider()
- {
- return [
- 'encryptedAegisJsonFile' => [
- LocalFile::fake()->encryptedAegisJsonFile(),
- ],
- 'invalidAegisJsonFile' => [
- LocalFile::fake()->invalidAegisJsonFile(),
- ],
- ];
- }
- /**
- * @test
- *
- * @dataProvider validPlainTextFileProvider
- */
- public function test_import_valid_plain_text_file_returns_success($file)
- {
- $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
- ->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/migration', [
- 'file' => $file,
- 'withSecret' => 1,
- ])
- ->assertOk()
- ->assertJsonCount(3, $key = null)
- ->assertJsonFragment([
- 'id' => 0,
- 'service' => OtpTestData::SERVICE,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'totp',
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_CUSTOM,
- 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
- 'period' => OtpTestData::PERIOD_CUSTOM,
- 'counter' => null,
- ])
- ->assertJsonFragment([
- 'id' => 0,
- 'service' => OtpTestData::SERVICE,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'hotp',
- 'secret' => OtpTestData::SECRET,
- 'digits' => OtpTestData::DIGITS_CUSTOM,
- 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
- 'period' => null,
- 'counter' => OtpTestData::COUNTER_CUSTOM,
- ])
- ->assertJsonFragment([
- 'id' => 0,
- 'service' => OtpTestData::STEAM,
- 'account' => OtpTestData::ACCOUNT,
- 'otp_type' => 'steamtotp',
- 'secret' => OtpTestData::STEAM_SECRET,
- 'digits' => OtpTestData::DIGITS_STEAM,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'period' => OtpTestData::PERIOD_DEFAULT,
- 'counter' => null,
- ]);
- }
- /**
- * Provide valid Plain Text files for import tests
- */
- public function validPlainTextFileProvider()
- {
- return [
- 'validPlainTextFile' => [
- LocalFile::fake()->validPlainTextFile(),
- ],
- 'validPlainTextFileWithNewLines' => [
- LocalFile::fake()->validPlainTextFileWithNewLines(),
- ],
- ];
- }
- /**
- * @test
- *
- * @dataProvider invalidPlainTextFileProvider
- */
- public function test_import_invalid_plain_text_file_returns_bad_request($file)
- {
- $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
- ->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/migration', [
- 'file' => $file,
- ])
- ->assertStatus(400);
- }
- /**
- * Provide invalid Plain Text files for import tests
- */
- public function invalidPlainTextFileProvider()
- {
- return [
- 'invalidPlainTextFileEmpty' => [
- LocalFile::fake()->invalidPlainTextFileEmpty(),
- ],
- 'invalidPlainTextFileNoUri' => [
- LocalFile::fake()->invalidPlainTextFileNoUri(),
- ],
- 'invalidPlainTextFileWithInvalidUri' => [
- LocalFile::fake()->invalidPlainTextFileWithInvalidUri(),
- ],
- 'invalidPlainTextFileWithInvalidLine' => [
- LocalFile::fake()->invalidPlainTextFileWithInvalidLine(),
- ],
- ];
- }
- /**
- * @test
- */
- public function test_reorder_returns_success()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/reorder', [
- 'orderedIds' => [$this->twofaccountB->id, $this->twofaccountA->id],
- ])
- ->assertStatus(200)
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @test
- */
- public function test_reorder_with_invalid_data_returns_validation_error()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/reorder', [
- 'orderedIds' => '3,2,1',
- ])
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_reorder_twofaccounts_of_another_user_is_forbidden()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/reorder', [
- 'orderedIds' => [$this->twofaccountB->id, $this->twofaccountD->id],
- ])
- ->assertForbidden()
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @test
- */
- public function test_preview_returns_success_with_resource()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/preview', [
- 'uri' => OtpTestData::TOTP_FULL_CUSTOM_URI,
- ])
- ->assertOk()
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_CUSTOM_TOTP);
- }
- /**
- * @test
- */
- public function test_preview_with_invalid_data_returns_validation_error()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/preview', [
- 'uri' => OtpTestData::INVALID_OTPAUTH_URI,
- ])
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_preview_with_unreachable_image_returns_success()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/preview', [
- 'uri' => OtpTestData::TOTP_URI_WITH_UNREACHABLE_IMAGE,
- ])
- ->assertOk()
- ->assertJsonFragment([
- 'icon' => null,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_using_totp_twofaccount_id_returns_consistent_resource()
- {
- $twofaccount = TwoFAccount::factory()->for($this->user)->create([
- 'otp_type' => 'totp',
- 'account' => OtpTestData::ACCOUNT,
- 'service' => OtpTestData::SERVICE,
- 'secret' => OtpTestData::SECRET,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'digits' => OtpTestData::DIGITS_DEFAULT,
- 'period' => OtpTestData::PERIOD_DEFAULT,
- 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
- 'icon' => '',
- ]);
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id . '/otp')
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP)
- ->assertJsonFragment([
- 'otp_type' => 'totp',
- 'period' => OtpTestData::PERIOD_DEFAULT,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_totp_uri_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/otp', [
- 'uri' => OtpTestData::TOTP_FULL_CUSTOM_URI,
- ])
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP)
- ->assertJsonFragment([
- 'otp_type' => 'totp',
- 'period' => OtpTestData::PERIOD_CUSTOM,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_totp_parameters_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/otp', OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP)
- ->assertJsonFragment([
- 'otp_type' => 'totp',
- 'period' => OtpTestData::PERIOD_CUSTOM,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_using_hotp_twofaccount_id_returns_consistent_resource()
- {
- $twofaccount = TwoFAccount::factory()->for($this->user)->create([
- 'otp_type' => 'hotp',
- 'account' => OtpTestData::ACCOUNT,
- 'service' => OtpTestData::SERVICE,
- 'secret' => OtpTestData::SECRET,
- 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
- 'digits' => OtpTestData::DIGITS_DEFAULT,
- 'period' => null,
- 'legacy_uri' => OtpTestData::HOTP_SHORT_URI,
- 'icon' => '',
- ]);
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id . '/otp')
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP)
- ->assertJsonFragment([
- 'otp_type' => 'hotp',
- 'counter' => OtpTestData::COUNTER_DEFAULT + 1,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_hotp_uri_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/otp', [
- 'uri' => OtpTestData::HOTP_FULL_CUSTOM_URI,
- ])
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP)
- ->assertJsonFragment([
- 'otp_type' => 'hotp',
- 'counter' => OtpTestData::COUNTER_CUSTOM + 1,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_hotp_parameters_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/otp', OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP)
- ->assertJsonFragment([
- 'otp_type' => 'hotp',
- 'counter' => OtpTestData::COUNTER_CUSTOM + 1,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_multiple_inputs_returns_bad_request()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/otp', [
- 'uri' => OtpTestData::HOTP_FULL_CUSTOM_URI,
- 'key' => 'value',
- ])
- ->assertStatus(400)
- ->assertJsonStructure([
- 'message',
- 'reason',
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_using_indecipherable_twofaccount_id_returns_bad_request()
- {
- Settings::set('useEncryption', true);
- $twofaccount = TwoFAccount::factory()->for($this->user)->create();
- DB::table('twofaccounts')
- ->where('id', $twofaccount->id)
- ->update([
- 'secret' => '**encrypted**',
- ]);
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id . '/otp')
- ->assertStatus(400)
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_using_missing_twofaccount_id_returns_not_found()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts/1000/otp')
- ->assertNotFound();
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_invalid_uri_returns_validation_error()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/otp', [
- 'uri' => OtpTestData::INVALID_OTPAUTH_URI,
- ])
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_invalid_parameters_returns_validation_error()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('POST', '/api/v1/twofaccounts/otp', self::ARRAY_OF_INVALID_PARAMETERS)
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_get_otp_of_another_user_twofaccount_is_forbidden()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts/' . $this->twofaccountC->id . '/otp')
- ->assertForbidden()
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @test
- */
- public function test_count_returns_right_number_of_twofaccounts()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('GET', '/api/v1/twofaccounts/count')
- ->assertStatus(200)
- ->assertExactJson([
- 'count' => 2,
- ]);
- }
- /**
- * @test
- */
- public function test_withdraw_returns_success()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('PATCH', '/api/v1/twofaccounts/withdraw?ids=1,2')
- ->assertOk()
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @test
- */
- public function test_withdraw_too_many_ids_returns_bad_request()
- {
- TwoFAccount::factory()->count(102)->for($this->user)->create();
- $ids = DB::table('twofaccounts')->where('user_id', $this->user->id)->pluck('id')->implode(',');
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('PATCH', '/api/v1/twofaccounts/withdraw?ids=' . $ids)
- ->assertStatus(400)
- ->assertJsonStructure([
- 'message',
- 'reason',
- ]);
- }
- /**
- * @test
- */
- public function test_destroy_twofaccount_returns_success()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('DELETE', '/api/v1/twofaccounts/' . $this->twofaccountA->id)
- ->assertNoContent();
- }
- /**
- * @test
- */
- public function test_destroy_missing_twofaccount_returns_not_found()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('DELETE', '/api/v1/twofaccounts/1000')
- ->assertNotFound();
- }
- /**
- * @test
- */
- public function test_destroy_twofaccount_of_another_user_is_forbidden()
- {
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('DELETE', '/api/v1/twofaccounts/' . $this->twofaccountC->id)
- ->assertForbidden()
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @test
- */
- public function test_batch_destroy_twofaccount_returns_success()
- {
- TwoFAccount::factory()->count(3)->for($this->user)->create();
- $ids = DB::table('twofaccounts')->where('user_id', $this->user->id)->pluck('id')->implode(',');
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('DELETE', '/api/v1/twofaccounts?ids=' . $this->twofaccountA->id . ',' . $this->twofaccountB->id)
- ->assertNoContent();
- }
- /**
- * @test
- */
- public function test_batch_destroy_too_many_twofaccounts_returns_bad_request()
- {
- TwoFAccount::factory()->count(102)->for($this->user)->create();
- $ids = DB::table('twofaccounts')->where('user_id', $this->user->id)->pluck('id')->implode(',');
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('DELETE', '/api/v1/twofaccounts?ids=' . $ids)
- ->assertStatus(400)
- ->assertJsonStructure([
- 'message',
- 'reason',
- ]);
- }
- /**
- * @test
- */
- public function test_batch_destroy_twofaccount_of_another_user_is_forbidden()
- {
- TwoFAccount::factory()->count(2)->for($this->anotherUser)->create();
- $ids = DB::table('twofaccounts')
- ->where('user_id', $this->anotherUser->id)
- ->pluck('id')
- ->implode(',');
- $response = $this->actingAs($this->user, 'api-guard')
- ->json('DELETE', '/api/v1/twofaccounts?ids=' . $ids)
- ->assertForbidden()
- ->assertJsonStructure([
- 'message',
- ]);
- }
- }
|