diff options
author | Morris Jobke <hey@morrisjobke.de> | 2017-01-30 19:30:04 -0600 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-01-30 19:30:04 -0600 |
commit | 5bad417e57231e129b7113ebb110b1f2b282f349 (patch) | |
tree | 77f5d78e2bd4cd4fe3a70e1659875cce4d832c56 /tests/lib | |
parent | e50320dc71135bf546c757f7dea6e23f5f48a906 (diff) | |
parent | 3ee5c7ea4e508b4945646013f871ad532d09ddbd (diff) | |
download | nextcloud-server-5bad417e57231e129b7113ebb110b1f2b282f349.tar.gz nextcloud-server-5bad417e57231e129b7113ebb110b1f2b282f349.zip |
Merge pull request #2044 from nextcloud/login-credential-store
Login credential store
Diffstat (limited to 'tests/lib')
-rw-r--r-- | tests/lib/Authentication/LoginCredentials/CredentialsTest.php | 66 | ||||
-rw-r--r-- | tests/lib/Authentication/LoginCredentials/StoreTest.php | 182 |
2 files changed, 248 insertions, 0 deletions
diff --git a/tests/lib/Authentication/LoginCredentials/CredentialsTest.php b/tests/lib/Authentication/LoginCredentials/CredentialsTest.php new file mode 100644 index 00000000000..308ccafb151 --- /dev/null +++ b/tests/lib/Authentication/LoginCredentials/CredentialsTest.php @@ -0,0 +1,66 @@ +<?php + +/** + * @copyright 2016 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2016 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Test\Authentication\LoginCredentials; + +use OC\Authentication\LoginCredentials\Credentials; +use Test\TestCase; + +class CredentialsTest extends TestCase { + + /** @var string */ + private $uid; + + /** @var string */ + private $user; + + /** @var string */ + private $password; + + /** @var Credentials */ + private $credentials; + + protected function setUp() { + parent::setUp(); + + $this->uid = 'user123'; + $this->user = 'User123'; + $this->password = '123456'; + + $this->credentials = new Credentials($this->uid, $this->user, $this->password); + } + + public function testGetUID() { + $this->assertEquals($this->uid, $this->credentials->getUID()); + } + + public function testGetUserName() { + $this->assertEquals($this->user, $this->credentials->getLoginName()); + } + + public function testGetPassword() { + $this->assertEquals($this->password, $this->credentials->getPassword()); + } + +} diff --git a/tests/lib/Authentication/LoginCredentials/StoreTest.php b/tests/lib/Authentication/LoginCredentials/StoreTest.php new file mode 100644 index 00000000000..9a719339b43 --- /dev/null +++ b/tests/lib/Authentication/LoginCredentials/StoreTest.php @@ -0,0 +1,182 @@ +<?php + +/** + * @copyright 2016 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @author 2016 Christoph Wurst <christoph@winzerhof-wurst.at> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * 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 + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Test\Authentication\LoginCredentials; + +use OC\Authentication\Exceptions\InvalidTokenException; +use OC\Authentication\Exceptions\PasswordlessTokenException; +use OC\Authentication\LoginCredentials\Credentials; +use OC\Authentication\LoginCredentials\Store; +use OC\Authentication\Token\IProvider; +use OC\Authentication\Token\IToken; +use OCP\Authentication\Exceptions\CredentialsUnavailableException; +use OCP\ILogger; +use OCP\ISession; +use OCP\Session\Exceptions\SessionNotAvailableException; +use PHPUnit_Framework_MockObject_MockObject; +use Test\TestCase; + +class StoreTest extends TestCase { + + /** @var ISession|PHPUnit_Framework_MockObject_MockObject */ + private $session; + + /** @var IProvider|PHPUnit_Framework_MockObject_MockObject */ + private $tokenProvider; + + /** @var ILogger|PHPUnit_Framework_MockObject_MockObject */ + private $logger; + + /** @var Store */ + private $store; + + protected function setUp() { + parent::setUp(); + + $this->session = $this->createMock(ISession::class); + $this->tokenProvider = $this->createMock(IProvider::class); + $this->logger = $this->createMock(ILogger::class); + + $this->store = new Store($this->session, $this->logger, $this->tokenProvider); + } + + public function testAuthenticate() { + $params = [ + 'run' => true, + 'uid' => 'user123', + 'password' => 123456, + ]; + + $this->session->expects($this->once()) + ->method('set') + ->with($this->equalTo('login_credentials'), $this->equalTo(json_encode($params))); + + $this->store->authenticate($params); + } + + public function testSetSession() { + $session = $this->createMock(ISession::class); + + $this->store->setSession($session); + } + + public function testGetLoginCredentialsNoTokenProvider() { + $this->store = new Store($this->session, $this->logger, null); + + $this->expectException(CredentialsUnavailableException::class); + + $this->store->getLoginCredentials(); + } + + public function testGetLoginCredentials() { + $uid = 'uid'; + $user = 'user123'; + $password = 'passme'; + $token = $this->createMock(IToken::class); + $this->session->expects($this->once()) + ->method('getId') + ->willReturn('sess2233'); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('sess2233') + ->willReturn($token); + $token->expects($this->once()) + ->method('getUID') + ->willReturn($uid); + $token->expects($this->once()) + ->method('getLoginName') + ->willReturn($user); + $this->tokenProvider->expects($this->once()) + ->method('getPassword') + ->with($token, 'sess2233') + ->willReturn($password); + $expected = new Credentials($uid, $user, $password); + + $creds = $this->store->getLoginCredentials(); + + $this->assertEquals($expected, $creds); + } + + public function testGetLoginCredentialsSessionNotAvailable() { + $this->session->expects($this->once()) + ->method('getId') + ->will($this->throwException(new SessionNotAvailableException())); + $this->expectException(CredentialsUnavailableException::class); + + $this->store->getLoginCredentials(); + } + + public function testGetLoginCredentialsInvalidToken() { + $this->session->expects($this->once()) + ->method('getId') + ->willReturn('sess2233'); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('sess2233') + ->will($this->throwException(new InvalidTokenException())); + $this->expectException(CredentialsUnavailableException::class); + + $this->store->getLoginCredentials(); + } + + public function testGetLoginCredentialsInvalidTokenLoginCredentials() { + $uid = 'user987'; + $password = '7389374'; + + $this->session->expects($this->once()) + ->method('getId') + ->willReturn('sess2233'); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('sess2233') + ->will($this->throwException(new InvalidTokenException())); + $this->session->expects($this->once()) + ->method('exists') + ->with($this->equalTo('login_credentials')) + ->willReturn(true); + $this->session->expects($this->once()) + ->method('get') + ->with($this->equalTo('login_credentials')) + ->willReturn('{"run":true,"uid":"user987","password":"7389374"}'); + $expected = new Credentials('user987', 'user987', '7389374'); + + $actual = $this->store->getLoginCredentials(); + + $this->assertEquals($expected, $actual); + } + + public function testGetLoginCredentialsPasswordlessToken() { + $this->session->expects($this->once()) + ->method('getId') + ->willReturn('sess2233'); + $this->tokenProvider->expects($this->once()) + ->method('getToken') + ->with('sess2233') + ->will($this->throwException(new PasswordlessTokenException())); + $this->expectException(CredentialsUnavailableException::class); + + $this->store->getLoginCredentials(); + } + +} |