1
0

DatabaseTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\User;
  8. use OC\User\User;
  9. use OCP\EventDispatcher\Event;
  10. use OCP\EventDispatcher\IEventDispatcher;
  11. use OCP\HintException;
  12. use OCP\Security\Events\ValidatePasswordPolicyEvent;
  13. use PHPUnit\Framework\MockObject\MockObject;
  14. /**
  15. * Class DatabaseTest
  16. *
  17. * @group DB
  18. */
  19. class DatabaseTest extends Backend {
  20. /** @var array */
  21. private $users;
  22. /** @var IEventDispatcher|MockObject */
  23. private $eventDispatcher;
  24. public function getUser() {
  25. $user = parent::getUser();
  26. $this->users[] = $user;
  27. return $user;
  28. }
  29. protected function setUp(): void {
  30. parent::setUp();
  31. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  32. $this->backend = new \OC\User\Database($this->eventDispatcher);
  33. }
  34. protected function tearDown(): void {
  35. if (!isset($this->users)) {
  36. return;
  37. }
  38. foreach ($this->users as $user) {
  39. $this->backend->deleteUser($user);
  40. }
  41. parent::tearDown();
  42. }
  43. public function testVerifyPasswordEvent(): void {
  44. $user = $this->getUser();
  45. $this->backend->createUser($user, 'pass1');
  46. $this->eventDispatcher->expects($this->once())->method('dispatchTyped')
  47. ->willReturnCallback(
  48. function (Event $event) {
  49. $this->assertInstanceOf(ValidatePasswordPolicyEvent::class, $event);
  50. /** @var ValidatePasswordPolicyEvent $event */
  51. $this->assertSame('newpass', $event->getPassword());
  52. }
  53. );
  54. $this->backend->setPassword($user, 'newpass');
  55. $this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
  56. }
  57. public function testVerifyPasswordEventFail(): void {
  58. $this->expectException(\OCP\HintException::class);
  59. $this->expectExceptionMessage('password change failed');
  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. throw new HintException('password change failed', 'password change failed');
  69. }
  70. );
  71. $this->backend->setPassword($user, 'newpass');
  72. $this->assertSame($user, $this->backend->checkPassword($user, 'newpass'));
  73. }
  74. public function testCreateUserInvalidatesCache(): void {
  75. $user1 = $this->getUniqueID('test_');
  76. $this->assertFalse($this->backend->userExists($user1));
  77. $this->backend->createUser($user1, 'pw');
  78. $this->assertTrue($this->backend->userExists($user1));
  79. }
  80. public function testDeleteUserInvalidatesCache(): void {
  81. $user1 = $this->getUniqueID('test_');
  82. $this->backend->createUser($user1, 'pw');
  83. $this->assertTrue($this->backend->userExists($user1));
  84. $this->backend->deleteUser($user1);
  85. $this->assertFalse($this->backend->userExists($user1));
  86. $this->backend->createUser($user1, 'pw2');
  87. $this->assertTrue($this->backend->userExists($user1));
  88. }
  89. public function testSearch(): void {
  90. parent::testSearch();
  91. $user1 = $this->getUser();
  92. $this->backend->createUser($user1, 'pass1');
  93. $user2 = $this->getUser();
  94. $this->backend->createUser($user2, 'pass1');
  95. $user1Obj = new User($user1, $this->backend, $this->createMock(IEventDispatcher::class));
  96. $user2Obj = new User($user2, $this->backend, $this->createMock(IEventDispatcher::class));
  97. $emailAddr1 = "$user1@nextcloud.com";
  98. $emailAddr2 = "$user2@nextcloud.com";
  99. $user1Obj->setDisplayName('User 1 Display');
  100. $result = $this->backend->getDisplayNames('display');
  101. $this->assertCount(1, $result);
  102. $result = $this->backend->getDisplayNames(strtoupper($user1));
  103. $this->assertCount(1, $result);
  104. $user1Obj->setEMailAddress($emailAddr1);
  105. $user2Obj->setEMailAddress($emailAddr2);
  106. $result = $this->backend->getUsers('@nextcloud.com');
  107. $this->assertCount(2, $result);
  108. $result = $this->backend->getDisplayNames('@nextcloud.com');
  109. $this->assertCount(2, $result);
  110. $result = $this->backend->getDisplayNames('@nextcloud.COM');
  111. $this->assertCount(2, $result);
  112. }
  113. }