mirror of
https://github.com/nextcloud/server.git
synced 2026-05-28 04:32:30 -04:00
Add tests for occ user:auth-tokens:delete
Signed-off-by: Lucas Azevedo <lhs_azevedo@hotmail.com>
This commit is contained in:
parent
fe9b9c1955
commit
771a7b92cc
4 changed files with 214 additions and 6 deletions
170
tests/Core/Command/User/AuthTokens/DeleteTest.php
Normal file
170
tests/Core/Command/User/AuthTokens/DeleteTest.php
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2023 Lucas Azevedo <lhs_azevedo@hotmail.com>
|
||||
*
|
||||
* @author Lucas Azevedo <lhs_azevedo@hotmail.com>
|
||||
*
|
||||
* @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 Tests\Core\Command\User\AuthTokens;
|
||||
|
||||
use OC\Core\Command\User\AuthTokens\Delete;
|
||||
use OC\Authentication\Token\IProvider;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Exception\RuntimeException;
|
||||
use Symfony\Component\Console\Input\InputInterface;
|
||||
use Symfony\Component\Console\Output\OutputInterface;
|
||||
use Test\TestCase;
|
||||
|
||||
class DeleteTest extends TestCase {
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
||||
protected $tokenProvider;
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
||||
protected $consoleInput;
|
||||
/** @var \PHPUnit\Framework\MockObject\MockObject */
|
||||
protected $consoleOutput;
|
||||
|
||||
/** @var \Symfony\Component\Console\Command\Command */
|
||||
protected $command;
|
||||
|
||||
protected function setUp(): void {
|
||||
parent::setUp();
|
||||
|
||||
$tokenProvider = $this->tokenProvider = $this->getMockBuilder(IProvider::class)
|
||||
->disableOriginalConstructor()
|
||||
->getMock();
|
||||
$this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
|
||||
$this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
|
||||
|
||||
/** @var \OC\Authentication\Token\IProvider $tokenProvider */
|
||||
$this->command = new Delete($tokenProvider);
|
||||
}
|
||||
|
||||
public function testDeleteTokenById() {
|
||||
$this->consoleInput->expects($this->exactly(2))
|
||||
->method('getArgument')
|
||||
->withConsecutive(['uid'], ['id'])
|
||||
->willReturnOnConsecutiveCalls('user', 42);
|
||||
|
||||
$this->consoleInput->expects($this->once())
|
||||
->method('getOption')
|
||||
->with('last-used-before')
|
||||
->willReturn(null);
|
||||
|
||||
$this->tokenProvider->expects($this->once())
|
||||
->method('invalidateTokenById')
|
||||
->with('user', 42);
|
||||
|
||||
$result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
||||
$this->assertSame(Command::SUCCESS, $result);
|
||||
}
|
||||
|
||||
public function testDeleteTokenByIdRequiresTokenId() {
|
||||
$this->consoleInput->expects($this->exactly(2))
|
||||
->method('getArgument')
|
||||
->withConsecutive(['uid'], ['id'])
|
||||
->willReturnOnConsecutiveCalls('user', null);
|
||||
|
||||
$this->consoleInput->expects($this->once())
|
||||
->method('getOption')
|
||||
->with('last-used-before')
|
||||
->willReturn(null);
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
$this->tokenProvider->expects($this->never())->method('invalidateTokenById');
|
||||
|
||||
$result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
||||
$this->assertSame(Command::FAILURE, $result);
|
||||
}
|
||||
|
||||
public function testDeleteTokensLastUsedBefore() {
|
||||
$this->consoleInput->expects($this->exactly(2))
|
||||
->method('getArgument')
|
||||
->withConsecutive(['uid'], ['id'])
|
||||
->willReturnOnConsecutiveCalls('user', null);
|
||||
|
||||
$this->consoleInput->expects($this->once())
|
||||
->method('getOption')
|
||||
->with('last-used-before')
|
||||
->willReturn('946684800');
|
||||
|
||||
$this->tokenProvider->expects($this->once())
|
||||
->method('invalidateLastUsedBefore')
|
||||
->with('user', 946684800);
|
||||
|
||||
$result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
||||
$this->assertSame(Command::SUCCESS, $result);
|
||||
}
|
||||
|
||||
public function testLastUsedBeforeAcceptsIso8601Expanded() {
|
||||
$this->consoleInput->expects($this->exactly(2))
|
||||
->method('getArgument')
|
||||
->withConsecutive(['uid'], ['id'])
|
||||
->willReturnOnConsecutiveCalls('user', null);
|
||||
|
||||
$this->consoleInput->expects($this->once())
|
||||
->method('getOption')
|
||||
->with('last-used-before')
|
||||
->willReturn('2000-01-01T00:00:00Z');
|
||||
|
||||
$this->tokenProvider->expects($this->once())
|
||||
->method('invalidateLastUsedBefore')
|
||||
->with('user', 946684800);
|
||||
|
||||
$result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
||||
$this->assertSame(Command::SUCCESS, $result);
|
||||
}
|
||||
|
||||
public function testLastUsedBeforeAcceptsYmd() {
|
||||
$this->consoleInput->expects($this->exactly(2))
|
||||
->method('getArgument')
|
||||
->withConsecutive(['uid'], ['id'])
|
||||
->willReturnOnConsecutiveCalls('user', null);
|
||||
|
||||
$this->consoleInput->expects($this->once())
|
||||
->method('getOption')
|
||||
->with('last-used-before')
|
||||
->willReturn('2000-01-01');
|
||||
|
||||
$this->tokenProvider->expects($this->once())
|
||||
->method('invalidateLastUsedBefore')
|
||||
->with('user', 946684800);
|
||||
|
||||
$result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
||||
$this->assertSame(Command::SUCCESS, $result);
|
||||
}
|
||||
|
||||
public function testIdAndLastUsedBeforeAreMutuallyExclusive() {
|
||||
$this->consoleInput->expects($this->exactly(2))
|
||||
->method('getArgument')
|
||||
->withConsecutive(['uid'], ['id'])
|
||||
->willReturnOnConsecutiveCalls('user', 42);
|
||||
|
||||
$this->consoleInput->expects($this->once())
|
||||
->method('getOption')
|
||||
->with('last-used-before')
|
||||
->willReturn('946684800');
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
|
||||
$this->tokenProvider->expects($this->never())->method('invalidateLastUsedBefore');
|
||||
|
||||
$result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
|
||||
$this->assertSame(Command::SUCCESS, $result);
|
||||
}
|
||||
}
|
||||
|
|
@ -243,6 +243,14 @@ class ManagerTest extends TestCase {
|
|||
$this->manager->invalidateOldTokens();
|
||||
}
|
||||
|
||||
public function testInvalidateLastUsedBefore() {
|
||||
$this->publicKeyTokenProvider->expects($this->once())
|
||||
->method('invalidateLastUsedBefore')
|
||||
->with('user', 946684800);
|
||||
|
||||
$this->manager->invalidateLastUsedBefore('user', 946684800);
|
||||
}
|
||||
|
||||
public function testGetTokenByUser() {
|
||||
$t1 = new PublicKeyToken();
|
||||
$t2 = new PublicKeyToken();
|
||||
|
|
|
|||
|
|
@ -113,6 +113,20 @@ class PublicKeyTokenMapperTest extends TestCase {
|
|||
'version' => $qb->createNamedParameter(2),
|
||||
'password_invalid' => $qb->createNamedParameter(1),
|
||||
])->execute();
|
||||
$qb->insert('authtoken')->values([
|
||||
'uid' => $qb->createNamedParameter('user3'),
|
||||
'login_name' => $qb->createNamedParameter('User3'),
|
||||
'password' => $qb->createNamedParameter('063de945d6f6b26862d9b6f40652f2d5|DZ/z520tfdXPtd0T|395f6b89be8d9d605e409e20b9d9abe477fde1be38a3223f9e508f979bf906e50d9eaa4dca983ca4fb22a241eb696c3f98654e7775f78c4caf13108f98642b53'),
|
||||
'name' => $qb->createNamedParameter('Iceweasel on Linux'),
|
||||
'token' => $qb->createNamedParameter('84c5808c6445b6d65b8aa5b03840f09b27de603f0fb970906fb14ea4b115b7bf5ec53fada5c093fe46afdcd7bbc9617253a4d105f7dfb32719f9973d72412f31'),
|
||||
'type' => $qb->createNamedParameter(IToken::PERMANENT_TOKEN),
|
||||
'last_activity' => $qb->createNamedParameter($this->time - 60 * 3, IQueryBuilder::PARAM_INT), // Three minutes ago
|
||||
'last_check' => $this->time - 60 * 10, // 10mins ago
|
||||
'public_key' => $qb->createNamedParameter('public key'),
|
||||
'private_key' => $qb->createNamedParameter('private key'),
|
||||
'version' => $qb->createNamedParameter(2),
|
||||
'password_invalid' => $qb->createNamedParameter(1),
|
||||
])->execute();
|
||||
}
|
||||
|
||||
private function getNumberOfTokens() {
|
||||
|
|
@ -129,7 +143,7 @@ class PublicKeyTokenMapperTest extends TestCase {
|
|||
|
||||
$this->mapper->invalidate($token);
|
||||
|
||||
$this->assertSame(3, $this->getNumberOfTokens());
|
||||
$this->assertSame(4, $this->getNumberOfTokens());
|
||||
}
|
||||
|
||||
public function testInvalidateInvalid() {
|
||||
|
|
@ -137,7 +151,7 @@ class PublicKeyTokenMapperTest extends TestCase {
|
|||
|
||||
$this->mapper->invalidate($token);
|
||||
|
||||
$this->assertSame(4, $this->getNumberOfTokens());
|
||||
$this->assertSame(5, $this->getNumberOfTokens());
|
||||
}
|
||||
|
||||
public function testInvalidateOld() {
|
||||
|
|
@ -145,7 +159,15 @@ class PublicKeyTokenMapperTest extends TestCase {
|
|||
|
||||
$this->mapper->invalidateOld($olderThan);
|
||||
|
||||
$this->assertSame(3, $this->getNumberOfTokens());
|
||||
$this->assertSame(4, $this->getNumberOfTokens());
|
||||
}
|
||||
|
||||
public function testInvalidateLastUsedBefore() {
|
||||
$before = $this->time - 60 * 2; // Two minutes
|
||||
|
||||
$this->mapper->invalidateLastUsedBefore('user3', $before);
|
||||
|
||||
$this->assertSame(4, $this->getNumberOfTokens());
|
||||
}
|
||||
|
||||
public function testGetToken() {
|
||||
|
|
@ -238,7 +260,7 @@ class PublicKeyTokenMapperTest extends TestCase {
|
|||
$id = $result->fetch()['id'];
|
||||
|
||||
$this->mapper->deleteById('user1', (int)$id);
|
||||
$this->assertEquals(3, $this->getNumberOfTokens());
|
||||
$this->assertEquals(4, $this->getNumberOfTokens());
|
||||
}
|
||||
|
||||
public function testDeleteByIdWrongUser() {
|
||||
|
|
@ -247,7 +269,7 @@ class PublicKeyTokenMapperTest extends TestCase {
|
|||
$id = 33;
|
||||
|
||||
$this->mapper->deleteById('user1000', $id);
|
||||
$this->assertEquals(4, $this->getNumberOfTokens());
|
||||
$this->assertEquals(5, $this->getNumberOfTokens());
|
||||
}
|
||||
|
||||
public function testDeleteByName() {
|
||||
|
|
@ -258,7 +280,7 @@ class PublicKeyTokenMapperTest extends TestCase {
|
|||
$result = $qb->execute();
|
||||
$name = $result->fetch()['name'];
|
||||
$this->mapper->deleteByName($name);
|
||||
$this->assertEquals(3, $this->getNumberOfTokens());
|
||||
$this->assertEquals(4, $this->getNumberOfTokens());
|
||||
}
|
||||
|
||||
public function testHasExpiredTokens() {
|
||||
|
|
|
|||
|
|
@ -361,6 +361,14 @@ class PublicKeyTokenProviderTest extends TestCase {
|
|||
$this->tokenProvider->invalidateOldTokens();
|
||||
}
|
||||
|
||||
public function testInvalidateLastUsedBefore() {
|
||||
$this->mapper->expects($this->once())
|
||||
->method('invalidateLastUsedBefore')
|
||||
->with('user', 946684800);
|
||||
|
||||
$this->tokenProvider->invalidateLastUsedBefore('user', 946684800);
|
||||
}
|
||||
|
||||
public function testRenewSessionTokenWithoutPassword() {
|
||||
$token = 'oldIdtokentokentokentoken';
|
||||
$uid = 'user';
|
||||
|
|
|
|||
Loading…
Reference in a new issue