BackupCodeMapperTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\TwoFactorBackupCodes\Tests\Db;
  8. use OCA\TwoFactorBackupCodes\Db\BackupCode;
  9. use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper;
  10. use OCP\IDBConnection;
  11. use OCP\IUser;
  12. use Test\TestCase;
  13. /**
  14. * @group DB
  15. */
  16. class BackupCodeMapperTest extends TestCase {
  17. /** @var IDBConnection */
  18. private $db;
  19. /** @var BackupCodeMapper */
  20. private $mapper;
  21. /** @var string */
  22. private $testUID = 'test123456';
  23. private function resetDB() {
  24. $qb = $this->db->getQueryBuilder();
  25. $qb->delete($this->mapper->getTableName())
  26. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($this->testUID)));
  27. $qb->execute();
  28. }
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->db = \OC::$server->getDatabaseConnection();
  32. $this->mapper = \OC::$server->query(BackupCodeMapper::class);
  33. $this->resetDB();
  34. }
  35. protected function tearDown(): void {
  36. parent::tearDown();
  37. $this->resetDB();
  38. }
  39. public function testGetBackupCodes(): void {
  40. $code1 = new BackupCode();
  41. $code1->setUserId($this->testUID);
  42. $code1->setCode('1|$2y$10$Fyo.DkMtkaHapVvRVbQBeeIdi5x/6nmPnxiBzD0GDKa08NMus5xze');
  43. $code1->setUsed(1);
  44. $code2 = new BackupCode();
  45. $code2->setUserId($this->testUID);
  46. $code2->setCode('1|$2y$10$nj3sZaCqGN8t6.SsnNADt.eX34UCkdX6FPx.r.rIwE6Jj3vi5wyt2');
  47. $code2->setUsed(0);
  48. $this->mapper->insert($code1);
  49. $this->mapper->insert($code2);
  50. $user = $this->getMockBuilder(IUser::class)->getMock();
  51. $user->expects($this->once())
  52. ->method('getUID')
  53. ->willReturn($this->testUID);
  54. $dbCodes = $this->mapper->getBackupCodes($user);
  55. $this->assertCount(2, $dbCodes);
  56. $this->assertInstanceOf(BackupCode::class, $dbCodes[0]);
  57. $this->assertInstanceOf(BackupCode::class, $dbCodes[1]);
  58. }
  59. public function testDeleteCodes(): void {
  60. $code = new BackupCode();
  61. $code->setUserId($this->testUID);
  62. $code->setCode('1|$2y$10$CagG8pEhZL.xDirtCCP/KuuWtnsAasgq60zY9rU46dBK4w8yW0Z/y');
  63. $code->setUsed(1);
  64. $user = $this->getMockBuilder(IUser::class)->getMock();
  65. $user->expects($this->any())
  66. ->method('getUID')
  67. ->willReturn($this->testUID);
  68. $this->mapper->insert($code);
  69. $this->assertCount(1, $this->mapper->getBackupCodes($user));
  70. $this->mapper->deleteCodes($user);
  71. $this->assertCount(0, $this->mapper->getBackupCodes($user));
  72. }
  73. public function testInsertArgonEncryptedCodes(): void {
  74. $code = new BackupCode();
  75. $code->setUserId($this->testUID);
  76. $code->setCode('2|$argon2i$v=19$m=1024,t=2,p=2$MjJWUjRFWndtMm5BWGxOag$BusVxLeFyiLLWtaVvX/JRFBiPdZcjRrzpQ/rAhn6vqY');
  77. $code->setUsed(1);
  78. $user = $this->getMockBuilder(IUser::class)->getMock();
  79. $user->expects($this->any())
  80. ->method('getUID')
  81. ->willReturn($this->testUID);
  82. $this->mapper->insert($code);
  83. $this->assertCount(1, $this->mapper->getBackupCodes($user));
  84. }
  85. }