lostcontrollertest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. /**
  3. * Copyright (c) 2014 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['SecureRandom'] = $this->getMockBuilder('\OCP\Security\ISecureRandom')
  43. ->disableOriginalConstructor()->getMock();
  44. $this->container['IsEncryptionEnabled'] = true;
  45. $this->lostController = $this->container['LostController'];
  46. }
  47. public function testResetFormUnsuccessful() {
  48. $userId = 'admin';
  49. $token = 'MySecretToken';
  50. $this->container['URLGenerator']
  51. ->expects($this->once())
  52. ->method('linkToRouteAbsolute')
  53. ->with('core.lost.setPassword', array('userId' => 'admin', 'token' => 'MySecretToken'))
  54. ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/'));
  55. $response = $this->lostController->resetform($token, $userId);
  56. $expectedResponse = new TemplateResponse('core/lostpassword',
  57. 'resetpassword',
  58. array(
  59. 'link' => 'https://ownCloud.com/index.php/lostpassword/',
  60. ),
  61. 'guest');
  62. $this->assertEquals($expectedResponse, $response);
  63. }
  64. public function testEmailUnsucessful() {
  65. $existingUser = 'ExistingUser';
  66. $nonExistingUser = 'NonExistingUser';
  67. $this->container['UserManager']
  68. ->expects($this->any())
  69. ->method('userExists')
  70. ->will($this->returnValueMap(array(
  71. array(true, $existingUser),
  72. array(false, $nonExistingUser)
  73. )));
  74. // With a non existing user
  75. $response = $this->lostController->email($nonExistingUser);
  76. $expectedResponse = [
  77. 'status' => 'error',
  78. 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
  79. ];
  80. $this->assertSame($expectedResponse, $response);
  81. // With no mail address
  82. $this->container['Config']
  83. ->expects($this->any())
  84. ->method('getUserValue')
  85. ->with($existingUser, 'settings', 'email')
  86. ->will($this->returnValue(null));
  87. $response = $this->lostController->email($existingUser);
  88. $expectedResponse = [
  89. 'status' => 'error',
  90. 'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
  91. ];
  92. $this->assertSame($expectedResponse, $response);
  93. }
  94. public function testEmailSuccessful() {
  95. /**
  96. * FIXME: Disable test for systems where no sendmail is available since code is static.
  97. * @link https://github.com/owncloud/core/pull/12085
  98. */
  99. if (is_null(\OC_Helper::findBinaryPath('sendmail'))) {
  100. $this->markTestSkipped('sendmail is not available');
  101. }
  102. $randomToken = $this->container['SecureRandom'];
  103. $this->container['SecureRandom']
  104. ->expects($this->once())
  105. ->method('generate')
  106. ->with('21')
  107. ->will($this->returnValue('ThisIsMaybeANotSoSecretToken!'));
  108. $this->container['UserManager']
  109. ->expects($this->once())
  110. ->method('userExists')
  111. ->with('ExistingUser')
  112. ->will($this->returnValue(true));
  113. $this->container['Config']
  114. ->expects($this->once())
  115. ->method('getUserValue')
  116. ->with('ExistingUser', 'settings', 'email')
  117. ->will($this->returnValue('test@example.com'));
  118. $this->container['SecureRandom']
  119. ->expects($this->once())
  120. ->method('getMediumStrengthGenerator')
  121. ->will($this->returnValue($randomToken));
  122. $this->container['Config']
  123. ->expects($this->once())
  124. ->method('setUserValue')
  125. ->with('ExistingUser', 'owncloud', 'lostpassword', 'ThisIsMaybeANotSoSecretToken!');
  126. $this->container['URLGenerator']
  127. ->expects($this->once())
  128. ->method('linkToRouteAbsolute')
  129. ->with('core.lost.resetform', array('userId' => 'ExistingUser', 'token' => 'ThisIsMaybeANotSoSecretToken!'))
  130. ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/'));
  131. $response = $this->lostController->email('ExistingUser');
  132. $expectedResponse = array('status' => 'success');
  133. $this->assertSame($expectedResponse, $response);
  134. }
  135. public function testSetPasswordUnsuccessful() {
  136. $this->container['Config']
  137. ->expects($this->once())
  138. ->method('getUserValue')
  139. ->with('InvalidTokenUser', 'owncloud', 'lostpassword', null)
  140. ->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword'));
  141. // With an invalid token
  142. $userName = 'InvalidTokenUser';
  143. $response = $this->lostController->setPassword('wrongToken', $userName, 'NewPassword', true);
  144. $expectedResponse = [
  145. 'status' => 'error',
  146. 'msg' => 'Couldn\'t reset password because the token is invalid'
  147. ];
  148. $this->assertSame($expectedResponse, $response);
  149. // With a valid token and no proceed
  150. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword!', $userName, 'NewPassword', false);
  151. $expectedResponse = ['status' => 'error', 'msg' => '', 'encryption' => true];
  152. $this->assertSame($expectedResponse, $response);
  153. }
  154. public function testSetPasswordSuccessful() {
  155. $this->container['Config']
  156. ->expects($this->once())
  157. ->method('getUserValue')
  158. ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
  159. ->will($this->returnValue('TheOnlyAndOnlyOneTokenToResetThePassword'));
  160. $user = $this->getMockBuilder('\OCP\IUser')
  161. ->disableOriginalConstructor()->getMock();
  162. $user->expects($this->once())
  163. ->method('setPassword')
  164. ->with('NewPassword')
  165. ->will($this->returnValue(true));
  166. $this->container['UserManager']
  167. ->expects($this->once())
  168. ->method('get')
  169. ->with('ValidTokenUser')
  170. ->will($this->returnValue($user));
  171. $this->container['Config']
  172. ->expects($this->once())
  173. ->method('deleteUserValue')
  174. ->with('ValidTokenUser', 'owncloud', 'lostpassword');
  175. $response = $this->lostController->setPassword('TheOnlyAndOnlyOneTokenToResetThePassword', 'ValidTokenUser', 'NewPassword', true);
  176. $expectedResponse = array('status' => 'success');
  177. $this->assertSame($expectedResponse, $response);
  178. }
  179. public function testIsSetPasswordWithoutTokenFailing() {
  180. $this->container['Config']
  181. ->expects($this->once())
  182. ->method('getUserValue')
  183. ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
  184. ->will($this->returnValue(null));
  185. $response = $this->lostController->setPassword('', 'ValidTokenUser', 'NewPassword', true);
  186. $expectedResponse = [
  187. 'status' => 'error',
  188. 'msg' => 'Couldn\'t reset password because the token is invalid'
  189. ];
  190. $this->assertSame($expectedResponse, $response);
  191. }
  192. }