ChangePasswordControllerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Tests\Core\Controller;
  7. use OC\User\Session;
  8. use OCA\Settings\Controller\ChangePasswordController;
  9. use OCP\App\IAppManager;
  10. use OCP\AppFramework\Http\JSONResponse;
  11. use OCP\HintException;
  12. use OCP\IGroupManager;
  13. use OCP\IL10N;
  14. use OCP\IRequest;
  15. use OCP\IUser;
  16. use OCP\IUserManager;
  17. class ChangePasswordControllerTest extends \Test\TestCase {
  18. /** @var string */
  19. private $userId = 'currentUser';
  20. /** @var string */
  21. private $loginName = 'ua1337';
  22. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  23. private $userManager;
  24. /** @var Session|\PHPUnit\Framework\MockObject\MockObject */
  25. private $userSession;
  26. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  27. private $groupManager;
  28. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  29. private $appManager;
  30. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  31. private $l;
  32. /** @var ChangePasswordController */
  33. private $controller;
  34. protected function setUp(): void {
  35. parent::setUp();
  36. $this->userManager = $this->createMock(\OC\User\Manager::class);
  37. $this->userSession = $this->createMock(Session::class);
  38. $this->groupManager = $this->createMock(\OC\Group\Manager::class);
  39. $this->appManager = $this->createMock(IAppManager::class);
  40. $this->l = $this->createMock(IL10N::class);
  41. $this->l->method('t')->willReturnArgument(0);
  42. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */
  43. $request = $this->createMock(IRequest::class);
  44. $this->controller = new ChangePasswordController(
  45. 'core',
  46. $request,
  47. $this->userId,
  48. $this->userManager,
  49. $this->userSession,
  50. $this->groupManager,
  51. $this->appManager,
  52. $this->l
  53. );
  54. }
  55. public function testChangePersonalPasswordWrongPassword() {
  56. $this->userSession->expects($this->once())
  57. ->method('getLoginName')
  58. ->willReturn($this->loginName);
  59. $this->userManager->expects($this->once())
  60. ->method('checkPassword')
  61. ->with($this->loginName, 'old')
  62. ->willReturn(false);
  63. $expects = new JSONResponse([
  64. 'status' => 'error',
  65. 'data' => [
  66. 'message' => 'Wrong password',
  67. ],
  68. ]);
  69. $expects->throttle();
  70. $actual = $this->controller->changePersonalPassword('old', 'new');
  71. $this->assertEquals($expects, $actual);
  72. }
  73. public function testChangePersonalPasswordCommonPassword() {
  74. $this->userSession->expects($this->once())
  75. ->method('getLoginName')
  76. ->willReturn($this->loginName);
  77. $user = $this->getMockBuilder(IUser::class)->getMock();
  78. $this->userManager->expects($this->once())
  79. ->method('checkPassword')
  80. ->with($this->loginName, 'old')
  81. ->willReturn($user);
  82. $user->expects($this->once())
  83. ->method('setPassword')
  84. ->with('new')
  85. ->will($this->throwException(new HintException('Common password')));
  86. $expects = new JSONResponse([
  87. 'status' => 'error',
  88. 'data' => [
  89. 'message' => 'Common password',
  90. ],
  91. ]);
  92. $actual = $this->controller->changePersonalPassword('old', 'new');
  93. $this->assertEquals($expects, $actual);
  94. }
  95. public function testChangePersonalPasswordNoNewPassword() {
  96. $this->userSession->expects($this->once())
  97. ->method('getLoginName')
  98. ->willReturn($this->loginName);
  99. $user = $this->getMockBuilder(IUser::class)->getMock();
  100. $this->userManager->expects($this->once())
  101. ->method('checkPassword')
  102. ->with($this->loginName, 'old')
  103. ->willReturn($user);
  104. $expects = [
  105. 'status' => 'error',
  106. 'data' => [
  107. 'message' => 'Unable to change personal password',
  108. ],
  109. ];
  110. $res = $this->controller->changePersonalPassword('old');
  111. $this->assertEquals($expects, $res->getData());
  112. }
  113. public function testChangePersonalPasswordCantSetPassword() {
  114. $this->userSession->expects($this->once())
  115. ->method('getLoginName')
  116. ->willReturn($this->loginName);
  117. $user = $this->getMockBuilder(IUser::class)->getMock();
  118. $this->userManager->expects($this->once())
  119. ->method('checkPassword')
  120. ->with($this->loginName, 'old')
  121. ->willReturn($user);
  122. $user->expects($this->once())
  123. ->method('setPassword')
  124. ->with('new')
  125. ->willReturn(false);
  126. $expects = new JSONResponse([
  127. 'status' => 'error',
  128. 'data' => [
  129. 'message' => 'Unable to change personal password',
  130. ],
  131. ]);
  132. $actual = $this->controller->changePersonalPassword('old', 'new');
  133. $this->assertEquals($expects, $actual);
  134. }
  135. public function testChangePersonalPassword() {
  136. $this->userSession->expects($this->once())
  137. ->method('getLoginName')
  138. ->willReturn($this->loginName);
  139. $user = $this->getMockBuilder(IUser::class)->getMock();
  140. $this->userManager->expects($this->once())
  141. ->method('checkPassword')
  142. ->with($this->loginName, 'old')
  143. ->willReturn($user);
  144. $user->expects($this->once())
  145. ->method('setPassword')
  146. ->with('new')
  147. ->willReturn(true);
  148. $this->userSession->expects($this->once())
  149. ->method('updateSessionTokenPassword')
  150. ->with('new');
  151. $expects = new JSONResponse([
  152. 'status' => 'success',
  153. 'data' => [
  154. 'message' => 'Saved',
  155. ],
  156. ]);
  157. $actual = $this->controller->changePersonalPassword('old', 'new');
  158. $this->assertEquals($expects, $actual);
  159. }
  160. }