ChangePasswordControllerTest.php 4.1 KB

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