RemoteWipeTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Authentication\Token;
  8. use OC\Authentication\Events\RemoteWipeFinished;
  9. use OC\Authentication\Events\RemoteWipeStarted;
  10. use OC\Authentication\Exceptions\WipeTokenException;
  11. use OC\Authentication\Token\IProvider;
  12. use OC\Authentication\Token\IProvider as ITokenProvider;
  13. use OC\Authentication\Token\IToken;
  14. use OC\Authentication\Token\IWipeableToken;
  15. use OC\Authentication\Token\RemoteWipe;
  16. use OCP\EventDispatcher\IEventDispatcher;
  17. use OCP\IUser;
  18. use PHPUnit\Framework\MockObject\MockObject;
  19. use Psr\Log\LoggerInterface;
  20. use Test\TestCase;
  21. class RemoteWipeTest extends TestCase {
  22. /** @var ITokenProvider|MockObject */
  23. private $tokenProvider;
  24. /** @var IEventDispatcher|MockObject */
  25. private $eventDispatcher;
  26. /** @var LoggerInterface|MockObject */
  27. private $logger;
  28. /** @var RemoteWipe */
  29. private $remoteWipe;
  30. protected function setUp(): void {
  31. parent::setUp();
  32. $this->tokenProvider = $this->createMock(IProvider::class);
  33. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  34. $this->logger = $this->createMock(LoggerInterface::class);
  35. $this->remoteWipe = new RemoteWipe(
  36. $this->tokenProvider,
  37. $this->eventDispatcher,
  38. $this->logger
  39. );
  40. }
  41. public function testMarkNonWipableTokenForWipe(): void {
  42. $token = $this->createMock(IToken::class);
  43. $result = $this->remoteWipe->markTokenForWipe($token);
  44. $this->assertFalse($result);
  45. }
  46. public function testMarkTokenForWipe(): void {
  47. $token = $this->createMock(IWipeableToken::class);
  48. $token->expects($this->once())
  49. ->method('wipe');
  50. $this->tokenProvider->expects($this->once())
  51. ->method('updateToken')
  52. ->with($token);
  53. $result = $this->remoteWipe->markTokenForWipe($token);
  54. $this->assertTrue($result);
  55. }
  56. public function testMarkAllTokensForWipeNoWipeableToken(): void {
  57. /** @var IUser|MockObject $user */
  58. $user = $this->createMock(IUser::class);
  59. $user->method('getUID')->willReturn('user123');
  60. $token1 = $this->createMock(IToken::class);
  61. $token2 = $this->createMock(IToken::class);
  62. $this->tokenProvider->expects($this->once())
  63. ->method('getTokenByUser')
  64. ->with('user123')
  65. ->willReturn([$token1, $token2]);
  66. $result = $this->remoteWipe->markAllTokensForWipe($user);
  67. $this->assertFalse($result);
  68. }
  69. public function testMarkAllTokensForWipe(): void {
  70. /** @var IUser|MockObject $user */
  71. $user = $this->createMock(IUser::class);
  72. $user->method('getUID')->willReturn('user123');
  73. $token1 = $this->createMock(IToken::class);
  74. $token2 = $this->createMock(IWipeableToken::class);
  75. $this->tokenProvider->expects($this->once())
  76. ->method('getTokenByUser')
  77. ->with('user123')
  78. ->willReturn([$token1, $token2]);
  79. $token2->expects($this->once())
  80. ->method('wipe');
  81. $this->tokenProvider->expects($this->once())
  82. ->method('updateToken')
  83. ->with($token2);
  84. $result = $this->remoteWipe->markAllTokensForWipe($user);
  85. $this->assertTrue($result);
  86. }
  87. public function testStartWipingNotAWipeToken(): void {
  88. $token = $this->createMock(IToken::class);
  89. $this->tokenProvider->expects($this->once())
  90. ->method('getToken')
  91. ->with('tk1')
  92. ->willReturn($token);
  93. $this->eventDispatcher->expects($this->never())
  94. ->method('dispatch');
  95. $result = $this->remoteWipe->start('tk1');
  96. $this->assertFalse($result);
  97. }
  98. public function testStartWiping(): void {
  99. $token = $this->createMock(IToken::class);
  100. $this->tokenProvider->expects($this->once())
  101. ->method('getToken')
  102. ->with('tk1')
  103. ->willThrowException(new WipeTokenException($token));
  104. $this->eventDispatcher->expects($this->once())
  105. ->method('dispatch');
  106. $this->eventDispatcher->expects($this->once())
  107. ->method('dispatch')
  108. ->with(RemoteWipeStarted::class, $this->equalTo(new RemoteWipeStarted($token)));
  109. $result = $this->remoteWipe->start('tk1');
  110. $this->assertTrue($result);
  111. }
  112. public function testFinishWipingNotAWipeToken(): void {
  113. $token = $this->createMock(IToken::class);
  114. $this->tokenProvider->expects($this->once())
  115. ->method('getToken')
  116. ->with('tk1')
  117. ->willReturn($token);
  118. $this->eventDispatcher->expects($this->never())
  119. ->method('dispatch');
  120. $result = $this->remoteWipe->finish('tk1');
  121. $this->assertFalse($result);
  122. }
  123. public function startFinishWiping() {
  124. $token = $this->createMock(IToken::class);
  125. $this->tokenProvider->expects($this->once())
  126. ->method('getToken')
  127. ->with('tk1')
  128. ->willThrowException(new WipeTokenException($token));
  129. $this->eventDispatcher->expects($this->once())
  130. ->method('dispatch');
  131. $this->tokenProvider->expects($this->once())
  132. ->method('invalidateToken')
  133. ->with($token);
  134. $this->eventDispatcher->expects($this->once())
  135. ->method('dispatch')
  136. ->with(RemoteWipeFinished::class, $this->equalTo(new RemoteWipeFinished($token)));
  137. $result = $this->remoteWipe->finish('tk1');
  138. $this->assertTrue($result);
  139. }
  140. }