diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Core/Controller/LoginControllerTest.php | 57 | ||||
-rw-r--r-- | tests/Core/Controller/TokenControllerTest.php | 9 | ||||
-rw-r--r-- | tests/core/controller/TwoFactorChallengeControllerTest.php | 219 | ||||
-rw-r--r-- | tests/core/middleware/TwoFactorMiddlewareTest.php | 182 | ||||
-rw-r--r-- | tests/data/app/expected-info.json | 3 | ||||
-rw-r--r-- | tests/lib/Authentication/Token/DefaultTokenMapperTest.php | 4 | ||||
-rw-r--r-- | tests/lib/Authentication/Token/DefaultTokenProviderTest.php | 4 | ||||
-rw-r--r-- | tests/lib/Files/Cache/ScannerTest.php | 27 | ||||
-rw-r--r-- | tests/lib/Files/Mount/ObjectHomeMountProviderTest.php | 244 | ||||
-rw-r--r-- | tests/lib/Files/ObjectStore/MapperTest.php | 50 | ||||
-rw-r--r-- | tests/lib/User/SessionTest.php | 140 | ||||
-rw-r--r-- | tests/lib/authentication/twofactorauth/managertest.php | 187 | ||||
-rw-r--r-- | tests/settings/controller/AuthSettingsControllerTest.php | 5 |
13 files changed, 1108 insertions, 23 deletions
diff --git a/tests/Core/Controller/LoginControllerTest.php b/tests/Core/Controller/LoginControllerTest.php index 32902a01530..ea9d6a44148 100644 --- a/tests/Core/Controller/LoginControllerTest.php +++ b/tests/Core/Controller/LoginControllerTest.php @@ -21,6 +21,7 @@ namespace Tests\Core\Controller; +use OC\Authentication\TwoFactorAuth\Manager; use OC\Core\Controller\LoginController; use OCP\AppFramework\Http\RedirectResponse; use OCP\AppFramework\Http\TemplateResponse; @@ -47,6 +48,8 @@ class LoginControllerTest extends TestCase { private $userSession; /** @var IURLGenerator */ private $urlGenerator; + /** @var Manager */ + private $twoFactorManager; public function setUp() { parent::setUp(); @@ -58,6 +61,9 @@ class LoginControllerTest extends TestCase { ->disableOriginalConstructor() ->getMock(); $this->urlGenerator = $this->getMock('\\OCP\\IURLGenerator'); + $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager') + ->disableOriginalConstructor() + ->getMock(); $this->loginController = new LoginController( 'core', @@ -66,7 +72,8 @@ class LoginControllerTest extends TestCase { $this->config, $this->session, $this->userSession, - $this->urlGenerator + $this->urlGenerator, + $this->twoFactorManager ); } @@ -297,8 +304,15 @@ class LoginControllerTest extends TestCase { ->method('checkPassword') ->will($this->returnValue($user)); $this->userSession->expects($this->once()) + ->method('login') + ->with($user, $password); + $this->userSession->expects($this->once()) ->method('createSessionToken') - ->with($this->request, $user->getUID(), $password); + ->with($this->request, $user->getUID(), $user, $password); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->with($user) + ->will($this->returnValue(false)); $this->urlGenerator->expects($this->once()) ->method('linkToRoute') ->with('files.view.index') @@ -319,11 +333,11 @@ class LoginControllerTest extends TestCase { $this->userManager->expects($this->once()) ->method('checkPassword') - ->with('jane', $password) + ->with('Jane', $password) ->will($this->returnValue($user)); $this->userSession->expects($this->once()) ->method('createSessionToken') - ->with($this->request, $user->getUID(), $password); + ->with($this->request, $user->getUID(), 'Jane', $password); $this->userSession->expects($this->once()) ->method('isLoggedIn') ->with() @@ -334,7 +348,40 @@ class LoginControllerTest extends TestCase { ->will($this->returnValue($redirectUrl)); $expected = new \OCP\AppFramework\Http\RedirectResponse(urldecode($redirectUrl)); - $this->assertEquals($expected, $this->loginController->tryLogin($user->getUID(), $password, $originalUrl)); + $this->assertEquals($expected, $this->loginController->tryLogin('Jane', $password, $originalUrl)); + } + + public function testLoginWithTwoFactorEnforced() { + $user = $this->getMock('\OCP\IUser'); + $user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('john')); + $password = 'secret'; + $challengeUrl = 'challenge/url'; + + $this->userManager->expects($this->once()) + ->method('checkPassword') + ->will($this->returnValue($user)); + $this->userSession->expects($this->once()) + ->method('login') + ->with('john@doe.com', $password); + $this->userSession->expects($this->once()) + ->method('createSessionToken') + ->with($this->request, $user->getUID(), 'john@doe.com', $password); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->with($user) + ->will($this->returnValue(true)); + $this->twoFactorManager->expects($this->once()) + ->method('prepareTwoFactorLogin') + ->with($user); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('core.TwoFactorChallenge.selectChallenge') + ->will($this->returnValue($challengeUrl)); + + $expected = new \OCP\AppFramework\Http\RedirectResponse($challengeUrl); + $this->assertEquals($expected, $this->loginController->tryLogin('john@doe.com', $password, null)); } } diff --git a/tests/Core/Controller/TokenControllerTest.php b/tests/Core/Controller/TokenControllerTest.php index b600bfa5451..386140a8a4f 100644 --- a/tests/Core/Controller/TokenControllerTest.php +++ b/tests/Core/Controller/TokenControllerTest.php @@ -75,14 +75,21 @@ class TokenControllerTest extends TestCase { } public function testWithValidCredentials() { + $user = $this->getMock('\OCP\IUser'); $this->userManager->expects($this->once()) ->method('checkPassword') ->with('john', '123456') - ->will($this->returnValue(true)); + ->will($this->returnValue($user)); + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('john')); $this->secureRandom->expects($this->once()) ->method('generate') ->with(128) ->will($this->returnValue('verysecurerandomtoken')); + $this->tokenProvider->expects($this->once()) + ->method('generateToken') + ->with('verysecurerandomtoken', 'john', 'john', '123456', 'unknown client', \OC\Authentication\Token\IToken::PERMANENT_TOKEN); $expected = [ 'token' => 'verysecurerandomtoken' ]; diff --git a/tests/core/controller/TwoFactorChallengeControllerTest.php b/tests/core/controller/TwoFactorChallengeControllerTest.php new file mode 100644 index 00000000000..c65625ec329 --- /dev/null +++ b/tests/core/controller/TwoFactorChallengeControllerTest.php @@ -0,0 +1,219 @@ +<?php + +/** + * @author Christoph Wurst <christoph@owncloud.com> + * + * @copyright Copyright (c) 2016, 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/> + * + */ + +namespace OC\Core\Controller; + +use Test\TestCase; + +class TwoFactorChallengeControllerTest extends TestCase { + + private $request; + private $twoFactorManager; + private $userSession; + private $session; + private $urlGenerator; + + /** TwoFactorChallengeController */ + private $controller; + + protected function setUp() { + parent::setUp(); + + $this->request = $this->getMock('\OCP\IRequest'); + $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->session = $this->getMock('\OCP\ISession'); + $this->urlGenerator = $this->getMock('\OCP\IURLGenerator'); + + $this->controller = new TwoFactorChallengeController( + 'core', $this->request, $this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator + ); + } + + public function testSelectChallenge() { + $user = $this->getMock('\OCP\IUser'); + $providers = [ + 'prov1', + 'prov2', + ]; + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProviders') + ->with($user) + ->will($this->returnValue($providers)); + + $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorselectchallenge', [ + 'providers' => $providers, + ], 'guest'); + + $this->assertEquals($expected, $this->controller->selectChallenge()); + } + + public function testShowChallenge() { + $user = $this->getMock('\OCP\IUser'); + $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider') + ->disableOriginalConstructor() + ->getMock(); + $tmpl = $this->getMockBuilder('\OCP\Template') + ->disableOriginalConstructor() + ->getMock(); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProvider') + ->with($user, 'myprovider') + ->will($this->returnValue($provider)); + + $this->session->expects($this->once()) + ->method('exists') + ->with('two_factor_auth_error') + ->will($this->returnValue(true)); + $this->session->expects($this->once()) + ->method('remove') + ->with('two_factor_auth_error'); + $provider->expects($this->once()) + ->method('getTemplate') + ->with($user) + ->will($this->returnValue($tmpl)); + $tmpl->expects($this->once()) + ->method('fetchPage') + ->will($this->returnValue('<html/>')); + + $expected = new \OCP\AppFramework\Http\TemplateResponse('core', 'twofactorshowchallenge', [ + 'error' => true, + 'provider' => $provider, + 'template' => '<html/>', + ], 'guest'); + + $this->assertEquals($expected, $this->controller->showChallenge('myprovider')); + } + + public function testShowInvalidChallenge() { + $user = $this->getMock('\OCP\IUser'); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProvider') + ->with($user, 'myprovider') + ->will($this->returnValue(null)); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('core.TwoFactorChallenge.selectChallenge') + ->will($this->returnValue('select/challenge/url')); + + $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url'); + + $this->assertEquals($expected, $this->controller->showChallenge('myprovider')); + } + + public function testSolveChallenge() { + $user = $this->getMock('\OCP\IUser'); + $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider') + ->disableOriginalConstructor() + ->getMock(); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProvider') + ->with($user, 'myprovider') + ->will($this->returnValue($provider)); + + $this->twoFactorManager->expects($this->once()) + ->method('verifyChallenge') + ->with('myprovider', $user, 'token') + ->will($this->returnValue(true)); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index') + ->will($this->returnValue('files/index/url')); + + $expected = new \OCP\AppFramework\Http\RedirectResponse('files/index/url'); + $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token')); + } + + public function testSolveChallengeInvalidProvider() { + $user = $this->getMock('\OCP\IUser'); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProvider') + ->with($user, 'myprovider') + ->will($this->returnValue(null)); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('core.TwoFactorChallenge.selectChallenge') + ->will($this->returnValue('select/challenge/url')); + + $expected = new \OCP\AppFramework\Http\RedirectResponse('select/challenge/url'); + + $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token')); + } + + public function testSolveInvalidChallenge() { + $user = $this->getMock('\OCP\IUser'); + $provider = $this->getMockBuilder('\OCP\Authentication\TwoFactorAuth\IProvider') + ->disableOriginalConstructor() + ->getMock(); + + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('getProvider') + ->with($user, 'myprovider') + ->will($this->returnValue($provider)); + + $this->twoFactorManager->expects($this->once()) + ->method('verifyChallenge') + ->with('myprovider', $user, 'token') + ->will($this->returnValue(false)); + $this->session->expects($this->once()) + ->method('set') + ->with('two_factor_auth_error', true); + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('core.TwoFactorChallenge.showChallenge', [ + 'challengeProviderId' => 'myprovider', + ]) + ->will($this->returnValue('files/index/url')); + $provider->expects($this->once()) + ->method('getId') + ->will($this->returnValue('myprovider')); + + $expected = new \OCP\AppFramework\Http\RedirectResponse('files/index/url'); + $this->assertEquals($expected, $this->controller->solveChallenge('myprovider', 'token')); + } + +} diff --git a/tests/core/middleware/TwoFactorMiddlewareTest.php b/tests/core/middleware/TwoFactorMiddlewareTest.php new file mode 100644 index 00000000000..12136db7fdf --- /dev/null +++ b/tests/core/middleware/TwoFactorMiddlewareTest.php @@ -0,0 +1,182 @@ +<?php + +/** + * @author Christoph Wurst <christoph@owncloud.com> + * + * @copyright Copyright (c) 2016, 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/> + * + */ + +namespace OC\Core\Middleware; + +use Test\TestCase; + +class TwoFactorMiddlewareTest extends TestCase { + + private $twoFactorManager; + private $userSession; + private $session; + private $urlGenerator; + private $reflector; + + /** @var TwoFactorMiddleware */ + private $middleware; + + protected function setUp() { + parent::setUp(); + + $this->twoFactorManager = $this->getMockBuilder('\OC\Authentication\TwoFactorAuth\Manager') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession = $this->getMockBuilder('\OC\User\Session') + ->disableOriginalConstructor() + ->getMock(); + $this->session = $this->getMock('\OCP\ISession'); + $this->urlGenerator = $this->getMock('\OCP\IURLGenerator'); + $this->reflector = $this->getMock('\OCP\AppFramework\Utility\IControllerMethodReflector'); + + $this->middleware = new TwoFactorMiddleware($this->twoFactorManager, $this->userSession, $this->session, $this->urlGenerator, $this->reflector); + } + + public function testBeforeControllerNotLoggedIn() { + $this->reflector->expects($this->once()) + ->method('hasAnnotation') + ->with('PublicPage') + ->will($this->returnValue(false)); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->will($this->returnValue(false)); + + $this->userSession->expects($this->never()) + ->method('getUser'); + + $this->middleware->beforeController(null, 'index'); + } + + public function testBeforeControllerPublicPage() { + $this->reflector->expects($this->once()) + ->method('hasAnnotation') + ->with('PublicPage') + ->will($this->returnValue(true)); + $this->userSession->expects($this->never()) + ->method('isLoggedIn'); + + $this->middleware->beforeController(null, 'create'); + } + + public function testBeforeControllerNoTwoFactorCheckNeeded() { + $user = $this->getMock('\OCP\IUser'); + + $this->reflector->expects($this->once()) + ->method('hasAnnotation') + ->with('PublicPage') + ->will($this->returnValue(false)); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->will($this->returnValue(true)); + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->with($user) + ->will($this->returnValue(false)); + + $this->middleware->beforeController(null, 'index'); + } + + /** + * @expectedException \OC\Authentication\Exceptions\TwoFactorAuthRequiredException + */ + public function testBeforeControllerTwoFactorAuthRequired() { + $user = $this->getMock('\OCP\IUser'); + + $this->reflector->expects($this->once()) + ->method('hasAnnotation') + ->with('PublicPage') + ->will($this->returnValue(false)); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->will($this->returnValue(true)); + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->with($user) + ->will($this->returnValue(true)); + $this->twoFactorManager->expects($this->once()) + ->method('needsSecondFactor') + ->will($this->returnValue(true)); + + $this->middleware->beforeController(null, 'index'); + } + + /** + * @expectedException \OC\Authentication\Exceptions\UserAlreadyLoggedInException + */ + public function testBeforeControllerUserAlreadyLoggedIn() { + $user = $this->getMock('\OCP\IUser'); + + $this->reflector->expects($this->once()) + ->method('hasAnnotation') + ->with('PublicPage') + ->will($this->returnValue(false)); + $this->userSession->expects($this->once()) + ->method('isLoggedIn') + ->will($this->returnValue(true)); + $this->userSession->expects($this->once()) + ->method('getUser') + ->will($this->returnValue($user)); + $this->twoFactorManager->expects($this->once()) + ->method('isTwoFactorAuthenticated') + ->with($user) + ->will($this->returnValue(true)); + $this->twoFactorManager->expects($this->once()) + ->method('needsSecondFactor') + ->will($this->returnValue(false)); + + $twoFactorChallengeController = $this->getMockBuilder('\OC\Core\Controller\TwoFactorChallengeController') + ->disableOriginalConstructor() + ->getMock(); + $this->middleware->beforeController($twoFactorChallengeController, 'index'); + } + + public function testAfterExceptionTwoFactorAuthRequired() { + $ex = new \OC\Authentication\Exceptions\TwoFactorAuthRequiredException(); + + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('core.TwoFactorChallenge.selectChallenge') + ->will($this->returnValue('redirect/url')); + $expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url'); + + $this->assertEquals($expected, $this->middleware->afterException(null, 'index', $ex)); + } + + public function testAfterException() { + $ex = new \OC\Authentication\Exceptions\UserAlreadyLoggedInException(); + + $this->urlGenerator->expects($this->once()) + ->method('linkToRoute') + ->with('files.view.index') + ->will($this->returnValue('redirect/url')); + $expected = new \OCP\AppFramework\Http\RedirectResponse('redirect/url'); + + $this->assertEquals($expected, $this->middleware->afterException(null, 'index', $ex)); + } + +} diff --git a/tests/data/app/expected-info.json b/tests/data/app/expected-info.json index 81de5341efa..6d8d85ab552 100644 --- a/tests/data/app/expected-info.json +++ b/tests/data/app/expected-info.json @@ -75,5 +75,6 @@ "live-migration": [], "uninstall": [] }, - "background-jobs": [] + "background-jobs": [], + "two-factor-providers": [] } diff --git a/tests/lib/Authentication/Token/DefaultTokenMapperTest.php b/tests/lib/Authentication/Token/DefaultTokenMapperTest.php index 9179e23bfb2..5d49f75aaa4 100644 --- a/tests/lib/Authentication/Token/DefaultTokenMapperTest.php +++ b/tests/lib/Authentication/Token/DefaultTokenMapperTest.php @@ -57,6 +57,7 @@ class DefaultTokenMapperTest extends TestCase { $qb->delete('authtoken')->execute(); $qb->insert('authtoken')->values([ 'uid' => $qb->createNamedParameter('user1'), + 'login_name' => $qb->createNamedParameter('User1'), 'password' => $qb->createNamedParameter('a75c7116460c082912d8f6860a850904|3nz5qbG1nNSLLi6V|c55365a0e54cfdfac4a175bcf11a7612aea74492277bba6e5d96a24497fa9272488787cb2f3ad34d8b9b8060934fce02f008d371df3ff3848f4aa61944851ff0'), 'name' => $qb->createNamedParameter('Firefox on Linux'), 'token' => $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'), @@ -65,6 +66,7 @@ class DefaultTokenMapperTest extends TestCase { ])->execute(); $qb->insert('authtoken')->values([ 'uid' => $qb->createNamedParameter('user2'), + 'login_name' => $qb->createNamedParameter('User2'), 'password' => $qb->createNamedParameter('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f'), 'name' => $qb->createNamedParameter('Firefox on Android'), 'token' => $qb->createNamedParameter('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b'), @@ -73,6 +75,7 @@ class DefaultTokenMapperTest extends TestCase { ])->execute(); $qb->insert('authtoken')->values([ 'uid' => $qb->createNamedParameter('user1'), + 'login_name' => $qb->createNamedParameter('User1'), 'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'), 'name' => $qb->createNamedParameter('Iceweasel on Linux'), 'token' => $qb->createNamedParameter('47af8697ba590fb82579b5f1b3b6e8066773a62100abbe0db09a289a62f5d980dc300fa3d98b01d7228468d1ab05c1aa14c8d14bd5b6eee9cdf1ac14864680c3'), @@ -118,6 +121,7 @@ class DefaultTokenMapperTest extends TestCase { $token = '1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b'; $token = new DefaultToken(); $token->setUid('user2'); + $token->setLoginName('User2'); $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f'); $token->setName('Firefox on Android'); $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b'); diff --git a/tests/lib/Authentication/Token/DefaultTokenProviderTest.php b/tests/lib/Authentication/Token/DefaultTokenProviderTest.php index 8af5e1e933a..e04424e4628 100644 --- a/tests/lib/Authentication/Token/DefaultTokenProviderTest.php +++ b/tests/lib/Authentication/Token/DefaultTokenProviderTest.php @@ -61,6 +61,7 @@ class DefaultTokenProviderTest extends TestCase { public function testGenerateToken() { $token = 'token'; $uid = 'user'; + $user = 'User'; $password = 'passme'; $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12' @@ -70,6 +71,7 @@ class DefaultTokenProviderTest extends TestCase { $toInsert = new DefaultToken(); $toInsert->setUid($uid); + $toInsert->setLoginName($user); $toInsert->setPassword('encryptedpassword'); $toInsert->setName($name); $toInsert->setToken(hash('sha512', $token . '1f4h9s')); @@ -88,7 +90,7 @@ class DefaultTokenProviderTest extends TestCase { ->method('insert') ->with($this->equalTo($toInsert)); - $actual = $this->tokenProvider->generateToken($token, $uid, $password, $name, $type); + $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type); $this->assertEquals($toInsert, $actual); } diff --git a/tests/lib/Files/Cache/ScannerTest.php b/tests/lib/Files/Cache/ScannerTest.php index 4a93f3ee014..b44b6f5d0f5 100644 --- a/tests/lib/Files/Cache/ScannerTest.php +++ b/tests/lib/Files/Cache/ScannerTest.php @@ -7,6 +7,7 @@ */ namespace Test\Files\Cache; + use OC\Files\Cache\CacheEntry; /** @@ -150,6 +151,32 @@ class ScannerTest extends \Test\TestCase { $this->assertFalse($this->cache->getIncomplete()); } + function testBackgroundScanOnlyRecurseIncomplete() { + $this->fillTestFolders(); + $this->storage->mkdir('folder2'); + $this->storage->file_put_contents('folder2/bar.txt', 'foobar'); + + $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_SHALLOW); + $this->assertFalse($this->cache->inCache('folder/bar.txt')); + $this->assertFalse($this->cache->inCache('folder/2bar.txt')); + $this->assertFalse($this->cache->inCache('folder2/bar.txt')); + $this->cache->put('folder2', ['size' => 1]); // mark as complete + + $cachedData = $this->cache->get(''); + $this->assertEquals(-1, $cachedData['size']); + + $this->scanner->scan('', \OC\Files\Cache\Scanner::SCAN_RECURSIVE_INCOMPLETE, \OC\Files\Cache\Scanner::REUSE_ETAG | \OC\Files\Cache\Scanner::REUSE_SIZE); + + $this->assertTrue($this->cache->inCache('folder/bar.txt')); + $this->assertTrue($this->cache->inCache('folder/bar.txt')); + $this->assertFalse($this->cache->inCache('folder2/bar.txt')); + + $cachedData = $this->cache->get(''); + $this->assertNotEquals(-1, $cachedData['size']); + + $this->assertFalse($this->cache->getIncomplete()); + } + public function testReuseExisting() { $this->fillTestFolders(); diff --git a/tests/lib/Files/Mount/ObjectHomeMountProviderTest.php b/tests/lib/Files/Mount/ObjectHomeMountProviderTest.php new file mode 100644 index 00000000000..5d987f0d059 --- /dev/null +++ b/tests/lib/Files/Mount/ObjectHomeMountProviderTest.php @@ -0,0 +1,244 @@ +<?php + +namespace Test\Files\Mount; + +use OC\Files\Mount\ObjectHomeMountProvider; +use OCP\Files\Storage\IStorageFactory; +use OCP\IConfig; +use OCP\IUser; + +class ObjectHomeMountProviderTest extends \Test\TestCase { + + /** @var ObjectHomeMountProvider */ + protected $provider; + + /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */ + protected $config; + + /** @var IUser|\PHPUnit_Framework_MockObject_MockObject */ + protected $user; + + /** @var IStorageFactory|\PHPUnit_Framework_MockObject_MockObject */ + protected $loader; + + public function setUp() { + parent::setUp(); + + $this->config = $this->getMock('OCP\IConfig'); + $this->user = $this->getMock('OCP\IUser'); + $this->loader = $this->getMock('OCP\Files\Storage\IStorageFactory'); + + $this->provider = new ObjectHomeMountProvider($this->config); + } + + public function testSingleBucket() { + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with($this->equalTo('objectstore'), '') + ->willReturn([ + 'class' => 'Test\Files\Mount\FakeObjectStore', + ]); + + $this->user->expects($this->never())->method($this->anything()); + $this->loader->expects($this->never())->method($this->anything()); + + $config = $this->invokePrivate($this->provider, 'getSingleBucketObjectStoreConfig', [$this->user, $this->loader]); + + $this->assertArrayHasKey('class', $config); + $this->assertEquals($config['class'], 'Test\Files\Mount\FakeObjectStore'); + $this->assertArrayHasKey('arguments', $config); + $this->assertArrayHasKey('user', $config['arguments']); + $this->assertSame($this->user, $config['arguments']['user']); + $this->assertArrayHasKey('objectstore', $config['arguments']); + $this->assertInstanceOf('Test\Files\Mount\FakeObjectStore', $config['arguments']['objectstore']); + } + + public function testMultiBucket() { + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with($this->equalTo('objectstore_multibucket'), '') + ->willReturn([ + 'class' => 'Test\Files\Mount\FakeObjectStore', + ]); + + $this->user->method('getUID') + ->willReturn('uid'); + $this->loader->expects($this->never())->method($this->anything()); + + $this->config->expects($this->once()) + ->method('getUserValue') + ->with( + $this->equalTo('uid'), + $this->equalTo('homeobjectstore'), + $this->equalTo('bucket'), + $this->equalTo(null) + )->willReturn(null); + + $this->config->expects($this->once()) + ->method('setUserValue') + ->with( + $this->equalTo('uid'), + $this->equalTo('homeobjectstore'), + $this->equalTo('bucket'), + $this->equalTo('987'), + $this->equalTo(null) + ); + + $config = $this->invokePrivate($this->provider, 'getMultiBucketObjectStoreConfig', [$this->user, $this->loader]); + + $this->assertArrayHasKey('class', $config); + $this->assertEquals($config['class'], 'Test\Files\Mount\FakeObjectStore'); + $this->assertArrayHasKey('arguments', $config); + $this->assertArrayHasKey('user', $config['arguments']); + $this->assertSame($this->user, $config['arguments']['user']); + $this->assertArrayHasKey('objectstore', $config['arguments']); + $this->assertInstanceOf('Test\Files\Mount\FakeObjectStore', $config['arguments']['objectstore']); + $this->assertArrayHasKey('bucket', $config['arguments']); + $this->assertEquals('987', $config['arguments']['bucket']); + } + + public function testMultiBucketWithPrefix() { + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with($this->equalTo('objectstore_multibucket'), '') + ->willReturn([ + 'class' => 'Test\Files\Mount\FakeObjectStore', + 'arguments' => [ + 'bucket' => 'myBucketPrefix', + ], + ]); + + $this->user->method('getUID') + ->willReturn('uid'); + $this->loader->expects($this->never())->method($this->anything()); + + $this->config->expects($this->once()) + ->method('getUserValue') + ->with( + $this->equalTo('uid'), + $this->equalTo('homeobjectstore'), + $this->equalTo('bucket'), + $this->equalTo(null) + )->willReturn(null); + + $this->config->expects($this->once()) + ->method('setUserValue') + ->with( + $this->equalTo('uid'), + $this->equalTo('homeobjectstore'), + $this->equalTo('bucket'), + $this->equalTo('myBucketPrefix987'), + $this->equalTo(null) + ); + + $config = $this->invokePrivate($this->provider, 'getMultiBucketObjectStoreConfig', [$this->user, $this->loader]); + + $this->assertArrayHasKey('class', $config); + $this->assertEquals($config['class'], 'Test\Files\Mount\FakeObjectStore'); + $this->assertArrayHasKey('arguments', $config); + $this->assertArrayHasKey('user', $config['arguments']); + $this->assertSame($this->user, $config['arguments']['user']); + $this->assertArrayHasKey('objectstore', $config['arguments']); + $this->assertInstanceOf('Test\Files\Mount\FakeObjectStore', $config['arguments']['objectstore']); + $this->assertArrayHasKey('bucket', $config['arguments']); + $this->assertEquals('myBucketPrefix987', $config['arguments']['bucket']); + } + + public function testMultiBucketBucketAlreadySet() { + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with($this->equalTo('objectstore_multibucket'), '') + ->willReturn([ + 'class' => 'Test\Files\Mount\FakeObjectStore', + 'arguments' => [ + 'bucket' => 'myBucketPrefix', + ], + ]); + + $this->user->method('getUID') + ->willReturn('uid'); + $this->loader->expects($this->never())->method($this->anything()); + + $this->config->expects($this->once()) + ->method('getUserValue') + ->with( + $this->equalTo('uid'), + $this->equalTo('homeobjectstore'), + $this->equalTo('bucket'), + $this->equalTo(null) + )->willReturn('awesomeBucket1'); + + $this->config->expects($this->never()) + ->method('setUserValue'); + + $config = $this->invokePrivate($this->provider, 'getMultiBucketObjectStoreConfig', [$this->user, $this->loader]); + + $this->assertArrayHasKey('class', $config); + $this->assertEquals($config['class'], 'Test\Files\Mount\FakeObjectStore'); + $this->assertArrayHasKey('arguments', $config); + $this->assertArrayHasKey('user', $config['arguments']); + $this->assertSame($this->user, $config['arguments']['user']); + $this->assertArrayHasKey('objectstore', $config['arguments']); + $this->assertInstanceOf('Test\Files\Mount\FakeObjectStore', $config['arguments']['objectstore']); + $this->assertArrayHasKey('bucket', $config['arguments']); + $this->assertEquals('awesomeBucket1', $config['arguments']['bucket']); + } + + public function testMultiBucketConfigFirst() { + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with($this->equalTo('objectstore_multibucket')) + ->willReturn([ + 'class' => 'Test\Files\Mount\FakeObjectStore', + ]); + + $this->user->method('getUID') + ->willReturn('uid'); + $this->loader->expects($this->never())->method($this->anything()); + + $mount = $this->provider->getHomeMountForUser($this->user, $this->loader); + $this->assertInstanceOf('OC\Files\Mount\MountPoint', $mount); + } + + public function testMultiBucketConfigFirstFallBackSingle() { + $this->config->expects($this->at(0)) + ->method('getSystemValue') + ->with($this->equalTo('objectstore_multibucket')) + ->willReturn(''); + + $this->config->expects($this->at(1)) + ->method('getSystemValue') + ->with($this->equalTo('objectstore')) + ->willReturn([ + 'class' => 'Test\Files\Mount\FakeObjectStore', + ]); + + $this->user->method('getUID') + ->willReturn('uid'); + $this->loader->expects($this->never())->method($this->anything()); + + $mount = $this->provider->getHomeMountForUser($this->user, $this->loader); + $this->assertInstanceOf('OC\Files\Mount\MountPoint', $mount); + } + + public function testNoObjectStore() { + $this->config->expects($this->exactly(2)) + ->method('getSystemValue') + ->willReturn(''); + + $mount = $this->provider->getHomeMountForUser($this->user, $this->loader); + $this->assertNull($mount); + } +} + +class FakeObjectStore { + private $arguments; + + public function __construct(array $arguments) { + $this->arguments = $arguments; + } + + public function getArguments() { + return $this->arguments; + } +}
\ No newline at end of file diff --git a/tests/lib/Files/ObjectStore/MapperTest.php b/tests/lib/Files/ObjectStore/MapperTest.php new file mode 100644 index 00000000000..1ebb67a6905 --- /dev/null +++ b/tests/lib/Files/ObjectStore/MapperTest.php @@ -0,0 +1,50 @@ +<?php +/** + * @author Roeland Jago Douma <rullzer@owncloud.com> + * + * @copyright Copyright (c) 2016, 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/> + * + */ +namespace Test\Files\ObjectStore; + + +use OC\Files\ObjectStore\Mapper; + +class MapperTest extends \Test\TestCase { + + public function dataGetBucket() { + return [ + ['user', substr(md5('user'), 0, 3)], + ['USER', substr(md5('USER'), 0, 3)], + ['bc0e8b52-a66c-1035-90c6-d9663bda9e3f', substr(md5('bc0e8b52-a66c-1035-90c6-d9663bda9e3f'), 0, 3)], + ]; + } + + /** + * @dataProvider dataGetBucket + * @param string $username + * @param string $expectedBucket + */ + public function testGetBucket($username, $expectedBucket) { + $user = $this->getMock('OCP\IUser'); + $user->method('getUID') + ->willReturn($username); + + $mapper = new Mapper($user); + + $this->assertSame($expectedBucket, $mapper->getBucket()); + } +}
\ No newline at end of file diff --git a/tests/lib/User/SessionTest.php b/tests/lib/User/SessionTest.php index 4438487e2a0..5ff2a16acb9 100644 --- a/tests/lib/User/SessionTest.php +++ b/tests/lib/User/SessionTest.php @@ -24,6 +24,9 @@ class SessionTest extends \Test\TestCase { /** @var \OC\Authentication\Token\DefaultTokenProvider */ protected $defaultProvider; + /** @var \OCP\IConfig */ + private $config; + protected function setUp() { parent::setUp(); @@ -34,12 +37,17 @@ class SessionTest extends \Test\TestCase { $this->defaultProvider = $this->getMockBuilder('\OC\Authentication\Token\DefaultTokenProvider') ->disableOriginalConstructor() ->getMock(); + $this->config = $this->getMock('\OCP\IConfig'); } public function testGetUser() { $token = new \OC\Authentication\Token\DefaultToken(); + $token->setLoginName('User123'); - $expectedUser = new User('foo', null); + $expectedUser = $this->getMock('\OCP\IUser'); + $expectedUser->expects($this->any()) + ->method('getUID') + ->will($this->returnValue('user123')); $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->at(0)) ->method('get') @@ -66,7 +74,10 @@ class SessionTest extends \Test\TestCase { ->will($this->returnValue('password123')); $manager->expects($this->once()) ->method('checkPassword') - ->with($expectedUser->getUID(), 'password123') + ->with('User123', 'password123') + ->will($this->returnValue(true)); + $expectedUser->expects($this->once()) + ->method('isEnabled') ->will($this->returnValue(true)); $session->expects($this->at(3)) ->method('set') @@ -88,7 +99,7 @@ class SessionTest extends \Test\TestCase { ->with($expectedUser->getUID()) ->will($this->returnValue($expectedUser)); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config); $user = $userSession->getUser(); $this->assertSame($expectedUser, $user); } @@ -111,7 +122,7 @@ class SessionTest extends \Test\TestCase { ->getMock(); $userSession = $this->getMockBuilder('\OC\User\Session') - ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider]) + ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider, $this->config]) ->setMethods([ 'getUser' ]) @@ -138,7 +149,7 @@ class SessionTest extends \Test\TestCase { ->method('getUID') ->will($this->returnValue('foo')); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config); $userSession->setUser($user); } @@ -190,7 +201,7 @@ class SessionTest extends \Test\TestCase { ->will($this->returnValue($user)); $userSession = $this->getMockBuilder('\OC\User\Session') - ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider]) + ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider, $this->config]) ->setMethods([ 'prepareUserLogin' ]) @@ -237,7 +248,7 @@ class SessionTest extends \Test\TestCase { ->with('foo', 'bar') ->will($this->returnValue($user)); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config); $userSession->login('foo', 'bar'); } @@ -273,7 +284,7 @@ class SessionTest extends \Test\TestCase { ->with('foo', 'bar') ->will($this->returnValue(false)); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config); $userSession->login('foo', 'bar'); } @@ -293,10 +304,64 @@ class SessionTest extends \Test\TestCase { ->with('foo', 'bar') ->will($this->returnValue(false)); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config); $userSession->login('foo', 'bar'); } + public function testLogClientInNoTokenPasswordWith2fa() { + $manager = $this->getMockBuilder('\OC\User\Manager') + ->disableOriginalConstructor() + ->getMock(); + $session = $this->getMock('\OCP\ISession'); + + /** @var \OC\User\Session $userSession */ + $userSession = $this->getMockBuilder('\OC\User\Session') + ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider, $this->config]) + ->setMethods(['login']) + ->getMock(); + + $this->defaultProvider->expects($this->once()) + ->method('getToken') + ->with('doe') + ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with('token_auth_enforced', false) + ->will($this->returnValue(true)); + + $this->assertFalse($userSession->logClientIn('john', 'doe')); + } + + public function testLogClientInNoTokenPasswordNo2fa() { + $manager = $this->getMockBuilder('\OC\User\Manager') + ->disableOriginalConstructor() + ->getMock(); + $session = $this->getMock('\OCP\ISession'); + $user = $this->getMock('\OCP\IUser'); + + /** @var \OC\User\Session $userSession */ + $userSession = $this->getMockBuilder('\OC\User\Session') + ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider, $this->config]) + ->setMethods(['login', 'isTwoFactorEnforced']) + ->getMock(); + + $this->defaultProvider->expects($this->once()) + ->method('getToken') + ->with('doe') + ->will($this->throwException(new \OC\Authentication\Exceptions\InvalidTokenException())); + $this->config->expects($this->once()) + ->method('getSystemValue') + ->with('token_auth_enforced', false) + ->will($this->returnValue(false)); + + $userSession->expects($this->once()) + ->method('isTwoFactorEnforced') + ->with('john') + ->will($this->returnValue(true)); + + $this->assertFalse($userSession->logClientIn('john', 'doe')); + } + public function testRememberLoginValidToken() { $session = $this->getMock('\OC\Session\Memory', array(), array('')); $session->expects($this->exactly(1)) @@ -348,7 +413,7 @@ class SessionTest extends \Test\TestCase { //override, otherwise tests will fail because of setcookie() array('setMagicInCookie'), //there are passed as parameters to the constructor - array($manager, $session, $this->timeFactory, $this->defaultProvider)); + array($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config)); $granted = $userSession->loginWithCookie('foo', $token); @@ -393,7 +458,7 @@ class SessionTest extends \Test\TestCase { $token = 'goodToken'; \OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time()); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config); $granted = $userSession->loginWithCookie('foo', 'badToken'); $this->assertSame($granted, false); @@ -436,7 +501,7 @@ class SessionTest extends \Test\TestCase { $token = 'goodToken'; \OC::$server->getConfig()->setUserValue('foo', 'login_token', $token, time()); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config); $granted = $userSession->loginWithCookie('foo', $token); $this->assertSame($granted, false); @@ -461,7 +526,7 @@ class SessionTest extends \Test\TestCase { $session = new Memory(''); $session->set('user_id', 'foo'); $userSession = $this->getMockBuilder('\OC\User\Session') - ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider]) + ->setConstructorArgs([$manager, $session, $this->timeFactory, $this->defaultProvider, $this->config]) ->setMethods([ 'validateSession' ]) @@ -484,7 +549,7 @@ class SessionTest extends \Test\TestCase { $session = new Memory(''); $token = $this->getMock('\OC\Authentication\Token\IToken'); $user = $this->getMock('\OCP\IUser'); - $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider); + $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->defaultProvider, $this->config); $request = $this->getMock('\OCP\IRequest'); $request->expects($this->once()) @@ -509,4 +574,51 @@ class SessionTest extends \Test\TestCase { $this->assertFalse($userSession->tryTokenLogin($request)); } + public function testValidateSessionDisabledUser() { + $userManager = $this->getMock('\OCP\IUserManager'); + $session = $this->getMock('\OCP\ISession'); + $timeFactory = $this->getMock('\OCP\AppFramework\Utility\ITimeFactory'); + $tokenProvider = $this->getMock('\OC\Authentication\Token\IProvider'); + $userSession = $this->getMockBuilder('\OC\User\Session') + ->setConstructorArgs([$userManager, $session, $timeFactory, $tokenProvider, $this->config]) + ->setMethods(['logout']) + ->getMock(); + + $user = $this->getMock('\OCP\IUser'); + $token = $this->getMock('\OC\Authentication\Token\IToken'); + + $session->expects($this->once()) + ->method('getId') + ->will($this->returnValue('sessionid')); + $tokenProvider->expects($this->once()) + ->method('getToken') + ->with('sessionid') + ->will($this->returnValue($token)); + $session->expects($this->once()) + ->method('get') + ->with('last_login_check') + ->will($this->returnValue(1000)); + $timeFactory->expects($this->once()) + ->method('getTime') + ->will($this->returnValue(5000)); + $tokenProvider->expects($this->once()) + ->method('getPassword') + ->with($token, 'sessionid') + ->will($this->returnValue('123456')); + $token->expects($this->once()) + ->method('getLoginName') + ->will($this->returnValue('User5')); + $userManager->expects($this->once()) + ->method('checkPassword') + ->with('User5', '123456') + ->will($this->returnValue(true)); + $user->expects($this->once()) + ->method('isEnabled') + ->will($this->returnValue(false)); + $userSession->expects($this->once()) + ->method('logout'); + + $this->invokePrivate($userSession, 'validateSession', [$user]); + } + } diff --git a/tests/lib/authentication/twofactorauth/managertest.php b/tests/lib/authentication/twofactorauth/managertest.php new file mode 100644 index 00000000000..14e517b363e --- /dev/null +++ b/tests/lib/authentication/twofactorauth/managertest.php @@ -0,0 +1,187 @@ +<?php + +/** + * @author Christoph Wurst <christoph@owncloud.com> + * + * @copyright Copyright (c) 2016, 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/> + * + */ + +namespace Test\Authentication\TwoFactorAuth; + +use Test\TestCase; +use OC\Authentication\TwoFactorAuth\Manager; + +class ManagerTest extends TestCase { + + /** @var OCP\IUser */ + private $user; + + /** @var OC\App\AppManager */ + private $appManager; + + /** @var OCP\ISession */ + private $session; + + /** @var Manager */ + private $manager; + + /** @var \OCP\IConfig */ + private $config; + + /** @var \OCP\Authentication\TwoFactorAuth\IProvider */ + private $fakeProvider; + + protected function setUp() { + parent::setUp(); + + $this->user = $this->getMock('\OCP\IUser'); + $this->appManager = $this->getMockBuilder('\OC\App\AppManager') + ->disableOriginalConstructor() + ->getMock(); + $this->session = $this->getMock('\OCP\ISession'); + $this->config = $this->getMock('\OCP\IConfig'); + + $this->manager = new Manager($this->appManager, $this->session, $this->config); + + $this->fakeProvider = $this->getMock('\OCP\Authentication\TwoFactorAuth\IProvider'); + $this->fakeProvider->expects($this->any()) + ->method('getId') + ->will($this->returnValue('email')); + $this->fakeProvider->expects($this->any()) + ->method('isTwoFactorAuthEnabledForUser') + ->will($this->returnValue(true)); + \OC::$server->registerService('\OCA\MyCustom2faApp\FakeProvider', function() { + return $this->fakeProvider; + }); + } + + private function prepareProviders() { + $this->appManager->expects($this->any()) + ->method('getEnabledAppsForUser') + ->with($this->user) + ->will($this->returnValue(['mycustom2faapp'])); + + $this->appManager->expects($this->once()) + ->method('getAppInfo') + ->with('mycustom2faapp') + ->will($this->returnValue([ + 'two-factor-providers' => [ + '\OCA\MyCustom2faApp\FakeProvider', + ], + ])); + } + + public function testIsTwoFactorAuthenticated() { + $this->prepareProviders(); + + $user = $this->getMock('\OCP\IUser'); + $user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('user123')); + $this->config->expects($this->once()) + ->method('getUserValue') + ->with('user123', 'core', 'two_factor_auth_disabled', 0) + ->will($this->returnValue(0)); + + $this->assertTrue($this->manager->isTwoFactorAuthenticated($user)); + } + + public function testGetProvider() { + $this->prepareProviders(); + + $this->assertSame($this->fakeProvider, $this->manager->getProvider($this->user, 'email')); + } + + public function testGetInvalidProvider() { + $this->prepareProviders(); + + $this->assertSame(null, $this->manager->getProvider($this->user, 'nonexistent')); + } + + public function testGetProviders() { + $this->prepareProviders(); + $expectedProviders = [ + 'email' => $this->fakeProvider, + ]; + + $this->assertEquals($expectedProviders, $this->manager->getProviders($this->user)); + } + + public function testVerifyChallenge() { + $this->prepareProviders(); + + $challenge = 'passme'; + $this->fakeProvider->expects($this->once()) + ->method('verifyChallenge') + ->with($this->user, $challenge) + ->will($this->returnValue(true)); + $this->session->expects($this->once()) + ->method('remove') + ->with('two_factor_auth_uid'); + + $this->assertEquals(true, $this->manager->verifyChallenge('email', $this->user, $challenge)); + } + + public function testVerifyChallengeInvalidProviderId() { + $this->prepareProviders(); + + $challenge = 'passme'; + $this->fakeProvider->expects($this->never()) + ->method('verifyChallenge') + ->with($this->user, $challenge); + $this->session->expects($this->never()) + ->method('remove'); + + $this->assertEquals(false, $this->manager->verifyChallenge('dontexist', $this->user, $challenge)); + } + + public function testVerifyInvalidChallenge() { + $this->prepareProviders(); + + $challenge = 'dontpassme'; + $this->fakeProvider->expects($this->once()) + ->method('verifyChallenge') + ->with($this->user, $challenge) + ->will($this->returnValue(false)); + $this->session->expects($this->never()) + ->method('remove'); + + $this->assertEquals(false, $this->manager->verifyChallenge('email', $this->user, $challenge)); + } + + public function testNeedsSecondFactor() { + $this->session->expects($this->once()) + ->method('exists') + ->with('two_factor_auth_uid') + ->will($this->returnValue(false)); + + $this->assertequals(false, $this->manager->needsSecondFactor()); + } + + public function testPrepareTwoFactorLogin() { + $this->user->expects($this->once()) + ->method('getUID') + ->will($this->returnValue('ferdinand')); + + $this->session->expects($this->once()) + ->method('set') + ->with('two_factor_auth_uid', 'ferdinand'); + + $this->manager->prepareTwoFactorLogin($this->user); + } + +} diff --git a/tests/settings/controller/AuthSettingsControllerTest.php b/tests/settings/controller/AuthSettingsControllerTest.php index 49491c8ff52..ee67b221022 100644 --- a/tests/settings/controller/AuthSettingsControllerTest.php +++ b/tests/settings/controller/AuthSettingsControllerTest.php @@ -89,6 +89,9 @@ class AuthSettingsControllerTest extends TestCase { ->method('getPassword') ->with($sessionToken, 'sessionid') ->will($this->returnValue($password)); + $sessionToken->expects($this->once()) + ->method('getLoginName') + ->will($this->returnValue('User13')); $this->secureRandom->expects($this->exactly(4)) ->method('generate') @@ -98,7 +101,7 @@ class AuthSettingsControllerTest extends TestCase { $this->tokenProvider->expects($this->once()) ->method('generateToken') - ->with($newToken, $this->uid, $password, $name, IToken::PERMANENT_TOKEN) + ->with($newToken, $this->uid, 'User13', $password, $name, IToken::PERMANENT_TOKEN) ->will($this->returnValue($deviceToken)); $expected = [ |