123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952 |
- <?php
- namespace Tests\Api\v1\Controllers;
- use App\User;
- use App\Group;
- use Tests\FeatureTestCase;
- use App\TwoFAccount;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Storage;
- /**
- * @covers \App\Api\v1\Controllers\TwoFAccountController
- * @covers \App\Api\v1\Resources\TwoFAccountReadResource
- * @covers \App\Api\v1\Resources\TwoFAccountStoreResource
- */
- class TwoFAccountControllerTest extends FeatureTestCase
- {
- /**
- * @var \App\User
- */
- protected $user;
- /**
- * @var \App\Group
- */
- protected $group;
- private const ACCOUNT = 'account';
- private const SERVICE = 'service';
- private const SECRET = 'A4GRFHVVRBGY7UIW';
- private const ALGORITHM_DEFAULT = 'sha1';
- private const ALGORITHM_CUSTOM = 'sha256';
- private const DIGITS_DEFAULT = 6;
- private const DIGITS_CUSTOM = 7;
- private const PERIOD_DEFAULT = 30;
- private const PERIOD_CUSTOM = 40;
- private const COUNTER_DEFAULT = 0;
- private const COUNTER_CUSTOM = 5;
- private const IMAGE = 'https%3A%2F%2Fen.opensuse.org%2Fimages%2F4%2F44%2FButton-filled-colour.png';
- private const ICON = 'test.png';
- private const TOTP_FULL_CUSTOM_URI = 'otpauth://totp/'.self::SERVICE.':'.self::ACCOUNT.'?secret='.self::SECRET.'&issuer='.self::SERVICE.'&digits='.self::DIGITS_CUSTOM.'&period='.self::PERIOD_CUSTOM.'&algorithm='.self::ALGORITHM_CUSTOM.'&image='.self::IMAGE;
- private const HOTP_FULL_CUSTOM_URI = 'otpauth://hotp/'.self::SERVICE.':'.self::ACCOUNT.'?secret='.self::SECRET.'&issuer='.self::SERVICE.'&digits='.self::DIGITS_CUSTOM.'&counter='.self::COUNTER_CUSTOM.'&algorithm='.self::ALGORITHM_CUSTOM.'&image='.self::IMAGE;
- private const TOTP_SHORT_URI = 'otpauth://totp/'.self::ACCOUNT.'?secret='.self::SECRET;
- private const HOTP_SHORT_URI = 'otpauth://hotp/'.self::ACCOUNT.'?secret='.self::SECRET;
- private const TOTP_URI_WITH_UNREACHABLE_IMAGE = 'otpauth://totp/service:account?secret=A4GRFHVVRBGY7UIW&image=https%3A%2F%2Fen.opensuse.org%2Fimage.png';
- private const INVALID_OTPAUTH_URI = 'otpauth://Xotp/'.self::ACCOUNT.'?secret='.self::SECRET;
- 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 ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP = [
- 'service' => self::SERVICE,
- 'account' => self::ACCOUNT,
- 'icon' => self::ICON,
- 'otp_type' => 'totp',
- 'secret' => self::SECRET,
- 'digits' => self::DIGITS_CUSTOM,
- 'algorithm' => self::ALGORITHM_CUSTOM,
- 'period' => self::PERIOD_CUSTOM,
- 'counter' => null,
- ];
- private const ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP = [
- 'account' => self::ACCOUNT,
- 'otp_type' => 'totp',
- 'secret' => self::SECRET,
- ];
- private const JSON_FRAGMENTS_FOR_CUSTOM_TOTP = [
- 'service' => self::SERVICE,
- 'account' => self::ACCOUNT,
- 'otp_type' => 'totp',
- 'secret' => self::SECRET,
- 'digits' => self::DIGITS_CUSTOM,
- 'algorithm' => self::ALGORITHM_CUSTOM,
- 'period' => self::PERIOD_CUSTOM,
- 'counter' => null,
- ];
- private const JSON_FRAGMENTS_FOR_DEFAULT_TOTP = [
- 'service' => null,
- 'account' => self::ACCOUNT,
- 'otp_type' => 'totp',
- 'secret' => self::SECRET,
- 'digits' => self::DIGITS_DEFAULT,
- 'algorithm' => self::ALGORITHM_DEFAULT,
- 'period' => self::PERIOD_DEFAULT,
- 'counter' => null,
- ];
- private const ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP = [
- 'service' => self::SERVICE,
- 'account' => self::ACCOUNT,
- 'icon' => self::ICON,
- 'otp_type' => 'hotp',
- 'secret' => self::SECRET,
- 'digits' => self::DIGITS_CUSTOM,
- 'algorithm' => self::ALGORITHM_CUSTOM,
- 'period' => null,
- 'counter' => self::COUNTER_CUSTOM,
- ];
- private const ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP = [
- 'account' => self::ACCOUNT,
- 'otp_type' => 'hotp',
- 'secret' => self::SECRET,
- ];
- private const JSON_FRAGMENTS_FOR_CUSTOM_HOTP = [
- 'service' => self::SERVICE,
- 'account' => self::ACCOUNT,
- 'otp_type' => 'hotp',
- 'secret' => self::SECRET,
- 'digits' => self::DIGITS_CUSTOM,
- 'algorithm' => self::ALGORITHM_CUSTOM,
- 'period' => null,
- 'counter' => self::COUNTER_CUSTOM,
- ];
- private const JSON_FRAGMENTS_FOR_DEFAULT_HOTP = [
- 'service' => null,
- 'account' => self::ACCOUNT,
- 'otp_type' => 'hotp',
- 'secret' => self::SECRET,
- 'digits' => self::DIGITS_DEFAULT,
- 'algorithm' => self::ALGORITHM_DEFAULT,
- 'period' => null,
- 'counter' => self::COUNTER_DEFAULT,
- ];
- private const ARRAY_OF_INVALID_PARAMETERS = [
- 'account' => null,
- 'otp_type' => 'totp',
- 'secret' => self::SECRET,
- ];
- /**
- * @test
- */
- public function setUp(): void
- {
- parent::setUp();
- $this->user = factory(User::class)->create();
- $this->group = factory(Group::class)->create();
- }
- /**
- * @test
- */
- public function test_index_returns_twofaccount_collection()
- {
- factory(TwoFAccount::class, 3)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/v1/twofaccounts')
- ->assertOk()
- ->assertJsonCount(3, $key = null)
- ->assertJsonStructure([
- '*' => self::VALID_RESOURCE_STRUCTURE_WITHOUT_SECRET
- ]);
- }
- /**
- * @test
- */
- public function test_index_returns_twofaccount_collection_with_secret()
- {
- factory(TwoFAccount::class, 3)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/v1/twofaccounts?withSecret=1')
- ->assertOk()
- ->assertJsonCount(3, $key = null)
- ->assertJsonStructure([
- '*' => self::VALID_RESOURCE_STRUCTURE_WITH_SECRET
- ]);
- }
- /**
- * @test
- */
- public function test_show_twofaccount_returns_twofaccount_resource_with_secret()
- {
- $twofaccount = factory(TwoFAccount::class)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id)
- ->assertOk()
- ->assertJsonStructure(self::VALID_RESOURCE_STRUCTURE_WITH_SECRET);
- }
- /**
- * @test
- */
- public function test_show_twofaccount_returns_twofaccount_resource_without_secret()
- {
- $twofaccount = factory(TwoFAccount::class)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->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 = factory(TwoFAccount::class)->create();
- // DB::table('twofaccounts')
- // ->where('id', $twofaccount->id)
- // ->update([
- // 'secret' => '**encrypted**',
- // 'account' => '**encrypted**',
- // ]);
- // $response = $this->actingAs($this->user, 'api')
- // ->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')
- ->json('GET', '/api/v1/twofaccounts/1000')
- ->assertNotFound()
- ->assertJsonStructure([
- 'message'
- ]);
- }
- /**
- * @dataProvider provideDataForTestStoreStructure
- * @test
- */
- public function test_store_returns_success_with_consistent_resource_structure(array $data)
- {
- Storage::put('test.png', 'emptied to prevent missing resource replaced by null by the model getter');
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', $data)
- ->assertCreated()
- ->assertJsonStructure(self::VALID_RESOURCE_STRUCTURE_WITH_SECRET);
- }
- /**
- * Provide data for TwoFAccount store test
- */
- public function provideDataForTestStoreStructure() : array
- {
- return [
- [[
- 'uri' => self::TOTP_FULL_CUSTOM_URI,
- ]],
- [[
- 'uri' => self::TOTP_SHORT_URI,
- ]],
- [
- self::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP
- ],
- [
- self::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP
- ],
- [[
- 'uri' => self::HOTP_FULL_CUSTOM_URI,
- ]],
- [[
- 'uri' => self::HOTP_SHORT_URI,
- ]],
- [
- self::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP
- ],
- [
- self::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP
- ],
- ];
- }
- /**
- * @test
- */
- public function test_store_totp_using_fully_custom_uri_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => self::TOTP_FULL_CUSTOM_URI,
- ])
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_CUSTOM_TOTP);
- }
- /**
- * @test
- */
- public function test_store_totp_using_short_uri_returns_resource_with_default_otp_parameter()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => self::TOTP_SHORT_URI,
- ])
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_DEFAULT_TOTP);
- }
- /**
- * @test
- */
- public function test_store_totp_using_fully_custom_parameters_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', self::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_CUSTOM_TOTP);
- }
- /**
- * @test
- */
- public function test_store_totp_using_minimum_parameters_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', self::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP)
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_DEFAULT_TOTP);
- }
- /**
- * @test
- */
- public function test_store_hotp_using_fully_custom_uri_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => self::HOTP_FULL_CUSTOM_URI,
- ])
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_CUSTOM_HOTP);
- }
- /**
- * @test
- */
- public function test_store_hotp_using_short_uri_returns_resource_with_default_otp_parameter()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => self::HOTP_SHORT_URI,
- ])
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_DEFAULT_HOTP);
- }
- /**
- * @test
- */
- public function test_store_hotp_using_fully_custom_parameters_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', self::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_CUSTOM_HOTP);
- }
- /**
- * @test
- */
- public function test_store_hotp_using_minimum_parameters_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', self::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP)
- ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_DEFAULT_HOTP);
- }
- /**
- * @test
- */
- public function test_store_with_invalid_uri_returns_validation_error()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => self::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
- $settingService = resolve('App\Services\SettingService');
- $settingService->set('defaultGroup', $this->group->id);
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => self::TOTP_SHORT_URI,
- ])
- ->assertJsonFragment([
- 'group_id' => $this->group->id
- ]);
- }
- /**
- * @test
- */
- public function test_store_assigns_created_account_when_default_group_is_the_active_one()
- {
- $settingService = resolve('App\Services\SettingService');
- // Set the default group to be the active one
- $settingService->set('defaultGroup', -1);
- // Set the active group
- $settingService->set('activeGroup', 1);
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => self::TOTP_SHORT_URI,
- ])
- ->assertJsonFragment([
- 'group_id' => 1
- ]);
- }
- /**
- * @test
- */
- public function test_store_assigns_created_account_when_default_group_is_no_group()
- {
- $settingService = resolve('App\Services\SettingService');
- // Set the default group to No group
- $settingService->set('defaultGroup', 0);
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => self::TOTP_SHORT_URI,
- ])
- ->assertJsonFragment([
- 'group_id' => null
- ]);
- }
- /**
- * @test
- */
- public function test_store_assigns_created_account_when_default_group_does_not_exist()
- {
- $settingService = resolve('App\Services\SettingService');
- // Set the default group to a non-existing one
- $settingService->set('defaultGroup', 1000);
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts', [
- 'uri' => self::TOTP_SHORT_URI,
- ])
- ->assertJsonFragment([
- 'group_id' => null
- ]);
- }
- /**
- * @test
- */
- public function test_update_totp_returns_success_with_updated_resource()
- {
- $twofaccount = factory(TwoFAccount::class)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('PUT', '/api/v1/twofaccounts/' . $twofaccount->id, self::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()
- {
- $twofaccount = factory(TwoFAccount::class)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('PUT', '/api/v1/twofaccounts/' . $twofaccount->id, self::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')
- ->json('PUT', '/api/v1/twofaccounts/1000', self::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)
- ->assertNotFound();
- }
- /**
- * @test
- */
- public function test_update_twofaccount_with_invalid_data_returns_validation_error()
- {
- $twofaccount = factory(TwoFAccount::class)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('PUT', '/api/v1/twofaccounts/' . $twofaccount->id, self::ARRAY_OF_INVALID_PARAMETERS)
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_reorder_returns_success()
- {
- factory(TwoFAccount::class, 3)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts/reorder', [
- 'orderedIds' => [3,2,1]])
- ->assertStatus(200)
- ->assertJsonStructure([
- 'message'
- ]);
- }
- /**
- * @test
- */
- public function test_reorder_with_invalid_data_returns_validation_error()
- {
- factory(TwoFAccount::class, 3)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts/reorder', [
- 'orderedIds' => '3,2,1'])
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_preview_returns_success_with_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts/preview', [
- 'uri' => self::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')
- ->json('POST', '/api/v1/twofaccounts/preview', [
- 'uri' => self::INVALID_OTPAUTH_URI,
- ])
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_preview_with_unreachable_image_returns_success()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts/preview', [
- 'uri' => self::TOTP_URI_WITH_UNREACHABLE_IMAGE,
- ])
- ->assertOk()
- ->assertJsonFragment([
- 'icon' => null
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_using_totp_twofaccount_id_returns_consistent_resource()
- {
- $twofaccount = factory(TwoFAccount::class)->create([
- 'otp_type' => 'totp',
- 'account' => self::ACCOUNT,
- 'service' => self::SERVICE,
- 'secret' => self::SECRET,
- 'algorithm' => self::ALGORITHM_DEFAULT,
- 'digits' => self::DIGITS_DEFAULT,
- 'period' => self::PERIOD_DEFAULT,
- 'legacy_uri' => self::TOTP_SHORT_URI,
- 'icon' => '',
- ]);
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id . '/otp')
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP)
- ->assertJsonFragment([
- 'otp_type' => 'totp',
- 'period' => self::PERIOD_DEFAULT,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_totp_uri_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts/otp', [
- 'uri' => self::TOTP_FULL_CUSTOM_URI,
- ])
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP)
- ->assertJsonFragment([
- 'otp_type' => 'totp',
- 'period' => self::PERIOD_CUSTOM,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_totp_parameters_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts/otp', self::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP)
- ->assertJsonFragment([
- 'otp_type' => 'totp',
- 'period' => self::PERIOD_CUSTOM,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_using_hotp_twofaccount_id_returns_consistent_resource()
- {
- $twofaccount = factory(TwoFAccount::class)->create([
- 'otp_type' => 'hotp',
- 'account' => self::ACCOUNT,
- 'service' => self::SERVICE,
- 'secret' => self::SECRET,
- 'algorithm' => self::ALGORITHM_DEFAULT,
- 'digits' => self::DIGITS_DEFAULT,
- 'period' => null,
- 'legacy_uri' => self::HOTP_SHORT_URI,
- 'icon' => '',
- ]);
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id . '/otp')
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP)
- ->assertJsonFragment([
- 'otp_type' => 'hotp',
- 'counter' => self::COUNTER_DEFAULT + 1,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_hotp_uri_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts/otp', [
- 'uri' => self::HOTP_FULL_CUSTOM_URI,
- ])
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP)
- ->assertJsonFragment([
- 'otp_type' => 'hotp',
- 'counter' => self::COUNTER_CUSTOM + 1,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_hotp_parameters_returns_consistent_resource()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts/otp', self::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)
- ->assertOk()
- ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP)
- ->assertJsonFragment([
- 'otp_type' => 'hotp',
- 'counter' => self::COUNTER_CUSTOM + 1,
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_multiple_inputs_returns_bad_request()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts/otp', [
- 'uri' => self::HOTP_FULL_CUSTOM_URI,
- 'key' => 'value',
- ])
- ->assertStatus(400)
- ->assertJsonStructure([
- 'message',
- 'reason',
- ]);
- }
- /**
- * @test
- */
- public function test_get_otp_using_indecipherable_twofaccount_id_returns_bad_request()
- {
- $settingService = resolve('App\Services\SettingService');
- $settingService->set('useEncryption', true);
- $twofaccount = factory(TwoFAccount::class)->create();
- DB::table('twofaccounts')
- ->where('id', $twofaccount->id)
- ->update([
- 'secret' => '**encrypted**',
- ]);
- $response = $this->actingAs($this->user, 'api')
- ->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')
- ->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')
- ->json('POST', '/api/v1/twofaccounts/otp', [
- 'uri' => self::INVALID_OTPAUTH_URI,
- ])
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_get_otp_by_posting_invalid_parameters_returns_validation_error()
- {
- $response = $this->actingAs($this->user, 'api')
- ->json('POST', '/api/v1/twofaccounts/otp', self::ARRAY_OF_INVALID_PARAMETERS)
- ->assertStatus(422);
- }
- /**
- * @test
- */
- public function test_count_returns_right_number_of_twofaccount()
- {
- factory(TwoFAccount::class, 3)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('GET', '/api/v1/twofaccounts/count')
- ->assertStatus(200)
- ->assertExactJson([
- 'count' => 3
- ]);
- }
- /**
- * @test
- */
- public function test_withdraw_returns_success()
- {
- factory(TwoFAccount::class, 3)->create();
- $ids = DB::table('twofaccounts')->pluck('id')->implode(',');
- $response = $this->actingAs($this->user, 'api')
- ->json('PATCH', '/api/v1/twofaccounts/withdraw?ids=1,2,3' . $ids)
- ->assertOk()
- ->assertJsonStructure([
- 'message',
- ]);
- }
- /**
- * @test
- */
- public function test_withdraw_too_many_ids_returns_bad_request()
- {
- factory(TwoFAccount::class, 102)->create();
- $ids = DB::table('twofaccounts')->pluck('id')->implode(',');
- $response = $this->actingAs($this->user, 'api')
- ->json('PATCH', '/api/v1/twofaccounts/withdraw?ids=' . $ids)
- ->assertStatus(400)
- ->assertJsonStructure([
- 'message',
- 'reason',
- ]);
- }
- /**
- * @test
- */
- public function test_destroy_twofaccount_returns_success()
- {
- $twofaccount = factory(TwoFAccount::class)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('DELETE', '/api/v1/twofaccounts/' . $twofaccount->id)
- ->assertNoContent();
- }
- /**
- * @test
- */
- public function test_destroy_missing_twofaccount_returns_not_found()
- {
- $twofaccount = factory(TwoFAccount::class)->create();
- $response = $this->actingAs($this->user, 'api')
- ->json('DELETE', '/api/v1/twofaccounts/1000')
- ->assertNotFound();
- }
- /**
- * @test
- */
- public function test_batch_destroy_twofaccount_returns_success()
- {
- factory(TwoFAccount::class, 3)->create();
- $ids = DB::table('twofaccounts')->pluck('id')->implode(',');
- $response = $this->actingAs($this->user, 'api')
- ->json('DELETE', '/api/v1/twofaccounts?ids=' . $ids)
- ->assertNoContent();
- }
- /**
- * @test
- */
- public function test_batch_destroy_too_many_twofaccounts_returns_bad_request()
- {
- factory(TwoFAccount::class, 102)->create();
- $ids = DB::table('twofaccounts')->pluck('id')->implode(',');
- $response = $this->actingAs($this->user, 'api')
- ->json('DELETE', '/api/v1/twofaccounts?ids=' . $ids)
- ->assertStatus(400)
- ->assertJsonStructure([
- 'message',
- 'reason',
- ]);
- }
- }
|