DeleteTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Tests\Core\Command\User\AuthTokens;
  7. use OC\Authentication\Token\IProvider;
  8. use OC\Core\Command\User\AuthTokens\Delete;
  9. use Symfony\Component\Console\Command\Command;
  10. use Symfony\Component\Console\Exception\RuntimeException;
  11. use Symfony\Component\Console\Input\InputInterface;
  12. use Symfony\Component\Console\Output\OutputInterface;
  13. use Test\TestCase;
  14. class DeleteTest extends TestCase {
  15. /** @var \PHPUnit\Framework\MockObject\MockObject */
  16. protected $tokenProvider;
  17. /** @var \PHPUnit\Framework\MockObject\MockObject */
  18. protected $consoleInput;
  19. /** @var \PHPUnit\Framework\MockObject\MockObject */
  20. protected $consoleOutput;
  21. /** @var \Symfony\Component\Console\Command\Command */
  22. protected $command;
  23. protected function setUp(): void {
  24. parent::setUp();
  25. $tokenProvider = $this->tokenProvider = $this->getMockBuilder(IProvider::class)
  26. ->disableOriginalConstructor()
  27. ->getMock();
  28. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  29. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  30. /** @var \OC\Authentication\Token\IProvider $tokenProvider */
  31. $this->command = new Delete($tokenProvider);
  32. }
  33. public function testDeleteTokenById() {
  34. $this->consoleInput->expects($this->exactly(2))
  35. ->method('getArgument')
  36. ->withConsecutive(['uid'], ['id'])
  37. ->willReturnOnConsecutiveCalls('user', 42);
  38. $this->consoleInput->expects($this->once())
  39. ->method('getOption')
  40. ->with('last-used-before')
  41. ->willReturn(null);
  42. $this->tokenProvider->expects($this->once())
  43. ->method('invalidateTokenById')
  44. ->with('user', 42);
  45. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  46. $this->assertSame(Command::SUCCESS, $result);
  47. }
  48. public function testDeleteTokenByIdRequiresTokenId() {
  49. $this->consoleInput->expects($this->exactly(2))
  50. ->method('getArgument')
  51. ->withConsecutive(['uid'], ['id'])
  52. ->willReturnOnConsecutiveCalls('user', null);
  53. $this->consoleInput->expects($this->once())
  54. ->method('getOption')
  55. ->with('last-used-before')
  56. ->willReturn(null);
  57. $this->expectException(RuntimeException::class);
  58. $this->tokenProvider->expects($this->never())->method('invalidateTokenById');
  59. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  60. $this->assertSame(Command::FAILURE, $result);
  61. }
  62. public function testDeleteTokensLastUsedBefore() {
  63. $this->consoleInput->expects($this->exactly(2))
  64. ->method('getArgument')
  65. ->withConsecutive(['uid'], ['id'])
  66. ->willReturnOnConsecutiveCalls('user', null);
  67. $this->consoleInput->expects($this->once())
  68. ->method('getOption')
  69. ->with('last-used-before')
  70. ->willReturn('946684800');
  71. $this->tokenProvider->expects($this->once())
  72. ->method('invalidateLastUsedBefore')
  73. ->with('user', 946684800);
  74. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  75. $this->assertSame(Command::SUCCESS, $result);
  76. }
  77. public function testLastUsedBeforeAcceptsIso8601Expanded() {
  78. $this->consoleInput->expects($this->exactly(2))
  79. ->method('getArgument')
  80. ->withConsecutive(['uid'], ['id'])
  81. ->willReturnOnConsecutiveCalls('user', null);
  82. $this->consoleInput->expects($this->once())
  83. ->method('getOption')
  84. ->with('last-used-before')
  85. ->willReturn('2000-01-01T00:00:00Z');
  86. $this->tokenProvider->expects($this->once())
  87. ->method('invalidateLastUsedBefore')
  88. ->with('user', 946684800);
  89. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  90. $this->assertSame(Command::SUCCESS, $result);
  91. }
  92. public function testLastUsedBeforeAcceptsYmd() {
  93. $this->consoleInput->expects($this->exactly(2))
  94. ->method('getArgument')
  95. ->withConsecutive(['uid'], ['id'])
  96. ->willReturnOnConsecutiveCalls('user', null);
  97. $this->consoleInput->expects($this->once())
  98. ->method('getOption')
  99. ->with('last-used-before')
  100. ->willReturn('2000-01-01');
  101. $this->tokenProvider->expects($this->once())
  102. ->method('invalidateLastUsedBefore')
  103. ->with('user', 946684800);
  104. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  105. $this->assertSame(Command::SUCCESS, $result);
  106. }
  107. public function testIdAndLastUsedBeforeAreMutuallyExclusive() {
  108. $this->consoleInput->expects($this->exactly(2))
  109. ->method('getArgument')
  110. ->withConsecutive(['uid'], ['id'])
  111. ->willReturnOnConsecutiveCalls('user', 42);
  112. $this->consoleInput->expects($this->once())
  113. ->method('getOption')
  114. ->with('last-used-before')
  115. ->willReturn('946684800');
  116. $this->expectException(RuntimeException::class);
  117. $this->tokenProvider->expects($this->never())->method('invalidateLastUsedBefore');
  118. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  119. $this->assertSame(Command::SUCCESS, $result);
  120. }
  121. }