ChangePasswordControllerTest.php 4.7 KB

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