diff options
Diffstat (limited to 'tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php')
-rw-r--r-- | tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php | 77 |
1 files changed, 26 insertions, 51 deletions
diff --git a/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php b/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php index 68962b26931..d1585dadc26 100644 --- a/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php +++ b/tests/lib/Authentication/Token/PublicKeyTokenMapperTest.php @@ -2,36 +2,20 @@ declare(strict_types=1); /** - * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl> - * - * @author Roeland Jago Douma <roeland@famdouma.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/>. - * + * SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later */ namespace Test\Authentication\Token; -use OC; -use OC\Authentication\Token\IToken; use OC\Authentication\Token\PublicKeyToken; use OC\Authentication\Token\PublicKeyTokenMapper; +use OCP\AppFramework\Db\DoesNotExistException; +use OCP\Authentication\Token\IToken; use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\IDBConnection; use OCP\IUser; +use OCP\Server; use Test\TestCase; /** @@ -50,7 +34,7 @@ class PublicKeyTokenMapperTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->dbConnection = OC::$server->getDatabaseConnection(); + $this->dbConnection = Server::get(IDBConnection::class); $this->time = time(); $this->resetDatabase(); @@ -135,10 +119,10 @@ class PublicKeyTokenMapperTest extends TestCase { ->from('authtoken') ->execute() ->fetch(); - return (int) $result['count']; + return (int)$result['count']; } - public function testInvalidate() { + public function testInvalidate(): void { $token = '9c5a2e661482b65597408a6bb6c4a3d1af36337381872ac56e445a06cdb7fea2b1039db707545c11027a4966919918b19d875a8b774840b18c6cbb7ae56fe206'; $this->mapper->invalidate($token); @@ -146,7 +130,7 @@ class PublicKeyTokenMapperTest extends TestCase { $this->assertSame(4, $this->getNumberOfTokens()); } - public function testInvalidateInvalid() { + public function testInvalidateInvalid(): void { $token = 'youwontfindthisoneinthedatabase'; $this->mapper->invalidate($token); @@ -154,7 +138,7 @@ class PublicKeyTokenMapperTest extends TestCase { $this->assertSame(5, $this->getNumberOfTokens()); } - public function testInvalidateOld() { + public function testInvalidateOld(): void { $olderThan = $this->time - 60 * 60; // One hour $this->mapper->invalidateOld($olderThan); @@ -162,7 +146,7 @@ class PublicKeyTokenMapperTest extends TestCase { $this->assertSame(4, $this->getNumberOfTokens()); } - public function testInvalidateLastUsedBefore() { + public function testInvalidateLastUsedBefore(): void { $before = $this->time - 60 * 2; // Two minutes $this->mapper->invalidateLastUsedBefore('user3', $before); @@ -170,7 +154,7 @@ class PublicKeyTokenMapperTest extends TestCase { $this->assertSame(4, $this->getNumberOfTokens()); } - public function testGetToken() { + public function testGetToken(): void { $token = new PublicKeyToken(); $token->setUid('user2'); $token->setLoginName('User2'); @@ -194,15 +178,15 @@ class PublicKeyTokenMapperTest extends TestCase { } - public function testGetInvalidToken() { - $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); + public function testGetInvalidToken(): void { + $this->expectException(DoesNotExistException::class); $token = 'thisisaninvalidtokenthatisnotinthedatabase'; $this->mapper->getToken($token); } - public function testGetTokenById() { + public function testGetTokenById(): void { $token = new PublicKeyToken(); $token->setUid('user2'); $token->setLoginName('User2'); @@ -226,30 +210,30 @@ class PublicKeyTokenMapperTest extends TestCase { } - public function testGetTokenByIdNotFound() { - $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); + public function testGetTokenByIdNotFound(): void { + $this->expectException(DoesNotExistException::class); $this->mapper->getTokenById(-1); } - public function testGetInvalidTokenById() { - $this->expectException(\OCP\AppFramework\Db\DoesNotExistException::class); + public function testGetInvalidTokenById(): void { + $this->expectException(DoesNotExistException::class); $id = '42'; $this->mapper->getToken($id); } - public function testGetTokenByUser() { + public function testGetTokenByUser(): void { $this->assertCount(2, $this->mapper->getTokenByUser('user1')); } - public function testGetTokenByUserNotFound() { + public function testGetTokenByUserNotFound(): void { $this->assertCount(0, $this->mapper->getTokenByUser('user1000')); } - public function testDeleteById() { + public function testGetById(): void { /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ $user = $this->createMock(IUser::class); $qb = $this->dbConnection->getQueryBuilder(); @@ -259,20 +243,11 @@ class PublicKeyTokenMapperTest extends TestCase { $result = $qb->execute(); $id = $result->fetch()['id']; - $this->mapper->deleteById('user1', (int)$id); - $this->assertEquals(4, $this->getNumberOfTokens()); - } - - public function testDeleteByIdWrongUser() { - /** @var IUser|\PHPUnit\Framework\MockObject\MockObject $user */ - $user = $this->createMock(IUser::class); - $id = 33; - - $this->mapper->deleteById('user1000', $id); - $this->assertEquals(5, $this->getNumberOfTokens()); + $token = $this->mapper->getTokenById((int)$id); + $this->assertEquals('user1', $token->getUID()); } - public function testDeleteByName() { + public function testDeleteByName(): void { $qb = $this->dbConnection->getQueryBuilder(); $qb->select('name') ->from('authtoken') @@ -283,7 +258,7 @@ class PublicKeyTokenMapperTest extends TestCase { $this->assertEquals(4, $this->getNumberOfTokens()); } - public function testHasExpiredTokens() { + public function testHasExpiredTokens(): void { $this->assertFalse($this->mapper->hasExpiredTokens('user1')); $this->assertTrue($this->mapper->hasExpiredTokens('user3')); } |