ProviderUserAssignmentDaoTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Authentication\TwoFactorAuth\Db;
  8. use OC;
  9. use OC\Authentication\TwoFactorAuth\Db\ProviderUserAssignmentDao;
  10. use OCP\IDBConnection;
  11. use Test\TestCase;
  12. /**
  13. * @group DB
  14. */
  15. class ProviderUserAssignmentDaoTest extends TestCase {
  16. /** @var IDBConnection */
  17. private $dbConn;
  18. /** @var ProviderUserAssignmentDao */
  19. private $dao;
  20. protected function setUp(): void {
  21. parent::setUp();
  22. $this->dbConn = OC::$server->getDatabaseConnection();
  23. $qb = $this->dbConn->getQueryBuilder();
  24. $q = $qb->delete(ProviderUserAssignmentDao::TABLE_NAME);
  25. $q->execute();
  26. $this->dao = new ProviderUserAssignmentDao($this->dbConn);
  27. }
  28. public function testGetState(): void {
  29. $qb = $this->dbConn->getQueryBuilder();
  30. $q1 = $qb->insert(ProviderUserAssignmentDao::TABLE_NAME)->values([
  31. 'provider_id' => $qb->createNamedParameter('twofactor_u2f'),
  32. 'uid' => $qb->createNamedParameter('user123'),
  33. 'enabled' => $qb->createNamedParameter(1),
  34. ]);
  35. $q1->execute();
  36. $q2 = $qb->insert(ProviderUserAssignmentDao::TABLE_NAME)->values([
  37. 'provider_id' => $qb->createNamedParameter('twofactor_totp'),
  38. 'uid' => $qb->createNamedParameter('user123'),
  39. 'enabled' => $qb->createNamedParameter(0),
  40. ]);
  41. $q2->execute();
  42. $expected = [
  43. 'twofactor_u2f' => true,
  44. 'twofactor_totp' => false,
  45. ];
  46. $state = $this->dao->getState('user123');
  47. $this->assertEquals($expected, $state);
  48. }
  49. public function testPersist(): void {
  50. $qb = $this->dbConn->getQueryBuilder();
  51. $this->dao->persist('twofactor_totp', 'user123', 0);
  52. $q = $qb
  53. ->select('*')
  54. ->from(ProviderUserAssignmentDao::TABLE_NAME)
  55. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter('twofactor_totp')))
  56. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter('user123')))
  57. ->andWhere($qb->expr()->eq('enabled', $qb->createNamedParameter(0)));
  58. $res = $q->execute();
  59. $data = $res->fetchAll();
  60. $res->closeCursor();
  61. $this->assertCount(1, $data);
  62. }
  63. public function testPersistTwice(): void {
  64. $qb = $this->dbConn->getQueryBuilder();
  65. $this->dao->persist('twofactor_totp', 'user123', 0);
  66. $this->dao->persist('twofactor_totp', 'user123', 1);
  67. $q = $qb
  68. ->select('*')
  69. ->from(ProviderUserAssignmentDao::TABLE_NAME)
  70. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter('twofactor_totp')))
  71. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter('user123')))
  72. ->andWhere($qb->expr()->eq('enabled', $qb->createNamedParameter(1)));
  73. $res = $q->execute();
  74. $data = $res->fetchAll();
  75. $res->closeCursor();
  76. $this->assertCount(1, $data);
  77. }
  78. public function testPersistSameStateTwice(): void {
  79. $qb = $this->dbConn->getQueryBuilder();
  80. $this->dao->persist('twofactor_totp', 'user123', 1);
  81. $this->dao->persist('twofactor_totp', 'user123', 1);
  82. $q = $qb
  83. ->select('*')
  84. ->from(ProviderUserAssignmentDao::TABLE_NAME)
  85. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter('twofactor_totp')))
  86. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter('user123')))
  87. ->andWhere($qb->expr()->eq('enabled', $qb->createNamedParameter(1)));
  88. $res = $q->execute();
  89. $data = $res->fetchAll();
  90. $res->closeCursor();
  91. $this->assertCount(1, $data);
  92. }
  93. public function testDeleteByUser(): void {
  94. $this->dao->persist('twofactor_fail', 'user1', 1);
  95. $this->dao->persist('twofactor_u2f', 'user1', 1);
  96. $this->dao->persist('twofactor_fail', 'user2', 0);
  97. $this->dao->persist('twofactor_u2f', 'user2', 0);
  98. $deleted = $this->dao->deleteByUser('user1');
  99. $this->assertEquals(
  100. [
  101. [
  102. 'uid' => 'user1',
  103. 'provider_id' => 'twofactor_fail',
  104. 'enabled' => true,
  105. ],
  106. [
  107. 'uid' => 'user1',
  108. 'provider_id' => 'twofactor_u2f',
  109. 'enabled' => true,
  110. ],
  111. ],
  112. $deleted
  113. );
  114. $statesUser1 = $this->dao->getState('user1');
  115. $statesUser2 = $this->dao->getState('user2');
  116. $this->assertCount(0, $statesUser1);
  117. $this->assertCount(2, $statesUser2);
  118. }
  119. public function testDeleteAll(): void {
  120. $this->dao->persist('twofactor_fail', 'user1', 1);
  121. $this->dao->persist('twofactor_u2f', 'user1', 1);
  122. $this->dao->persist('twofactor_fail', 'user2', 0);
  123. $this->dao->persist('twofactor_u2f', 'user1', 0);
  124. $this->dao->deleteAll('twofactor_fail');
  125. $statesUser1 = $this->dao->getState('user1');
  126. $statesUser2 = $this->dao->getState('user2');
  127. $this->assertCount(1, $statesUser1);
  128. $this->assertCount(0, $statesUser2);
  129. }
  130. }