123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- <?php
- /**
- * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
- * SPDX-License-Identifier: AGPL-3.0-or-later
- */
- namespace Tests\Core\Command\User\AuthTokens;
- use OC\Authentication\Token\IProvider;
- use OC\Core\Command\User\AuthTokens\Delete;
- 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);
- }
- }
|