TwoFAccountControllerTest.php 38 KB

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