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