TwoFAccountControllerTest.php 53 KB

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