1
0

DatabaseTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /**
  30. * Class DatabaseTest
  31. *
  32. * @group DB
  33. */
  34. class DatabaseTest extends Backend {
  35. /** @var array */
  36. private $users;
  37. /** @var IEventDispatcher|MockObject */
  38. private $eventDispatcher;
  39. public function getUser() {
  40. $user = parent::getUser();
  41. $this->users[] = $user;
  42. return $user;
  43. }
  44. protected function setUp(): void {
  45. parent::setUp();
  46. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  47. $this->backend = new \OC\User\Database($this->eventDispatcher);
  48. }
  49. protected function tearDown(): void {
  50. if (!isset($this->users)) {
  51. return;
  52. }
  53. foreach ($this->users as $user) {
  54. $this->backend->deleteUser($user);
  55. }
  56. parent::tearDown();
  57. }
  58. public function testVerifyPasswordEvent() {
  59. $user = $this->getUser();
  60. $this->backend->createUser($user, 'pass1');
  61. $this->eventDispatcher->expects($this->once())->method('dispatchTyped')
  62. ->willReturnCallback(
  63. function (Event $event) {
  64. $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
  65. /** @var ValidatePasswordPolicyEvent $event */
  66. $this->assertSame('newpass', $event->getPassword());
  67. }
  68. );
  69. $this->backend->setPassword($user, 'newpass');
  70. $this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
  71. }
  72. public function testVerifyPasswordEventFail() {
  73. $this->expectException(\OCP\HintException::class);
  74. $this->expectExceptionMessage('password change failed');
  75. $user = $this->getUser();
  76. $this->backend->createUser($user, 'pass1');
  77. $this->eventDispatcher->expects($this->once())->method('dispatchTyped')
  78. ->willReturnCallback(
  79. function (Event $event) {
  80. $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
  81. /** @var ValidatePasswordPolicyEvent $event */
  82. $this->assertSame('newpass', $event->getPassword());
  83. throw new HintException('password change failed', 'password change failed');
  84. }
  85. );
  86. $this->backend->setPassword($user, 'newpass');
  87. $this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
  88. }
  89. public function testCreateUserInvalidatesCache() {
  90. $user1 = $this->getUniqueID('test_');
  91. $this->assertFalse($this->backend->userExists($user1));
  92. $this->backend->createUser($user1, 'pw');
  93. $this->assertTrue($this->backend->userExists($user1));
  94. }
  95. public function testDeleteUserInvalidatesCache() {
  96. $user1 = $this->getUniqueID('test_');
  97. $this->backend->createUser($user1, 'pw');
  98. $this->assertTrue($this->backend->userExists($user1));
  99. $this->backend->deleteUser($user1);
  100. $this->assertFalse($this->backend->userExists($user1));
  101. $this->backend->createUser($user1, 'pw2');
  102. $this->assertTrue($this->backend->userExists($user1));
  103. }
  104. public function testSearch() {
  105. parent::testSearch();
  106. $user1 = $this->getUser();
  107. $this->backend->createUser($user1, 'pass1');
  108. $user2 = $this->getUser();
  109. $this->backend->createUser($user2, 'pass1');
  110. $user1Obj = new User($user1, $this->backend, $this->createMock(IEventDispatcher::class));
  111. $user2Obj = new User($user2, $this->backend, $this->createMock(IEventDispatcher::class));
  112. $emailAddr1 = "$user1@nextcloud.com";
  113. $emailAddr2 = "$user2@nextcloud.com";
  114. $user1Obj->setDisplayName('User 1 Display');
  115. $result = $this->backend->getDisplayNames('display');
  116. $this->assertCount(1, $result);
  117. $result = $this->backend->getDisplayNames(strtoupper($user1));
  118. $this->assertCount(1, $result);
  119. $user1Obj->setEMailAddress($emailAddr1);
  120. $user2Obj->setEMailAddress($emailAddr2);
  121. $result = $this->backend->getUsers('@nextcloud.com');
  122. $this->assertCount(2, $result);
  123. $result = $this->backend->getDisplayNames('@nextcloud.com');
  124. $this->assertCount(2, $result);
  125. $result = $this->backend->getDisplayNames('@nextcloud.COM');
  126. $this->assertCount(2, $result);
  127. }
  128. }