2014-10-20 21:05:48 +04:00
< ? php
/**
2015-08-22 21:42:45 +03:00
* @ author Lukas Reschke < lukas @ owncloud . com >
*
* @ copyright Copyright ( c ) 2015 , ownCloud , Inc .
* @ license AGPL - 3.0
*
* This code is free software : you can redistribute it and / or modify
* it under the terms of the GNU Affero General Public License , version 3 ,
* as published by the Free Software Foundation .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Affero General Public License for more details .
*
* You should have received a copy of the GNU Affero General Public License , version 3 ,
* along with this program . If not , see < http :// www . gnu . org / licenses />
*
2014-10-20 21:05:48 +04:00
*/
2016-05-19 11:43:49 +03:00
namespace Tests\Core\Controller ;
2015-12-01 14:05:40 +03:00
2016-05-19 11:43:49 +03:00
use OC\Core\Controller\LostController ;
2016-11-03 21:08:56 +03:00
use OC\Mail\Message ;
2017-04-14 14:42:40 +03:00
use OCP\AppFramework\Http\JSONResponse ;
2014-10-20 21:05:48 +04:00
use OCP\AppFramework\Http\TemplateResponse ;
2015-12-01 14:05:40 +03:00
use OCP\AppFramework\Utility\ITimeFactory ;
2017-04-07 23:42:43 +03:00
use OCP\Defaults ;
2018-08-10 17:40:09 +03:00
use OCP\Encryption\IEncryptionModule ;
2016-09-29 17:38:29 +03:00
use OCP\Encryption\IManager ;
2015-12-01 14:05:40 +03:00
use OCP\IConfig ;
use OCP\IL10N ;
use OCP\IRequest ;
use OCP\IURLGenerator ;
use OCP\IUser ;
use OCP\IUserManager ;
2017-04-12 01:24:58 +03:00
use OCP\Mail\IEMailTemplate ;
2015-12-01 14:05:40 +03:00
use OCP\Mail\IMailer ;
2016-08-28 15:22:29 +03:00
use OCP\Security\ICrypto ;
2015-12-01 14:05:40 +03:00
use OCP\Security\ISecureRandom ;
use PHPUnit_Framework_MockObject_MockObject ;
2014-10-20 21:05:48 +04:00
/**
* Class LostControllerTest
*
2016-01-20 12:42:19 +03:00
* @ package OC\Core\Controller
2014-10-20 21:05:48 +04:00
*/
2016-10-06 15:11:04 +03:00
class LostControllerTest extends \Test\TestCase {
2014-10-20 21:05:48 +04:00
/** @var LostController */
private $lostController ;
2015-12-01 14:05:40 +03:00
/** @var IUser */
private $existingUser ;
/** @var IURLGenerator | PHPUnit_Framework_MockObject_MockObject */
private $urlGenerator ;
/** @var IL10N */
private $l10n ;
/** @var IUserManager | PHPUnit_Framework_MockObject_MockObject */
private $userManager ;
2017-04-07 23:42:43 +03:00
/** @var Defaults */
2015-12-01 14:05:40 +03:00
private $defaults ;
/** @var IConfig | PHPUnit_Framework_MockObject_MockObject */
private $config ;
/** @var IMailer | PHPUnit_Framework_MockObject_MockObject */
private $mailer ;
/** @var ISecureRandom | PHPUnit_Framework_MockObject_MockObject */
private $secureRandom ;
2016-09-29 17:38:29 +03:00
/** @var IManager|PHPUnit_Framework_MockObject_MockObject */
private $encryptionManager ;
2015-12-01 14:05:40 +03:00
/** @var ITimeFactory | PHPUnit_Framework_MockObject_MockObject */
private $timeFactory ;
/** @var IRequest */
private $request ;
2016-11-03 21:08:56 +03:00
/** @var ICrypto|\PHPUnit_Framework_MockObject_MockObject */
2016-08-28 15:22:29 +03:00
private $crypto ;
2014-10-20 21:05:48 +04:00
protected function setUp () {
2016-10-06 15:11:04 +03:00
parent :: setUp ();
2015-12-01 14:05:40 +03:00
2016-11-03 21:08:56 +03:00
$this -> existingUser = $this -> createMock ( IUser :: class );
2017-03-28 21:39:36 +03:00
$this -> existingUser -> expects ( $this -> any ())
-> method ( 'getEMailAddress' )
2015-12-01 14:05:40 +03:00
-> willReturn ( 'test@example.com' );
2017-03-28 21:39:36 +03:00
$this -> existingUser -> expects ( $this -> any ())
-> method ( 'getUID' )
-> willReturn ( 'ExistingUser' );
2017-08-18 14:03:40 +03:00
$this -> existingUser -> expects ( $this -> any ())
-> method ( 'isEnabled' )
-> willReturn ( true );
2015-12-01 14:05:40 +03:00
2016-11-03 21:08:56 +03:00
$this -> config = $this -> createMock ( IConfig :: class );
2017-05-11 17:46:43 +03:00
$this -> config -> expects ( $this -> any ())
-> method ( 'getSystemValue' )
-> willReturnMap ([
[ 'secret' , null , 'SECRET' ],
[ 'secret' , '' , 'SECRET' ],
[ 'lost_password_link' , '' , '' ],
]);
2016-11-03 21:08:56 +03:00
$this -> l10n = $this -> createMock ( IL10N :: class );
2015-12-01 14:05:40 +03:00
$this -> l10n
2015-02-01 19:46:48 +03:00
-> expects ( $this -> any ())
-> method ( 't' )
-> will ( $this -> returnCallback ( function ( $text , $parameters = array ()) {
return vsprintf ( $text , $parameters );
}));
2017-04-07 23:42:43 +03:00
$this -> defaults = $this -> getMockBuilder ( '\OCP\Defaults' )
2014-10-20 21:05:48 +04:00
-> disableOriginalConstructor () -> getMock ();
2017-10-24 16:26:53 +03:00
$this -> userManager = $this -> getMockBuilder ( IUserManager :: class )
2014-10-20 21:05:48 +04:00
-> disableOriginalConstructor () -> getMock ();
2017-10-24 16:26:53 +03:00
$this -> urlGenerator = $this -> getMockBuilder ( IURLGenerator :: class )
2014-10-20 21:05:48 +04:00
-> disableOriginalConstructor () -> getMock ();
2015-12-01 14:05:40 +03:00
$this -> mailer = $this -> getMockBuilder ( '\OCP\Mail\IMailer' )
2014-10-20 21:05:48 +04:00
-> disableOriginalConstructor () -> getMock ();
2015-12-01 14:05:40 +03:00
$this -> secureRandom = $this -> getMockBuilder ( '\OCP\Security\ISecureRandom' )
2015-02-12 18:03:51 +03:00
-> disableOriginalConstructor () -> getMock ();
2015-12-01 14:05:40 +03:00
$this -> timeFactory = $this -> getMockBuilder ( '\OCP\AppFramework\Utility\ITimeFactory' )
2014-10-20 21:05:48 +04:00
-> disableOriginalConstructor () -> getMock ();
2017-10-24 16:26:53 +03:00
$this -> request = $this -> getMockBuilder ( IRequest :: class )
2015-08-22 21:42:45 +03:00
-> disableOriginalConstructor () -> getMock ();
2016-09-29 17:38:29 +03:00
$this -> encryptionManager = $this -> getMockBuilder ( IManager :: class )
-> disableOriginalConstructor () -> getMock ();
$this -> encryptionManager -> expects ( $this -> any ())
-> method ( 'isEnabled' )
-> willReturn ( true );
2016-08-28 15:22:29 +03:00
$this -> crypto = $this -> createMock ( ICrypto :: class );
2015-12-01 14:05:40 +03:00
$this -> lostController = new LostController (
'Core' ,
$this -> request ,
$this -> urlGenerator ,
$this -> userManager ,
$this -> defaults ,
$this -> l10n ,
$this -> config ,
$this -> secureRandom ,
'lostpassword-noreply@localhost' ,
2016-09-29 17:38:29 +03:00
$this -> encryptionManager ,
2015-12-01 14:05:40 +03:00
$this -> mailer ,
2016-08-28 15:22:29 +03:00
$this -> timeFactory ,
$this -> crypto
2015-12-01 14:05:40 +03:00
);
2014-10-20 21:05:48 +04:00
}
2016-08-28 15:22:29 +03:00
public function testResetFormWithNotExistingUser () {
2016-11-03 21:08:56 +03:00
$this -> userManager -> method ( 'get' )
2016-08-28 15:22:29 +03:00
-> with ( 'NotExistingUser' )
-> willReturn ( null );
$expectedResponse = new TemplateResponse (
'core' ,
'error' ,
[
'errors' => [
[ 'error' => 'Couldn\'t reset password because the token is invalid' ],
]
],
'guest'
);
2016-11-03 21:08:56 +03:00
$this -> assertEquals ( $expectedResponse , $this -> lostController -> resetform ( 'MySecretToken' , 'NotExistingUser' ));
2016-05-19 23:40:53 +03:00
}
public function testResetFormInvalidTokenMatch () {
2016-11-03 21:08:56 +03:00
$this -> config -> method ( 'getUserValue' )
2016-08-23 16:01:38 +03:00
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
2016-11-03 21:08:56 +03:00
-> willReturn ( 'encryptedToken' );
$this -> existingUser -> method ( 'getLastLogin' )
2016-05-19 23:40:53 +03:00
-> will ( $this -> returnValue ( 12344 ));
2016-11-03 21:08:56 +03:00
$this -> userManager -> method ( 'get' )
2016-05-19 23:40:53 +03:00
-> with ( 'ValidTokenUser' )
2016-11-03 21:08:56 +03:00
-> willReturn ( $this -> existingUser );
$this -> crypto -> method ( 'decrypt' )
-> with (
$this -> equalTo ( 'encryptedToken' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( '12345:TheOnlyAndOnlyOneTokenToResetThePassword' );
$response = $this -> lostController -> resetform ( '12345:MySecretToken' , 'ValidTokenUser' );
2016-05-19 23:40:53 +03:00
$expectedResponse = new TemplateResponse ( 'core' ,
'error' ,
[
'errors' => [
[ 'error' => 'Couldn\'t reset password because the token is invalid' ],
]
],
'guest' );
$this -> assertEquals ( $expectedResponse , $response );
}
public function testResetFormExpiredToken () {
2016-11-03 21:08:56 +03:00
$this -> userManager -> method ( 'get' )
2016-05-19 23:40:53 +03:00
-> with ( 'ValidTokenUser' )
2016-11-03 21:08:56 +03:00
-> willReturn ( $this -> existingUser );
2016-05-19 23:40:53 +03:00
$this -> config
-> expects ( $this -> once ())
-> method ( 'getUserValue' )
2016-08-23 16:01:38 +03:00
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
2016-11-03 21:08:56 +03:00
-> will ( $this -> returnValue ( 'encryptedToken' ));
$this -> crypto -> method ( 'decrypt' )
-> with (
$this -> equalTo ( 'encryptedToken' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( '12345:TheOnlyAndOnlyOneTokenToResetThePassword' );
$this -> timeFactory
-> expects ( $this -> once ())
-> method ( 'getTime' )
-> willReturn ( 999999 );
$response = $this -> lostController -> resetform ( 'TheOnlyAndOnlyOneTokenToResetThePassword' , 'ValidTokenUser' );
2016-05-19 23:40:53 +03:00
$expectedResponse = new TemplateResponse ( 'core' ,
'error' ,
[
'errors' => [
[ 'error' => 'Couldn\'t reset password because the token is expired' ],
]
],
'guest' );
$this -> assertEquals ( $expectedResponse , $response );
}
2014-10-20 21:05:48 +04:00
2016-05-19 23:40:53 +03:00
public function testResetFormValidToken () {
2016-11-03 21:08:56 +03:00
$this -> existingUser -> method ( 'getLastLogin' )
-> willReturn ( 12344 );
$this -> userManager -> method ( 'get' )
2016-05-19 23:40:53 +03:00
-> with ( 'ValidTokenUser' )
2016-11-03 21:08:56 +03:00
-> willReturn ( $this -> existingUser );
2016-05-19 23:40:53 +03:00
$this -> timeFactory
-> expects ( $this -> once ())
-> method ( 'getTime' )
2016-11-03 21:08:56 +03:00
-> willReturn ( 12348 );
$this -> config -> method ( 'getUserValue' )
2016-08-23 16:01:38 +03:00
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
2016-11-03 21:08:56 +03:00
-> willReturn ( 'encryptedToken' );
$this -> crypto -> method ( 'decrypt' )
-> with (
$this -> equalTo ( 'encryptedToken' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( '12345:TheOnlyAndOnlyOneTokenToResetThePassword' );
2015-12-01 14:05:40 +03:00
$this -> urlGenerator
2014-10-20 21:05:48 +04:00
-> expects ( $this -> once ())
-> method ( 'linkToRouteAbsolute' )
2016-05-19 23:40:53 +03:00
-> with ( 'core.lost.setPassword' , array ( 'userId' => 'ValidTokenUser' , 'token' => 'TheOnlyAndOnlyOneTokenToResetThePassword' ))
2016-08-23 16:01:38 +03:00
-> will ( $this -> returnValue ( 'https://example.tld/index.php/lostpassword/' ));
2014-10-20 21:05:48 +04:00
2016-11-03 21:08:56 +03:00
$response = $this -> lostController -> resetform ( 'TheOnlyAndOnlyOneTokenToResetThePassword' , 'ValidTokenUser' );
2016-01-20 12:42:19 +03:00
$expectedResponse = new TemplateResponse ( 'core' ,
'lostpassword/resetpassword' ,
2014-10-20 21:05:48 +04:00
array (
2016-08-23 16:01:38 +03:00
'link' => 'https://example.tld/index.php/lostpassword/' ,
2014-10-20 21:05:48 +04:00
),
'guest' );
$this -> assertEquals ( $expectedResponse , $response );
}
2017-04-14 14:42:40 +03:00
public function testEmailUnsuccessful () {
2014-10-20 21:05:48 +04:00
$existingUser = 'ExistingUser' ;
$nonExistingUser = 'NonExistingUser' ;
2015-12-01 14:05:40 +03:00
$this -> userManager
2014-10-20 21:05:48 +04:00
-> expects ( $this -> any ())
-> method ( 'userExists' )
-> will ( $this -> returnValueMap ( array (
array ( true , $existingUser ),
array ( false , $nonExistingUser )
)));
2017-08-09 16:46:51 +03:00
$this -> userManager
-> method ( 'getByEmail' )
-> willReturn ([]);
2014-10-20 21:05:48 +04:00
// With a non existing user
$response = $this -> lostController -> email ( $nonExistingUser );
2017-04-14 14:42:40 +03:00
$expectedResponse = new JSONResponse ([
2015-02-01 19:46:48 +03:00
'status' => 'error' ,
'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
2017-04-14 14:42:40 +03:00
]);
$expectedResponse -> throttle ();
$this -> assertEquals ( $expectedResponse , $response );
2014-10-20 21:05:48 +04:00
// With no mail address
2015-12-01 14:05:40 +03:00
$this -> config
2014-10-20 21:05:48 +04:00
-> expects ( $this -> any ())
-> method ( 'getUserValue' )
-> with ( $existingUser , 'settings' , 'email' )
-> will ( $this -> returnValue ( null ));
$response = $this -> lostController -> email ( $existingUser );
2017-04-14 14:42:40 +03:00
$expectedResponse = new JSONResponse ([
2015-02-01 19:46:48 +03:00
'status' => 'error' ,
'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
2017-04-14 14:42:40 +03:00
]);
$expectedResponse -> throttle ();
$this -> assertEquals ( $expectedResponse , $response );
2014-10-20 21:05:48 +04:00
}
public function testEmailSuccessful () {
2015-12-01 14:05:40 +03:00
$this -> secureRandom
2014-10-20 21:05:48 +04:00
-> expects ( $this -> once ())
-> method ( 'generate' )
-> with ( '21' )
-> will ( $this -> returnValue ( 'ThisIsMaybeANotSoSecretToken!' ));
2015-12-01 14:05:40 +03:00
$this -> userManager
-> expects ( $this -> any ())
-> method ( 'get' )
-> with ( 'ExistingUser' )
-> willReturn ( $this -> existingUser );
$this -> timeFactory
2015-08-22 21:42:45 +03:00
-> expects ( $this -> once ())
-> method ( 'getTime' )
-> will ( $this -> returnValue ( 12348 ));
2015-12-01 14:05:40 +03:00
$this -> config
2014-10-20 21:05:48 +04:00
-> expects ( $this -> once ())
-> method ( 'setUserValue' )
2016-11-03 21:08:56 +03:00
-> with ( 'ExistingUser' , 'core' , 'lostpassword' , 'encryptedToken' );
2015-12-01 14:05:40 +03:00
$this -> urlGenerator
2014-10-20 21:05:48 +04:00
-> expects ( $this -> once ())
-> method ( 'linkToRouteAbsolute' )
2014-10-24 13:45:30 +04:00
-> with ( 'core.lost.resetform' , array ( 'userId' => 'ExistingUser' , 'token' => 'ThisIsMaybeANotSoSecretToken!' ))
2016-08-23 16:01:38 +03:00
-> will ( $this -> returnValue ( 'https://example.tld/index.php/lostpassword/' ));
2015-02-12 18:03:51 +03:00
$message = $this -> getMockBuilder ( '\OC\Mail\Message' )
-> disableOriginalConstructor () -> getMock ();
$message
-> expects ( $this -> at ( 0 ))
-> method ( 'setTo' )
-> with ([ 'test@example.com' => 'ExistingUser' ]);
$message
-> expects ( $this -> at ( 1 ))
-> method ( 'setFrom' )
-> with ([ 'lostpassword-noreply@localhost' => null ]);
2017-09-15 12:01:21 +03:00
2017-04-12 01:24:58 +03:00
$emailTemplate = $this -> createMock ( IEMailTemplate :: class );
$emailTemplate -> expects ( $this -> any ())
2017-04-19 01:09:25 +03:00
-> method ( 'renderHtml' )
2017-04-12 01:24:58 +03:00
-> willReturn ( 'HTML body' );
$emailTemplate -> expects ( $this -> any ())
-> method ( 'renderText' )
-> willReturn ( 'text body' );
2017-09-15 12:01:21 +03:00
$message
-> expects ( $this -> at ( 2 ))
-> method ( 'useTemplate' )
-> with ( $emailTemplate );
2015-12-01 14:05:40 +03:00
$this -> mailer
2015-02-12 18:03:51 +03:00
-> expects ( $this -> at ( 0 ))
2017-04-12 01:24:58 +03:00
-> method ( 'createEMailTemplate' )
-> willReturn ( $emailTemplate );
$this -> mailer
-> expects ( $this -> at ( 1 ))
2015-02-12 18:03:51 +03:00
-> method ( 'createMessage' )
-> will ( $this -> returnValue ( $message ));
2015-12-01 14:05:40 +03:00
$this -> mailer
2017-04-12 01:24:58 +03:00
-> expects ( $this -> at ( 2 ))
2015-02-12 18:03:51 +03:00
-> method ( 'send' )
-> with ( $message );
2014-10-20 21:05:48 +04:00
2016-11-03 21:08:56 +03:00
$this -> crypto -> method ( 'encrypt' )
-> with (
$this -> equalTo ( '12348:ThisIsMaybeANotSoSecretToken!' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( 'encryptedToken' );
2014-10-21 20:31:41 +04:00
$response = $this -> lostController -> email ( 'ExistingUser' );
2017-04-14 14:42:40 +03:00
$expectedResponse = new JSONResponse ([ 'status' => 'success' ]);
$expectedResponse -> throttle ();
$this -> assertEquals ( $expectedResponse , $response );
2014-10-20 21:05:48 +04:00
}
2017-03-28 21:39:36 +03:00
public function testEmailWithMailSuccessful () {
2015-12-01 14:05:40 +03:00
$this -> secureRandom
2015-02-12 18:03:51 +03:00
-> expects ( $this -> once ())
-> method ( 'generate' )
-> with ( '21' )
-> will ( $this -> returnValue ( 'ThisIsMaybeANotSoSecretToken!' ));
2015-12-01 14:05:40 +03:00
$this -> userManager
2017-03-28 21:39:36 +03:00
-> expects ( $this -> any ())
-> method ( 'get' )
-> with ( 'test@example.com' )
-> willReturn ( null );
$this -> userManager
-> expects ( $this -> any ())
-> method ( 'getByEmail' )
-> with ( 'test@example.com' )
-> willReturn ([ $this -> existingUser ]);
$this -> timeFactory
2015-02-12 18:03:51 +03:00
-> expects ( $this -> once ())
2017-03-28 21:39:36 +03:00
-> method ( 'getTime' )
-> will ( $this -> returnValue ( 12348 ));
$this -> config
-> expects ( $this -> once ())
-> method ( 'setUserValue' )
-> with ( 'ExistingUser' , 'core' , 'lostpassword' , 'encryptedToken' );
$this -> urlGenerator
-> expects ( $this -> once ())
-> method ( 'linkToRouteAbsolute' )
-> with ( 'core.lost.resetform' , array ( 'userId' => 'ExistingUser' , 'token' => 'ThisIsMaybeANotSoSecretToken!' ))
-> will ( $this -> returnValue ( 'https://example.tld/index.php/lostpassword/' ));
$message = $this -> getMockBuilder ( '\OC\Mail\Message' )
-> disableOriginalConstructor () -> getMock ();
$message
-> expects ( $this -> at ( 0 ))
-> method ( 'setTo' )
-> with ([ 'test@example.com' => 'ExistingUser' ]);
$message
-> expects ( $this -> at ( 1 ))
-> method ( 'setFrom' )
-> with ([ 'lostpassword-noreply@localhost' => null ]);
2017-09-15 12:01:21 +03:00
2017-04-12 01:24:58 +03:00
$emailTemplate = $this -> createMock ( IEMailTemplate :: class );
$emailTemplate -> expects ( $this -> any ())
2017-04-19 01:09:25 +03:00
-> method ( 'renderHtml' )
2017-04-12 01:24:58 +03:00
-> willReturn ( 'HTML body' );
$emailTemplate -> expects ( $this -> any ())
-> method ( 'renderText' )
-> willReturn ( 'text body' );
2017-09-15 12:01:21 +03:00
$message
-> expects ( $this -> at ( 2 ))
-> method ( 'useTemplate' )
-> with ( $emailTemplate );
2017-03-28 21:39:36 +03:00
$this -> mailer
-> expects ( $this -> at ( 0 ))
2017-04-12 01:24:58 +03:00
-> method ( 'createEMailTemplate' )
-> willReturn ( $emailTemplate );
$this -> mailer
-> expects ( $this -> at ( 1 ))
2017-03-28 21:39:36 +03:00
-> method ( 'createMessage' )
-> will ( $this -> returnValue ( $message ));
$this -> mailer
2017-04-12 01:24:58 +03:00
-> expects ( $this -> at ( 2 ))
2017-03-28 21:39:36 +03:00
-> method ( 'send' )
-> with ( $message );
$this -> crypto -> method ( 'encrypt' )
-> with (
$this -> equalTo ( '12348:ThisIsMaybeANotSoSecretToken!' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( 'encryptedToken' );
$response = $this -> lostController -> email ( 'test@example.com' );
2017-04-14 14:42:40 +03:00
$expectedResponse = new JSONResponse ([ 'status' => 'success' ]);
$expectedResponse -> throttle ();
$this -> assertEquals ( $expectedResponse , $response );
2017-03-28 21:39:36 +03:00
}
public function testEmailCantSendException () {
$this -> secureRandom
-> expects ( $this -> once ())
-> method ( 'generate' )
-> with ( '21' )
-> will ( $this -> returnValue ( 'ThisIsMaybeANotSoSecretToken!' ));
2015-12-01 14:05:40 +03:00
$this -> userManager
-> expects ( $this -> any ())
-> method ( 'get' )
-> with ( 'ExistingUser' )
-> willReturn ( $this -> existingUser );
$this -> config
2015-02-12 18:03:51 +03:00
-> expects ( $this -> once ())
-> method ( 'setUserValue' )
2016-11-03 21:08:56 +03:00
-> with ( 'ExistingUser' , 'core' , 'lostpassword' , 'encryptedToken' );
2015-12-01 14:05:40 +03:00
$this -> timeFactory
2015-08-22 21:42:45 +03:00
-> expects ( $this -> once ())
-> method ( 'getTime' )
-> will ( $this -> returnValue ( 12348 ));
2015-12-01 14:05:40 +03:00
$this -> urlGenerator
2015-02-12 18:03:51 +03:00
-> expects ( $this -> once ())
-> method ( 'linkToRouteAbsolute' )
-> with ( 'core.lost.resetform' , array ( 'userId' => 'ExistingUser' , 'token' => 'ThisIsMaybeANotSoSecretToken!' ))
2016-08-23 16:01:38 +03:00
-> will ( $this -> returnValue ( 'https://example.tld/index.php/lostpassword/' ));
2016-11-03 21:08:56 +03:00
$message = $this -> createMock ( Message :: class );
2015-02-12 18:03:51 +03:00
$message
-> expects ( $this -> at ( 0 ))
-> method ( 'setTo' )
-> with ([ 'test@example.com' => 'ExistingUser' ]);
$message
-> expects ( $this -> at ( 1 ))
-> method ( 'setFrom' )
-> with ([ 'lostpassword-noreply@localhost' => null ]);
2017-09-15 12:01:21 +03:00
2017-04-12 01:24:58 +03:00
$emailTemplate = $this -> createMock ( IEMailTemplate :: class );
$emailTemplate -> expects ( $this -> any ())
2017-04-19 01:09:25 +03:00
-> method ( 'renderHtml' )
2017-04-12 01:24:58 +03:00
-> willReturn ( 'HTML body' );
$emailTemplate -> expects ( $this -> any ())
-> method ( 'renderText' )
-> willReturn ( 'text body' );
2017-09-15 12:01:21 +03:00
$message
-> expects ( $this -> at ( 2 ))
-> method ( 'useTemplate' )
-> with ( $emailTemplate );
2015-12-01 14:05:40 +03:00
$this -> mailer
2015-02-12 18:03:51 +03:00
-> expects ( $this -> at ( 0 ))
2017-04-12 01:24:58 +03:00
-> method ( 'createEMailTemplate' )
-> willReturn ( $emailTemplate );
$this -> mailer
-> expects ( $this -> at ( 1 ))
2015-02-12 18:03:51 +03:00
-> method ( 'createMessage' )
-> will ( $this -> returnValue ( $message ));
2015-12-01 14:05:40 +03:00
$this -> mailer
2017-04-12 01:24:58 +03:00
-> expects ( $this -> at ( 2 ))
2015-02-12 18:03:51 +03:00
-> method ( 'send' )
-> with ( $message )
-> will ( $this -> throwException ( new \Exception ()));
2016-11-03 21:08:56 +03:00
$this -> crypto -> method ( 'encrypt' )
-> with (
$this -> equalTo ( '12348:ThisIsMaybeANotSoSecretToken!' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( 'encryptedToken' );
2015-02-12 18:03:51 +03:00
$response = $this -> lostController -> email ( 'ExistingUser' );
2017-04-14 14:42:40 +03:00
$expectedResponse = new JSONResponse ([ 'status' => 'error' , 'msg' => 'Couldn\'t send reset email. Please contact your administrator.' ]);
$expectedResponse -> throttle ();
$this -> assertEquals ( $expectedResponse , $response );
2015-02-12 18:03:51 +03:00
}
2014-10-20 21:05:48 +04:00
public function testSetPasswordUnsuccessful () {
2016-11-03 21:08:56 +03:00
$this -> config -> method ( 'getUserValue' )
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
-> willReturn ( 'encryptedData' );
$this -> existingUser -> method ( 'getLastLogin' )
-> will ( $this -> returnValue ( 12344 ));
$this -> existingUser -> expects ( $this -> once ())
-> method ( 'setPassword' )
-> with ( 'NewPassword' )
-> willReturn ( false );
$this -> userManager -> method ( 'get' )
-> with ( 'ValidTokenUser' )
-> willReturn ( $this -> existingUser );
$this -> config -> expects ( $this -> never ())
-> method ( 'deleteUserValue' );
$this -> timeFactory -> method ( 'getTime' )
-> will ( $this -> returnValue ( 12348 ));
2014-10-20 21:05:48 +04:00
2016-11-03 21:08:56 +03:00
$this -> crypto -> method ( 'decrypt' )
-> with (
$this -> equalTo ( 'encryptedData' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( '12345:TheOnlyAndOnlyOneTokenToResetThePassword' );
2014-10-20 21:05:48 +04:00
2016-11-03 21:08:56 +03:00
$response = $this -> lostController -> setPassword ( 'TheOnlyAndOnlyOneTokenToResetThePassword' , 'ValidTokenUser' , 'NewPassword' , true );
$expectedResponse = array ( 'status' => 'error' , 'msg' => '' );
2014-10-20 21:05:48 +04:00
$this -> assertSame ( $expectedResponse , $response );
}
public function testSetPasswordSuccessful () {
2016-11-03 21:08:56 +03:00
$this -> config -> method ( 'getUserValue' )
2016-08-23 16:01:38 +03:00
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
2016-11-03 21:08:56 +03:00
-> willReturn ( 'encryptedData' );
$this -> existingUser -> method ( 'getLastLogin' )
2015-08-22 21:42:45 +03:00
-> will ( $this -> returnValue ( 12344 ));
2016-11-03 21:08:56 +03:00
$this -> existingUser -> expects ( $this -> once ())
2014-10-20 21:05:48 +04:00
-> method ( 'setPassword' )
-> with ( 'NewPassword' )
2016-11-03 21:08:56 +03:00
-> willReturn ( true );
$this -> userManager -> method ( 'get' )
2014-10-20 21:05:48 +04:00
-> with ( 'ValidTokenUser' )
2016-11-03 21:08:56 +03:00
-> willReturn ( $this -> existingUser );
$this -> config -> expects ( $this -> once ())
2014-10-20 21:05:48 +04:00
-> method ( 'deleteUserValue' )
2016-08-23 16:01:38 +03:00
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' );
2016-11-03 21:08:56 +03:00
$this -> timeFactory -> method ( 'getTime' )
2015-08-22 21:42:45 +03:00
-> will ( $this -> returnValue ( 12348 ));
2014-10-20 21:05:48 +04:00
2016-11-03 21:08:56 +03:00
$this -> crypto -> method ( 'decrypt' )
-> with (
$this -> equalTo ( 'encryptedData' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( '12345:TheOnlyAndOnlyOneTokenToResetThePassword' );
2014-10-20 21:05:48 +04:00
$response = $this -> lostController -> setPassword ( 'TheOnlyAndOnlyOneTokenToResetThePassword' , 'ValidTokenUser' , 'NewPassword' , true );
2018-06-19 18:02:20 +03:00
$expectedResponse = array ( 'user' => 'ValidTokenUser' , 'status' => 'success' );
2014-10-20 21:05:48 +04:00
$this -> assertSame ( $expectedResponse , $response );
}
2015-02-01 19:34:03 +03:00
2015-08-22 21:42:45 +03:00
public function testSetPasswordExpiredToken () {
2016-11-03 21:08:56 +03:00
$this -> config -> method ( 'getUserValue' )
2016-08-23 16:01:38 +03:00
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
2016-11-03 21:08:56 +03:00
-> willReturn ( 'encryptedData' );
$this -> userManager -> method ( 'get' )
2015-08-22 21:42:45 +03:00
-> with ( 'ValidTokenUser' )
2016-11-03 21:08:56 +03:00
-> willReturn ( $this -> existingUser );
$this -> timeFactory -> method ( 'getTime' )
2018-08-31 10:26:09 +03:00
-> willReturn ( 617146 );
2016-11-03 21:08:56 +03:00
$this -> crypto -> method ( 'decrypt' )
-> with (
$this -> equalTo ( 'encryptedData' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( '12345:TheOnlyAndOnlyOneTokenToResetThePassword' );
2015-08-22 21:42:45 +03:00
$response = $this -> lostController -> setPassword ( 'TheOnlyAndOnlyOneTokenToResetThePassword' , 'ValidTokenUser' , 'NewPassword' , true );
$expectedResponse = [
'status' => 'error' ,
'msg' => 'Couldn\'t reset password because the token is expired' ,
];
$this -> assertSame ( $expectedResponse , $response );
}
public function testSetPasswordInvalidDataInDb () {
2016-11-03 21:08:56 +03:00
$this -> config -> method ( 'getUserValue' )
2016-08-23 16:01:38 +03:00
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
2016-11-03 21:08:56 +03:00
-> willReturn ( 'invalidEncryptedData' );
2015-12-01 14:05:40 +03:00
$this -> userManager
2015-08-22 21:42:45 +03:00
-> method ( 'get' )
-> with ( 'ValidTokenUser' )
2016-11-03 21:08:56 +03:00
-> willReturn ( $this -> existingUser );
$this -> crypto -> method ( 'decrypt' )
-> with (
$this -> equalTo ( 'invalidEncryptedData' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( 'TheOnlyAndOnlyOneTokenToResetThePassword' );
2015-08-22 21:42:45 +03:00
$response = $this -> lostController -> setPassword ( 'TheOnlyAndOnlyOneTokenToResetThePassword' , 'ValidTokenUser' , 'NewPassword' , true );
$expectedResponse = [
'status' => 'error' ,
'msg' => 'Couldn\'t reset password because the token is invalid' ,
];
$this -> assertSame ( $expectedResponse , $response );
}
public function testSetPasswordExpiredTokenDueToLogin () {
2016-11-03 21:08:56 +03:00
$this -> config -> method ( 'getUserValue' )
2016-08-23 16:01:38 +03:00
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
2016-11-03 21:08:56 +03:00
-> willReturn ( 'encryptedData' );
$this -> existingUser -> method ( 'getLastLogin' )
2015-08-22 21:42:45 +03:00
-> will ( $this -> returnValue ( 12346 ));
2015-12-01 14:05:40 +03:00
$this -> userManager
2015-08-22 21:42:45 +03:00
-> method ( 'get' )
-> with ( 'ValidTokenUser' )
2016-11-03 21:08:56 +03:00
-> willReturn ( $this -> existingUser );
2015-12-01 14:05:40 +03:00
$this -> timeFactory
2015-08-22 21:42:45 +03:00
-> method ( 'getTime' )
-> will ( $this -> returnValue ( 12345 ));
2016-11-03 21:08:56 +03:00
$this -> crypto -> method ( 'decrypt' )
-> with (
$this -> equalTo ( 'encryptedData' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( '12345:TheOnlyAndOnlyOneTokenToResetThePassword' );
2015-08-22 21:42:45 +03:00
$response = $this -> lostController -> setPassword ( 'TheOnlyAndOnlyOneTokenToResetThePassword' , 'ValidTokenUser' , 'NewPassword' , true );
$expectedResponse = [
'status' => 'error' ,
'msg' => 'Couldn\'t reset password because the token is expired' ,
];
$this -> assertSame ( $expectedResponse , $response );
}
2015-02-01 19:34:03 +03:00
public function testIsSetPasswordWithoutTokenFailing () {
2016-11-03 21:08:56 +03:00
$this -> config -> method ( 'getUserValue' )
2016-08-23 16:01:38 +03:00
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
2018-01-13 15:58:06 +03:00
-> willReturn ( 'aValidtoken' );
2016-11-03 21:08:56 +03:00
$this -> userManager -> method ( 'get' )
-> with ( 'ValidTokenUser' )
-> willReturn ( $this -> existingUser );
$this -> crypto -> method ( 'decrypt' )
-> with (
2018-01-13 15:58:06 +03:00
$this -> equalTo ( 'aValidtoken' ),
2016-11-03 21:08:56 +03:00
$this -> equalTo ( 'test@example.comSECRET' )
) -> willThrowException ( new \Exception ());
2015-02-01 19:34:03 +03:00
$response = $this -> lostController -> setPassword ( '' , 'ValidTokenUser' , 'NewPassword' , true );
2015-02-01 19:46:48 +03:00
$expectedResponse = [
'status' => 'error' ,
'msg' => 'Couldn\'t reset password because the token is invalid'
];
2015-02-01 19:34:03 +03:00
$this -> assertSame ( $expectedResponse , $response );
}
2017-08-18 14:03:40 +03:00
public function testSetPasswordForDisabledUser () {
$user = $this -> createMock ( IUser :: class );
$user -> expects ( $this -> any ())
-> method ( 'isEnabled' )
-> willReturn ( false );
$user -> expects ( $this -> never ())
-> method ( 'setPassword' );
$this -> config -> method ( 'getUserValue' )
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
-> willReturn ( 'encryptedData' );
$this -> userManager -> method ( 'get' )
-> with ( 'DisabledUser' )
-> willReturn ( $this -> existingUser );
$response = $this -> lostController -> setPassword ( 'TheOnlyAndOnlyOneTokenToResetThePassword' , 'DisabledUser' , 'NewPassword' , true );
$expectedResponse = [
'status' => 'error' ,
'msg' => 'Couldn\'t reset password because the token is invalid'
];
$this -> assertSame ( $expectedResponse , $response );
}
2016-11-03 21:08:56 +03:00
public function testSendEmailNoEmail () {
$user = $this -> createMock ( IUser :: class );
2017-08-18 14:03:40 +03:00
$user -> expects ( $this -> any ())
-> method ( 'isEnabled' )
-> willReturn ( true );
2016-11-03 21:08:56 +03:00
$this -> userManager -> method ( 'userExists' )
-> with ( 'ExistingUser' )
-> willReturn ( true );
$this -> userManager -> method ( 'get' )
-> with ( 'ExistingUser' )
-> willReturn ( $user );
$response = $this -> lostController -> email ( 'ExistingUser' );
2017-04-14 14:42:40 +03:00
$expectedResponse = new JSONResponse ([ 'status' => 'error' , 'msg' => 'Could not send reset email because there is no email address for this username. Please contact your administrator.' ]);
$expectedResponse -> throttle ();
$this -> assertEquals ( $expectedResponse , $response );
2016-11-03 21:08:56 +03:00
}
2018-08-10 17:40:09 +03:00
public function testSetPasswordEncryptionDontProceedPerUserKey () {
/** @var IEncryptionModule|PHPUnit_Framework_MockObject_MockObject $encryptionModule */
$encryptionModule = $this -> createMock ( IEncryptionModule :: class );
$encryptionModule -> expects ( $this -> once ()) -> method ( 'needDetailedAccessList' ) -> willReturn ( true );
$this -> encryptionManager -> expects ( $this -> once ()) -> method ( 'getEncryptionModules' )
-> willReturn ([ 0 => [ 'callback' => function () use ( $encryptionModule ) { return $encryptionModule ; }]]);
2016-11-03 21:08:56 +03:00
$response = $this -> lostController -> setPassword ( 'myToken' , 'user' , 'newpass' , false );
$expectedResponse = [ 'status' => 'error' , 'msg' => '' , 'encryption' => true ];
$this -> assertSame ( $expectedResponse , $response );
}
2018-08-10 17:40:09 +03:00
public function testSetPasswordDontProceedMasterKey () {
$encryptionModule = $this -> createMock ( IEncryptionModule :: class );
$encryptionModule -> expects ( $this -> once ()) -> method ( 'needDetailedAccessList' ) -> willReturn ( false );
$this -> encryptionManager -> expects ( $this -> once ()) -> method ( 'getEncryptionModules' )
-> willReturn ([ 0 => [ 'callback' => function () use ( $encryptionModule ) { return $encryptionModule ; }]]);
$this -> config -> method ( 'getUserValue' )
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' , null )
-> willReturn ( 'encryptedData' );
$this -> existingUser -> method ( 'getLastLogin' )
-> will ( $this -> returnValue ( 12344 ));
$this -> existingUser -> expects ( $this -> once ())
-> method ( 'setPassword' )
-> with ( 'NewPassword' )
-> willReturn ( true );
$this -> userManager -> method ( 'get' )
-> with ( 'ValidTokenUser' )
-> willReturn ( $this -> existingUser );
$this -> config -> expects ( $this -> once ())
-> method ( 'deleteUserValue' )
-> with ( 'ValidTokenUser' , 'core' , 'lostpassword' );
$this -> timeFactory -> method ( 'getTime' )
-> will ( $this -> returnValue ( 12348 ));
$this -> crypto -> method ( 'decrypt' )
-> with (
$this -> equalTo ( 'encryptedData' ),
$this -> equalTo ( 'test@example.comSECRET' )
) -> willReturn ( '12345:TheOnlyAndOnlyOneTokenToResetThePassword' );
$response = $this -> lostController -> setPassword ( 'TheOnlyAndOnlyOneTokenToResetThePassword' , 'ValidTokenUser' , 'NewPassword' , false );
$expectedResponse = array ( 'user' => 'ValidTokenUser' , 'status' => 'success' );
$this -> assertSame ( $expectedResponse , $response );
}
2018-08-18 17:51:59 +03:00
public function testTwoUsersWithSameEmail () {
$user1 = $this -> createMock ( IUser :: class );
$user1 -> expects ( $this -> any ())
-> method ( 'getEMailAddress' )
-> willReturn ( 'test@example.com' );
$user1 -> expects ( $this -> any ())
-> method ( 'getUID' )
-> willReturn ( 'User1' );
$user1 -> expects ( $this -> any ())
-> method ( 'isEnabled' )
-> willReturn ( true );
$user2 = $this -> createMock ( IUser :: class );
$user2 -> expects ( $this -> any ())
-> method ( 'getEMailAddress' )
-> willReturn ( 'test@example.com' );
$user2 -> expects ( $this -> any ())
-> method ( 'getUID' )
-> willReturn ( 'User2' );
$user2 -> expects ( $this -> any ())
-> method ( 'isEnabled' )
-> willReturn ( true );
$this -> userManager
-> method ( 'get' )
-> willReturn ( null );
$this -> userManager
-> method ( 'getByEmail' )
-> willReturn ([ $user1 , $user2 ]);
// request password reset for test@example.com
$response = $this -> lostController -> email ( 'test@example.com' );
$expectedResponse = new JSONResponse ([
'status' => 'error' ,
'msg' => 'Couldn\'t send reset email. Please make sure your username is correct.'
]);
$expectedResponse -> throttle ();
$this -> assertEquals ( $expectedResponse , $response );
}
public function testTwoUsersWithSameEmailOneDisabled () {
$user1 = $this -> createMock ( IUser :: class );
$user1 -> expects ( $this -> any ())
-> method ( 'getEMailAddress' )
-> willReturn ( 'test@example.com' );
$user1 -> expects ( $this -> any ())
-> method ( 'getUID' )
-> willReturn ( 'User1' );
$user1 -> expects ( $this -> any ())
-> method ( 'isEnabled' )
-> willReturn ( true );
$user2 = $this -> createMock ( IUser :: class );
$user2 -> expects ( $this -> any ())
-> method ( 'getEMailAddress' )
-> willReturn ( 'test@example.com' );
$user2 -> expects ( $this -> any ())
-> method ( 'getUID' )
-> willReturn ( 'User2' );
$user2 -> expects ( $this -> any ())
-> method ( 'isEnabled' )
-> willReturn ( false );
$this -> userManager
-> method ( 'get' )
-> willReturn ( null );
$this -> userManager
-> method ( 'getByEmail' )
-> willReturn ([ $user1 , $user2 ]);
// request password reset for test@example.com
$response = $this -> lostController -> email ( 'test@example.com' );
$expectedResponse = new JSONResponse ([
'status' => 'success'
]);
$expectedResponse -> throttle ();
$this -> assertEquals ( $expectedResponse , $response );
}
2014-10-20 21:05:48 +04:00
}