TwoFAccountControllerTest.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355
  1. <?php
  2. namespace Tests\Api\v1\Controllers;
  3. use App\Api\v1\Controllers\TwoFAccountController;
  4. use App\Api\v1\Resources\TwoFAccountCollection;
  5. use App\Api\v1\Resources\TwoFAccountExportCollection;
  6. use App\Api\v1\Resources\TwoFAccountExportResource;
  7. use App\Api\v1\Resources\TwoFAccountReadResource;
  8. use App\Api\v1\Resources\TwoFAccountStoreResource;
  9. use App\Facades\Settings;
  10. use App\Models\Group;
  11. use App\Models\TwoFAccount;
  12. use App\Models\User;
  13. use App\Policies\TwoFAccountPolicy;
  14. use App\Providers\MigrationServiceProvider;
  15. use App\Providers\TwoFAuthServiceProvider;
  16. use Illuminate\Support\Facades\DB;
  17. use Illuminate\Support\Facades\Storage;
  18. use PHPUnit\Framework\Attributes\CoversClass;
  19. use PHPUnit\Framework\Attributes\DataProvider;
  20. use Tests\Classes\LocalFile;
  21. use Tests\Data\MigrationTestData;
  22. use Tests\Data\OtpTestData;
  23. use Tests\FeatureTestCase;
  24. /**
  25. * TwoFAccountControllerTest test class
  26. */
  27. #[CoversClass(TwoFAccountController::class)]
  28. #[CoversClass(TwoFAccountCollection::class)]
  29. #[CoversClass(TwoFAccountReadResource::class)]
  30. #[CoversClass(TwoFAccountStoreResource::class)]
  31. #[CoversClass(TwoFAccountExportResource::class)]
  32. #[CoversClass(TwoFAccountExportCollection::class)]
  33. #[CoversClass(MigrationServiceProvider::class)]
  34. #[CoversClass(TwoFAuthServiceProvider::class)]
  35. #[CoversClass(TwoFAccountPolicy::class)]
  36. class TwoFAccountControllerTest extends FeatureTestCase
  37. {
  38. /**
  39. * @var \App\Models\User|\Illuminate\Contracts\Auth\Authenticatable
  40. */
  41. protected $user;
  42. protected $anotherUser;
  43. /**
  44. * @var App\Models\Group
  45. */
  46. protected $userGroupA;
  47. protected $userGroupB;
  48. protected $anotherUserGroupA;
  49. protected $anotherUserGroupB;
  50. /**
  51. * @var App\Models\TwoFAccount
  52. */
  53. protected $twofaccountA;
  54. protected $twofaccountB;
  55. protected $twofaccountC;
  56. protected $twofaccountD;
  57. private const VALID_RESOURCE_STRUCTURE_WITHOUT_SECRET = [
  58. 'id',
  59. 'group_id',
  60. 'service',
  61. 'account',
  62. 'icon',
  63. 'otp_type',
  64. 'digits',
  65. 'algorithm',
  66. 'period',
  67. 'counter',
  68. ];
  69. private const VALID_RESOURCE_STRUCTURE_WITH_SECRET = [
  70. 'id',
  71. 'group_id',
  72. 'service',
  73. 'account',
  74. 'icon',
  75. 'otp_type',
  76. 'secret',
  77. 'digits',
  78. 'algorithm',
  79. 'period',
  80. 'counter',
  81. ];
  82. private const VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP = [
  83. 'generated_at',
  84. 'otp_type',
  85. 'password',
  86. 'period',
  87. ];
  88. private const VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP = [
  89. 'otp_type',
  90. 'password',
  91. 'counter',
  92. ];
  93. private const VALID_EXPORT_STRUTURE = [
  94. 'app',
  95. 'schema',
  96. 'datetime',
  97. 'data' => [
  98. '*' => [
  99. 'otp_type',
  100. 'account',
  101. 'service',
  102. 'icon',
  103. 'icon_mime',
  104. 'icon_file',
  105. 'secret',
  106. 'digits',
  107. 'algorithm',
  108. 'period',
  109. 'counter',
  110. 'legacy_uri',
  111. ], ],
  112. ];
  113. private const JSON_FRAGMENTS_FOR_CUSTOM_TOTP = [
  114. 'service' => OtpTestData::SERVICE,
  115. 'account' => OtpTestData::ACCOUNT,
  116. 'otp_type' => 'totp',
  117. 'secret' => OtpTestData::SECRET,
  118. 'digits' => OtpTestData::DIGITS_CUSTOM,
  119. 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
  120. 'period' => OtpTestData::PERIOD_CUSTOM,
  121. 'counter' => null,
  122. ];
  123. private const JSON_FRAGMENTS_FOR_DEFAULT_TOTP = [
  124. 'service' => null,
  125. 'account' => OtpTestData::ACCOUNT,
  126. 'otp_type' => 'totp',
  127. 'secret' => OtpTestData::SECRET,
  128. 'digits' => OtpTestData::DIGITS_DEFAULT,
  129. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  130. 'period' => OtpTestData::PERIOD_DEFAULT,
  131. 'counter' => null,
  132. ];
  133. private const JSON_FRAGMENTS_FOR_CUSTOM_HOTP = [
  134. 'service' => OtpTestData::SERVICE,
  135. 'account' => OtpTestData::ACCOUNT,
  136. 'otp_type' => 'hotp',
  137. 'secret' => OtpTestData::SECRET,
  138. 'digits' => OtpTestData::DIGITS_CUSTOM,
  139. 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
  140. 'period' => null,
  141. 'counter' => OtpTestData::COUNTER_CUSTOM,
  142. ];
  143. private const JSON_FRAGMENTS_FOR_DEFAULT_HOTP = [
  144. 'service' => null,
  145. 'account' => OtpTestData::ACCOUNT,
  146. 'otp_type' => 'hotp',
  147. 'secret' => OtpTestData::SECRET,
  148. 'digits' => OtpTestData::DIGITS_DEFAULT,
  149. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  150. 'period' => null,
  151. 'counter' => OtpTestData::COUNTER_DEFAULT,
  152. ];
  153. private const ARRAY_OF_INVALID_PARAMETERS = [
  154. 'account' => null,
  155. 'otp_type' => 'totp',
  156. 'secret' => OtpTestData::SECRET,
  157. ];
  158. /**
  159. * @test
  160. */
  161. public function setUp() : void
  162. {
  163. parent::setUp();
  164. $this->user = User::factory()->create();
  165. $this->userGroupA = Group::factory()->for($this->user)->create();
  166. $this->userGroupB = Group::factory()->for($this->user)->create();
  167. $this->twofaccountA = TwoFAccount::factory()->for($this->user)->create([
  168. 'group_id' => $this->userGroupA->id,
  169. ]);
  170. $this->twofaccountB = TwoFAccount::factory()->for($this->user)->create([
  171. 'group_id' => $this->userGroupA->id,
  172. ]);
  173. $this->anotherUser = User::factory()->create();
  174. $this->anotherUserGroupA = Group::factory()->for($this->anotherUser)->create();
  175. $this->anotherUserGroupB = Group::factory()->for($this->anotherUser)->create();
  176. $this->twofaccountC = TwoFAccount::factory()->for($this->anotherUser)->create([
  177. 'group_id' => $this->anotherUserGroupA->id,
  178. ]);
  179. $this->twofaccountD = TwoFAccount::factory()->for($this->anotherUser)->create([
  180. 'group_id' => $this->anotherUserGroupB->id,
  181. ]);
  182. }
  183. /**
  184. * @test
  185. */
  186. #[DataProvider('indexUrlParameterProvider')]
  187. public function test_index_returns_user_twofaccounts_only($urlParameter, $expected)
  188. {
  189. $response = $this->actingAs($this->user, 'api-guard')
  190. ->json('GET', '/api/v1/twofaccounts' . $urlParameter)
  191. ->assertOk()
  192. ->assertJsonCount(2, $key = null)
  193. ->assertJsonStructure([
  194. '*' => $expected,
  195. ])
  196. ->assertJsonFragment([
  197. 'id' => $this->twofaccountA->id,
  198. ])
  199. ->assertJsonFragment([
  200. 'id' => $this->twofaccountB->id,
  201. ])
  202. ->assertJsonMissing([
  203. 'id' => $this->twofaccountC->id,
  204. ])
  205. ->assertJsonMissing([
  206. 'id' => $this->twofaccountD->id,
  207. ]);
  208. }
  209. /**
  210. * Provide data for index tests
  211. */
  212. public static function indexUrlParameterProvider()
  213. {
  214. return [
  215. 'VALID_RESOURCE_STRUCTURE_WITHOUT_SECRET' => [
  216. '',
  217. self::VALID_RESOURCE_STRUCTURE_WITHOUT_SECRET,
  218. ],
  219. 'VALID_RESOURCE_STRUCTURE_WITH_SECRET' => [
  220. '?withSecret=1',
  221. self::VALID_RESOURCE_STRUCTURE_WITH_SECRET,
  222. ],
  223. ];
  224. }
  225. /**
  226. * @test
  227. */
  228. public function test_show_returns_twofaccount_resource_with_secret()
  229. {
  230. $response = $this->actingAs($this->user, 'api-guard')
  231. ->json('GET', '/api/v1/twofaccounts/' . $this->twofaccountA->id)
  232. ->assertOk()
  233. ->assertJsonStructure(self::VALID_RESOURCE_STRUCTURE_WITH_SECRET);
  234. }
  235. /**
  236. * @test
  237. */
  238. public function test_show_returns_twofaccount_resource_without_secret()
  239. {
  240. $response = $this->actingAs($this->user, 'api-guard')
  241. ->json('GET', '/api/v1/twofaccounts/' . $this->twofaccountA->id . '?withSecret=0')
  242. ->assertOk()
  243. ->assertJsonStructure(self::VALID_RESOURCE_STRUCTURE_WITHOUT_SECRET);
  244. }
  245. /**
  246. * @test
  247. */
  248. // public function test_show_twofaccount_with_indeciphered_data_returns_replaced_data()
  249. // {
  250. // $dbEncryptionService = resolve('App\Services\DbEncryptionService');
  251. // $dbEncryptionService->setTo(true);
  252. // $twofaccount = TwoFAccount::factory()->create();
  253. // DB::table('twofaccounts')
  254. // ->where('id', $twofaccount->id)
  255. // ->update([
  256. // 'secret' => '**encrypted**',
  257. // 'account' => '**encrypted**',
  258. // ]);
  259. // $response = $this->actingAs($this->user, 'api-guard')
  260. // ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id)
  261. // ->assertJsonFragment([
  262. // 'secret' => '*indecipherable*',
  263. // 'account' => '*indecipherable*',
  264. // ]);
  265. // }
  266. /**
  267. * @test
  268. */
  269. public function test_show_missing_twofaccount_returns_not_found()
  270. {
  271. $response = $this->actingAs($this->user, 'api-guard')
  272. ->json('GET', '/api/v1/twofaccounts/1000')
  273. ->assertNotFound()
  274. ->assertJsonStructure([
  275. 'message',
  276. ]);
  277. }
  278. /**
  279. * @test
  280. */
  281. public function test_show_twofaccount_of_another_user_is_forbidden()
  282. {
  283. $response = $this->actingAs($this->user, 'api-guard')
  284. ->json('GET', '/api/v1/twofaccounts/' . $this->twofaccountC->id)
  285. ->assertForbidden()
  286. ->assertJsonStructure([
  287. 'message',
  288. ]);
  289. }
  290. /**
  291. * @test
  292. */
  293. #[DataProvider('accountCreationProvider')]
  294. public function test_store_without_encryption_returns_success_with_consistent_resource_structure($payload, $expected)
  295. {
  296. Settings::set('useEncryption', false);
  297. Storage::put('test.png', 'emptied to prevent missing resource replaced by null by the model getter');
  298. $response = $this->actingAs($this->user, 'api-guard')
  299. ->json('POST', '/api/v1/twofaccounts', $payload)
  300. ->assertCreated()
  301. ->assertJsonStructure(self::VALID_RESOURCE_STRUCTURE_WITH_SECRET)
  302. ->assertJsonFragment($expected);
  303. }
  304. /**
  305. * @test
  306. */
  307. #[DataProvider('accountCreationProvider')]
  308. public function test_store_with_encryption_returns_success_with_consistent_resource_structure($payload, $expected)
  309. {
  310. Settings::set('useEncryption', true);
  311. Storage::put('test.png', 'emptied to prevent missing resource replaced by null by the model getter');
  312. $response = $this->actingAs($this->user, 'api-guard')
  313. ->json('POST', '/api/v1/twofaccounts', $payload)
  314. ->assertCreated()
  315. ->assertJsonStructure(self::VALID_RESOURCE_STRUCTURE_WITH_SECRET)
  316. ->assertJsonFragment($expected);
  317. }
  318. /**
  319. * Provide data for TwoFAccount store tests
  320. */
  321. public static function accountCreationProvider()
  322. {
  323. return [
  324. 'TOTP_FULL_CUSTOM_URI' => [
  325. [
  326. 'uri' => OtpTestData::TOTP_FULL_CUSTOM_URI,
  327. ],
  328. self::JSON_FRAGMENTS_FOR_CUSTOM_TOTP,
  329. ],
  330. 'TOTP_SHORT_URI' => [
  331. [
  332. 'uri' => OtpTestData::TOTP_SHORT_URI,
  333. ],
  334. self::JSON_FRAGMENTS_FOR_DEFAULT_TOTP,
  335. ],
  336. 'ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP' => [
  337. OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP,
  338. self::JSON_FRAGMENTS_FOR_CUSTOM_TOTP,
  339. ],
  340. 'ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP' => [
  341. OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP,
  342. self::JSON_FRAGMENTS_FOR_DEFAULT_TOTP,
  343. ],
  344. 'HOTP_FULL_CUSTOM_URI' => [
  345. [
  346. 'uri' => OtpTestData::HOTP_FULL_CUSTOM_URI,
  347. ],
  348. self::JSON_FRAGMENTS_FOR_CUSTOM_HOTP,
  349. ],
  350. 'HOTP_SHORT_URI' => [
  351. [
  352. 'uri' => OtpTestData::HOTP_SHORT_URI,
  353. ],
  354. self::JSON_FRAGMENTS_FOR_DEFAULT_HOTP,
  355. ],
  356. 'ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP' => [
  357. OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP,
  358. self::JSON_FRAGMENTS_FOR_CUSTOM_HOTP,
  359. ],
  360. 'ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP' => [
  361. OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP,
  362. self::JSON_FRAGMENTS_FOR_DEFAULT_HOTP,
  363. ],
  364. ];
  365. }
  366. /**
  367. * @test
  368. */
  369. public function test_store_with_invalid_uri_returns_validation_error()
  370. {
  371. $response = $this->actingAs($this->user, 'api-guard')
  372. ->json('POST', '/api/v1/twofaccounts', [
  373. 'uri' => OtpTestData::INVALID_OTPAUTH_URI,
  374. ])
  375. ->assertStatus(422);
  376. }
  377. /**
  378. * @test
  379. */
  380. public function test_store_assigns_created_account_when_default_group_is_a_specific_one()
  381. {
  382. // Set the default group to a specific one
  383. $this->user['preferences->defaultGroup'] = $this->userGroupA->id;
  384. $this->user->save();
  385. $response = $this->actingAs($this->user, 'api-guard')
  386. ->json('POST', '/api/v1/twofaccounts', [
  387. 'uri' => OtpTestData::TOTP_SHORT_URI,
  388. ])
  389. ->assertJsonFragment([
  390. 'group_id' => $this->userGroupA->id,
  391. ]);
  392. }
  393. /**
  394. * @test
  395. */
  396. public function test_store_assigns_created_account_when_default_group_is_the_active_one()
  397. {
  398. // Set the default group to be the active one
  399. $this->user['preferences->defaultGroup'] = -1;
  400. // Set the active group
  401. $this->user['preferences->activeGroup'] = $this->userGroupA->id;
  402. $this->user->save();
  403. $response = $this->actingAs($this->user, 'api-guard')
  404. ->json('POST', '/api/v1/twofaccounts', [
  405. 'uri' => OtpTestData::TOTP_SHORT_URI,
  406. ])
  407. ->assertJsonFragment([
  408. 'group_id' => $this->userGroupA->id,
  409. ]);
  410. }
  411. /**
  412. * @test
  413. */
  414. public function test_store_assigns_created_account_when_default_group_is_no_group()
  415. {
  416. // Set the default group to No group
  417. $this->user['preferences->defaultGroup'] = 0;
  418. $this->user->save();
  419. $response = $this->actingAs($this->user, 'api-guard')
  420. ->json('POST', '/api/v1/twofaccounts', [
  421. 'uri' => OtpTestData::TOTP_SHORT_URI,
  422. ])
  423. ->assertJsonFragment([
  424. 'group_id' => null,
  425. ]);
  426. }
  427. /**
  428. * @test
  429. */
  430. public function test_store_assigns_created_account_when_default_group_does_not_exist()
  431. {
  432. // Set the default group to a non-existing one
  433. $this->user['preferences->defaultGroup'] = 1000;
  434. $this->user->save();
  435. $response = $this->actingAs($this->user, 'api-guard')
  436. ->json('POST', '/api/v1/twofaccounts', [
  437. 'uri' => OtpTestData::TOTP_SHORT_URI,
  438. ])
  439. ->assertJsonFragment([
  440. 'group_id' => null,
  441. ]);
  442. }
  443. /**
  444. * @test
  445. */
  446. public function test_update_totp_returns_success_with_updated_resource()
  447. {
  448. $response = $this->actingAs($this->user, 'api-guard')
  449. ->json('PUT', '/api/v1/twofaccounts/' . $this->twofaccountA->id, OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)
  450. ->assertOk()
  451. ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_CUSTOM_TOTP);
  452. }
  453. /**
  454. * @test
  455. */
  456. public function test_update_hotp_returns_success_with_updated_resource()
  457. {
  458. $response = $this->actingAs($this->user, 'api-guard')
  459. ->json('PUT', '/api/v1/twofaccounts/' . $this->twofaccountA->id, OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)
  460. ->assertOk()
  461. ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_CUSTOM_HOTP);
  462. }
  463. /**
  464. * @test
  465. */
  466. public function test_update_missing_twofaccount_returns_not_found()
  467. {
  468. $response = $this->actingAs($this->user, 'api-guard')
  469. ->json('PUT', '/api/v1/twofaccounts/1000', OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)
  470. ->assertNotFound();
  471. }
  472. /**
  473. * @test
  474. */
  475. public function test_update_twofaccount_with_invalid_data_returns_validation_error()
  476. {
  477. $twofaccount = TwoFAccount::factory()->create();
  478. $response = $this->actingAs($this->user, 'api-guard')
  479. ->json('PUT', '/api/v1/twofaccounts/' . $this->twofaccountA->id, self::ARRAY_OF_INVALID_PARAMETERS)
  480. ->assertStatus(422);
  481. }
  482. /**
  483. * @test
  484. */
  485. public function test_update_twofaccount_of_another_user_is_forbidden()
  486. {
  487. $response = $this->actingAs($this->user, 'api-guard')
  488. ->json('PUT', '/api/v1/twofaccounts/' . $this->twofaccountC->id, OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)
  489. ->assertForbidden()
  490. ->assertJsonStructure([
  491. 'message',
  492. ]);
  493. }
  494. /**
  495. * @test
  496. */
  497. public function test_migrate_valid_gauth_payload_returns_success_with_consistent_resources()
  498. {
  499. $response = $this->actingAs($this->user, 'api-guard')
  500. ->json('POST', '/api/v1/twofaccounts/migration', [
  501. 'payload' => MigrationTestData::GOOGLE_AUTH_MIGRATION_URI,
  502. 'withSecret' => 1,
  503. ])
  504. ->assertOk()
  505. ->assertJsonCount(2, $key = null)
  506. ->assertJsonFragment([
  507. 'id' => 0,
  508. 'service' => OtpTestData::SERVICE,
  509. 'account' => OtpTestData::ACCOUNT,
  510. 'otp_type' => 'totp',
  511. 'secret' => OtpTestData::SECRET,
  512. 'digits' => OtpTestData::DIGITS_DEFAULT,
  513. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  514. 'period' => OtpTestData::PERIOD_DEFAULT,
  515. 'counter' => null,
  516. ])
  517. ->assertJsonFragment([
  518. 'id' => 0,
  519. 'service' => OtpTestData::SERVICE . '_bis',
  520. 'account' => OtpTestData::ACCOUNT . '_bis',
  521. 'otp_type' => 'totp',
  522. 'secret' => OtpTestData::SECRET,
  523. 'digits' => OtpTestData::DIGITS_DEFAULT,
  524. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  525. 'period' => OtpTestData::PERIOD_DEFAULT,
  526. 'counter' => null,
  527. ]);
  528. }
  529. /**
  530. * @test
  531. */
  532. public function test_migrate_with_invalid_gauth_payload_returns_validation_error()
  533. {
  534. $response = $this->actingAs($this->user, 'api-guard')
  535. ->json('POST', '/api/v1/twofaccounts/migration', [
  536. 'uri' => MigrationTestData::INVALID_GOOGLE_AUTH_MIGRATION_URI,
  537. ])
  538. ->assertStatus(422);
  539. }
  540. /**
  541. * @test
  542. */
  543. public function test_migrate_payload_with_duplicates_returns_negative_ids()
  544. {
  545. $twofaccount = TwoFAccount::factory()->for($this->user)->create([
  546. 'otp_type' => 'totp',
  547. 'account' => OtpTestData::ACCOUNT,
  548. 'service' => OtpTestData::SERVICE,
  549. 'secret' => OtpTestData::SECRET,
  550. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  551. 'digits' => OtpTestData::DIGITS_DEFAULT,
  552. 'period' => OtpTestData::PERIOD_DEFAULT,
  553. 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
  554. 'icon' => '',
  555. ]);
  556. $response = $this->actingAs($this->user, 'api-guard')
  557. ->json('POST', '/api/v1/twofaccounts/migration?withSecret=1', [
  558. 'payload' => MigrationTestData::GOOGLE_AUTH_MIGRATION_URI,
  559. ])
  560. ->assertOk()
  561. ->assertJsonFragment([
  562. 'id' => -1,
  563. 'service' => OtpTestData::SERVICE,
  564. 'account' => OtpTestData::ACCOUNT,
  565. 'otp_type' => 'totp',
  566. 'secret' => OtpTestData::SECRET,
  567. 'digits' => OtpTestData::DIGITS_DEFAULT,
  568. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  569. 'period' => OtpTestData::PERIOD_DEFAULT,
  570. 'counter' => null,
  571. ])
  572. ->assertJsonFragment([
  573. 'id' => 0,
  574. 'service' => OtpTestData::SERVICE . '_bis',
  575. 'account' => OtpTestData::ACCOUNT . '_bis',
  576. 'otp_type' => 'totp',
  577. 'secret' => OtpTestData::SECRET,
  578. 'digits' => OtpTestData::DIGITS_DEFAULT,
  579. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  580. 'period' => OtpTestData::PERIOD_DEFAULT,
  581. 'counter' => null,
  582. ]);
  583. }
  584. /**
  585. * @test
  586. */
  587. public function test_migrate_identify_duplicates_in_authenticated_user_twofaccounts_only()
  588. {
  589. $twofaccount = TwoFAccount::factory()->for($this->anotherUser)->create([
  590. 'otp_type' => 'totp',
  591. 'account' => OtpTestData::ACCOUNT,
  592. 'service' => OtpTestData::SERVICE,
  593. 'secret' => OtpTestData::SECRET,
  594. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  595. 'digits' => OtpTestData::DIGITS_DEFAULT,
  596. 'period' => OtpTestData::PERIOD_DEFAULT,
  597. 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
  598. 'icon' => '',
  599. ]);
  600. $response = $this->actingAs($this->user, 'api-guard')
  601. ->json('POST', '/api/v1/twofaccounts/migration?withSecret=1', [
  602. 'payload' => MigrationTestData::GOOGLE_AUTH_MIGRATION_URI,
  603. ])
  604. ->assertOk()
  605. ->assertJsonFragment([
  606. 'id' => 0,
  607. 'account' => OtpTestData::ACCOUNT,
  608. 'service' => OtpTestData::SERVICE,
  609. 'otp_type' => 'totp',
  610. 'secret' => OtpTestData::SECRET,
  611. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  612. 'digits' => OtpTestData::DIGITS_DEFAULT,
  613. 'period' => OtpTestData::PERIOD_DEFAULT,
  614. 'icon' => null,
  615. ])
  616. ->assertJsonFragment([
  617. 'id' => 0,
  618. 'service' => OtpTestData::SERVICE . '_bis',
  619. 'account' => OtpTestData::ACCOUNT . '_bis',
  620. 'otp_type' => 'totp',
  621. 'secret' => OtpTestData::SECRET,
  622. 'digits' => OtpTestData::DIGITS_DEFAULT,
  623. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  624. 'period' => OtpTestData::PERIOD_DEFAULT,
  625. 'counter' => null,
  626. ]);
  627. }
  628. /**
  629. * @test
  630. */
  631. public function test_migrate_invalid_gauth_payload_returns_bad_request()
  632. {
  633. $response = $this->actingAs($this->user, 'api-guard')
  634. ->json('POST', '/api/v1/twofaccounts/migration', [
  635. 'payload' => MigrationTestData::GOOGLE_AUTH_MIGRATION_URI_WITH_INVALID_DATA,
  636. ])
  637. ->assertStatus(400)
  638. ->assertJsonStructure([
  639. 'message',
  640. ]);
  641. }
  642. /**
  643. * @test
  644. */
  645. public function test_migrate_valid_aegis_json_file_returns_success()
  646. {
  647. $file = LocalFile::fake()->validAegisJsonFile();
  648. $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
  649. ->actingAs($this->user, 'api-guard')
  650. ->json('POST', '/api/v1/twofaccounts/migration', [
  651. 'file' => $file,
  652. 'withSecret' => 1,
  653. ])
  654. ->assertOk()
  655. ->assertJsonCount(3, $key = null)
  656. ->assertJsonFragment([
  657. 'id' => 0,
  658. 'service' => OtpTestData::SERVICE,
  659. 'account' => OtpTestData::ACCOUNT,
  660. 'otp_type' => 'totp',
  661. 'secret' => OtpTestData::SECRET,
  662. 'digits' => OtpTestData::DIGITS_CUSTOM,
  663. 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
  664. 'period' => OtpTestData::PERIOD_CUSTOM,
  665. 'counter' => null,
  666. ])
  667. ->assertJsonFragment([
  668. 'id' => 0,
  669. 'service' => OtpTestData::SERVICE,
  670. 'account' => OtpTestData::ACCOUNT,
  671. 'otp_type' => 'hotp',
  672. 'secret' => OtpTestData::SECRET,
  673. 'digits' => OtpTestData::DIGITS_CUSTOM,
  674. 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
  675. 'period' => null,
  676. 'counter' => OtpTestData::COUNTER_CUSTOM,
  677. ])
  678. ->assertJsonFragment([
  679. 'id' => 0,
  680. 'service' => OtpTestData::STEAM,
  681. 'account' => OtpTestData::ACCOUNT,
  682. 'otp_type' => 'steamtotp',
  683. 'secret' => OtpTestData::STEAM_SECRET,
  684. 'digits' => OtpTestData::DIGITS_STEAM,
  685. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  686. 'period' => OtpTestData::PERIOD_DEFAULT,
  687. 'counter' => null,
  688. ]);
  689. }
  690. /**
  691. * @test
  692. */
  693. #[DataProvider('invalidAegisJsonFileProvider')]
  694. public function test_migrate_invalid_aegis_json_file_returns_bad_request($file)
  695. {
  696. $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
  697. ->actingAs($this->user, 'api-guard')
  698. ->json('POST', '/api/v1/twofaccounts/migration', [
  699. 'file' => $file,
  700. ])
  701. ->assertStatus(400);
  702. }
  703. /**
  704. * Provide invalid Aegis JSON files for import tests
  705. */
  706. public static function invalidAegisJsonFileProvider()
  707. {
  708. return [
  709. 'encryptedAegisJsonFile' => [
  710. LocalFile::fake()->encryptedAegisJsonFile(),
  711. ],
  712. 'invalidAegisJsonFile' => [
  713. LocalFile::fake()->invalidAegisJsonFile(),
  714. ],
  715. ];
  716. }
  717. /**
  718. * @test
  719. */
  720. #[DataProvider('validPlainTextFileProvider')]
  721. public function test_migrate_valid_plain_text_file_returns_success($file)
  722. {
  723. $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
  724. ->actingAs($this->user, 'api-guard')
  725. ->json('POST', '/api/v1/twofaccounts/migration', [
  726. 'file' => $file,
  727. 'withSecret' => 1,
  728. ])
  729. ->assertOk()
  730. ->assertJsonCount(3, $key = null)
  731. ->assertJsonFragment([
  732. 'id' => 0,
  733. 'service' => OtpTestData::SERVICE,
  734. 'account' => OtpTestData::ACCOUNT,
  735. 'otp_type' => 'totp',
  736. 'secret' => OtpTestData::SECRET,
  737. 'digits' => OtpTestData::DIGITS_CUSTOM,
  738. 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
  739. 'period' => OtpTestData::PERIOD_CUSTOM,
  740. 'counter' => null,
  741. ])
  742. ->assertJsonFragment([
  743. 'id' => 0,
  744. 'service' => OtpTestData::SERVICE,
  745. 'account' => OtpTestData::ACCOUNT,
  746. 'otp_type' => 'hotp',
  747. 'secret' => OtpTestData::SECRET,
  748. 'digits' => OtpTestData::DIGITS_CUSTOM,
  749. 'algorithm' => OtpTestData::ALGORITHM_CUSTOM,
  750. 'period' => null,
  751. 'counter' => OtpTestData::COUNTER_CUSTOM,
  752. ])
  753. ->assertJsonFragment([
  754. 'id' => 0,
  755. 'service' => OtpTestData::STEAM,
  756. 'account' => OtpTestData::ACCOUNT,
  757. 'otp_type' => 'steamtotp',
  758. 'secret' => OtpTestData::STEAM_SECRET,
  759. 'digits' => OtpTestData::DIGITS_STEAM,
  760. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  761. 'period' => OtpTestData::PERIOD_DEFAULT,
  762. 'counter' => null,
  763. ]);
  764. }
  765. /**
  766. * Provide valid Plain Text files for import tests
  767. */
  768. public static function validPlainTextFileProvider()
  769. {
  770. return [
  771. 'validPlainTextFile' => [
  772. LocalFile::fake()->validPlainTextFile(),
  773. ],
  774. 'validPlainTextFileWithNewLines' => [
  775. LocalFile::fake()->validPlainTextFileWithNewLines(),
  776. ],
  777. ];
  778. }
  779. /**
  780. * @test
  781. */
  782. #[DataProvider('invalidPlainTextFileProvider')]
  783. public function test_migrate_invalid_plain_text_file_returns_bad_request($file)
  784. {
  785. $response = $this->withHeaders(['Content-Type' => 'multipart/form-data'])
  786. ->actingAs($this->user, 'api-guard')
  787. ->json('POST', '/api/v1/twofaccounts/migration', [
  788. 'file' => $file,
  789. ])
  790. ->assertStatus(400);
  791. }
  792. /**
  793. * Provide invalid Plain Text files for import tests
  794. */
  795. public static function invalidPlainTextFileProvider()
  796. {
  797. return [
  798. 'invalidPlainTextFileEmpty' => [
  799. LocalFile::fake()->invalidPlainTextFileEmpty(),
  800. ],
  801. 'invalidPlainTextFileNoUri' => [
  802. LocalFile::fake()->invalidPlainTextFileNoUri(),
  803. ],
  804. 'invalidPlainTextFileWithInvalidUri' => [
  805. LocalFile::fake()->invalidPlainTextFileWithInvalidUri(),
  806. ],
  807. 'invalidPlainTextFileWithInvalidLine' => [
  808. LocalFile::fake()->invalidPlainTextFileWithInvalidLine(),
  809. ],
  810. ];
  811. }
  812. /**
  813. * @test
  814. */
  815. public function test_reorder_returns_success()
  816. {
  817. $response = $this->actingAs($this->user, 'api-guard')
  818. ->json('POST', '/api/v1/twofaccounts/reorder', [
  819. 'orderedIds' => [$this->twofaccountB->id, $this->twofaccountA->id],
  820. ])
  821. ->assertStatus(200)
  822. ->assertJsonStructure([
  823. 'message',
  824. ]);
  825. }
  826. /**
  827. * @test
  828. */
  829. public function test_reorder_with_invalid_data_returns_validation_error()
  830. {
  831. $response = $this->actingAs($this->user, 'api-guard')
  832. ->json('POST', '/api/v1/twofaccounts/reorder', [
  833. 'orderedIds' => '3,2,1',
  834. ])
  835. ->assertStatus(422);
  836. }
  837. /**
  838. * @test
  839. */
  840. public function test_reorder_twofaccounts_of_another_user_is_forbidden()
  841. {
  842. $response = $this->actingAs($this->user, 'api-guard')
  843. ->json('POST', '/api/v1/twofaccounts/reorder', [
  844. 'orderedIds' => [$this->twofaccountB->id, $this->twofaccountD->id],
  845. ])
  846. ->assertForbidden()
  847. ->assertJsonStructure([
  848. 'message',
  849. ]);
  850. }
  851. /**
  852. * @test
  853. */
  854. public function test_preview_returns_success_with_resource()
  855. {
  856. $response = $this->actingAs($this->user, 'api-guard')
  857. ->json('POST', '/api/v1/twofaccounts/preview', [
  858. 'uri' => OtpTestData::TOTP_FULL_CUSTOM_URI,
  859. ])
  860. ->assertOk()
  861. ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_CUSTOM_TOTP);
  862. }
  863. /**
  864. * @test
  865. */
  866. public function test_preview_with_invalid_data_returns_validation_error()
  867. {
  868. $response = $this->actingAs($this->user, 'api-guard')
  869. ->json('POST', '/api/v1/twofaccounts/preview', [
  870. 'uri' => OtpTestData::INVALID_OTPAUTH_URI,
  871. ])
  872. ->assertStatus(422);
  873. }
  874. /**
  875. * @test
  876. */
  877. public function test_preview_with_unreachable_image_returns_success()
  878. {
  879. $response = $this->actingAs($this->user, 'api-guard')
  880. ->json('POST', '/api/v1/twofaccounts/preview', [
  881. 'uri' => OtpTestData::TOTP_URI_WITH_UNREACHABLE_IMAGE,
  882. ])
  883. ->assertOk()
  884. ->assertJsonFragment([
  885. 'icon' => null,
  886. ]);
  887. }
  888. /**
  889. * @test
  890. */
  891. public function test_export_returns_json_migration_resource()
  892. {
  893. $this->twofaccountA = TwoFAccount::factory()->for($this->user)->create(self::JSON_FRAGMENTS_FOR_DEFAULT_TOTP);
  894. $this->twofaccountB = TwoFAccount::factory()->for($this->user)->create(self::JSON_FRAGMENTS_FOR_DEFAULT_HOTP);
  895. $this->actingAs($this->user, 'api-guard')
  896. ->json('GET', '/api/v1/twofaccounts/export?ids=' . $this->twofaccountA->id . ',' . $this->twofaccountB->id)
  897. ->assertOk()
  898. ->assertJsonStructure(self::VALID_EXPORT_STRUTURE)
  899. ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_DEFAULT_TOTP)
  900. ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_DEFAULT_HOTP);
  901. }
  902. /**
  903. * @test
  904. */
  905. public function test_export_too_many_ids_returns_bad_request()
  906. {
  907. TwoFAccount::factory()->count(102)->for($this->user)->create();
  908. $ids = DB::table('twofaccounts')->where('user_id', $this->user->id)->pluck('id')->implode(',');
  909. $response = $this->actingAs($this->user, 'api-guard')
  910. ->json('GET', '/api/v1/twofaccounts/export?ids=' . $ids)
  911. ->assertStatus(400)
  912. ->assertJsonStructure([
  913. 'message',
  914. 'reason',
  915. ]);
  916. }
  917. /**
  918. * @test
  919. */
  920. public function test_export_missing_twofaccount_returns_existing_ones_only()
  921. {
  922. $this->twofaccountA = TwoFAccount::factory()->for($this->user)->create(self::JSON_FRAGMENTS_FOR_DEFAULT_TOTP);
  923. $response = $this->actingAs($this->user, 'api-guard')
  924. ->json('GET', '/api/v1/twofaccounts/export?ids=' . $this->twofaccountA->id . ',1000')
  925. ->assertJsonFragment(self::JSON_FRAGMENTS_FOR_DEFAULT_TOTP);
  926. }
  927. /**
  928. * @test
  929. */
  930. public function test_export_twofaccount_of_another_user_is_forbidden()
  931. {
  932. $response = $this->actingAs($this->user, 'api-guard')
  933. ->json('GET', '/api/v1/twofaccounts/export?ids=' . $this->twofaccountC->id)
  934. ->assertForbidden()
  935. ->assertJsonStructure([
  936. 'message',
  937. ]);
  938. }
  939. /**
  940. * @test
  941. */
  942. public function test_get_otp_using_totp_twofaccount_id_returns_consistent_resource()
  943. {
  944. $twofaccount = TwoFAccount::factory()->for($this->user)->create([
  945. 'otp_type' => 'totp',
  946. 'account' => OtpTestData::ACCOUNT,
  947. 'service' => OtpTestData::SERVICE,
  948. 'secret' => OtpTestData::SECRET,
  949. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  950. 'digits' => OtpTestData::DIGITS_DEFAULT,
  951. 'period' => OtpTestData::PERIOD_DEFAULT,
  952. 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
  953. 'icon' => '',
  954. ]);
  955. $response = $this->actingAs($this->user, 'api-guard')
  956. ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id . '/otp')
  957. ->assertOk()
  958. ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP)
  959. ->assertJsonFragment([
  960. 'otp_type' => 'totp',
  961. 'period' => OtpTestData::PERIOD_DEFAULT,
  962. ]);
  963. }
  964. /**
  965. * @test
  966. */
  967. public function test_get_otp_by_posting_totp_uri_returns_consistent_resource()
  968. {
  969. $response = $this->actingAs($this->user, 'api-guard')
  970. ->json('POST', '/api/v1/twofaccounts/otp', [
  971. 'uri' => OtpTestData::TOTP_FULL_CUSTOM_URI,
  972. ])
  973. ->assertOk()
  974. ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP)
  975. ->assertJsonFragment([
  976. 'otp_type' => 'totp',
  977. 'period' => OtpTestData::PERIOD_CUSTOM,
  978. ]);
  979. }
  980. /**
  981. * @test
  982. */
  983. public function test_get_otp_by_posting_totp_parameters_returns_consistent_resource()
  984. {
  985. $response = $this->actingAs($this->user, 'api-guard')
  986. ->json('POST', '/api/v1/twofaccounts/otp', OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)
  987. ->assertOk()
  988. ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_TOTP)
  989. ->assertJsonFragment([
  990. 'otp_type' => 'totp',
  991. 'period' => OtpTestData::PERIOD_CUSTOM,
  992. ]);
  993. }
  994. /**
  995. * @test
  996. */
  997. public function test_get_otp_using_hotp_twofaccount_id_returns_consistent_resource()
  998. {
  999. $twofaccount = TwoFAccount::factory()->for($this->user)->create([
  1000. 'otp_type' => 'hotp',
  1001. 'account' => OtpTestData::ACCOUNT,
  1002. 'service' => OtpTestData::SERVICE,
  1003. 'secret' => OtpTestData::SECRET,
  1004. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  1005. 'digits' => OtpTestData::DIGITS_DEFAULT,
  1006. 'period' => null,
  1007. 'legacy_uri' => OtpTestData::HOTP_SHORT_URI,
  1008. 'icon' => '',
  1009. ]);
  1010. $response = $this->actingAs($this->user, 'api-guard')
  1011. ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id . '/otp')
  1012. ->assertOk()
  1013. ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP)
  1014. ->assertJsonFragment([
  1015. 'otp_type' => 'hotp',
  1016. 'counter' => OtpTestData::COUNTER_DEFAULT + 1,
  1017. ]);
  1018. }
  1019. /**
  1020. * @test
  1021. */
  1022. public function test_get_otp_by_posting_hotp_uri_returns_consistent_resource()
  1023. {
  1024. $response = $this->actingAs($this->user, 'api-guard')
  1025. ->json('POST', '/api/v1/twofaccounts/otp', [
  1026. 'uri' => OtpTestData::HOTP_FULL_CUSTOM_URI,
  1027. ])
  1028. ->assertOk()
  1029. ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP)
  1030. ->assertJsonFragment([
  1031. 'otp_type' => 'hotp',
  1032. 'counter' => OtpTestData::COUNTER_CUSTOM + 1,
  1033. ]);
  1034. }
  1035. /**
  1036. * @test
  1037. */
  1038. public function test_get_otp_by_posting_hotp_parameters_returns_consistent_resource()
  1039. {
  1040. $response = $this->actingAs($this->user, 'api-guard')
  1041. ->json('POST', '/api/v1/twofaccounts/otp', OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)
  1042. ->assertOk()
  1043. ->assertJsonStructure(self::VALID_OTP_RESOURCE_STRUCTURE_FOR_HOTP)
  1044. ->assertJsonFragment([
  1045. 'otp_type' => 'hotp',
  1046. 'counter' => OtpTestData::COUNTER_CUSTOM + 1,
  1047. ]);
  1048. }
  1049. /**
  1050. * @test
  1051. */
  1052. public function test_get_otp_by_posting_multiple_inputs_returns_bad_request()
  1053. {
  1054. $response = $this->actingAs($this->user, 'api-guard')
  1055. ->json('POST', '/api/v1/twofaccounts/otp', [
  1056. 'uri' => OtpTestData::HOTP_FULL_CUSTOM_URI,
  1057. 'key' => 'value',
  1058. ])
  1059. ->assertStatus(400)
  1060. ->assertJsonStructure([
  1061. 'message',
  1062. 'reason',
  1063. ]);
  1064. }
  1065. /**
  1066. * @test
  1067. */
  1068. public function test_get_otp_using_indecipherable_twofaccount_id_returns_bad_request()
  1069. {
  1070. Settings::set('useEncryption', true);
  1071. $twofaccount = TwoFAccount::factory()->for($this->user)->create();
  1072. DB::table('twofaccounts')
  1073. ->where('id', $twofaccount->id)
  1074. ->update([
  1075. 'secret' => '**encrypted**',
  1076. ]);
  1077. $response = $this->actingAs($this->user, 'api-guard')
  1078. ->json('GET', '/api/v1/twofaccounts/' . $twofaccount->id . '/otp')
  1079. ->assertStatus(400)
  1080. ->assertJsonStructure([
  1081. 'message',
  1082. ]);
  1083. }
  1084. /**
  1085. * @test
  1086. */
  1087. public function test_get_otp_using_missing_twofaccount_id_returns_not_found()
  1088. {
  1089. $response = $this->actingAs($this->user, 'api-guard')
  1090. ->json('GET', '/api/v1/twofaccounts/1000/otp')
  1091. ->assertNotFound();
  1092. }
  1093. /**
  1094. * @test
  1095. */
  1096. public function test_get_otp_by_posting_invalid_uri_returns_validation_error()
  1097. {
  1098. $response = $this->actingAs($this->user, 'api-guard')
  1099. ->json('POST', '/api/v1/twofaccounts/otp', [
  1100. 'uri' => OtpTestData::INVALID_OTPAUTH_URI,
  1101. ])
  1102. ->assertStatus(422);
  1103. }
  1104. /**
  1105. * @test
  1106. */
  1107. public function test_get_otp_by_posting_invalid_parameters_returns_validation_error()
  1108. {
  1109. $response = $this->actingAs($this->user, 'api-guard')
  1110. ->json('POST', '/api/v1/twofaccounts/otp', self::ARRAY_OF_INVALID_PARAMETERS)
  1111. ->assertStatus(422);
  1112. }
  1113. /**
  1114. * @test
  1115. */
  1116. public function test_get_otp_of_another_user_twofaccount_is_forbidden()
  1117. {
  1118. $response = $this->actingAs($this->user, 'api-guard')
  1119. ->json('GET', '/api/v1/twofaccounts/' . $this->twofaccountC->id . '/otp')
  1120. ->assertForbidden()
  1121. ->assertJsonStructure([
  1122. 'message',
  1123. ]);
  1124. }
  1125. /**
  1126. * @test
  1127. */
  1128. public function test_count_returns_right_number_of_twofaccounts()
  1129. {
  1130. $response = $this->actingAs($this->user, 'api-guard')
  1131. ->json('GET', '/api/v1/twofaccounts/count')
  1132. ->assertStatus(200)
  1133. ->assertExactJson([
  1134. 'count' => 2,
  1135. ]);
  1136. }
  1137. /**
  1138. * @test
  1139. */
  1140. public function test_withdraw_returns_success()
  1141. {
  1142. $response = $this->actingAs($this->user, 'api-guard')
  1143. ->json('PATCH', '/api/v1/twofaccounts/withdraw?ids=1,2')
  1144. ->assertOk()
  1145. ->assertJsonStructure([
  1146. 'message',
  1147. ]);
  1148. }
  1149. /**
  1150. * @test
  1151. */
  1152. public function test_withdraw_too_many_ids_returns_bad_request()
  1153. {
  1154. TwoFAccount::factory()->count(102)->for($this->user)->create();
  1155. $ids = DB::table('twofaccounts')->where('user_id', $this->user->id)->pluck('id')->implode(',');
  1156. $response = $this->actingAs($this->user, 'api-guard')
  1157. ->json('PATCH', '/api/v1/twofaccounts/withdraw?ids=' . $ids)
  1158. ->assertStatus(400)
  1159. ->assertJsonStructure([
  1160. 'message',
  1161. 'reason',
  1162. ]);
  1163. }
  1164. /**
  1165. * @test
  1166. */
  1167. public function test_destroy_twofaccount_returns_success()
  1168. {
  1169. $response = $this->actingAs($this->user, 'api-guard')
  1170. ->json('DELETE', '/api/v1/twofaccounts/' . $this->twofaccountA->id)
  1171. ->assertNoContent();
  1172. }
  1173. /**
  1174. * @test
  1175. */
  1176. public function test_destroy_missing_twofaccount_returns_not_found()
  1177. {
  1178. $response = $this->actingAs($this->user, 'api-guard')
  1179. ->json('DELETE', '/api/v1/twofaccounts/1000')
  1180. ->assertNotFound();
  1181. }
  1182. /**
  1183. * @test
  1184. */
  1185. public function test_destroy_twofaccount_of_another_user_is_forbidden()
  1186. {
  1187. $response = $this->actingAs($this->user, 'api-guard')
  1188. ->json('DELETE', '/api/v1/twofaccounts/' . $this->twofaccountC->id)
  1189. ->assertForbidden()
  1190. ->assertJsonStructure([
  1191. 'message',
  1192. ]);
  1193. }
  1194. /**
  1195. * @test
  1196. */
  1197. public function test_batch_destroy_twofaccount_returns_success()
  1198. {
  1199. TwoFAccount::factory()->count(3)->for($this->user)->create();
  1200. $response = $this->actingAs($this->user, 'api-guard')
  1201. ->json('DELETE', '/api/v1/twofaccounts?ids=' . $this->twofaccountA->id . ',' . $this->twofaccountB->id)
  1202. ->assertNoContent();
  1203. }
  1204. /**
  1205. * @test
  1206. */
  1207. public function test_batch_destroy_too_many_twofaccounts_returns_bad_request()
  1208. {
  1209. TwoFAccount::factory()->count(102)->for($this->user)->create();
  1210. $ids = DB::table('twofaccounts')->where('user_id', $this->user->id)->pluck('id')->implode(',');
  1211. $response = $this->actingAs($this->user, 'api-guard')
  1212. ->json('DELETE', '/api/v1/twofaccounts?ids=' . $ids)
  1213. ->assertStatus(400)
  1214. ->assertJsonStructure([
  1215. 'message',
  1216. 'reason',
  1217. ]);
  1218. }
  1219. /**
  1220. * @test
  1221. */
  1222. public function test_batch_destroy_twofaccount_of_another_user_is_forbidden()
  1223. {
  1224. TwoFAccount::factory()->count(2)->for($this->anotherUser)->create();
  1225. $ids = DB::table('twofaccounts')
  1226. ->where('user_id', $this->anotherUser->id)
  1227. ->pluck('id')
  1228. ->implode(',');
  1229. $response = $this->actingAs($this->user, 'api-guard')
  1230. ->json('DELETE', '/api/v1/twofaccounts?ids=' . $ids)
  1231. ->assertForbidden()
  1232. ->assertJsonStructure([
  1233. 'message',
  1234. ]);
  1235. }
  1236. }