PageRenderTime 35ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/core/lostpassword/controller/lostcontrollertest.php

https://gitlab.com/Red54/core
PHP | 296 lines | 258 code | 22 blank | 16 comment | 0 complexity | 644ac39124cde31c52ec1e82d6fef95d MD5 | raw file
  1. <?php
  2. /**
  3. * Copyright (c) 2014-2015 Lukas Reschke <lukas@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace OC\Core\LostPassword\Controller;
  9. use OC\Core\Application;
  10. use OCP\AppFramework\Http\TemplateResponse;
  11. /**
  12. * Class LostControllerTest
  13. *
  14. * @package OC\Core\LostPassword\Controller
  15. */
  16. class LostControllerTest extends \PHPUnit_Framework_TestCase {
  17. private $container;
  18. /** @var LostController */
  19. private $lostController;
  20. protected function setUp() {
  21. $app = new Application();
  22. $this->container = $app->getContainer();
  23. $this->container['AppName'] = 'core';
  24. $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig')
  25. ->disableOriginalConstructor()->getMock();
  26. $this->container['L10N'] = $this->getMockBuilder('\OCP\IL10N')
  27. ->disableOriginalConstructor()->getMock();
  28. $this->container['L10N']
  29. ->expects($this->any())
  30. ->method('t')
  31. ->will($this->returnCallback(function($text, $parameters = array()) {
  32. return vsprintf($text, $parameters);
  33. }));
  34. $this->container['Defaults'] = $this->getMockBuilder('\OC_Defaults')
  35. ->disableOriginalConstructor()->getMock();
  36. $this->container['UserManager'] = $this->getMockBuilder('\OCP\IUserManager')
  37. ->disableOriginalConstructor()->getMock();
  38. $this->container['Config'] = $this->getMockBuilder('\OCP\IConfig')
  39. ->disableOriginalConstructor()->getMock();
  40. $this->container['URLGenerator'] = $this->getMockBuilder('\OCP\IURLGenerator')
  41. ->disableOriginalConstructor()->getMock();
  42. $this->container['Mailer'] = $this->getMockBuilder('\OCP\Mail\IMailer')
  43. ->disableOriginalConstructor()->getMock();
  44. $this->container['SecureRandom'] = $this->getMockBuilder('\OCP\Security\ISecureRandom')
  45. ->disableOriginalConstructor()->getMock();
  46. $this->container['IsEncryptionEnabled'] = true;
  47. $this->lostController = $this->container['LostController'];
  48. }
  49. public function testResetFormUnsuccessful() {
  50. $userId = 'admin';
  51. $token = 'MySecretToken';
  52. $this->container['URLGenerator']
  53. ->expects($this->once())
  54. ->method('linkToRouteAbsolute')
  55. ->with('core.lost.setPassword', array('userId' => 'admin', 'token' => 'MySecretToken'))
  56. ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/'));
  57. $response = $this->lostController->resetform($token, $userId);
  58. $expectedResponse = new TemplateResponse('core/lostpassword',
  59. 'resetpassword',
  60. array(
  61. 'link' => 'https://ownCloud.com/index.php/lostpassword/',
  62. ),
  63. 'guest');
  64. $this->assertEquals($expectedResponse, $response);
  65. }
  66. public function testEmailUnsucessful() {
  67. $existingUser = 'ExistingUser';
  68. $nonExistingUser = 'NonExistingUser';
  69. $this->container['UserManager']
  70. ->expects($this->any())
  71. ->method('userExists')
  72. ->will($this->returnValueMap(array(
  73. array(true, $existingUser),
  74. array(false, $nonExistingUser)
  75. )));
  76. // With a non existing user
  77. $response = $this->lostController->email($nonExistingUser);
  78. $expectedResponse = [
  79. 'status' => 'error',
  80. 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
  81. ];
  82. $this->assertSame($expectedResponse, $response);
  83. // With no mail address
  84. $this->container['Config']
  85. ->expects($this->any())
  86. ->method('getUserValue')
  87. ->with($existingUser, 'settings', 'email')
  88. ->will($this->returnValue(null));
  89. $response = $this->lostController->email($existingUser);
  90. $expectedResponse = [
  91. 'status' => 'error',
  92. 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
  93. ];
  94. $this->assertSame($expectedResponse, $response);
  95. }
  96. public function testEmailSuccessful() {
  97. $randomToken = $this->container['SecureRandom'];
  98. $this->container['SecureRandom']
  99. ->expects($this->once())
  100. ->method('generate')
  101. ->with('21')
  102. ->will($this->returnValue('ThisIsMaybeANotSoSecretToken!'));
  103. $this->container['UserManager']
  104. ->expects($this->once())
  105. ->method('userExists')
  106. ->with('ExistingUser')
  107. ->will($this->returnValue(true));
  108. $this->container['Config']
  109. ->expects($this->once())
  110. ->method('getUserValue')
  111. ->with('ExistingUser', 'settings', 'email')
  112. ->will($this->returnValue('test@example.com'));
  113. $this->container['SecureRandom']
  114. ->expects($this->once())
  115. ->method('getMediumStrengthGenerator')
  116. ->will($this->returnValue($randomToken));
  117. $this->container['Config']
  118. ->expects($this->once())
  119. ->method('setUserValue')
  120. ->with('ExistingUser', 'owncloud', 'lostpassword', 'ThisIsMaybeANotSoSecretToken!');
  121. $this->container['URLGenerator']
  122. ->expects($this->once())
  123. ->method('linkToRouteAbsolute')
  124. ->with('core.lost.resetform', array('userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'))
  125. ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/'));
  126. $message = $this->getMockBuilder('\OC\Mail\Message')
  127. ->disableOriginalConstructor()->getMock();
  128. $message
  129. ->expects($this->at(0))
  130. ->method('setTo')
  131. ->with(['test@example.com' => 'ExistingUser']);
  132. $message
  133. ->expects($this->at(1))
  134. ->method('setSubject')
  135. ->with(' password reset');
  136. $message
  137. ->expects($this->at(2))
  138. ->method('setPlainBody')
  139. ->with('Use the following link to reset your password: https://ownCloud.com/index.php/lostpassword/');
  140. $message
  141. ->expects($this->at(3))
  142. ->method('setFrom')
  143. ->with(['lostpassword-noreply@localhost' => null]);
  144. $this->container['Mailer']
  145. ->expects($this->at(0))
  146. ->method('createMessage')
  147. ->will($this->returnValue($message));
  148. $this->container['Mailer']
  149. ->expects($this->at(1))
  150. ->method('send')
  151. ->with($message);
  152. $response = $this->lostController->email('ExistingUser');
  153. $expectedResponse = array('status' => 'success');
  154. $this->assertSame($expectedResponse, $response);
  155. }
  156. public function testEmailCantSendException() {
  157. $randomToken = $this->container['SecureRandom'];
  158. $this->container['SecureRandom']
  159. ->expects($this->once())
  160. ->method('generate')
  161. ->with('21')
  162. ->will($this->returnValue('ThisIsMaybeANotSoSecretToken!'));
  163. $this->container['UserManager']
  164. ->expects($this->once())
  165. ->method('userExists')
  166. ->with('ExistingUser')
  167. ->will($this->returnValue(true));
  168. $this->container['Config']
  169. ->expects($this->once())
  170. ->method('getUserValue')
  171. ->with('ExistingUser', 'settings', 'email')
  172. ->will($this->returnValue('test@example.com'));
  173. $this->container['SecureRandom']
  174. ->expects($this->once())
  175. ->method('getMediumStrengthGenerator')
  176. ->will($this->returnValue($randomToken));
  177. $this->container['Config']
  178. ->expects($this->once())
  179. ->method('setUserValue')
  180. ->with('ExistingUser', 'owncloud', 'lostpassword', 'ThisIsMaybeANotSoSecretToken!');
  181. $this->container['URLGenerator']
  182. ->expects($this->once())
  183. ->method('linkToRouteAbsolute')
  184. ->with('core.lost.resetform', array('userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'))
  185. ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/'));
  186. $message = $this->getMockBuilder('\OC\Mail\Message')
  187. ->disableOriginalConstructor()->getMock();
  188. $message
  189. ->expects($this->at(0))
  190. ->method('setTo')
  191. ->with(['test@example.com' => 'ExistingUser']);
  192. $message
  193. ->expects($this->at(1))
  194. ->method('setSubject')
  195. ->with(' password reset');
  196. $message
  197. ->expects($this->at(2))
  198. ->method('setPlainBody')
  199. ->with('Use the following link to reset your password: https://ownCloud.com/index.php/lostpassword/');
  200. $message
  201. ->expects($this->at(3))
  202. ->method('setFrom')
  203. ->with(['lostpassword-noreply@localhost' => null]);
  204. $this->container['Mailer']
  205. ->expects($this->at(0))
  206. ->method('createMessage')
  207. ->will($this->returnValue($message));
  208. $this->container['Mailer']
  209. ->expects($this->at(1))
  210. ->method('send')
  211. ->with($message)
  212. ->will($this->throwException(new \Exception()));
  213. $response = $this->lostController->email('ExistingUser');
  214. $expectedResponse = ['status' => 'error', 'msg' => 'Couldn\'t send reset email. Please contact your administrator.'];
  215. $this->assertSame($expectedResponse, $response);
  216. }
  217. public function testSetPasswordUnsuccessful() {
  218. $this->container['Config']
  219. ->expects($this->once())
  220. ->method('getUserValue')
  221. ->with('InvalidTokenUser', 'owncloud', 'lostpassword', null)
  222. ->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword'));
  223. // With an invalid token
  224. $userName = 'InvalidTokenUser';
  225. $response = $this->lostController->setPassword('wrongToken', $userName, 'NewPassword', true);
  226. $expectedResponse = [
  227. 'status' => 'error',
  228. 'msg' => 'Couldn\'t reset password because the token is invalid'
  229. ];
  230. $this->assertSame($expectedResponse, $response);
  231. // With a valid token and no proceed
  232. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword!', $userName, 'NewPassword', false);
  233. $expectedResponse = ['status' => 'error', 'msg' => '', 'encryption' => true];
  234. $this->assertSame($expectedResponse, $response);
  235. }
  236. public function testSetPasswordSuccessful() {
  237. $this->container['Config']
  238. ->expects($this->once())
  239. ->method('getUserValue')
  240. ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
  241. ->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword'));
  242. $user = $this->getMockBuilder('\OCP\IUser')
  243. ->disableOriginalConstructor()->getMock();
  244. $user->expects($this->once())
  245. ->method('setPassword')
  246. ->with('NewPassword')
  247. ->will($this->returnValue(true));
  248. $this->container['UserManager']
  249. ->expects($this->once())
  250. ->method('get')
  251. ->with('ValidTokenUser')
  252. ->will($this->returnValue($user));
  253. $this->container['Config']
  254. ->expects($this->once())
  255. ->method('deleteUserValue')
  256. ->with('ValidTokenUser', 'owncloud', 'lostpassword');
  257. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
  258. $expectedResponse = array('status' => 'success');
  259. $this->assertSame($expectedResponse, $response);
  260. }
  261. public function testIsSetPasswordWithoutTokenFailing() {
  262. $this->container['Config']
  263. ->expects($this->once())
  264. ->method('getUserValue')
  265. ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
  266. ->will($this->returnValue(null));
  267. $response = $this->lostController->setPassword('', 'ValidTokenUser', 'NewPassword', true);
  268. $expectedResponse = [
  269. 'status' => 'error',
  270. 'msg' => 'Couldn\'t reset password because the token is invalid'
  271. ];
  272. $this->assertSame($expectedResponse, $response);
  273. }
  274. }