TwoFAccountModelTest.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. namespace Tests\Feature\Models;
  3. use App\Models\TwoFAccount;
  4. use Tests\FeatureTestCase;
  5. use Tests\Classes\OtpTestData;
  6. /**
  7. * @covers \App\Models\TwoFAccount
  8. */
  9. class TwoFAccountModelTest extends FeatureTestCase
  10. {
  11. /**
  12. * App\Models\TwoFAccount $customTotpTwofaccount
  13. */
  14. protected $customTotpTwofaccount;
  15. /**
  16. * App\Models\TwoFAccount $customTotpTwofaccount
  17. */
  18. protected $customHotpTwofaccount;
  19. /**
  20. * @test
  21. */
  22. public function setUp() : void
  23. {
  24. parent::setUp();
  25. $this->customTotpTwofaccount = new TwoFAccount;
  26. $this->customTotpTwofaccount->legacy_uri = OtpTestData::TOTP_FULL_CUSTOM_URI;
  27. $this->customTotpTwofaccount->service = OtpTestData::SERVICE;
  28. $this->customTotpTwofaccount->account = OtpTestData::ACCOUNT;
  29. $this->customTotpTwofaccount->icon = OtpTestData::ICON;
  30. $this->customTotpTwofaccount->otp_type = 'totp';
  31. $this->customTotpTwofaccount->secret = OtpTestData::SECRET;
  32. $this->customTotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM;
  33. $this->customTotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
  34. $this->customTotpTwofaccount->period = OtpTestData::PERIOD_CUSTOM;
  35. $this->customTotpTwofaccount->counter = null;
  36. $this->customTotpTwofaccount->save();
  37. $this->customHotpTwofaccount = new TwoFAccount;
  38. $this->customHotpTwofaccount->legacy_uri = OtpTestData::HOTP_FULL_CUSTOM_URI;
  39. $this->customHotpTwofaccount->service = OtpTestData::SERVICE;
  40. $this->customHotpTwofaccount->account = OtpTestData::ACCOUNT;
  41. $this->customHotpTwofaccount->icon = OtpTestData::ICON;
  42. $this->customHotpTwofaccount->otp_type = 'hotp';
  43. $this->customHotpTwofaccount->secret = OtpTestData::SECRET;
  44. $this->customHotpTwofaccount->digits = OtpTestData::DIGITS_CUSTOM;
  45. $this->customHotpTwofaccount->algorithm = OtpTestData::ALGORITHM_CUSTOM;
  46. $this->customHotpTwofaccount->period = null;
  47. $this->customHotpTwofaccount->counter = OtpTestData::COUNTER_CUSTOM;
  48. $this->customHotpTwofaccount->save();
  49. $this->customSteamTotpTwofaccount = new TwoFAccount;
  50. $this->customSteamTotpTwofaccount->legacy_uri = OtpTestData::STEAM_TOTP_URI;
  51. $this->customSteamTotpTwofaccount->service = OtpTestData::STEAM;
  52. $this->customSteamTotpTwofaccount->account = OtpTestData::ACCOUNT;
  53. $this->customSteamTotpTwofaccount->otp_type = 'steamtotp';
  54. $this->customSteamTotpTwofaccount->secret = OtpTestData::STEAM_SECRET;
  55. $this->customSteamTotpTwofaccount->digits = OtpTestData::DIGITS_STEAM;
  56. $this->customSteamTotpTwofaccount->algorithm = OtpTestData::ALGORITHM_DEFAULT;
  57. $this->customSteamTotpTwofaccount->period = OtpTestData::PERIOD_DEFAULT;
  58. $this->customSteamTotpTwofaccount->counter = null;
  59. $this->customSteamTotpTwofaccount->save();
  60. }
  61. /**
  62. * @test
  63. */
  64. public function test_fill_with_custom_totp_uri_returns_correct_value()
  65. {
  66. $twofaccount = new TwoFAccount;
  67. $twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI);
  68. $this->assertEquals('totp', $twofaccount->otp_type);
  69. $this->assertEquals(OtpTestData::TOTP_FULL_CUSTOM_URI, $twofaccount->legacy_uri);
  70. $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
  71. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  72. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  73. $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
  74. $this->assertEquals(OtpTestData::PERIOD_CUSTOM, $twofaccount->period);
  75. $this->assertEquals(null, $twofaccount->counter);
  76. $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
  77. $this->assertStringEndsWith('.png',$twofaccount->icon);
  78. }
  79. /**
  80. * @test
  81. */
  82. public function test_fill_with_basic_totp_uri_returns_default_value()
  83. {
  84. $twofaccount = new TwoFAccount;
  85. $twofaccount->fillWithURI(OtpTestData::TOTP_SHORT_URI);
  86. $this->assertEquals('totp', $twofaccount->otp_type);
  87. $this->assertEquals(OtpTestData::TOTP_SHORT_URI, $twofaccount->legacy_uri);
  88. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  89. $this->assertEquals(null, $twofaccount->service);
  90. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  91. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  92. $this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccount->period);
  93. $this->assertEquals(null, $twofaccount->counter);
  94. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  95. $this->assertEquals(null, $twofaccount->icon);
  96. }
  97. /**
  98. * @test
  99. */
  100. public function test_fill_with_custom_hotp_uri_returns_correct_value()
  101. {
  102. $twofaccount = new TwoFAccount;
  103. $twofaccount->fillWithURI(OtpTestData::HOTP_FULL_CUSTOM_URI);
  104. $this->assertEquals('hotp', $twofaccount->otp_type);
  105. $this->assertEquals(OtpTestData::HOTP_FULL_CUSTOM_URI, $twofaccount->legacy_uri);
  106. $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
  107. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  108. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  109. $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
  110. $this->assertEquals(null, $twofaccount->period);
  111. $this->assertEquals(OtpTestData::COUNTER_CUSTOM, $twofaccount->counter);
  112. $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
  113. $this->assertStringEndsWith('.png',$twofaccount->icon);
  114. }
  115. /**
  116. * @test
  117. */
  118. public function test_fill_with_basic_hotp_uri_returns_default_value()
  119. {
  120. $twofaccount = new TwoFAccount;
  121. $twofaccount->fillWithURI(OtpTestData::HOTP_SHORT_URI);
  122. $this->assertEquals('hotp', $twofaccount->otp_type);
  123. $this->assertEquals(OtpTestData::HOTP_SHORT_URI, $twofaccount->legacy_uri);
  124. $this->assertEquals(null, $twofaccount->service);
  125. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  126. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  127. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  128. $this->assertEquals(null, $twofaccount->period);
  129. $this->assertEquals(OtpTestData::COUNTER_DEFAULT, $twofaccount->counter);
  130. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  131. $this->assertEquals(null, $twofaccount->icon);
  132. }
  133. /**
  134. * @test
  135. */
  136. public function test_filled_with_uri_persists_correct_values_to_db()
  137. {
  138. $twofaccount = new TwoFAccount;
  139. $twofaccount->fillWithURI(OtpTestData::TOTP_SHORT_URI);
  140. $twofaccount->save();
  141. $this->assertDatabaseHas('twofaccounts', [
  142. 'otp_type' => 'totp',
  143. 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
  144. 'service' => null,
  145. 'account' => OtpTestData::ACCOUNT,
  146. 'secret' => OtpTestData::SECRET,
  147. 'digits' => OtpTestData::DIGITS_DEFAULT,
  148. 'period' => OtpTestData::PERIOD_DEFAULT,
  149. 'counter' => null,
  150. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  151. 'icon' => null,
  152. ]);
  153. }
  154. /**
  155. * @test
  156. */
  157. public function test_fill_with_invalid_uri_returns_ValidationException()
  158. {
  159. $this->expectException(\Illuminate\Validation\ValidationException::class);
  160. $twofaccount = new TwoFAccount;
  161. $twofaccount->fillWithURI(OtpTestData::INVALID_OTPAUTH_URI);
  162. }
  163. /**
  164. * @test
  165. */
  166. public function test_fill_with_uri_without_label_returns_ValidationException()
  167. {
  168. $this->expectException(\Illuminate\Validation\ValidationException::class);
  169. $twofaccount = new TwoFAccount;
  170. $twofaccount->fillWithURI('otpauth://totp/?secret='.OtpTestData::SECRET);
  171. }
  172. /**
  173. * @test
  174. */
  175. public function test_create_custom_totp_from_parameters_returns_correct_value()
  176. {
  177. $twofaccount = new TwoFAccount;
  178. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP);
  179. $this->assertEquals('totp', $twofaccount->otp_type);
  180. $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
  181. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  182. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  183. $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
  184. $this->assertEquals(OtpTestData::PERIOD_CUSTOM, $twofaccount->period);
  185. $this->assertEquals(null, $twofaccount->counter);
  186. $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
  187. $this->assertStringEndsWith('.png',$twofaccount->icon);
  188. }
  189. /**
  190. * @test
  191. */
  192. public function test_create_basic_totp_from_parameters_returns_correct_value()
  193. {
  194. $twofaccount = new TwoFAccount;
  195. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
  196. $this->assertEquals('totp', $twofaccount->otp_type);
  197. $this->assertEquals(null, $twofaccount->service);
  198. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  199. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  200. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  201. $this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccount->period);
  202. $this->assertEquals(null, $twofaccount->counter);
  203. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  204. $this->assertEquals(null, $twofaccount->icon);
  205. }
  206. /**
  207. * @test
  208. */
  209. public function test_create_custom_hotp_from_parameters_returns_correct_value()
  210. {
  211. $twofaccount = new TwoFAccount;
  212. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP);
  213. $this->assertEquals('hotp', $twofaccount->otp_type);
  214. $this->assertEquals(OtpTestData::SERVICE, $twofaccount->service);
  215. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  216. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  217. $this->assertEquals(OtpTestData::DIGITS_CUSTOM, $twofaccount->digits);
  218. $this->assertEquals(null, $twofaccount->period);
  219. $this->assertEquals(OtpTestData::COUNTER_CUSTOM, $twofaccount->counter);
  220. $this->assertEquals(OtpTestData::ALGORITHM_CUSTOM, $twofaccount->algorithm);
  221. $this->assertStringEndsWith('.png',$twofaccount->icon);
  222. }
  223. /**
  224. * @test
  225. */
  226. public function test_create_basic_hotp_from_parameters_returns_correct_value()
  227. {
  228. $twofaccount = new TwoFAccount;
  229. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP);
  230. $this->assertEquals('hotp', $twofaccount->otp_type);
  231. $this->assertEquals(null, $twofaccount->service);
  232. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  233. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  234. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  235. $this->assertEquals(null, $twofaccount->period);
  236. $this->assertEquals(OtpTestData::COUNTER_DEFAULT, $twofaccount->counter);
  237. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  238. $this->assertEquals(null, $twofaccount->icon);
  239. }
  240. /**
  241. * @test
  242. */
  243. public function test_create_from_parameters_persists_correct_values_to_db()
  244. {
  245. $twofaccount = new TwoFAccount;
  246. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
  247. $twofaccount->save();
  248. $this->assertDatabaseHas('twofaccounts', [
  249. 'otp_type' => 'totp',
  250. 'legacy_uri' => OtpTestData::TOTP_SHORT_URI,
  251. 'service' => null,
  252. 'account' => OtpTestData::ACCOUNT,
  253. 'secret' => OtpTestData::SECRET,
  254. 'digits' => OtpTestData::DIGITS_DEFAULT,
  255. 'period' => OtpTestData::PERIOD_DEFAULT,
  256. 'counter' => null,
  257. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  258. 'icon' => null,
  259. ]);
  260. }
  261. /**
  262. * @test
  263. */
  264. public function test_create_from_unsupported_parameters_returns_unsupportedOtpTypeException()
  265. {
  266. $this->expectException(\App\Exceptions\UnsupportedOtpTypeException::class);
  267. $twofaccount = new TwoFAccount;
  268. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_PARAMETERS_FOR_UNSUPPORTED_OTP_TYPE);
  269. }
  270. /**
  271. * @test
  272. */
  273. public function test_create_from_invalid_parameters_type_returns_InvalidOtpParameterException()
  274. {
  275. $this->expectException(\App\Exceptions\InvalidOtpParameterException::class);
  276. $twofaccount = new TwoFAccount;
  277. $twofaccount->fillWithOtpParameters([
  278. 'account' => OtpTestData::ACCOUNT,
  279. 'otp_type' => 'totp',
  280. 'digits' => 'notsupported',
  281. ]);
  282. }
  283. /**
  284. * @test
  285. */
  286. public function test_create_from_invalid_parameters_returns_InvalidOtpParameterException()
  287. {
  288. $this->expectException(\App\Exceptions\InvalidOtpParameterException::class);
  289. $twofaccount = new TwoFAccount;
  290. $twofaccount->fillWithOtpParameters([
  291. 'account' => OtpTestData::ACCOUNT,
  292. 'otp_type' => 'totp',
  293. 'algorithm' => 'notsupported',
  294. ]);
  295. }
  296. /**
  297. * @test
  298. */
  299. public function test_update_totp_returns_updated_model()
  300. {
  301. $twofaccount = $this->customTotpTwofaccount;
  302. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
  303. $this->assertEquals('totp', $twofaccount->otp_type);
  304. $this->assertEquals(null, $twofaccount->service);
  305. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  306. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  307. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  308. $this->assertEquals(OtpTestData::PERIOD_DEFAULT, $twofaccount->period);
  309. $this->assertEquals(null, $twofaccount->counter);
  310. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  311. $this->assertEquals(null, $twofaccount->counter);
  312. $this->assertEquals(null, $twofaccount->icon);
  313. }
  314. /**
  315. * @test
  316. */
  317. public function test_update_hotp_returns_updated_model()
  318. {
  319. $twofaccount = $this->customTotpTwofaccount;
  320. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_HOTP);
  321. $this->assertEquals('hotp', $twofaccount->otp_type);
  322. $this->assertEquals(null, $twofaccount->service);
  323. $this->assertEquals(OtpTestData::ACCOUNT, $twofaccount->account);
  324. $this->assertEquals(OtpTestData::SECRET, $twofaccount->secret);
  325. $this->assertEquals(OtpTestData::DIGITS_DEFAULT, $twofaccount->digits);
  326. $this->assertEquals(null, $twofaccount->period);
  327. $this->assertEquals(OtpTestData::COUNTER_DEFAULT, $twofaccount->counter);
  328. $this->assertEquals(OtpTestData::ALGORITHM_DEFAULT, $twofaccount->algorithm);
  329. $this->assertEquals(null, $twofaccount->counter);
  330. $this->assertEquals(null, $twofaccount->icon);
  331. }
  332. /**
  333. * @test
  334. */
  335. public function test_update_totp_persists_updated_model()
  336. {
  337. $twofaccount = $this->customTotpTwofaccount;
  338. $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_MINIMUM_VALID_PARAMETERS_FOR_TOTP);
  339. $twofaccount->save();
  340. $this->assertDatabaseHas('twofaccounts', [
  341. 'otp_type' => 'totp',
  342. 'service' => null,
  343. 'account' => OtpTestData::ACCOUNT,
  344. 'secret' => OtpTestData::SECRET,
  345. 'digits' => OtpTestData::DIGITS_DEFAULT,
  346. 'period' => OtpTestData::PERIOD_DEFAULT,
  347. 'counter' => null,
  348. 'algorithm' => OtpTestData::ALGORITHM_DEFAULT,
  349. 'icon' => null,
  350. ]);
  351. }
  352. /**
  353. * @test
  354. */
  355. public function test_getOTP_for_totp_returns_the_same_password()
  356. {
  357. $twofaccount = new TwoFAccount;
  358. $otp_from_model = $this->customTotpTwofaccount->getOTP();
  359. $otp_from_uri = $twofaccount->fillWithURI(OtpTestData::TOTP_FULL_CUSTOM_URI)->getOTP();
  360. if ($otp_from_model->generated_at === $otp_from_uri->generated_at) {
  361. $this->assertEquals($otp_from_model, $otp_from_uri);
  362. }
  363. $otp_from_model = $this->customTotpTwofaccount->getOTP();
  364. $otp_from_parameters = $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_TOTP)->getOTP();
  365. if ($otp_from_model->generated_at === $otp_from_parameters->generated_at) {
  366. $this->assertEquals($otp_from_model, $otp_from_parameters);
  367. }
  368. }
  369. /**
  370. * @test
  371. */
  372. public function test_getOTP_for_hotp_returns_the_same_password()
  373. {
  374. $twofaccount = new TwoFAccount;
  375. $otp_from_model = $this->customHotpTwofaccount->getOTP();
  376. $otp_from_uri = $twofaccount->fillWithURI(OtpTestData::HOTP_FULL_CUSTOM_URI)->getOTP();
  377. $this->assertEquals($otp_from_model, $otp_from_uri);
  378. $otp_from_parameters = $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_CUSTOM_HOTP)->getOTP();
  379. $this->assertEquals($otp_from_model, $otp_from_parameters);
  380. }
  381. /**
  382. * @test
  383. */
  384. public function test_getOTP_for_steamtotp_returns_the_same_password()
  385. {
  386. $twofaccount = new TwoFAccount;
  387. $otp_from_model = $this->customSteamTotpTwofaccount->getOTP();
  388. $otp_from_uri = $twofaccount->fillWithURI(OtpTestData::STEAM_TOTP_URI)->getOTP();
  389. if ($otp_from_model->generated_at === $otp_from_uri->generated_at) {
  390. $this->assertEquals($otp_from_model, $otp_from_uri);
  391. }
  392. $otp_from_model = $this->customSteamTotpTwofaccount->getOTP();
  393. $otp_from_parameters = $twofaccount->fillWithOtpParameters(OtpTestData::ARRAY_OF_FULL_VALID_PARAMETERS_FOR_STEAM_TOTP)->getOTP();
  394. if ($otp_from_model->generated_at === $otp_from_parameters->generated_at) {
  395. $this->assertEquals($otp_from_model, $otp_from_parameters);
  396. }
  397. }
  398. /**
  399. * @test
  400. */
  401. public function test_getOTP_for_totp_with_invalid_secret_returns_InvalidSecretException()
  402. {
  403. $twofaccount = new TwoFAccount;
  404. $this->expectException(\App\Exceptions\InvalidSecretException::class);
  405. $otp_from_uri = $twofaccount->fillWithURI('otpauth://totp/'.OtpTestData::ACCOUNT.'?secret=0')->getOTP();
  406. }
  407. /**
  408. * @test
  409. */
  410. public function test_getOTP_for_totp_with_undecipherable_secret_returns_UndecipherableException()
  411. {
  412. $twofaccount = new TwoFAccount;
  413. $this->expectException(\App\Exceptions\UndecipherableException::class);
  414. $otp_from_uri = $twofaccount->fillWithOtpParameters([
  415. 'account' => OtpTestData::ACCOUNT,
  416. 'otp_type' => 'totp',
  417. 'secret' => __('errors.indecipherable'),
  418. ])->getOTP();
  419. }
  420. /**
  421. * @test
  422. */
  423. public function test_getURI_for_custom_totp_model_returns_uri()
  424. {
  425. $uri = $this->customTotpTwofaccount->getURI();
  426. $this->assertStringContainsString('otpauth://totp/', $uri);
  427. $this->assertStringContainsString(OtpTestData::SERVICE, $uri);
  428. $this->assertStringContainsString(OtpTestData::ACCOUNT, $uri);
  429. $this->assertStringContainsString('secret='.OtpTestData::SECRET, $uri);
  430. $this->assertStringContainsString('digits='.OtpTestData::DIGITS_CUSTOM, $uri);
  431. $this->assertStringContainsString('period='.OtpTestData::PERIOD_CUSTOM, $uri);
  432. $this->assertStringContainsString('algorithm='.OtpTestData::ALGORITHM_CUSTOM, $uri);
  433. }
  434. /**
  435. * @test
  436. */
  437. public function test_getURI_for_custom_hotp_model_returns_uri()
  438. {
  439. $uri = $this->customHotpTwofaccount->getURI();
  440. $this->assertStringContainsString('otpauth://hotp/', $uri);
  441. $this->assertStringContainsString(OtpTestData::SERVICE, $uri);
  442. $this->assertStringContainsString(OtpTestData::ACCOUNT, $uri);
  443. $this->assertStringContainsString('secret='.OtpTestData::SECRET, $uri);
  444. $this->assertStringContainsString('digits='.OtpTestData::DIGITS_CUSTOM, $uri);
  445. $this->assertStringContainsString('counter='.OtpTestData::COUNTER_CUSTOM, $uri);
  446. $this->assertStringContainsString('algorithm='.OtpTestData::ALGORITHM_CUSTOM, $uri);
  447. }
  448. }