OCSAuthAPIControllerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Federation\Tests\Controller;
  8. use OC\BackgroundJob\JobList;
  9. use OCA\Federation\Controller\OCSAuthAPIController;
  10. use OCA\Federation\DbHandler;
  11. use OCA\Federation\TrustedServers;
  12. use OCP\AppFramework\OCS\OCSForbiddenException;
  13. use OCP\AppFramework\Utility\ITimeFactory;
  14. use OCP\IRequest;
  15. use OCP\Security\Bruteforce\IThrottler;
  16. use OCP\Security\ISecureRandom;
  17. use Psr\Log\LoggerInterface;
  18. use Test\TestCase;
  19. class OCSAuthAPIControllerTest extends TestCase {
  20. /** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
  21. private $request;
  22. /** @var \PHPUnit\Framework\MockObject\MockObject|ISecureRandom */
  23. private $secureRandom;
  24. /** @var \PHPUnit\Framework\MockObject\MockObject|JobList */
  25. private $jobList;
  26. /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */
  27. private $trustedServers;
  28. /** @var \PHPUnit\Framework\MockObject\MockObject|DbHandler */
  29. private $dbHandler;
  30. /** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */
  31. private $logger;
  32. /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
  33. private $timeFactory;
  34. /** @var \PHPUnit\Framework\MockObject\MockObject|IThrottler */
  35. private $throttler;
  36. private OCSAuthAPIController $ocsAuthApi;
  37. /** @var int simulated timestamp */
  38. private int $currentTime = 1234567;
  39. protected function setUp(): void {
  40. parent::setUp();
  41. $this->request = $this->createMock(IRequest::class);
  42. $this->secureRandom = $this->createMock(ISecureRandom::class);
  43. $this->trustedServers = $this->createMock(TrustedServers::class);
  44. $this->dbHandler = $this->createMock(DbHandler::class);
  45. $this->jobList = $this->createMock(JobList::class);
  46. $this->logger = $this->createMock(LoggerInterface::class);
  47. $this->timeFactory = $this->createMock(ITimeFactory::class);
  48. $this->throttler = $this->createMock(IThrottler::class);
  49. $this->ocsAuthApi = new OCSAuthAPIController(
  50. 'federation',
  51. $this->request,
  52. $this->secureRandom,
  53. $this->jobList,
  54. $this->trustedServers,
  55. $this->dbHandler,
  56. $this->logger,
  57. $this->timeFactory,
  58. $this->throttler
  59. );
  60. $this->timeFactory->method('getTime')
  61. ->willReturn($this->currentTime);
  62. }
  63. /**
  64. * @dataProvider dataTestRequestSharedSecret
  65. */
  66. public function testRequestSharedSecret(string $token, string $localToken, bool $isTrustedServer, bool $ok): void {
  67. $url = 'url';
  68. $this->trustedServers
  69. ->expects($this->once())
  70. ->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
  71. $this->dbHandler->expects($this->any())
  72. ->method('getToken')->with($url)->willReturn($localToken);
  73. if ($ok) {
  74. $this->jobList->expects($this->once())->method('add')
  75. ->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
  76. } else {
  77. $this->jobList->expects($this->never())->method('add');
  78. $this->jobList->expects($this->never())->method('remove');
  79. if (!$isTrustedServer) {
  80. $this->throttler->expects($this->once())
  81. ->method('registerAttempt')
  82. ->with('federationSharedSecret');
  83. }
  84. }
  85. try {
  86. $this->ocsAuthApi->requestSharedSecret($url, $token);
  87. $this->assertTrue($ok);
  88. } catch (OCSForbiddenException $e) {
  89. $this->assertFalse($ok);
  90. }
  91. }
  92. public function dataTestRequestSharedSecret() {
  93. return [
  94. ['token2', 'token1', true, true],
  95. ['token1', 'token2', false, false],
  96. ['token1', 'token2', true, false],
  97. ];
  98. }
  99. /**
  100. * @dataProvider dataTestGetSharedSecret
  101. */
  102. public function testGetSharedSecret(bool $isTrustedServer, bool $isValidToken, bool $ok): void {
  103. $url = 'url';
  104. $token = 'token';
  105. /** @var OCSAuthAPIController | \PHPUnit\Framework\MockObject\MockObject $ocsAuthApi */
  106. $ocsAuthApi = $this->getMockBuilder('OCA\Federation\Controller\OCSAuthAPIController')
  107. ->setConstructorArgs(
  108. [
  109. 'federation',
  110. $this->request,
  111. $this->secureRandom,
  112. $this->jobList,
  113. $this->trustedServers,
  114. $this->dbHandler,
  115. $this->logger,
  116. $this->timeFactory,
  117. $this->throttler
  118. ]
  119. )->setMethods(['isValidToken'])->getMock();
  120. $this->trustedServers
  121. ->expects($this->any())
  122. ->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
  123. $ocsAuthApi->expects($this->any())
  124. ->method('isValidToken')->with($url, $token)->willReturn($isValidToken);
  125. if ($ok) {
  126. $this->secureRandom->expects($this->once())->method('generate')->with(32)
  127. ->willReturn('secret');
  128. $this->trustedServers->expects($this->once())
  129. ->method('addSharedSecret')->with($url, 'secret');
  130. } else {
  131. $this->secureRandom->expects($this->never())->method('generate');
  132. $this->trustedServers->expects($this->never())->method('addSharedSecret');
  133. $this->throttler->expects($this->once())
  134. ->method('registerAttempt')
  135. ->with('federationSharedSecret');
  136. }
  137. try {
  138. $result = $ocsAuthApi->getSharedSecret($url, $token);
  139. $this->assertTrue($ok);
  140. $data = $result->getData();
  141. $this->assertSame('secret', $data['sharedSecret']);
  142. } catch (OCSForbiddenException $e) {
  143. $this->assertFalse($ok);
  144. }
  145. }
  146. public function dataTestGetSharedSecret() {
  147. return [
  148. [true, true, true],
  149. [false, true, false],
  150. [true, false, false],
  151. [false, false, false],
  152. ];
  153. }
  154. }