DatabaseTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\User;
  23. use OC\User\User;
  24. use OCP\EventDispatcher\Event;
  25. use OCP\EventDispatcher\IEventDispatcher;
  26. use OCP\HintException;
  27. use OCP\Security\Events\ValidatePasswordPolicyEvent;
  28. use PHPUnit\Framework\MockObject\MockObject;
  29. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  30. /**
  31. * Class DatabaseTest
  32. *
  33. * @group DB
  34. */
  35. class DatabaseTest extends Backend {
  36. /** @var array */
  37. private $users;
  38. /** @var IEventDispatcher|MockObject */
  39. private $eventDispatcher;
  40. public function getUser() {
  41. $user = parent::getUser();
  42. $this->users[] = $user;
  43. return $user;
  44. }
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  48. $this->backend = new \OC\User\Database($this->eventDispatcher);
  49. }
  50. protected function tearDown(): void {
  51. if (!isset($this->users)) {
  52. return;
  53. }
  54. foreach ($this->users as $user) {
  55. $this->backend->deleteUser($user);
  56. }
  57. parent::tearDown();
  58. }
  59. public function testVerifyPasswordEvent() {
  60. $user = $this->getUser();
  61. $this->backend->createUser($user, 'pass1');
  62. $this->eventDispatcher->expects($this->once())->method('dispatchTyped')
  63. ->willReturnCallback(
  64. function (Event $event) {
  65. $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
  66. /** @var ValidatePasswordPolicyEvent $event */
  67. $this->assertSame('newpass', $event->getPassword());
  68. }
  69. );
  70. $this->backend->setPassword($user, 'newpass');
  71. $this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
  72. }
  73. public function testVerifyPasswordEventFail() {
  74. $this->expectException(\OCP\HintException::class);
  75. $this->expectExceptionMessage('password change failed');
  76. $user = $this->getUser();
  77. $this->backend->createUser($user, 'pass1');
  78. $this->eventDispatcher->expects($this->once())->method('dispatchTyped')
  79. ->willReturnCallback(
  80. function (Event $event) {
  81. $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
  82. /** @var ValidatePasswordPolicyEvent $event */
  83. $this->assertSame('newpass', $event->getPassword());
  84. throw new HintException('password change failed', 'password change failed');
  85. }
  86. );
  87. $this->backend->setPassword($user, 'newpass');
  88. $this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
  89. }
  90. public function testCreateUserInvalidatesCache() {
  91. $user1 = $this->getUniqueID('test_');
  92. $this->assertFalse($this->backend->userExists($user1));
  93. $this->backend->createUser($user1, 'pw');
  94. $this->assertTrue($this->backend->userExists($user1));
  95. }
  96. public function testDeleteUserInvalidatesCache() {
  97. $user1 = $this->getUniqueID('test_');
  98. $this->backend->createUser($user1, 'pw');
  99. $this->assertTrue($this->backend->userExists($user1));
  100. $this->backend->deleteUser($user1);
  101. $this->assertFalse($this->backend->userExists($user1));
  102. $this->backend->createUser($user1, 'pw2');
  103. $this->assertTrue($this->backend->userExists($user1));
  104. }
  105. public function testSearch() {
  106. parent::testSearch();
  107. $user1 = $this->getUser();
  108. $this->backend->createUser($user1, 'pass1');
  109. $user2 = $this->getUser();
  110. $this->backend->createUser($user2, 'pass1');
  111. $user1Obj = new User($user1, $this->backend, $this->createMock(EventDispatcherInterface::class));
  112. $user2Obj = new User($user2, $this->backend, $this->createMock(EventDispatcherInterface::class));
  113. $emailAddr1 = "$user1@nextcloud.com";
  114. $emailAddr2 = "$user2@nextcloud.com";
  115. $user1Obj->setDisplayName('User 1 Display');
  116. $result = $this->backend->getDisplayNames('display');
  117. $this->assertCount(1, $result);
  118. $result = $this->backend->getDisplayNames(strtoupper($user1));
  119. $this->assertCount(1, $result);
  120. $user1Obj->setEMailAddress($emailAddr1);
  121. $user2Obj->setEMailAddress($emailAddr2);
  122. $result = $this->backend->getUsers('@nextcloud.com');
  123. $this->assertCount(2, $result);
  124. $result = $this->backend->getDisplayNames('@nextcloud.com');
  125. $this->assertCount(2, $result);
  126. $result = $this->backend->getDisplayNames('@nextcloud.COM');
  127. $this->assertCount(2, $result);
  128. }
  129. }