TwoFAccountModelTest.php 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. <?php
  2. namespace Tests\Feature\Models;
  3. use App\Models\TwoFAccount;
  4. use Tests\Data\OtpTestData;
  5. use Tests\FeatureTestCase;
  6. use Illuminate\Support\Facades\Storage;
  7. use Illuminate\Http\Testing\FileFactory;
  8. use Illuminate\Support\Facades\Http;
  9. use App\Helpers\Helpers;
  10. use Mockery\MockInterface;
  11. use Tests\Data\HttpRequestTestData;
  12. /**
  13. * @covers \App\Models\TwoFAccount
  14. */
  15. class TwoFAccountModelTest extends FeatureTestCase
  16. {
  17. /**
  18. * App\Models\TwoFAccount $customTotpTwofaccount
  19. */
  20. protected $customTotpTwofaccount;
  21. /**
  22. * App\Models\TwoFAccount $customTotpTwofaccount
  23. */
  24. protected $customHotpTwofaccount;
  25. /**
  26. *
  27. */
  28. const ICON_NAME = 'oDBngpjQaQAgLtHqGuYiPRqftCXv6Sj4hSAXARpA.png';
  29. /**
  30. * @test
  31. */
  32. public function setUp(): void
  33. {
  34. parent::setUp();
  35. $this->customTotpTwofaccount = new TwoFAccount;
  36. $this->customTotpTwofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI;
  37. $this->customTotpTwofaccount->service = OtpTestData::SERVICE;
  38. $this->customTotpTwofaccount->account = OtpTestData::ACCOUNT;
  39. $this->customTotpTwofaccount->icon = OtpTestData::ICON;
  40. $this->customTotpTwofaccount->otp_type = 'totp';
  41. $this->customTotpTwofaccount->secret = OtpTestData::SECRET;
  42. $this->customTotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM;
  43. $this->customTotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
  44. $this->customTotpTwofaccount->period = OtpTestData::PERIOD_CUSTOM;
  45. $this->customTotpTwofaccount->counter = null;
  46. $this->customTotpTwofaccount->save();
  47. $this->customHotpTwofaccount = new TwoFAccount;
  48. $this->customHotpTwofaccount->legacy_uri = OtpTestData::HOTP_FULL_CUSTOM_URI;
  49. $this->customHotpTwofaccount->service = OtpTestData::SERVICE;
  50. $this->customHotpTwofaccount->account = OtpTestData::ACCOUNT;
  51. $this->customHotpTwofaccount->icon = OtpTestData::ICON;
  52. $this->customHotpTwofaccount->otp_type = 'hotp';
  53. $this->customHotpTwofaccount->secret = OtpTestData::SECRET;
  54. $this->customHotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM;
  55. $this->customHotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
  56. $this->customHotpTwofaccount->period = null;
  57. $this->customHotpTwofaccount->counter = OtpTestData::COUNTER_CUSTOM;
  58. $this->customHotpTwofaccount->save();
  59. $this->customSteamTotpTwofaccount = new TwoFAccount;
  60. $this->customSteamTotpTwofaccount->legacy_uri = OtpTestData::STEAM_TOTP_URI;
  61. $this->customSteamTotpTwofaccount->service = OtpTestData::STEAM;
  62. $this->customSteamTotpTwofaccount->account = OtpTestData::ACCOUNT;
  63. $this->customSteamTotpTwofaccount->otp_type = 'steamtotp';
  64. $this->customSteamTotpTwofaccount->secret = OtpTestData::STEAM_SECRET;
  65. $this->customSteamTotpTwofaccount->digits = OtpTestData::DIGITS_STEAM;
  66. $this->customSteamTotpTwofaccount->algorithm = OtpTestData::ALGORITHM_DEFAULT;
  67. $this->customSteamTotpTwofaccount->period = OtpTestData::PERIOD_DEFAULT;
  68. $this->customSteamTotpTwofaccount->counter = null;
  69. $this->customSteamTotpTwofaccount->save();
  70. }
  71. /**
  72. * @test
  73. *
  74. * @runInSeparateProcess
  75. * @preserveGlobalState disabled
  76. */
  77. public function test_fill_with_custom_totp_uri_returns_correct_value()
  78. {
  79. $this->mock('alias:' . Helpers::class, function (MockInterface $helper) {
  80. $helper->shouldReceive('getUniqueFilename')
  81. ->andReturn(self::ICON_NAME);
  82. $helper->shouldReceive('isValidImage')
  83. ->andReturn(true);
  84. });
  85. $file = (new FileFactory)->image(self::ICON_NAME, 10, 10);
  86. Http::preventStrayRequests();
  87. Http::fake([
  88. 'https://en.opensuse.org/images/4/44/Button-filled-colour.png' => Http::response($file->tempFile, 200),
  89. ]);
  90. Storage::fake('imagesLink');
  91. Storage::fake('icons');
  92. $twofaccount = new TwoFAccount;
  93. $twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI);
  94. Storage::disk('icons')->assertExists(self::ICON_NAME);
  95. Storage::disk('imagesLink')->assertMissing(self::ICON_NAME);
  96. $this->assertEquals('totp', $twofaccount->otp_type);
  97. $this->assertEquals(OtpTestData::TOTP_FULL_CUSTOM_URI, $twofaccount->legacy_uri);
  98. $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
  99. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  100. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  101. $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
  102. $this->assertEquals(OtpTestData::PERIOD_CUSTOM, $twofaccount->period);
  103. $this->assertEquals(null, $twofaccount->counter);
  104. $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
  105. $this->assertEquals(self::ICON_NAME, $twofaccount->icon);
  106. }
  107. /**
  108. * @test
  109. */
  110. public function test_fill_with_basic_totp_uri_returns_default_value()
  111. {
  112. $twofaccount = new TwoFAccount;
  113. $twofaccount->fillWithURI(OtpTestData::TOTP_SHORT_URI);
  114. $this->assertEquals('totp', $twofaccount->otp_type);
  115. $this->assertEquals(OtpTestData::TOTP_SHORT_URI, $twofaccount->legacy_uri);
  116. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  117. $this->assertEquals(null, $twofaccount->service);
  118. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  119. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  120. $this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccount->period);
  121. $this->assertEquals(null, $twofaccount->counter);
  122. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  123. $this->assertEquals(null, $twofaccount->icon);
  124. }
  125. /**
  126. * @test
  127. *
  128. * @runInSeparateProcess
  129. * @preserveGlobalState disabled
  130. */
  131. public function test_fill_with_custom_hotp_uri_returns_correct_value()
  132. {
  133. $this->mock('alias:' . Helpers::class, function (MockInterface $helper) {
  134. $helper->shouldReceive('getUniqueFilename')
  135. ->andReturn(self::ICON_NAME);
  136. $helper->shouldReceive('isValidImage')
  137. ->andReturn(true);
  138. });
  139. $file = (new FileFactory)->image(self::ICON_NAME, 10, 10);
  140. Http::preventStrayRequests();
  141. Http::fake([
  142. 'https://en.opensuse.org/images/4/44/Button-filled-colour.png' => Http::response($file->tempFile, 200),
  143. ]);
  144. Storage::fake('imagesLink');
  145. Storage::fake('icons');
  146. $twofaccount = new TwoFAccount;
  147. $twofaccount->fillWithURI(OtpTestData::HOTP_FULL_CUSTOM_URI);
  148. Storage::disk('icons')->assertExists(self::ICON_NAME);
  149. Storage::disk('imagesLink')->assertMissing(self::ICON_NAME);
  150. $this->assertEquals('hotp', $twofaccount->otp_type);
  151. $this->assertEquals(OtpTestData::HOTP_FULL_CUSTOM_URI, $twofaccount->legacy_uri);
  152. $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
  153. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  154. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  155. $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
  156. $this->assertEquals(null, $twofaccount->period);
  157. $this->assertEquals(OtpTestData::COUNTER_CUSTOM, $twofaccount->counter);
  158. $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
  159. $this->assertEquals(self::ICON_NAME, $twofaccount->icon);
  160. }
  161. /**
  162. * @test
  163. */
  164. public function test_fill_with_basic_hotp_uri_returns_default_value()
  165. {
  166. $twofaccount = new TwoFAccount;
  167. $twofaccount->fillWithURI(OtpTestData::HOTP_SHORT_URI);
  168. $this->assertEquals('hotp', $twofaccount->otp_type);
  169. $this->assertEquals(OtpTestData::HOTP_SHORT_URI, $twofaccount->legacy_uri);
  170. $this->assertEquals(null, $twofaccount->service);
  171. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  172. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  173. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  174. $this->assertEquals(null, $twofaccount->period);
  175. $this->assertEquals(OtpTestData::COUNTER_DEFAULT, $twofaccount->counter);
  176. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  177. $this->assertEquals(null, $twofaccount->icon);
  178. }
  179. /**
  180. * @test
  181. */
  182. public function test_filled_with_uri_persists_correct_values_to_db()
  183. {
  184. $twofaccount = new TwoFAccount;
  185. $twofaccount->fillWithURI(OtpTestData::TOTP_SHORT_URI);
  186. $twofaccount->save();
  187. $this->assertDatabaseHas('twofaccounts', [
  188. 'otp_type' => 'totp',
  189. 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
  190. 'service' => null,
  191. 'account' => OtpTestData::ACCOUNT,
  192. 'secret' => OtpTestData::SECRET,
  193. 'digits' => OtpTestData::DIGITS_DEFAULT,
  194. 'period' => OtpTestData::PERIOD_DEFAULT,
  195. 'counter' => null,
  196. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  197. 'icon' => null,
  198. ]);
  199. }
  200. /**
  201. * @test
  202. */
  203. public function test_fill_with_invalid_uri_returns_ValidationException()
  204. {
  205. $this->expectException(\Illuminate\Validation\ValidationException::class);
  206. $twofaccount = new TwoFAccount;
  207. $twofaccount->fillWithURI(OtpTestData::INVALID_OTPAUTH_URI);
  208. }
  209. /**
  210. * @test
  211. */
  212. public function test_fill_with_uri_without_label_returns_ValidationException()
  213. {
  214. $this->expectException(\Illuminate\Validation\ValidationException::class);
  215. $twofaccount = new TwoFAccount;
  216. $twofaccount->fillWithURI('otpauth://totp/?secret=' . OtpTestData::SECRET);
  217. }
  218. /**
  219. * @test
  220. */
  221. public function test_create_custom_totp_from_parameters_returns_correct_value()
  222. {
  223. $twofaccount = new TwoFAccount;
  224. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP);
  225. $this->assertEquals('totp', $twofaccount->otp_type);
  226. $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
  227. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  228. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  229. $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
  230. $this->assertEquals(OtpTestData::PERIOD_CUSTOM, $twofaccount->period);
  231. $this->assertEquals(null, $twofaccount->counter);
  232. $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
  233. $this->assertStringEndsWith('.png', $twofaccount->icon);
  234. }
  235. /**
  236. * @test
  237. */
  238. public function test_create_basic_totp_from_parameters_returns_correct_value()
  239. {
  240. $twofaccount = new TwoFAccount;
  241. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
  242. $this->assertEquals('totp', $twofaccount->otp_type);
  243. $this->assertEquals(null, $twofaccount->service);
  244. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  245. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  246. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  247. $this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccount->period);
  248. $this->assertEquals(null, $twofaccount->counter);
  249. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  250. $this->assertEquals(null, $twofaccount->icon);
  251. }
  252. /**
  253. * @test
  254. */
  255. public function test_create_custom_hotp_from_parameters_returns_correct_value()
  256. {
  257. $twofaccount = new TwoFAccount;
  258. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP);
  259. $this->assertEquals('hotp', $twofaccount->otp_type);
  260. $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
  261. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  262. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  263. $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
  264. $this->assertEquals(null, $twofaccount->period);
  265. $this->assertEquals(OtpTestData::COUNTER_CUSTOM, $twofaccount->counter);
  266. $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
  267. $this->assertStringEndsWith('.png', $twofaccount->icon);
  268. }
  269. /**
  270. * @test
  271. */
  272. public function test_create_basic_hotp_from_parameters_returns_correct_value()
  273. {
  274. $twofaccount = new TwoFAccount;
  275. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP);
  276. $this->assertEquals('hotp', $twofaccount->otp_type);
  277. $this->assertEquals(null, $twofaccount->service);
  278. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  279. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  280. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  281. $this->assertEquals(null, $twofaccount->period);
  282. $this->assertEquals(OtpTestData::COUNTER_DEFAULT, $twofaccount->counter);
  283. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  284. $this->assertEquals(null, $twofaccount->icon);
  285. }
  286. /**
  287. * @test
  288. */
  289. public function test_create_from_parameters_persists_correct_values_to_db()
  290. {
  291. $twofaccount = new TwoFAccount;
  292. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
  293. $twofaccount->save();
  294. $this->assertDatabaseHas('twofaccounts', [
  295. 'otp_type' => 'totp',
  296. 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
  297. 'service' => null,
  298. 'account' => OtpTestData::ACCOUNT,
  299. 'secret' => OtpTestData::SECRET,
  300. 'digits' => OtpTestData::DIGITS_DEFAULT,
  301. 'period' => OtpTestData::PERIOD_DEFAULT,
  302. 'counter' => null,
  303. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  304. 'icon' => null,
  305. ]);
  306. }
  307. /**
  308. * @test
  309. */
  310. public function test_create_from_unsupported_parameters_returns_unsupportedOtpTypeException()
  311. {
  312. $this->expectException(\App\Exceptions\UnsupportedOtpTypeException::class);
  313. $twofaccount = new TwoFAccount;
  314. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_PARAMETERS_FOR_UNSUPPORTED_OTP_TYPE);
  315. }
  316. /**
  317. * @test
  318. */
  319. public function test_create_from_invalid_parameters_type_returns_InvalidOtpParameterException()
  320. {
  321. $this->expectException(\App\Exceptions\InvalidOtpParameterException::class);
  322. $twofaccount = new TwoFAccount;
  323. $twofaccount->fillWithOtpParameters([
  324. 'account' => OtpTestData::ACCOUNT,
  325. 'otp_type' => 'totp',
  326. 'digits' => 'notsupported',
  327. ]);
  328. }
  329. /**
  330. * @test
  331. */
  332. public function test_create_from_invalid_parameters_returns_InvalidOtpParameterException()
  333. {
  334. $this->expectException(\App\Exceptions\InvalidOtpParameterException::class);
  335. $twofaccount = new TwoFAccount;
  336. $twofaccount->fillWithOtpParameters([
  337. 'account' => OtpTestData::ACCOUNT,
  338. 'otp_type' => 'totp',
  339. 'algorithm' => 'notsupported',
  340. ]);
  341. }
  342. /**
  343. * @test
  344. */
  345. public function test_update_totp_returns_updated_model()
  346. {
  347. $twofaccount = $this->customTotpTwofaccount;
  348. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
  349. $this->assertEquals('totp', $twofaccount->otp_type);
  350. $this->assertEquals(null, $twofaccount->service);
  351. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  352. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  353. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  354. $this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccount->period);
  355. $this->assertEquals(null, $twofaccount->counter);
  356. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  357. $this->assertEquals(null, $twofaccount->counter);
  358. $this->assertEquals(null, $twofaccount->icon);
  359. }
  360. /**
  361. * @test
  362. */
  363. public function test_update_hotp_returns_updated_model()
  364. {
  365. $twofaccount = $this->customTotpTwofaccount;
  366. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP);
  367. $this->assertEquals('hotp', $twofaccount->otp_type);
  368. $this->assertEquals(null, $twofaccount->service);
  369. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  370. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  371. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  372. $this->assertEquals(null, $twofaccount->period);
  373. $this->assertEquals(OtpTestData::COUNTER_DEFAULT, $twofaccount->counter);
  374. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  375. $this->assertEquals(null, $twofaccount->counter);
  376. $this->assertEquals(null, $twofaccount->icon);
  377. }
  378. /**
  379. * @test
  380. */
  381. public function test_update_totp_persists_updated_model()
  382. {
  383. $twofaccount = $this->customTotpTwofaccount;
  384. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
  385. $twofaccount->save();
  386. $this->assertDatabaseHas('twofaccounts', [
  387. 'otp_type' => 'totp',
  388. 'service' => null,
  389. 'account' => OtpTestData::ACCOUNT,
  390. 'secret' => OtpTestData::SECRET,
  391. 'digits' => OtpTestData::DIGITS_DEFAULT,
  392. 'period' => OtpTestData::PERIOD_DEFAULT,
  393. 'counter' => null,
  394. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  395. 'icon' => null,
  396. ]);
  397. }
  398. /**
  399. * @test
  400. *
  401. * @runInSeparateProcess
  402. * @preserveGlobalState disabled
  403. */
  404. public function test_getOTP_for_totp_returns_the_same_password()
  405. {
  406. $this->mock('alias:' . Helpers::class, function (MockInterface $helper) {
  407. $helper->shouldReceive('getUniqueFilename')
  408. ->andReturn(self::ICON_NAME);
  409. $helper->shouldReceive('isValidImage')
  410. ->andReturn(true);
  411. });
  412. Http::preventStrayRequests();
  413. Http::fake([
  414. 'https://en.opensuse.org/images/4/44/Button-filled-colour.png' => Http::response(HttpRequestTestData::ICON_PNG, 200),
  415. ]);
  416. Storage::fake('imagesLink');
  417. Storage::fake('icons');
  418. $twofaccount = new TwoFAccount;
  419. $otp_from_model = $this->customTotpTwofaccount->getOTP();
  420. $otp_from_uri = $twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI)->getOTP();
  421. if ($otp_from_model->generated_at === $otp_from_uri->generated_at) {
  422. $this->assertEquals($otp_from_model, $otp_from_uri);
  423. }
  424. $otp_from_model = $this->customTotpTwofaccount->getOTP();
  425. $otp_from_parameters = $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)->getOTP();
  426. if ($otp_from_model->generated_at === $otp_from_parameters->generated_at) {
  427. $this->assertEquals($otp_from_model, $otp_from_parameters);
  428. }
  429. }
  430. /**
  431. * @test
  432. *
  433. * @runInSeparateProcess
  434. * @preserveGlobalState disabled
  435. */
  436. public function test_getOTP_for_hotp_returns_the_same_password()
  437. {
  438. $this->mock('alias:' . Helpers::class, function (MockInterface $helper) {
  439. $helper->shouldReceive('getUniqueFilename')
  440. ->andReturn(self::ICON_NAME);
  441. $helper->shouldReceive('isValidImage')
  442. ->andReturn(true);
  443. });
  444. Http::preventStrayRequests();
  445. Http::fake([
  446. 'https://en.opensuse.org/images/4/44/Button-filled-colour.png' => Http::response(HttpRequestTestData::ICON_PNG, 200),
  447. ]);
  448. Storage::fake('imagesLink');
  449. Storage::fake('icons');
  450. $twofaccount = new TwoFAccount;
  451. $otp_from_model = $this->customHotpTwofaccount->getOTP();
  452. $otp_from_uri = $twofaccount->fillWithURI(OtpTestData::HOTP_FULL_CUSTOM_URI)->getOTP();
  453. $this->assertEquals($otp_from_model, $otp_from_uri);
  454. $otp_from_parameters = $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)->getOTP();
  455. $this->assertEquals($otp_from_model, $otp_from_parameters);
  456. }
  457. /**
  458. * @test
  459. */
  460. public function test_getOTP_for_steamtotp_returns_the_same_password()
  461. {
  462. $twofaccount = new TwoFAccount;
  463. $otp_from_model = $this->customSteamTotpTwofaccount->getOTP();
  464. $otp_from_uri = $twofaccount->fillWithURI(OtpTestData::STEAM_TOTP_URI)->getOTP();
  465. if ($otp_from_model->generated_at === $otp_from_uri->generated_at) {
  466. $this->assertEquals($otp_from_model, $otp_from_uri);
  467. }
  468. $otp_from_model = $this->customSteamTotpTwofaccount->getOTP();
  469. $otp_from_parameters = $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_STEAM_TOTP)->getOTP();
  470. if ($otp_from_model->generated_at === $otp_from_parameters->generated_at) {
  471. $this->assertEquals($otp_from_model, $otp_from_parameters);
  472. }
  473. }
  474. /**
  475. * @test
  476. */
  477. public function test_getOTP_for_totp_with_invalid_secret_returns_InvalidSecretException()
  478. {
  479. $twofaccount = new TwoFAccount;
  480. $this->expectException(\App\Exceptions\InvalidSecretException::class);
  481. $otp_from_uri = $twofaccount->fillWithURI('otpauth://totp/' . OtpTestData::ACCOUNT . '?secret=0')->getOTP();
  482. }
  483. /**
  484. * @test
  485. */
  486. public function test_getOTP_for_totp_with_undecipherable_secret_returns_UndecipherableException()
  487. {
  488. $twofaccount = new TwoFAccount;
  489. $this->expectException(\App\Exceptions\UndecipherableException::class);
  490. $otp_from_uri = $twofaccount->fillWithOtpParameters([
  491. 'account' => OtpTestData::ACCOUNT,
  492. 'otp_type' => 'totp',
  493. 'secret' => __('errors.indecipherable'),
  494. ])->getOTP();
  495. }
  496. /**
  497. * @test
  498. */
  499. public function test_getURI_for_custom_totp_model_returns_uri()
  500. {
  501. $uri = $this->customTotpTwofaccount->getURI();
  502. $this->assertStringContainsString('otpauth://totp/', $uri);
  503. $this->assertStringContainsString(OtpTestData::SERVICE, $uri);
  504. $this->assertStringContainsString(OtpTestData::ACCOUNT, $uri);
  505. $this->assertStringContainsString('secret=' . OtpTestData::SECRET, $uri);
  506. $this->assertStringContainsString('digits=' . OtpTestData::DIGITS_CUSTOM, $uri);
  507. $this->assertStringContainsString('period=' . OtpTestData::PERIOD_CUSTOM, $uri);
  508. $this->assertStringContainsString('algorithm=' . OtpTestData::ALGORITHM_CUSTOM, $uri);
  509. }
  510. /**
  511. * @test
  512. */
  513. public function test_getURI_for_custom_hotp_model_returns_uri()
  514. {
  515. $uri = $this->customHotpTwofaccount->getURI();
  516. $this->assertStringContainsString('otpauth://hotp/', $uri);
  517. $this->assertStringContainsString(OtpTestData::SERVICE, $uri);
  518. $this->assertStringContainsString(OtpTestData::ACCOUNT, $uri);
  519. $this->assertStringContainsString('secret=' . OtpTestData::SECRET, $uri);
  520. $this->assertStringContainsString('digits=' . OtpTestData::DIGITS_CUSTOM, $uri);
  521. $this->assertStringContainsString('counter=' . OtpTestData::COUNTER_CUSTOM, $uri);
  522. $this->assertStringContainsString('algorithm=' . OtpTestData::ALGORITHM_CUSTOM, $uri);
  523. }
  524. /**
  525. * @test
  526. *
  527. * @runInSeparateProcess
  528. * @preserveGlobalState disabled
  529. */
  530. public function test_fill_succeed_when_image_fetching_fails()
  531. {
  532. $this->mock('alias:' . Helpers::class, function (MockInterface $helper) {
  533. $helper->shouldReceive('getUniqueFilename')
  534. ->andReturn(self::ICON_NAME);
  535. });
  536. Http::preventStrayRequests();
  537. Storage::fake('imagesLink');
  538. Storage::fake('icons');
  539. $twofaccount = new TwoFAccount;
  540. $twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI);
  541. Storage::disk('icons')->assertMissing(self::ICON_NAME);
  542. Storage::disk('imagesLink')->assertMissing(self::ICON_NAME);
  543. }
  544. /**
  545. * @test
  546. */
  547. public function test_saving_totp_without_period_set_default_one()
  548. {
  549. $twofaccount = new TwoFAccount;
  550. $twofaccount->service = OtpTestData::SERVICE;
  551. $twofaccount->account = OtpTestData::ACCOUNT;
  552. $twofaccount->otp_type = TwoFAccount::TOTP;
  553. $twofaccount->secret = OtpTestData::SECRET;
  554. $twofaccount->save();
  555. $account = TwoFAccount::find($twofaccount->id);
  556. $this->assertEquals(TwoFAccount::DEFAULT_PERIOD, $account->period);
  557. }
  558. /**
  559. * @test
  560. */
  561. public function test_saving_hotp_without_counter_set_default_one()
  562. {
  563. $twofaccount = new TwoFAccount;
  564. $twofaccount->service = OtpTestData::SERVICE;
  565. $twofaccount->account = OtpTestData::ACCOUNT;
  566. $twofaccount->otp_type = TwoFAccount::HOTP;
  567. $twofaccount->secret = OtpTestData::SECRET;
  568. $twofaccount->save();
  569. $account = TwoFAccount::find($twofaccount->id);
  570. $this->assertEquals(TwoFAccount::DEFAULT_COUNTER, $account->counter);
  571. }
  572. /**
  573. * @test
  574. */
  575. public function test_equals_returns_true()
  576. {
  577. $twofaccount = new TwoFAccount;
  578. $twofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI;
  579. $twofaccount->service = OtpTestData::SERVICE;
  580. $twofaccount->account = OtpTestData::ACCOUNT;
  581. $twofaccount->icon = OtpTestData::ICON;
  582. $twofaccount->otp_type = 'totp';
  583. $twofaccount->secret = OtpTestData::SECRET;
  584. $twofaccount->digits = OtpTestData::DIGITS_CUSTOM;
  585. $twofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
  586. $twofaccount->period = OtpTestData::PERIOD_CUSTOM;
  587. $twofaccount->counter = null;
  588. $twofaccount->save();
  589. $this->assertTrue($twofaccount->equals($this->customTotpTwofaccount));
  590. }
  591. /**
  592. * @test
  593. */
  594. public function test_equals_returns_false()
  595. {
  596. $twofaccount = new TwoFAccount;
  597. $twofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI;
  598. $twofaccount->service = OtpTestData::SERVICE;
  599. $twofaccount->account = OtpTestData::ACCOUNT;
  600. $twofaccount->icon = OtpTestData::ICON;
  601. $twofaccount->otp_type = 'totp';
  602. $twofaccount->secret = OtpTestData::SECRET;
  603. $twofaccount->digits = OtpTestData::DIGITS_CUSTOM;
  604. $twofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
  605. $twofaccount->period = OtpTestData::PERIOD_CUSTOM;
  606. $twofaccount->counter = null;
  607. $twofaccount->save();
  608. $this->assertFalse($twofaccount->equals($this->customHotpTwofaccount));
  609. }
  610. }