aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/Authentication/Token/ManagerTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/Authentication/Token/ManagerTest.php')
-rw-r--r--tests/lib/Authentication/Token/ManagerTest.php395
1 files changed, 137 insertions, 258 deletions
diff --git a/tests/lib/Authentication/Token/ManagerTest.php b/tests/lib/Authentication/Token/ManagerTest.php
index fb92b3e5018..58bbe236248 100644
--- a/tests/lib/Authentication/Token/ManagerTest.php
+++ b/tests/lib/Authentication/Token/ManagerTest.php
@@ -3,34 +3,14 @@
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 Doctrine\DBAL\Exception\UniqueConstraintViolationException;
use OC\Authentication\Exceptions\InvalidTokenException;
-use OC\Authentication\Exceptions\PasswordlessTokenException;
-use OC\Authentication\Token\DefaultToken;
-use OC\Authentication\Token\DefaultTokenProvider;
use OC\Authentication\Token\IToken;
use OC\Authentication\Token\Manager;
use OC\Authentication\Token\PublicKeyToken;
@@ -39,11 +19,8 @@ use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class ManagerTest extends TestCase {
-
/** @var PublicKeyTokenProvider|MockObject */
private $publicKeyTokenProvider;
- /** @var DefaultTokenProvider|MockObject */
- private $defaultTokenProvider;
/** @var Manager */
private $manager;
@@ -51,17 +28,12 @@ class ManagerTest extends TestCase {
parent::setUp();
$this->publicKeyTokenProvider = $this->createMock(PublicKeyTokenProvider::class);
- $this->defaultTokenProvider = $this->createMock(DefaultTokenProvider::class);
$this->manager = new Manager(
- $this->defaultTokenProvider,
$this->publicKeyTokenProvider
);
}
- public function testGenerateToken() {
- $this->defaultTokenProvider->expects($this->never())
- ->method('generateToken');
-
+ public function testGenerateToken(): void {
$token = new PublicKeyToken();
$this->publicKeyTokenProvider->expects($this->once())
@@ -89,11 +61,9 @@ class ManagerTest extends TestCase {
$this->assertSame($token, $actual);
}
- public function testGenerateConflictingToken() {
+ public function testGenerateConflictingToken(): void {
/** @var MockObject|UniqueConstraintViolationException $exception */
$exception = $this->createMock(UniqueConstraintViolationException::class);
- $this->defaultTokenProvider->expects($this->never())
- ->method('generateToken');
$token = new PublicKeyToken();
$token->setUid('uid');
@@ -127,20 +97,45 @@ class ManagerTest extends TestCase {
$this->assertSame($token, $actual);
}
- public function tokenData(): array {
+ public function testGenerateTokenTooLongName(): void {
+ $token = $this->createMock(IToken::class);
+ $token->method('getName')
+ ->willReturn(str_repeat('a', 120) . '…');
+
+
+ $this->publicKeyTokenProvider->expects($this->once())
+ ->method('generateToken')
+ ->with(
+ 'token',
+ 'uid',
+ 'loginName',
+ 'password',
+ str_repeat('a', 120) . '…',
+ IToken::TEMPORARY_TOKEN,
+ IToken::REMEMBER
+ )->willReturn($token);
+
+ $actual = $this->manager->generateToken(
+ 'token',
+ 'uid',
+ 'loginName',
+ 'password',
+ str_repeat('a', 200),
+ IToken::TEMPORARY_TOKEN,
+ IToken::REMEMBER
+ );
+
+ $this->assertSame(121, mb_strlen($actual->getName()));
+ }
+
+ public static function tokenData(): array {
return [
- [new DefaultToken()],
[new PublicKeyToken()],
- [$this->createMock(IToken::class)],
+ [IToken::class],
];
}
protected function setNoCall(IToken $token) {
- if (!($token instanceof DefaultToken)) {
- $this->defaultTokenProvider->expects($this->never())
- ->method($this->anything());
- }
-
if (!($token instanceof PublicKeyToken)) {
$this->publicKeyTokenProvider->expects($this->never())
->method($this->anything());
@@ -148,13 +143,6 @@ class ManagerTest extends TestCase {
}
protected function setCall(IToken $token, string $function, $return = null) {
- if ($token instanceof DefaultToken) {
- $this->defaultTokenProvider->expects($this->once())
- ->method($function)
- ->with($token)
- ->willReturn($return);
- }
-
if ($token instanceof PublicKeyToken) {
$this->publicKeyTokenProvider->expects($this->once())
->method($function)
@@ -164,15 +152,17 @@ class ManagerTest extends TestCase {
}
protected function setException(IToken $token) {
- if (!($token instanceof DefaultToken) && !($token instanceof PublicKeyToken)) {
+ if (!($token instanceof PublicKeyToken)) {
$this->expectException(InvalidTokenException::class);
}
}
- /**
- * @dataProvider tokenData
- */
- public function testUpdateToken(IToken $token) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ public function testUpdateToken(IToken|string $token): void {
+ if (is_string($token)) {
+ $token = $this->createMock($token);
+ }
+
$this->setNoCall($token);
$this->setCall($token, 'updateToken');
$this->setException($token);
@@ -180,10 +170,12 @@ class ManagerTest extends TestCase {
$this->manager->updateToken($token);
}
- /**
- * @dataProvider tokenData
- */
- public function testUpdateTokenActivity(IToken $token) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ public function testUpdateTokenActivity(IToken|string $token): void {
+ if (is_string($token)) {
+ $token = $this->createMock($token);
+ }
+
$this->setNoCall($token);
$this->setCall($token, 'updateTokenActivity');
$this->setException($token);
@@ -191,10 +183,12 @@ class ManagerTest extends TestCase {
$this->manager->updateTokenActivity($token);
}
- /**
- * @dataProvider tokenData
- */
- public function testGetPassword(IToken $token) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ public function testGetPassword(IToken|string $token): void {
+ if (is_string($token)) {
+ $token = $this->createMock($token);
+ }
+
$this->setNoCall($token);
$this->setCall($token, 'getPassword', 'password');
$this->setException($token);
@@ -204,10 +198,12 @@ class ManagerTest extends TestCase {
$this->assertSame('password', $result);
}
- /**
- * @dataProvider tokenData
- */
- public function testSetPassword(IToken $token) {
+ #[\PHPUnit\Framework\Attributes\DataProvider('tokenData')]
+ public function testSetPassword(IToken|string $token): void {
+ if (is_string($token)) {
+ $token = $this->createMock($token);
+ }
+
$this->setNoCall($token);
$this->setCall($token, 'setPassword');
$this->setException($token);
@@ -215,11 +211,7 @@ class ManagerTest extends TestCase {
$this->manager->setPassword($token, 'tokenId', 'password');
}
- public function testInvalidateTokens() {
- $this->defaultTokenProvider->expects($this->once())
- ->method('invalidateToken')
- ->with('token');
-
+ public function testInvalidateTokens(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('invalidateToken')
->with('token');
@@ -227,11 +219,7 @@ class ManagerTest extends TestCase {
$this->manager->invalidateToken('token');
}
- public function testInvalidateTokenById() {
- $this->defaultTokenProvider->expects($this->once())
- ->method('invalidateTokenById')
- ->with('uid', 42);
-
+ public function testInvalidateTokenById(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('invalidateTokenById')
->with('uid', 42);
@@ -239,39 +227,35 @@ class ManagerTest extends TestCase {
$this->manager->invalidateTokenById('uid', 42);
}
- public function testInvalidateOldTokens() {
- $this->defaultTokenProvider->expects($this->once())
- ->method('invalidateOldTokens');
-
+ public function testInvalidateOldTokens(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('invalidateOldTokens');
$this->manager->invalidateOldTokens();
}
- public function testGetTokenByUser() {
- $t1 = new DefaultToken();
- $t2 = new DefaultToken();
- $t3 = new PublicKeyToken();
- $t4 = new PublicKeyToken();
+ public function testInvalidateLastUsedBefore(): void {
+ $this->publicKeyTokenProvider->expects($this->once())
+ ->method('invalidateLastUsedBefore')
+ ->with('user', 946684800);
- $this->defaultTokenProvider
- ->method('getTokenByUser')
- ->willReturn([$t1, $t2]);
+ $this->manager->invalidateLastUsedBefore('user', 946684800);
+ }
+
+ public function testGetTokenByUser(): void {
+ $t1 = new PublicKeyToken();
+ $t2 = new PublicKeyToken();
$this->publicKeyTokenProvider
->method('getTokenByUser')
- ->willReturn([$t3, $t4]);
+ ->willReturn([$t1, $t2]);
$result = $this->manager->getTokenByUser('uid');
- $this->assertEquals([$t1, $t2, $t3, $t4], $result);
+ $this->assertEquals([$t1, $t2], $result);
}
- public function testRenewSessionTokenPublicKey() {
- $this->defaultTokenProvider->expects($this->never())
- ->method($this->anything());
-
+ public function testRenewSessionTokenPublicKey(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('renewSessionToken')
->with('oldId', 'newId');
@@ -279,35 +263,17 @@ class ManagerTest extends TestCase {
$this->manager->renewSessionToken('oldId', 'newId');
}
- public function testRenewSessionTokenDefault() {
+ public function testRenewSessionInvalid(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('renewSessionToken')
->with('oldId', 'newId')
->willThrowException(new InvalidTokenException());
- $this->defaultTokenProvider->expects($this->once())
- ->method('renewSessionToken')
- ->with('oldId', 'newId');
-
- $this->manager->renewSessionToken('oldId', 'newId');
- }
-
- public function testRenewSessionInvalid() {
- $this->publicKeyTokenProvider->expects($this->once())
- ->method('renewSessionToken')
- ->with('oldId', 'newId')
- ->willThrowException(new InvalidTokenException());
-
- $this->defaultTokenProvider->expects($this->once())
- ->method('renewSessionToken')
- ->with('oldId', 'newId')
- ->willThrowException(new InvalidTokenException());
-
$this->expectException(InvalidTokenException::class);
$this->manager->renewSessionToken('oldId', 'newId');
}
- public function testGetTokenByIdPublicKey() {
+ public function testGetTokenByIdPublicKey(): void {
$token = $this->createMock(IToken::class);
$this->publicKeyTokenProvider->expects($this->once())
@@ -315,50 +281,22 @@ class ManagerTest extends TestCase {
->with(42)
->willReturn($token);
- $this->defaultTokenProvider->expects($this->never())
- ->method($this->anything());
-
-
$this->assertSame($token, $this->manager->getTokenById(42));
}
- public function testGetTokenByIdDefault() {
- $token = $this->createMock(IToken::class);
-
+ public function testGetTokenByIdInvalid(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('getTokenById')
->with(42)
->willThrowException(new InvalidTokenException());
- $this->defaultTokenProvider->expects($this->once())
- ->method('getTokenById')
- ->with(42)
- ->willReturn($token);
-
- $this->assertSame($token, $this->manager->getTokenById(42));
- }
-
- public function testGetTokenByIdInvalid() {
- $this->publicKeyTokenProvider->expects($this->once())
- ->method('getTokenById')
- ->with(42)
- ->willThrowException(new InvalidTokenException());
-
- $this->defaultTokenProvider->expects($this->once())
- ->method('getTokenById')
- ->with(42)
- ->willThrowException(new InvalidTokenException());
-
$this->expectException(InvalidTokenException::class);
$this->manager->getTokenById(42);
}
- public function testGetTokenPublicKey() {
+ public function testGetTokenPublicKey(): void {
$token = new PublicKeyToken();
- $this->defaultTokenProvider->expects($this->never())
- ->method($this->anything());
-
$this->publicKeyTokenProvider
->method('getToken')
->with('tokenId')
@@ -367,12 +305,7 @@ class ManagerTest extends TestCase {
$this->assertSame($token, $this->manager->getToken('tokenId'));
}
- public function testGetTokenInvalid() {
- $this->defaultTokenProvider
- ->method('getToken')
- ->with('tokenId')
- ->willThrowException(new InvalidTokenException());
-
+ public function testGetTokenInvalid(): void {
$this->publicKeyTokenProvider
->method('getToken')
->with('tokenId')
@@ -382,64 +315,12 @@ class ManagerTest extends TestCase {
$this->manager->getToken('tokenId');
}
- public function testGetTokenConvertPassword() {
- $oldToken = new DefaultToken();
- $newToken = new PublicKeyToken();
-
- $this->publicKeyTokenProvider
- ->method('getToken')
- ->with('tokenId')
- ->willThrowException(new InvalidTokenException());
-
- $this->defaultTokenProvider
- ->method('getToken')
- ->willReturn($oldToken);
-
- $this->defaultTokenProvider
- ->method('getPassword')
- ->with($oldToken, 'tokenId')
- ->willReturn('password');
-
- $this->publicKeyTokenProvider
- ->method('convertToken')
- ->with($oldToken, 'tokenId', 'password')
- ->willReturn($newToken);
-
- $this->assertSame($newToken, $this->manager->getToken('tokenId'));
- }
-
- public function testGetTokenConvertNoPassword() {
- $oldToken = new DefaultToken();
- $newToken = new PublicKeyToken();
-
- $this->publicKeyTokenProvider
- ->method('getToken')
- ->with('tokenId')
- ->willThrowException(new InvalidTokenException());
-
- $this->defaultTokenProvider
- ->method('getToken')
- ->willReturn($oldToken);
-
- $this->defaultTokenProvider
- ->method('getPassword')
- ->with($oldToken, 'tokenId')
- ->willThrowException(new PasswordlessTokenException());
-
- $this->publicKeyTokenProvider
- ->method('convertToken')
- ->with($oldToken, 'tokenId', null)
- ->willReturn($newToken);
-
- $this->assertSame($newToken, $this->manager->getToken('tokenId'));
- }
-
- public function testRotateInvalid() {
+ public function testRotateInvalid(): void {
$this->expectException(InvalidTokenException::class);
$this->manager->rotate($this->createMock(IToken::class), 'oldId', 'newId');
}
- public function testRotatePublicKey() {
+ public function testRotatePublicKey(): void {
$token = new PublicKeyToken();
$this->publicKeyTokenProvider
@@ -450,78 +331,76 @@ class ManagerTest extends TestCase {
$this->assertSame($token, $this->manager->rotate($token, 'oldId', 'newId'));
}
- public function testRotateConvertPassword() {
- $oldToken = new DefaultToken();
- $newToken = new PublicKeyToken();
-
- $this->defaultTokenProvider
- ->method('getPassword')
- ->with($oldToken, 'oldId')
- ->willReturn('password');
-
- $this->publicKeyTokenProvider
- ->method('convertToken')
- ->with($oldToken, 'newId', 'password')
- ->willReturn($newToken);
-
- $this->assertSame($newToken, $this->manager->rotate($oldToken, 'oldId', 'newId'));
- }
-
- public function testRotateConvertNoPassword() {
- $oldToken = new DefaultToken();
- $newToken = new PublicKeyToken();
-
- $this->defaultTokenProvider
- ->method('getPassword')
- ->with($oldToken, 'oldId')
- ->willThrowException(new PasswordlessTokenException());
-
- $this->publicKeyTokenProvider
- ->method('convertToken')
- ->with($oldToken, 'newId', null)
- ->willReturn($newToken);
-
- $this->assertSame($newToken, $this->manager->rotate($oldToken, 'oldId', 'newId'));
- }
-
- public function testMarkPasswordInvalidDefault() {
- $token = $this->createMock(DefaultToken::class);
-
- $this->defaultTokenProvider->expects($this->once())
- ->method('markPasswordInvalid')
- ->with($token, 'tokenId');
- $this->publicKeyTokenProvider->expects($this->never())
- ->method($this->anything());
-
- $this->manager->markPasswordInvalid($token, 'tokenId');
- }
-
- public function testMarkPasswordInvalidPublicKey() {
+ public function testMarkPasswordInvalidPublicKey(): void {
$token = $this->createMock(PublicKeyToken::class);
$this->publicKeyTokenProvider->expects($this->once())
->method('markPasswordInvalid')
->with($token, 'tokenId');
- $this->defaultTokenProvider->expects($this->never())
- ->method($this->anything());
$this->manager->markPasswordInvalid($token, 'tokenId');
}
- public function testMarkPasswordInvalidInvalidToken() {
+ public function testMarkPasswordInvalidInvalidToken(): void {
$this->expectException(InvalidTokenException::class);
$this->manager->markPasswordInvalid($this->createMock(IToken::class), 'tokenId');
}
- public function testUpdatePasswords() {
- $this->defaultTokenProvider->expects($this->once())
- ->method('updatePasswords')
- ->with('uid', 'pass');
+ public function testUpdatePasswords(): void {
$this->publicKeyTokenProvider->expects($this->once())
->method('updatePasswords')
->with('uid', 'pass');
$this->manager->updatePasswords('uid', 'pass');
}
+
+ public function testInvalidateTokensOfUserNoClientName(): void {
+ $t1 = new PublicKeyToken();
+ $t2 = new PublicKeyToken();
+ $t1->setId(123);
+ $t2->setId(456);
+
+ $this->publicKeyTokenProvider
+ ->expects($this->once())
+ ->method('getTokenByUser')
+ ->with('theUser')
+ ->willReturn([$t1, $t2]);
+
+ $calls = [
+ ['theUser', 123],
+ ['theUser', 456],
+ ];
+ $this->publicKeyTokenProvider
+ ->expects($this->exactly(2))
+ ->method('invalidateTokenById')
+ ->willReturnCallback(function () use (&$calls): void {
+ $expected = array_shift($calls);
+ $this->assertEquals($expected, func_get_args());
+ });
+ $this->manager->invalidateTokensOfUser('theUser', null);
+ }
+
+ public function testInvalidateTokensOfUserClientNameGiven(): void {
+ $t1 = new PublicKeyToken();
+ $t2 = new PublicKeyToken();
+ $t3 = new PublicKeyToken();
+ $t1->setId(123);
+ $t1->setName('Firefox session');
+ $t2->setId(456);
+ $t2->setName('My Client Name');
+ $t3->setId(789);
+ $t3->setName('mobile client');
+
+ $this->publicKeyTokenProvider
+ ->expects($this->once())
+ ->method('getTokenByUser')
+ ->with('theUser')
+ ->willReturn([$t1, $t2, $t3]);
+ $this->publicKeyTokenProvider
+ ->expects($this->once())
+ ->method('invalidateTokenById')
+ ->with('theUser', 456);
+ $this->manager->invalidateTokensOfUser('theUser', 'My Client Name');
+ }
}