You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BackupCodeMapperTest.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. /**
  3. *
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\TwoFactorBackupCodes\Tests\Db;
  26. use OCA\TwoFactorBackupCodes\Db\BackupCode;
  27. use OCA\TwoFactorBackupCodes\Db\BackupCodeMapper;
  28. use OCP\IDBConnection;
  29. use OCP\IUser;
  30. use Test\TestCase;
  31. /**
  32. * @group DB
  33. */
  34. class BackupCodeMapperTest extends TestCase {
  35. /** @var IDBConnection */
  36. private $db;
  37. /** @var BackupCodeMapper */
  38. private $mapper;
  39. /** @var string */
  40. private $testUID = 'test123456';
  41. private function resetDB() {
  42. $qb = $this->db->getQueryBuilder();
  43. $qb->delete($this->mapper->getTableName())
  44. ->where($qb->expr()->eq('user_id', $qb->createNamedParameter($this->testUID)));
  45. $qb->execute();
  46. }
  47. protected function setUp(): void {
  48. parent::setUp();
  49. $this->db = \OC::$server->getDatabaseConnection();
  50. $this->mapper = \OC::$server->query(BackupCodeMapper::class);
  51. $this->resetDB();
  52. }
  53. protected function tearDown(): void {
  54. parent::tearDown();
  55. $this->resetDB();
  56. }
  57. public function testGetBackupCodes() {
  58. $code1 = new BackupCode();
  59. $code1->setUserId($this->testUID);
  60. $code1->setCode('1|$2y$10$Fyo.DkMtkaHapVvRVbQBeeIdi5x/6nmPnxiBzD0GDKa08NMus5xze');
  61. $code1->setUsed(1);
  62. $code2 = new BackupCode();
  63. $code2->setUserId($this->testUID);
  64. $code2->setCode('1|$2y$10$nj3sZaCqGN8t6.SsnNADt.eX34UCkdX6FPx.r.rIwE6Jj3vi5wyt2');
  65. $code2->setUsed(0);
  66. $this->mapper->insert($code1);
  67. $this->mapper->insert($code2);
  68. $user = $this->getMockBuilder(IUser::class)->getMock();
  69. $user->expects($this->once())
  70. ->method('getUID')
  71. ->willReturn($this->testUID);
  72. $dbCodes = $this->mapper->getBackupCodes($user);
  73. $this->assertCount(2, $dbCodes);
  74. $this->assertInstanceOf(BackupCode::class, $dbCodes[0]);
  75. $this->assertInstanceOf(BackupCode::class, $dbCodes[1]);
  76. }
  77. public function testDeleteCodes() {
  78. $code = new BackupCode();
  79. $code->setUserId($this->testUID);
  80. $code->setCode('1|$2y$10$CagG8pEhZL.xDirtCCP/KuuWtnsAasgq60zY9rU46dBK4w8yW0Z/y');
  81. $code->setUsed(1);
  82. $user = $this->getMockBuilder(IUser::class)->getMock();
  83. $user->expects($this->any())
  84. ->method('getUID')
  85. ->willReturn($this->testUID);
  86. $this->mapper->insert($code);
  87. $this->assertCount(1, $this->mapper->getBackupCodes($user));
  88. $this->mapper->deleteCodes($user);
  89. $this->assertCount(0, $this->mapper->getBackupCodes($user));
  90. }
  91. public function testInsertArgonEncryptedCodes() {
  92. $code = new BackupCode();
  93. $code->setUserId($this->testUID);
  94. $code->setCode('2|$argon2i$v=19$m=1024,t=2,p=2$MjJWUjRFWndtMm5BWGxOag$BusVxLeFyiLLWtaVvX/JRFBiPdZcjRrzpQ/rAhn6vqY');
  95. $code->setUsed(1);
  96. $this->mapper->insert($code);
  97. }
  98. }