BackupCodeMapperTest.php 3.7 KB

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