DeleteTest.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2023 Lucas Azevedo <lhs_azevedo@hotmail.com>
  4. *
  5. * @author Lucas Azevedo <lhs_azevedo@hotmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Tests\Core\Command\User\AuthTokens;
  24. use OC\Authentication\Token\IProvider;
  25. use OC\Core\Command\User\AuthTokens\Delete;
  26. use Symfony\Component\Console\Command\Command;
  27. use Symfony\Component\Console\Exception\RuntimeException;
  28. use Symfony\Component\Console\Input\InputInterface;
  29. use Symfony\Component\Console\Output\OutputInterface;
  30. use Test\TestCase;
  31. class DeleteTest extends TestCase {
  32. /** @var \PHPUnit\Framework\MockObject\MockObject */
  33. protected $tokenProvider;
  34. /** @var \PHPUnit\Framework\MockObject\MockObject */
  35. protected $consoleInput;
  36. /** @var \PHPUnit\Framework\MockObject\MockObject */
  37. protected $consoleOutput;
  38. /** @var \Symfony\Component\Console\Command\Command */
  39. protected $command;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $tokenProvider = $this->tokenProvider = $this->getMockBuilder(IProvider::class)
  43. ->disableOriginalConstructor()
  44. ->getMock();
  45. $this->consoleInput = $this->getMockBuilder(InputInterface::class)->getMock();
  46. $this->consoleOutput = $this->getMockBuilder(OutputInterface::class)->getMock();
  47. /** @var \OC\Authentication\Token\IProvider $tokenProvider */
  48. $this->command = new Delete($tokenProvider);
  49. }
  50. public function testDeleteTokenById() {
  51. $this->consoleInput->expects($this->exactly(2))
  52. ->method('getArgument')
  53. ->withConsecutive(['uid'], ['id'])
  54. ->willReturnOnConsecutiveCalls('user', 42);
  55. $this->consoleInput->expects($this->once())
  56. ->method('getOption')
  57. ->with('last-used-before')
  58. ->willReturn(null);
  59. $this->tokenProvider->expects($this->once())
  60. ->method('invalidateTokenById')
  61. ->with('user', 42);
  62. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  63. $this->assertSame(Command::SUCCESS, $result);
  64. }
  65. public function testDeleteTokenByIdRequiresTokenId() {
  66. $this->consoleInput->expects($this->exactly(2))
  67. ->method('getArgument')
  68. ->withConsecutive(['uid'], ['id'])
  69. ->willReturnOnConsecutiveCalls('user', null);
  70. $this->consoleInput->expects($this->once())
  71. ->method('getOption')
  72. ->with('last-used-before')
  73. ->willReturn(null);
  74. $this->expectException(RuntimeException::class);
  75. $this->tokenProvider->expects($this->never())->method('invalidateTokenById');
  76. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  77. $this->assertSame(Command::FAILURE, $result);
  78. }
  79. public function testDeleteTokensLastUsedBefore() {
  80. $this->consoleInput->expects($this->exactly(2))
  81. ->method('getArgument')
  82. ->withConsecutive(['uid'], ['id'])
  83. ->willReturnOnConsecutiveCalls('user', null);
  84. $this->consoleInput->expects($this->once())
  85. ->method('getOption')
  86. ->with('last-used-before')
  87. ->willReturn('946684800');
  88. $this->tokenProvider->expects($this->once())
  89. ->method('invalidateLastUsedBefore')
  90. ->with('user', 946684800);
  91. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  92. $this->assertSame(Command::SUCCESS, $result);
  93. }
  94. public function testLastUsedBeforeAcceptsIso8601Expanded() {
  95. $this->consoleInput->expects($this->exactly(2))
  96. ->method('getArgument')
  97. ->withConsecutive(['uid'], ['id'])
  98. ->willReturnOnConsecutiveCalls('user', null);
  99. $this->consoleInput->expects($this->once())
  100. ->method('getOption')
  101. ->with('last-used-before')
  102. ->willReturn('2000-01-01T00:00:00Z');
  103. $this->tokenProvider->expects($this->once())
  104. ->method('invalidateLastUsedBefore')
  105. ->with('user', 946684800);
  106. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  107. $this->assertSame(Command::SUCCESS, $result);
  108. }
  109. public function testLastUsedBeforeAcceptsYmd() {
  110. $this->consoleInput->expects($this->exactly(2))
  111. ->method('getArgument')
  112. ->withConsecutive(['uid'], ['id'])
  113. ->willReturnOnConsecutiveCalls('user', null);
  114. $this->consoleInput->expects($this->once())
  115. ->method('getOption')
  116. ->with('last-used-before')
  117. ->willReturn('2000-01-01');
  118. $this->tokenProvider->expects($this->once())
  119. ->method('invalidateLastUsedBefore')
  120. ->with('user', 946684800);
  121. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  122. $this->assertSame(Command::SUCCESS, $result);
  123. }
  124. public function testIdAndLastUsedBeforeAreMutuallyExclusive() {
  125. $this->consoleInput->expects($this->exactly(2))
  126. ->method('getArgument')
  127. ->withConsecutive(['uid'], ['id'])
  128. ->willReturnOnConsecutiveCalls('user', 42);
  129. $this->consoleInput->expects($this->once())
  130. ->method('getOption')
  131. ->with('last-used-before')
  132. ->willReturn('946684800');
  133. $this->expectException(RuntimeException::class);
  134. $this->tokenProvider->expects($this->never())->method('invalidateLastUsedBefore');
  135. $result = self::invokePrivate($this->command, 'execute', [$this->consoleInput, $this->consoleOutput]);
  136. $this->assertSame(Command::SUCCESS, $result);
  137. }
  138. }