]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add more tests for OC\Core\Controller\LostController
authorJulius Haertl <jus@bitgrid.net>
Thu, 19 May 2016 20:40:53 +0000 (22:40 +0200)
committerJulius Haertl <jus@bitgrid.net>
Mon, 23 May 2016 14:48:10 +0000 (16:48 +0200)
- remove testResetFormUnsuccessful as it is now splitted up in different test cases
- add testResetFormInvalidToken to check if timestamp and token are present
- add testResetFormInvalidTokenMatch to check if the saved token matches the provided
- add testResetFormExpiredToken to check if expiration detection works
- add testResetFormValidToken to check if detection of valid tokens works

tests/Core/Controller/LostControllerTest.php

index ca63c3404eb696a3c42a874fa12700e19d741976..492a04bcfde2736163c3d63f5fb5d0313772b058 100644 (file)
@@ -114,14 +114,115 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase {
                );
        }
 
-       public function testResetFormUnsuccessful() {
+       public function testResetFormInvalidToken() {
                $userId = 'admin';
                $token = 'MySecretToken';
+               $response = $this->lostController->resetform($token, $userId);
+               $expectedResponse = new TemplateResponse('core',
+                       'error',
+                       [
+                               'errors' => [
+                                       ['error' => 'Couldn\'t reset password because the token is invalid'],
+                               ]
+                       ],
+                       'guest');
+               $this->assertEquals($expectedResponse, $response);
+       }
+
+       public function testResetFormInvalidTokenMatch() {
+               $this->config
+                       ->expects($this->once())
+                       ->method('getUserValue')
+                       ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
+                       ->will($this->returnValue('12345:TheOnlyAndOnlyOneTokenToResetThePassword'));
+               $user = $this->getMockBuilder('\OCP\IUser')
+                       ->disableOriginalConstructor()->getMock();
+               $user
+                       ->expects($this->once())
+                       ->method('getLastLogin')
+                       ->will($this->returnValue(12344));
+               $this->userManager
+                       ->expects($this->once())
+                       ->method('get')
+                       ->with('ValidTokenUser')
+                       ->will($this->returnValue($user));
+               $userId = 'ValidTokenUser';
+               $token = '12345:MySecretToken';
+               $response = $this->lostController->resetform($token, $userId);
+               $expectedResponse = new TemplateResponse('core',
+                       'error',
+                       [
+                               'errors' => [
+                                       ['error' => 'Couldn\'t reset password because the token is invalid'],
+                               ]
+                       ],
+                       'guest');
+               $this->assertEquals($expectedResponse, $response);
+       }
+
+
+       public function testResetFormExpiredToken() {
+               $userId = 'ValidTokenUser';
+               $token = '12345:TheOnlyAndOnlyOneTokenToResetThePassword';
+               $user = $this->getMockBuilder('\OCP\IUser')
+                       ->disableOriginalConstructor()->getMock();
+               $this->userManager
+                       ->expects($this->once())
+                       ->method('get')
+                       ->with('ValidTokenUser')
+                       ->will($this->returnValue($user));
+               $this->timeFactory
+                       ->expects($this->once())
+                       ->method('getTime')
+                       ->will($this->returnValue(12345*60*60*12));
+               $userId = 'ValidTokenUser';
+               $token = 'TheOnlyAndOnlyOneTokenToResetThePassword';
+               $this->config
+                       ->expects($this->once())
+                       ->method('getUserValue')
+                       ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
+                       ->will($this->returnValue('12345:TheOnlyAndOnlyOneTokenToResetThePassword'));
+               $response = $this->lostController->resetform($token, $userId);
+               $expectedResponse = new TemplateResponse('core',
+                       'error',
+                       [
+                               'errors' => [
+                                       ['error' => 'Couldn\'t reset password because the token is expired'],
+                               ]
+                       ],
+                       'guest');
+               $this->assertEquals($expectedResponse, $response);
+       }
 
+       public function testResetFormValidToken() {
+               $userId = 'ValidTokenUser';
+               $token = '12345:TheOnlyAndOnlyOneTokenToResetThePassword';
+               $user = $this->getMockBuilder('\OCP\IUser')
+                       ->disableOriginalConstructor()->getMock();
+               $user
+                       ->expects($this->once())
+                       ->method('getLastLogin')
+                       ->will($this->returnValue(12344));
+               $this->userManager
+                       ->expects($this->once())
+                       ->method('get')
+                       ->with('ValidTokenUser')
+                       ->will($this->returnValue($user));
+               $this->timeFactory
+                       ->expects($this->once())
+                       ->method('getTime')
+                       ->will($this->returnValue(12348));
+               $userId = 'ValidTokenUser';
+               $token = 'TheOnlyAndOnlyOneTokenToResetThePassword';
+               $this->config
+                       ->expects($this->once())
+                       ->method('getUserValue')
+                       ->with('ValidTokenUser', 'owncloud', 'lostpassword', null)
+                       ->will($this->returnValue('12345:TheOnlyAndOnlyOneTokenToResetThePassword'));
                $this->urlGenerator
                        ->expects($this->once())
                        ->method('linkToRouteAbsolute')
-                       ->with('core.lost.setPassword', array('userId' => 'admin', 'token' => 'MySecretToken'))
+                       ->with('core.lost.setPassword', array('userId' => 'ValidTokenUser', 'token' => 'TheOnlyAndOnlyOneTokenToResetThePassword'))
                        ->will($this->returnValue('https://ownCloud.com/index.php/lostpassword/'));
 
                $response = $this->lostController->resetform($token, $userId);
@@ -329,7 +430,7 @@ class LostControllerTest extends \PHPUnit_Framework_TestCase {
                        ->with('NewPassword')
                        ->will($this->returnValue(true));
                $this->userManager
-                       ->expects($this->once())
+                       ->expects($this->exactly(2))
                        ->method('get')
                        ->with('ValidTokenUser')
                        ->will($this->returnValue($user));