diff options
author | Joas Schilling <coding@schilljs.com> | 2021-12-01 21:40:14 +0100 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2021-12-01 21:40:14 +0100 |
commit | 9f001790379829096939015e790f0a35b6a38787 (patch) | |
tree | 6cfd9b8080d4d0665284917f89a00cdfe2085e62 /tests/lib/Authentication | |
parent | a8866b0253d55fb06c60a2dcae8a1c317828e510 (diff) | |
download | nextcloud-server-9f001790379829096939015e790f0a35b6a38787.tar.gz nextcloud-server-9f001790379829096939015e790f0a35b6a38787.zip |
More unit test adjustments
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'tests/lib/Authentication')
4 files changed, 0 insertions, 878 deletions
diff --git a/tests/lib/Authentication/Token/DefaultTokenCleanupJobTest.php b/tests/lib/Authentication/Token/DefaultTokenCleanupJobTest.php deleted file mode 100644 index 0991c8b1fc8..00000000000 --- a/tests/lib/Authentication/Token/DefaultTokenCleanupJobTest.php +++ /dev/null @@ -1,52 +0,0 @@ -<?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\Token; - -use OC\Authentication\Token\DefaultTokenCleanupJob; -use OC\Authentication\Token\IProvider; -use OC\Authentication\Token\Manager; -use Test\TestCase; - -class DefaultTokenCleanupJobTest extends TestCase { - - /** @var DefaultTokenCleanupJob */ - private $job; - private $tokenProvider; - - protected function setUp(): void { - parent::setUp(); - - $this->tokenProvider = $this->getMockBuilder(Manager::class) - ->disableOriginalConstructor() - ->getMock(); - $this->overwriteService(IProvider::class, $this->tokenProvider); - $this->job = new DefaultTokenCleanupJob(); - } - - public function testRun() { - $this->tokenProvider->expects($this->once()) - ->method('invalidateOldTokens') - ->with(); - $this->invokePrivate($this->job, 'run', [null]); - } -} diff --git a/tests/lib/Authentication/Token/DefaultTokenMapperTest.php b/tests/lib/Authentication/Token/DefaultTokenMapperTest.php deleted file mode 100644 index da779be0807..00000000000 --- a/tests/lib/Authentication/Token/DefaultTokenMapperTest.php +++ /dev/null @@ -1,233 +0,0 @@ -<?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\Token; - -use OC; -use OC\Authentication\Token\DefaultToken; -use OC\Authentication\Token\DefaultTokenMapper; -use OC\Authentication\Token\IToken; -use OCP\DB\QueryBuilder\IQueryBuilder; -use OCP\IDBConnection; -use OCP\IUser; -use Test\TestCase; - -/** - * Class DefaultTokenMapperTest - * - * @group DB - * @package Test\Authentication - */ -class DefaultTokenMapperTest extends TestCase { - - /** @var DefaultTokenMapper */ - private $mapper; - - /** @var IDBConnection */ - private $dbConnection; - private $time; - - protected function setUp(): void { - parent::setUp(); - - $this->dbConnection = OC::$server->getDatabaseConnection(); - $this->time = time(); - $this->resetDatabase(); - - $this->mapper = new DefaultTokenMapper($this->dbConnection); - } - - private function resetDatabase() { - $qb = $this->dbConnection->getQueryBuilder(); - $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'), - 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN), - 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago - 'last_check' => $this->time - 60 * 10, // 10mins ago - ])->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'), - 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN), - 'last_activity' => $qb->createNamedParameter($this->time - 60 * 60 * 24 * 3, IQueryBuilder::PARAM_INT), // Three days ago - 'last_check' => $this->time - 10, // 10secs ago - ])->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'), - 'type' => $qb->createNamedParameter(IToken::TEMPORARY_TOKEN), - 'last_activity' => $qb->createNamedParameter($this->time - 120, IQueryBuilder::PARAM_INT), // Two minutes ago - 'last_check' => $this->time - 60 * 10, // 10mins ago - ])->execute(); - } - - private function getNumberOfTokens() { - $qb = $this->dbConnection->getQueryBuilder(); - $result = $qb->select($qb->func()->count('*', 'count')) - ->from('authtoken') - ->execute() - ->fetch(); - return (int) $result['count']; - } - - public function testInvalidate() { - $token = '9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'; - - $this->mapper->invalidate($token); - - $this->assertSame(2, $this->getNumberOfTokens()); - } - - public function testInvalidateInvalid() { - $token = 'youwontfindthisoneinthedatabase'; - - $this->mapper->invalidate($token); - - $this->assertSame(3, $this->getNumberOfTokens()); - } - - public function testInvalidateOld() { - $olderThan = $this->time - 60 * 60; // One hour - - $this->mapper->invalidateOld($olderThan); - - $this->assertSame(2, $this->getNumberOfTokens()); - } - - public function testGetToken() { - $token = new DefaultToken(); - $token->setUid('user2'); - $token->setLoginName('User2'); - $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f'); - $token->setName('Firefox on Android'); - $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b'); - $token->setType(IToken::TEMPORARY_TOKEN); - $token->setRemember(IToken::DO_NOT_REMEMBER); - $token->setLastActivity($this->time - 60 * 60 * 24 * 3); - $token->setLastCheck($this->time - 10); - $token->setVersion(DefaultToken::VERSION); - - $dbToken = $this->mapper->getToken($token->getToken()); - - $token->setId($dbToken->getId()); // We don't know the ID - $token->resetUpdatedFields(); - - $this->assertEquals($token, $dbToken); - } - - - public function testGetInvalidToken() { - $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); - - $token = 'thisisaninvalidtokenthatisnotinthedatabase'; - - $this->mapper->getToken($token); - } - - public function testGetTokenById() { - $token = new DefaultToken(); - $token->setUid('user2'); - $token->setLoginName('User2'); - $token->setPassword('971a337057853344700bbeccf836519f|UwOQwyb34sJHtqPV|036d4890f8c21d17bbc7b88072d8ef049a5c832a38e97f3e3d5f9186e896c2593aee16883f617322fa242728d0236ff32d163caeb4bd45e14ca002c57a88665f'); - $token->setName('Firefox on Android'); - $token->setToken('1504445f1524fc801035448a95681a9378ba2e83930c814546c56e5d6ebde221198792fd900c88ed5ead0555780dad1ebce3370d7e154941cd5de87eb419899b'); - $token->setType(IToken::TEMPORARY_TOKEN); - $token->setRemember(IToken::DO_NOT_REMEMBER); - $token->setLastActivity($this->time - 60 * 60 * 24 * 3); - $token->setLastCheck($this->time - 10); - $token->setVersion(DefaultToken::VERSION); - - $dbToken = $this->mapper->getToken($token->getToken()); - $token->setId($dbToken->getId()); // We don't know the ID - $token->resetUpdatedFields(); - - $dbToken = $this->mapper->getTokenById($token->getId()); - $this->assertEquals($token, $dbToken); - } - - - public function testGetTokenByIdNotFound() { - $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); - - $this->mapper->getTokenById(-1); - } - - - public function testGetInvalidTokenById() { - $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); - - $id = 42; - - $this->mapper->getToken($id); - } - - public function testGetTokenByUser() { - $this->assertCount(2, $this->mapper->getTokenByUser('user1')); - } - - public function testGetTokenByUserNotFound() { - $this->assertCount(0, $this->mapper->getTokenByUser('user1000')); - } - - public function testDeleteById() { - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ - $user = $this->createMock(IUser::class); - $qb = $this->dbConnection->getQueryBuilder(); - $qb->select('id') - ->from('authtoken') - ->where($qb->expr()->eq('token', $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'))); - $result = $qb->execute(); - $id = $result->fetch()['id']; - - $this->mapper->deleteById('user1', $id); - $this->assertEquals(2, $this->getNumberOfTokens()); - } - - public function testDeleteByIdWrongUser() { - $id = 33; - - $this->mapper->deleteById('user1000', $id); - $this->assertEquals(3, $this->getNumberOfTokens()); - } - - public function testDeleteByName() { - $qb = $this->dbConnection->getQueryBuilder(); - $qb->select('name') - ->from('authtoken') - ->where($qb->expr()->eq('token', $qb->createNamedParameter('9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'))); - $result = $qb->execute(); - $name = $result->fetch()['name']; - $this->mapper->deleteByName($name); - $this->assertEquals(2, $this->getNumberOfTokens()); - } -} diff --git a/tests/lib/Authentication/Token/DefaultTokenProviderTest.php b/tests/lib/Authentication/Token/DefaultTokenProviderTest.php deleted file mode 100644 index 7a8915a7c14..00000000000 --- a/tests/lib/Authentication/Token/DefaultTokenProviderTest.php +++ /dev/null @@ -1,552 +0,0 @@ -<?php -/** - * @author Christoph Wurst <christoph@owncloud.com> - * - * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch> - * @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\Token; - -use OC\Authentication\Exceptions\ExpiredTokenException; -use OC\Authentication\Exceptions\InvalidTokenException; -use OC\Authentication\Token\DefaultToken; -use OC\Authentication\Token\DefaultTokenMapper; -use OC\Authentication\Token\DefaultTokenProvider; -use OC\Authentication\Token\IToken; -use OC\Authentication\Token\PublicKeyToken; -use OCP\AppFramework\Db\DoesNotExistException; -use OCP\AppFramework\Utility\ITimeFactory; -use OCP\IConfig; -use OCP\Security\ICrypto; -use Psr\Log\LoggerInterface; -use Test\TestCase; - -class DefaultTokenProviderTest extends TestCase { - - /** @var DefaultTokenProvider|\PHPUnit\Framework\MockObject\MockObject */ - private $tokenProvider; - /** @var DefaultTokenMapper|\PHPUnit\Framework\MockObject\MockObject */ - private $mapper; - /** @var ICrypto|\PHPUnit\Framework\MockObject\MockObject */ - private $crypto; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - private $config; - /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */ - private $logger; - /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */ - private $timeFactory; - /** @var int */ - private $time; - - protected function setUp(): void { - parent::setUp(); - - $this->mapper = $this->createMock(DefaultTokenMapper::class); - $this->crypto = $this->createMock(ICrypto::class); - $this->config = $this->createMock(IConfig::class); - $this->logger = $this->createMock(LoggerInterface::class); - $this->timeFactory = $this->createMock(ITimeFactory::class); - $this->time = 1313131; - $this->timeFactory->expects($this->any()) - ->method('getTime') - ->willReturn($this->time); - - $this->tokenProvider = new DefaultTokenProvider($this->mapper, $this->crypto, $this->config, $this->logger, - $this->timeFactory); - } - - 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' - . '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'; - $type = IToken::PERMANENT_TOKEN; - - $toInsert = new DefaultToken(); - $toInsert->setUid($uid); - $toInsert->setLoginName($user); - $toInsert->setPassword('encryptedpassword'); - $toInsert->setName($name); - $toInsert->setToken(hash('sha512', $token . '1f4h9s')); - $toInsert->setType($type); - $toInsert->setRemember(IToken::DO_NOT_REMEMBER); - $toInsert->setLastActivity($this->time); - $toInsert->setLastCheck($this->time); - $toInsert->setVersion(DefaultToken::VERSION); - - $this->config->expects($this->any()) - ->method('getSystemValue') - ->with('secret') - ->willReturn('1f4h9s'); - $this->crypto->expects($this->once()) - ->method('encrypt') - ->with($password, $token . '1f4h9s') - ->willReturn('encryptedpassword'); - $this->mapper->expects($this->once()) - ->method('insert') - ->with($this->equalTo($toInsert)); - - $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER); - - $this->assertEquals($toInsert, $actual); - } - - public function testUpdateToken() { - $tk = new DefaultToken(); - $tk->setLastActivity($this->time - 200); - $this->mapper->expects($this->once()) - ->method('update') - ->with($tk); - - $this->tokenProvider->updateTokenActivity($tk); - - $this->assertEquals($this->time, $tk->getLastActivity()); - } - - public function testUpdateTokenDebounce() { - $tk = new DefaultToken(); - $tk->setLastActivity($this->time - 30); - $this->mapper->expects($this->never()) - ->method('update') - ->with($tk); - - $this->tokenProvider->updateTokenActivity($tk); - } - - public function testGetTokenByUser() { - $this->mapper->expects($this->once()) - ->method('getTokenByUser') - ->with('uid') - ->willReturn(['token']); - - $this->assertEquals(['token'], $this->tokenProvider->getTokenByUser('uid')); - } - - public function testGetPassword() { - $token = 'token1234'; - $tk = new DefaultToken(); - $tk->setPassword('someencryptedvalue'); - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with('secret') - ->willReturn('1f4h9s'); - $this->crypto->expects($this->once()) - ->method('decrypt') - ->with('someencryptedvalue', $token . '1f4h9s') - ->willReturn('passme'); - - $actual = $this->tokenProvider->getPassword($tk, $token); - - $this->assertEquals('passme', $actual); - } - - - public function testGetPasswordPasswordLessToken() { - $this->expectException(\OC\Authentication\Exceptions\PasswordlessTokenException::class); - - $token = 'token1234'; - $tk = new DefaultToken(); - $tk->setPassword(null); - - $this->tokenProvider->getPassword($tk, $token); - } - - - public function testGetPasswordDeletesInvalidToken() { - $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class); - - $token = 'token1234'; - $tk = new DefaultToken(); - $tk->setPassword('someencryptedvalue'); - /* @var $tokenProvider DefaultTokenProvider */ - $tokenProvider = $this->getMockBuilder('\OC\Authentication\Token\DefaultTokenProvider') - ->setMethods([ - 'invalidateToken' - ]) - ->setConstructorArgs([$this->mapper, $this->crypto, $this->config, $this->logger, - $this->timeFactory]) - ->getMock(); - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with('secret') - ->willReturn('1f4h9s'); - $this->crypto->expects($this->once()) - ->method('decrypt') - ->with('someencryptedvalue', $token . '1f4h9s') - ->will($this->throwException(new \Exception('some crypto error occurred'))); - $tokenProvider->expects($this->once()) - ->method('invalidateToken') - ->with($token); - - $tokenProvider->getPassword($tk, $token); - } - - public function testSetPassword() { - $token = new DefaultToken(); - $tokenId = 'token123'; - $password = '123456'; - - $this->config->expects($this->once()) - ->method('getSystemValue') - ->with('secret') - ->willReturn('ocsecret'); - $this->crypto->expects($this->once()) - ->method('encrypt') - ->with($password, $tokenId . 'ocsecret') - ->willReturn('encryptedpassword'); - $this->mapper->expects($this->once()) - ->method('update') - ->with($token); - - $this->tokenProvider->setPassword($token, $tokenId, $password); - - $this->assertEquals('encryptedpassword', $token->getPassword()); - } - - - public function testSetPasswordInvalidToken() { - $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class); - - $token = $this->createMock(IToken::class); - $tokenId = 'token123'; - $password = '123456'; - - $this->tokenProvider->setPassword($token, $tokenId, $password); - } - - public function testInvalidateToken() { - $this->mapper->expects($this->once()) - ->method('invalidate') - ->with(hash('sha512', 'token7')); - - $this->tokenProvider->invalidateToken('token7'); - } - - public function testInvaildateTokenById() { - $id = 123; - - $this->mapper->expects($this->once()) - ->method('deleteById') - ->with('uid', $id); - - $this->tokenProvider->invalidateTokenById('uid', $id); - } - - public function testInvalidateOldTokens() { - $defaultSessionLifetime = 60 * 60 * 24; - $defaultRememberMeLifetime = 60 * 60 * 24 * 15; - $this->config->expects($this->exactly(2)) - ->method('getSystemValue') - ->willReturnMap([ - ['session_lifetime', $defaultSessionLifetime, 150], - ['remember_login_cookie_lifetime', $defaultRememberMeLifetime, 300], - ]); - $this->mapper->expects($this->at(0)) - ->method('invalidateOld') - ->with($this->time - 150); - $this->mapper->expects($this->at(1)) - ->method('invalidateOld') - ->with($this->time - 300); - - $this->tokenProvider->invalidateOldTokens(); - } - - public function testRenewSessionTokenWithoutPassword() { - $token = $this->getMockBuilder(DefaultToken::class) - ->disableOriginalConstructor() - ->setMethods(['getUID', 'getLoginName', 'getPassword', 'getName', 'getRemember']) - ->getMock(); - $token - ->expects($this->at(0)) - ->method('getUID') - ->willReturn('UserUid'); - $token - ->expects($this->at(1)) - ->method('getLoginName') - ->willReturn('UserLoginName'); - $token - ->expects($this->at(2)) - ->method('getPassword') - ->willReturn(null); - $token - ->expects($this->at(3)) - ->method('getName') - ->willReturn('MyTokenName'); - $token - ->expects($this->at(4)) - ->method('getRemember') - ->willReturn(IToken::DO_NOT_REMEMBER); - $this->config - ->expects($this->exactly(2)) - ->method('getSystemValue') - ->with('secret') - ->willReturn('MyInstanceSecret'); - $this->mapper - ->expects($this->at(0)) - ->method('getToken') - ->with(hash('sha512', 'oldId' . 'MyInstanceSecret')) - ->willReturn($token); - $newToken = new DefaultToken(); - $newToken->setUid('UserUid'); - $newToken->setLoginName('UserLoginName'); - $newToken->setName('MyTokenName'); - $newToken->setToken(hash('sha512', 'newId' . 'MyInstanceSecret')); - $newToken->setType(IToken::TEMPORARY_TOKEN); - $newToken->setRemember(IToken::DO_NOT_REMEMBER); - $newToken->setLastActivity(1313131); - $this->mapper - ->expects($this->at(1)) - ->method('insert') - ->with($newToken); - $this->mapper - ->expects($this->at(2)) - ->method('delete') - ->with($token); - - $this->tokenProvider->renewSessionToken('oldId', 'newId'); - } - - public function testRenewSessionTokenWithPassword() { - $token = $this->getMockBuilder(DefaultToken::class) - ->disableOriginalConstructor() - ->setMethods(['getUID', 'getLoginName', 'getPassword', 'getName', 'getRemember']) - ->getMock(); - $token - ->expects($this->at(0)) - ->method('getUID') - ->willReturn('UserUid'); - $token - ->expects($this->at(1)) - ->method('getLoginName') - ->willReturn('UserLoginName'); - $token - ->expects($this->at(2)) - ->method('getPassword') - ->willReturn('EncryptedPassword'); - $token - ->expects($this->at(3)) - ->method('getPassword') - ->willReturn('EncryptedPassword'); - $token - ->expects($this->at(4)) - ->method('getName') - ->willReturn('MyTokenName'); - $token - ->expects($this->at(5)) - ->method('getRemember') - ->willReturn(IToken::REMEMBER); - $this->crypto - ->expects($this->any(0)) - ->method('decrypt') - ->with('EncryptedPassword', 'oldIdMyInstanceSecret') - ->willReturn('ClearTextPassword'); - $this->crypto - ->expects($this->any(1)) - ->method('encrypt') - ->with('ClearTextPassword', 'newIdMyInstanceSecret') - ->willReturn('EncryptedPassword'); - $this->config - ->expects($this->exactly(4)) - ->method('getSystemValue') - ->with('secret') - ->willReturn('MyInstanceSecret'); - $this->mapper - ->expects($this->at(0)) - ->method('getToken') - ->with(hash('sha512', 'oldId' . 'MyInstanceSecret')) - ->willReturn($token); - $newToken = new DefaultToken(); - $newToken->setUid('UserUid'); - $newToken->setLoginName('UserLoginName'); - $newToken->setName('MyTokenName'); - $newToken->setToken(hash('sha512', 'newId' . 'MyInstanceSecret')); - $newToken->setType(IToken::TEMPORARY_TOKEN); - $newToken->setRemember(IToken::REMEMBER); - $newToken->setLastActivity(1313131); - $newToken->setPassword('EncryptedPassword'); - $this->mapper - ->expects($this->at(1)) - ->method('insert') - ->with($this->equalTo($newToken)); - $this->mapper - ->expects($this->at(2)) - ->method('delete') - ->with($token); - - $this->tokenProvider->renewSessionToken('oldId', 'newId'); - } - - public function testGetToken() { - $token = new DefaultToken(); - - $this->config->method('getSystemValue') - ->with('secret') - ->willReturn('mysecret'); - - $this->mapper->method('getToken') - ->with( - $this->callback(function (string $token) { - return hash('sha512', 'unhashedTokenmysecret') === $token; - }) - )->willReturn($token); - - $this->assertSame($token, $this->tokenProvider->getToken('unhashedToken')); - } - - public function testGetInvalidToken() { - $this->expectException(InvalidTokenException::class); - - $this->config->method('getSystemValue') - ->with('secret') - ->willReturn('mysecret'); - - $this->mapper->method('getToken') - ->with( - $this->callback(function (string $token) { - return hash('sha512', 'unhashedTokenmysecret') === $token; - }) - )->willThrowException(new InvalidTokenException()); - - $this->tokenProvider->getToken('unhashedToken'); - } - - public function testGetExpiredToken() { - $token = new DefaultToken(); - $token->setExpires(42); - - $this->config->method('getSystemValue') - ->with('secret') - ->willReturn('mysecret'); - - $this->mapper->method('getToken') - ->with( - $this->callback(function (string $token) { - return hash('sha512', 'unhashedTokenmysecret') === $token; - }) - )->willReturn($token); - - try { - $this->tokenProvider->getToken('unhashedToken'); - } catch (ExpiredTokenException $e) { - $this->assertSame($token, $e->getToken()); - } - } - - public function testGetTokenById() { - $token = $this->createMock(DefaultToken::class); - - $this->mapper->expects($this->once()) - ->method('getTokenById') - ->with($this->equalTo(42)) - ->willReturn($token); - - $this->assertSame($token, $this->tokenProvider->getTokenById(42)); - } - - public function testGetInvalidTokenById() { - $this->expectException(InvalidTokenException::class); - - $this->mapper->expects($this->once()) - ->method('getTokenById') - ->with($this->equalTo(42)) - ->willThrowException(new DoesNotExistException('nope')); - - $this->tokenProvider->getTokenById(42); - } - - public function testGetExpiredTokenById() { - $token = new DefaultToken(); - $token->setExpires(42); - - $this->mapper->expects($this->once()) - ->method('getTokenById') - ->with($this->equalTo(42)) - ->willReturn($token); - - try { - $this->tokenProvider->getTokenById(42); - $this->fail(); - } catch (ExpiredTokenException $e) { - $this->assertSame($token, $e->getToken()); - } - } - - public function testRotate() { - $token = new DefaultToken(); - $token->setPassword('oldencryptedpassword'); - - $this->config->method('getSystemValue') - ->with('secret') - ->willReturn('mysecret'); - - $this->crypto->method('decrypt') - ->with('oldencryptedpassword', 'oldtokenmysecret') - ->willReturn('mypassword'); - $this->crypto->method('encrypt') - ->with('mypassword', 'newtokenmysecret') - ->willReturn('newencryptedpassword'); - - $this->mapper->expects($this->once()) - ->method('update') - ->with($this->callback(function (DefaultToken $token) { - return $token->getPassword() === 'newencryptedpassword' && - $token->getToken() === hash('sha512', 'newtokenmysecret'); - })); - - $this->tokenProvider->rotate($token, 'oldtoken', 'newtoken'); - } - - public function testRotateNoPassword() { - $token = new DefaultToken(); - - $this->config->method('getSystemValue') - ->with('secret') - ->willReturn('mysecret'); - - $this->mapper->expects($this->once()) - ->method('update') - ->with($this->callback(function (DefaultToken $token) { - return $token->getPassword() === null && - $token->getToken() === hash('sha512', 'newtokenmysecret'); - })); - - $this->tokenProvider->rotate($token, 'oldtoken', 'newtoken'); - } - - public function testMarkPasswordInvalidInvalidToken() { - $token = $this->createMock(PublicKeyToken::class); - - $this->expectException(InvalidTokenException::class); - - $this->tokenProvider->markPasswordInvalid($token, 'tokenId'); - } - - public function testMarkPasswordInvalid() { - $token = $this->createMock(DefaultToken::class); - - $this->mapper->expects($this->once()) - ->method('invalidate') - ->with('0c7db0098fe8ddba6032b22719ec18867c69a1820fa36d71c28bf96d52843bdc44a112bd24093b049be5bb54769bcb72d67190a4a9690e51aac263cba38186fb'); - - $this->tokenProvider->markPasswordInvalid($token, 'tokenId'); - } -} diff --git a/tests/lib/Authentication/Token/DefaultTokenTest.php b/tests/lib/Authentication/Token/DefaultTokenTest.php deleted file mode 100644 index 76b976586a9..00000000000 --- a/tests/lib/Authentication/Token/DefaultTokenTest.php +++ /dev/null @@ -1,41 +0,0 @@ -<?php -/** - * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl> - * - * @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\Token; - -use OC\Authentication\Token\DefaultToken; -use Test\TestCase; - -class DefaultTokenTest extends TestCase { - public function testSetScopeAsArray() { - $scope = ['filesystem' => false]; - $token = new DefaultToken(); - $token->setScope($scope); - $this->assertEquals(json_encode($scope), $token->getScope()); - $this->assertEquals($scope, $token->getScopeAsArray()); - } - - public function testDefaultScope() { - $scope = ['filesystem' => true]; - $token = new DefaultToken(); - $this->assertEquals($scope, $token->getScopeAsArray()); - } -} |