ProviderUserAssignmentDaoTest.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2018 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace Test\Authentication\TwoFactorAuth\Db;
  25. use OC;
  26. use OC\Authentication\TwoFactorAuth\Db\ProviderUserAssignmentDao;
  27. use OCP\IDBConnection;
  28. use Test\TestCase;
  29. /**
  30. * @group DB
  31. */
  32. class ProviderUserAssignmentDaoTest extends TestCase {
  33. /** @var IDBConnection */
  34. private $dbConn;
  35. /** @var ProviderUserAssignmentDao */
  36. private $dao;
  37. protected function setUp() {
  38. parent::setUp();
  39. $this->dbConn = OC::$server->getDatabaseConnection();
  40. $qb = $this->dbConn->getQueryBuilder();
  41. $q = $qb->delete(ProviderUserAssignmentDao::TABLE_NAME);
  42. $q->execute();
  43. $this->dao = new ProviderUserAssignmentDao($this->dbConn);
  44. }
  45. public function testGetState() {
  46. $qb = $this->dbConn->getQueryBuilder();
  47. $q1 = $qb->insert(ProviderUserAssignmentDao::TABLE_NAME)->values([
  48. 'provider_id' => $qb->createNamedParameter('twofactor_u2f'),
  49. 'uid' => $qb->createNamedParameter('user123'),
  50. 'enabled' => $qb->createNamedParameter(1),
  51. ]);
  52. $q1->execute();
  53. $q2 = $qb->insert(ProviderUserAssignmentDao::TABLE_NAME)->values([
  54. 'provider_id' => $qb->createNamedParameter('twofactor_totp'),
  55. 'uid' => $qb->createNamedParameter('user123'),
  56. 'enabled' => $qb->createNamedParameter(0),
  57. ]);
  58. $q2->execute();
  59. $expected = [
  60. 'twofactor_u2f' => true,
  61. 'twofactor_totp' => false,
  62. ];
  63. $state = $this->dao->getState('user123');
  64. $this->assertEquals($expected, $state);
  65. }
  66. public function testPersist() {
  67. $qb = $this->dbConn->getQueryBuilder();
  68. $this->dao->persist('twofactor_totp', 'user123', 0);
  69. $q = $qb
  70. ->select('*')
  71. ->from(ProviderUserAssignmentDao::TABLE_NAME)
  72. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter('twofactor_totp')))
  73. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter('user123')))
  74. ->andWhere($qb->expr()->eq('enabled', $qb->createNamedParameter(0)));
  75. $res = $q->execute();
  76. $data = $res->fetchAll();
  77. $res->closeCursor();
  78. $this->assertCount(1, $data);
  79. }
  80. public function testPersistTwice() {
  81. $qb = $this->dbConn->getQueryBuilder();
  82. $this->dao->persist('twofactor_totp', 'user123', 0);
  83. $this->dao->persist('twofactor_totp', 'user123', 1);
  84. $q = $qb
  85. ->select('*')
  86. ->from(ProviderUserAssignmentDao::TABLE_NAME)
  87. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter('twofactor_totp')))
  88. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter('user123')))
  89. ->andWhere($qb->expr()->eq('enabled', $qb->createNamedParameter(1)));
  90. $res = $q->execute();
  91. $data = $res->fetchAll();
  92. $res->closeCursor();
  93. $this->assertCount(1, $data);
  94. }
  95. public function testPersistSameStateTwice() {
  96. $qb = $this->dbConn->getQueryBuilder();
  97. $this->dao->persist('twofactor_totp', 'user123', 1);
  98. $this->dao->persist('twofactor_totp', 'user123', 1);
  99. $q = $qb
  100. ->select('*')
  101. ->from(ProviderUserAssignmentDao::TABLE_NAME)
  102. ->where($qb->expr()->eq('provider_id', $qb->createNamedParameter('twofactor_totp')))
  103. ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter('user123')))
  104. ->andWhere($qb->expr()->eq('enabled', $qb->createNamedParameter(1)));
  105. $res = $q->execute();
  106. $data = $res->fetchAll();
  107. $res->closeCursor();
  108. $this->assertCount(1, $data);
  109. }
  110. public function testDeleteAll() {
  111. $this->dao->persist('twofactor_fail', 'user1', 1);
  112. $this->dao->persist('twofactor_u2f', 'user1', 1);
  113. $this->dao->persist('twofactor_fail', 'user2', 0);
  114. $this->dao->persist('twofactor_u2f', 'user1', 0);
  115. $this->dao->deleteAll('twofactor_fail');
  116. $statesUser1 = $this->dao->getState('user1');
  117. $statesUser2 = $this->dao->getState('user2');
  118. $this->assertCount(1, $statesUser1);
  119. $this->assertCount(0, $statesUser2);
  120. }
  121. }