BackupCodeMapperTest.php 3.3 KB

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