1
0

RemoteWipeTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author 2019 Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. namespace Test\Authentication\Token;
  24. use OC\Authentication\Events\RemoteWipeFinished;
  25. use OC\Authentication\Events\RemoteWipeStarted;
  26. use OC\Authentication\Exceptions\WipeTokenException;
  27. use OC\Authentication\Token\IProvider;
  28. use OC\Authentication\Token\IProvider as ITokenProvider;
  29. use OC\Authentication\Token\IToken;
  30. use OC\Authentication\Token\IWipeableToken;
  31. use OC\Authentication\Token\RemoteWipe;
  32. use OCP\EventDispatcher\IEventDispatcher;
  33. use OCP\IUser;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. use Psr\Log\LoggerInterface;
  36. use Test\TestCase;
  37. class RemoteWipeTest extends TestCase {
  38. /** @var ITokenProvider|MockObject */
  39. private $tokenProvider;
  40. /** @var IEventDispatcher|MockObject */
  41. private $eventDispatcher;
  42. /** @var LoggerInterface|MockObject */
  43. private $logger;
  44. /** @var RemoteWipe */
  45. private $remoteWipe;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->tokenProvider = $this->createMock(IProvider::class);
  49. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  50. $this->logger = $this->createMock(LoggerInterface::class);
  51. $this->remoteWipe = new RemoteWipe(
  52. $this->tokenProvider,
  53. $this->eventDispatcher,
  54. $this->logger
  55. );
  56. }
  57. public function testMarkNonWipableTokenForWipe(): void {
  58. $token = $this->createMock(IToken::class);
  59. $result = $this->remoteWipe->markTokenForWipe($token);
  60. $this->assertFalse($result);
  61. }
  62. public function testMarkTokenForWipe(): void {
  63. $token = $this->createMock(IWipeableToken::class);
  64. $token->expects($this->once())
  65. ->method('wipe');
  66. $this->tokenProvider->expects($this->once())
  67. ->method('updateToken')
  68. ->with($token);
  69. $result = $this->remoteWipe->markTokenForWipe($token);
  70. $this->assertTrue($result);
  71. }
  72. public function testMarkAllTokensForWipeNoWipeableToken(): void {
  73. /** @var IUser|MockObject $user */
  74. $user = $this->createMock(IUser::class);
  75. $user->method('getUID')->willReturn('user123');
  76. $token1 = $this->createMock(IToken::class);
  77. $token2 = $this->createMock(IToken::class);
  78. $this->tokenProvider->expects($this->once())
  79. ->method('getTokenByUser')
  80. ->with('user123')
  81. ->willReturn([$token1, $token2]);
  82. $result = $this->remoteWipe->markAllTokensForWipe($user);
  83. $this->assertFalse($result);
  84. }
  85. public function testMarkAllTokensForWipe(): void {
  86. /** @var IUser|MockObject $user */
  87. $user = $this->createMock(IUser::class);
  88. $user->method('getUID')->willReturn('user123');
  89. $token1 = $this->createMock(IToken::class);
  90. $token2 = $this->createMock(IWipeableToken::class);
  91. $this->tokenProvider->expects($this->once())
  92. ->method('getTokenByUser')
  93. ->with('user123')
  94. ->willReturn([$token1, $token2]);
  95. $token2->expects($this->once())
  96. ->method('wipe');
  97. $this->tokenProvider->expects($this->once())
  98. ->method('updateToken')
  99. ->with($token2);
  100. $result = $this->remoteWipe->markAllTokensForWipe($user);
  101. $this->assertTrue($result);
  102. }
  103. public function testStartWipingNotAWipeToken() {
  104. $token = $this->createMock(IToken::class);
  105. $this->tokenProvider->expects($this->once())
  106. ->method('getToken')
  107. ->with('tk1')
  108. ->willReturn($token);
  109. $this->eventDispatcher->expects($this->never())
  110. ->method('dispatch');
  111. $result = $this->remoteWipe->start('tk1');
  112. $this->assertFalse($result);
  113. }
  114. public function testStartWiping() {
  115. $token = $this->createMock(IToken::class);
  116. $this->tokenProvider->expects($this->once())
  117. ->method('getToken')
  118. ->with('tk1')
  119. ->willThrowException(new WipeTokenException($token));
  120. $this->eventDispatcher->expects($this->once())
  121. ->method('dispatch');
  122. $this->eventDispatcher->expects($this->once())
  123. ->method('dispatch')
  124. ->with(RemoteWipeStarted::class, $this->equalTo(new RemoteWipeStarted($token)));
  125. $result = $this->remoteWipe->start('tk1');
  126. $this->assertTrue($result);
  127. }
  128. public function testFinishWipingNotAWipeToken() {
  129. $token = $this->createMock(IToken::class);
  130. $this->tokenProvider->expects($this->once())
  131. ->method('getToken')
  132. ->with('tk1')
  133. ->willReturn($token);
  134. $this->eventDispatcher->expects($this->never())
  135. ->method('dispatch');
  136. $result = $this->remoteWipe->finish('tk1');
  137. $this->assertFalse($result);
  138. }
  139. public function startFinishWiping() {
  140. $token = $this->createMock(IToken::class);
  141. $this->tokenProvider->expects($this->once())
  142. ->method('getToken')
  143. ->with('tk1')
  144. ->willThrowException(new WipeTokenException($token));
  145. $this->eventDispatcher->expects($this->once())
  146. ->method('dispatch');
  147. $this->tokenProvider->expects($this->once())
  148. ->method('invalidateToken')
  149. ->with($token);
  150. $this->eventDispatcher->expects($this->once())
  151. ->method('dispatch')
  152. ->with(RemoteWipeFinished::class, $this->equalTo(new RemoteWipeFinished($token)));
  153. $result = $this->remoteWipe->finish('tk1');
  154. $this->assertTrue($result);
  155. }
  156. }