ChangePasswordControllerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <?php
  2. /**
  3. *
  4. * @author Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @license GNU AGPL version 3 or any later version
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License as
  10. * published by the Free Software Foundation, either version 3 of the
  11. * License, or (at your option) any later version.
  12. *
  13. * This program 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 License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Tests\Core\Controller;
  23. use OC\User\Session;
  24. use OCA\Settings\Controller\ChangePasswordController;
  25. use OCP\App\IAppManager;
  26. use OCP\AppFramework\Http\JSONResponse;
  27. use OCP\HintException;
  28. use OCP\IGroupManager;
  29. use OCP\IL10N;
  30. use OCP\IRequest;
  31. use OCP\IUser;
  32. use OCP\IUserManager;
  33. class ChangePasswordControllerTest extends \Test\TestCase {
  34. /** @var string */
  35. private $userId = 'currentUser';
  36. /** @var string */
  37. private $loginName = 'ua1337';
  38. /** @var IUserManager|\PHPUnit\Framework\MockObject\MockObject */
  39. private $userManager;
  40. /** @var Session|\PHPUnit\Framework\MockObject\MockObject */
  41. private $userSession;
  42. /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
  43. private $groupManager;
  44. /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
  45. private $appManager;
  46. /** @var IL10N|\PHPUnit\Framework\MockObject\MockObject */
  47. private $l;
  48. /** @var ChangePasswordController */
  49. private $controller;
  50. protected function setUp(): void {
  51. parent::setUp();
  52. $this->userManager = $this->createMock(\OC\User\Manager::class);
  53. $this->userSession = $this->createMock(Session::class);
  54. $this->groupManager = $this->createMock(\OC\Group\Manager::class);
  55. $this->appManager = $this->createMock(IAppManager::class);
  56. $this->l = $this->createMock(IL10N::class);
  57. $this->l->method('t')->willReturnArgument(0);
  58. /** @var IRequest|\PHPUnit\Framework\MockObject\MockObject $request */
  59. $request = $this->createMock(IRequest::class);
  60. $this->controller = new ChangePasswordController(
  61. 'core',
  62. $request,
  63. $this->userId,
  64. $this->userManager,
  65. $this->userSession,
  66. $this->groupManager,
  67. $this->appManager,
  68. $this->l
  69. );
  70. }
  71. public function testChangePersonalPasswordWrongPassword() {
  72. $this->userSession->expects($this->once())
  73. ->method('getLoginName')
  74. ->willReturn($this->loginName);
  75. $this->userManager->expects($this->once())
  76. ->method('checkPassword')
  77. ->with($this->loginName, 'old')
  78. ->willReturn(false);
  79. $expects = new JSONResponse([
  80. 'status' => 'error',
  81. 'data' => [
  82. 'message' => 'Wrong password',
  83. ],
  84. ]);
  85. $expects->throttle();
  86. $actual = $this->controller->changePersonalPassword('old', 'new');
  87. $this->assertEquals($expects, $actual);
  88. }
  89. public function testChangePersonalPasswordCommonPassword() {
  90. $this->userSession->expects($this->once())
  91. ->method('getLoginName')
  92. ->willReturn($this->loginName);
  93. $user = $this->getMockBuilder(IUser::class)->getMock();
  94. $this->userManager->expects($this->once())
  95. ->method('checkPassword')
  96. ->with($this->loginName, 'old')
  97. ->willReturn($user);
  98. $user->expects($this->once())
  99. ->method('setPassword')
  100. ->with('new')
  101. ->will($this->throwException(new HintException('Common password')));
  102. $expects = new JSONResponse([
  103. 'status' => 'error',
  104. 'data' => [
  105. 'message' => 'Common password',
  106. ],
  107. ]);
  108. $actual = $this->controller->changePersonalPassword('old', 'new');
  109. $this->assertEquals($expects, $actual);
  110. }
  111. public function testChangePersonalPasswordNoNewPassword() {
  112. $this->userSession->expects($this->once())
  113. ->method('getLoginName')
  114. ->willReturn($this->loginName);
  115. $user = $this->getMockBuilder(IUser::class)->getMock();
  116. $this->userManager->expects($this->once())
  117. ->method('checkPassword')
  118. ->with($this->loginName, 'old')
  119. ->willReturn($user);
  120. $expects = [
  121. 'status' => 'error',
  122. 'data' => [
  123. 'message' => 'Unable to change personal password',
  124. ],
  125. ];
  126. $res = $this->controller->changePersonalPassword('old');
  127. $this->assertEquals($expects, $res->getData());
  128. }
  129. public function testChangePersonalPasswordCantSetPassword() {
  130. $this->userSession->expects($this->once())
  131. ->method('getLoginName')
  132. ->willReturn($this->loginName);
  133. $user = $this->getMockBuilder(IUser::class)->getMock();
  134. $this->userManager->expects($this->once())
  135. ->method('checkPassword')
  136. ->with($this->loginName, 'old')
  137. ->willReturn($user);
  138. $user->expects($this->once())
  139. ->method('setPassword')
  140. ->with('new')
  141. ->willReturn(false);
  142. $expects = new JSONResponse([
  143. 'status' => 'error',
  144. 'data' => [
  145. 'message' => 'Unable to change personal password',
  146. ],
  147. ]);
  148. $actual = $this->controller->changePersonalPassword('old', 'new');
  149. $this->assertEquals($expects, $actual);
  150. }
  151. public function testChangePersonalPassword() {
  152. $this->userSession->expects($this->once())
  153. ->method('getLoginName')
  154. ->willReturn($this->loginName);
  155. $user = $this->getMockBuilder(IUser::class)->getMock();
  156. $this->userManager->expects($this->once())
  157. ->method('checkPassword')
  158. ->with($this->loginName, 'old')
  159. ->willReturn($user);
  160. $user->expects($this->once())
  161. ->method('setPassword')
  162. ->with('new')
  163. ->willReturn(true);
  164. $this->userSession->expects($this->once())
  165. ->method('updateSessionTokenPassword')
  166. ->with('new');
  167. $expects = new JSONResponse([
  168. 'status' => 'success',
  169. 'data' => [
  170. 'message' => 'Saved',
  171. ],
  172. ]);
  173. $actual = $this->controller->changePersonalPassword('old', 'new');
  174. $this->assertEquals($expects, $actual);
  175. }
  176. }