1
0

ChangePasswordControllerTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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\HintException;
  24. use OC\Settings\Controller\ChangePasswordController;
  25. use OC\User\Session;
  26. use OCP\App\IAppManager;
  27. use OCP\AppFramework\Http\JSONResponse;
  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 IUserManager|\PHPUnit_Framework_MockObject_MockObject */
  37. private $userManager;
  38. /** @var Session|\PHPUnit_Framework_MockObject_MockObject */
  39. private $userSession;
  40. /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject */
  41. private $groupManager;
  42. /** @var IAppManager|\PHPUnit_Framework_MockObject_MockObject */
  43. private $appManager;
  44. /** @var IL10N|\PHPUnit_Framework_MockObject_MockObject */
  45. private $l;
  46. /** @var ChangePasswordController */
  47. private $controller;
  48. public function setUp() {
  49. parent::setUp();
  50. $this->userManager = $this->createMock(\OC\User\Manager::class);
  51. $this->userSession = $this->createMock(Session::class);
  52. $this->groupManager = $this->createMock(\OC\Group\Manager::class);
  53. $this->appManager = $this->createMock(IAppManager::class);
  54. $this->l = $this->createMock(IL10N::class);
  55. $this->l->method('t')->will($this->returnArgument(0));
  56. /** @var IRequest|\PHPUnit_Framework_MockObject_MockObject $request */
  57. $request = $this->createMock(IRequest::class);
  58. $this->controller = new ChangePasswordController(
  59. 'core',
  60. $request,
  61. $this->userId,
  62. $this->userManager,
  63. $this->userSession,
  64. $this->groupManager,
  65. $this->appManager,
  66. $this->l
  67. );
  68. }
  69. public function testChangePersonalPasswordWrongPassword() {
  70. $this->userManager->expects($this->once())
  71. ->method('checkPassword')
  72. ->with($this->userId, 'old')
  73. ->willReturn(false);
  74. $expects = new JSONResponse([
  75. 'status' => 'error',
  76. 'data' => [
  77. 'message' => 'Wrong password',
  78. ],
  79. ]);
  80. $expects->throttle();
  81. $actual = $this->controller->changePersonalPassword('old', 'new');
  82. $this->assertEquals($expects, $actual);
  83. }
  84. public function testChangePersonalPasswordCommonPassword() {
  85. $user = $this->getMockBuilder(IUser::class)->getMock();
  86. $this->userManager->expects($this->once())
  87. ->method('checkPassword')
  88. ->with($this->userId, 'old')
  89. ->willReturn($user);
  90. $user->expects($this->once())
  91. ->method('setPassword')
  92. ->with('new')
  93. ->will($this->throwException(new HintException('Common password')));
  94. $expects = new JSONResponse([
  95. 'status' => 'error',
  96. 'data' => [
  97. 'message' => 'Common password',
  98. ],
  99. ]);
  100. $actual = $this->controller->changePersonalPassword('old', 'new');
  101. $this->assertEquals($expects, $actual);
  102. }
  103. public function testChangePersonalPasswordNoNewPassword() {
  104. $user = $this->getMockBuilder(IUser::class)->getMock();
  105. $this->userManager->expects($this->once())
  106. ->method('checkPassword')
  107. ->with($this->userId, 'old')
  108. ->willReturn($user);
  109. $expects = [
  110. 'status' => 'error',
  111. ];
  112. $res = $this->controller->changePersonalPassword('old');
  113. $this->assertEquals($expects, $res->getData());
  114. }
  115. public function testChangePersonalPasswordCantSetPassword() {
  116. $user = $this->getMockBuilder(IUser::class)->getMock();
  117. $this->userManager->expects($this->once())
  118. ->method('checkPassword')
  119. ->with($this->userId, 'old')
  120. ->willReturn($user);
  121. $user->expects($this->once())
  122. ->method('setPassword')
  123. ->with('new')
  124. ->willReturn(false);
  125. $expects = new JSONResponse([
  126. 'status' => 'error',
  127. ]);
  128. $actual = $this->controller->changePersonalPassword('old', 'new');
  129. $this->assertEquals($expects, $actual);
  130. }
  131. public function testChangePersonalPassword() {
  132. $user = $this->getMockBuilder(IUser::class)->getMock();
  133. $this->userManager->expects($this->once())
  134. ->method('checkPassword')
  135. ->with($this->userId, 'old')
  136. ->willReturn($user);
  137. $user->expects($this->once())
  138. ->method('setPassword')
  139. ->with('new')
  140. ->willReturn(true);
  141. $this->userSession->expects($this->once())
  142. ->method('updateSessionTokenPassword')
  143. ->with('new');
  144. $expects = new JSONResponse([
  145. 'status' => 'success',
  146. 'data' => [
  147. 'message' => 'Saved',
  148. ],
  149. ]);
  150. $actual = $this->controller->changePersonalPassword('old', 'new');
  151. $this->assertEquals($expects, $actual);
  152. }
  153. }