OCSAuthAPIControllerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Robin Appelman <robin@icewind.nl>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Federation\Tests\Controller;
  28. use OC\BackgroundJob\JobList;
  29. use OCA\Federation\Controller\OCSAuthAPIController;
  30. use OCA\Federation\DbHandler;
  31. use OCA\Federation\TrustedServers;
  32. use OCP\AppFramework\OCS\OCSForbiddenException;
  33. use OCP\AppFramework\Utility\ITimeFactory;
  34. use OCP\IRequest;
  35. use OCP\Security\ISecureRandom;
  36. use Psr\Log\LoggerInterface;
  37. use Test\TestCase;
  38. class OCSAuthAPIControllerTest extends TestCase {
  39. /** @var \PHPUnit\Framework\MockObject\MockObject|IRequest */
  40. private $request;
  41. /** @var \PHPUnit\Framework\MockObject\MockObject|ISecureRandom */
  42. private $secureRandom;
  43. /** @var \PHPUnit\Framework\MockObject\MockObject|JobList */
  44. private $jobList;
  45. /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */
  46. private $trustedServers;
  47. /** @var \PHPUnit\Framework\MockObject\MockObject|DbHandler */
  48. private $dbHandler;
  49. /** @var \PHPUnit\Framework\MockObject\MockObject|ILogger */
  50. private $logger;
  51. /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
  52. private $timeFactory;
  53. private OCSAuthAPIController $ocsAuthApi;
  54. /** @var int simulated timestamp */
  55. private int $currentTime = 1234567;
  56. protected function setUp(): void {
  57. parent::setUp();
  58. $this->request = $this->createMock(IRequest::class);
  59. $this->secureRandom = $this->createMock(ISecureRandom::class);
  60. $this->trustedServers = $this->createMock(TrustedServers::class);
  61. $this->dbHandler = $this->createMock(DbHandler::class);
  62. $this->jobList = $this->createMock(JobList::class);
  63. $this->logger = $this->createMock(LoggerInterface::class);
  64. $this->timeFactory = $this->createMock(ITimeFactory::class);
  65. $this->ocsAuthApi = new OCSAuthAPIController(
  66. 'federation',
  67. $this->request,
  68. $this->secureRandom,
  69. $this->jobList,
  70. $this->trustedServers,
  71. $this->dbHandler,
  72. $this->logger,
  73. $this->timeFactory
  74. );
  75. $this->timeFactory->method('getTime')
  76. ->willReturn($this->currentTime);
  77. }
  78. /**
  79. * @dataProvider dataTestRequestSharedSecret
  80. */
  81. public function testRequestSharedSecret(string $token, string $localToken, bool $isTrustedServer, bool $ok): void {
  82. $url = 'url';
  83. $this->trustedServers
  84. ->expects($this->once())
  85. ->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
  86. $this->dbHandler->expects($this->any())
  87. ->method('getToken')->with($url)->willReturn($localToken);
  88. if ($ok) {
  89. $this->jobList->expects($this->once())->method('add')
  90. ->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
  91. } else {
  92. $this->jobList->expects($this->never())->method('add');
  93. $this->jobList->expects($this->never())->method('remove');
  94. }
  95. try {
  96. $this->ocsAuthApi->requestSharedSecret($url, $token);
  97. $this->assertTrue($ok);
  98. } catch (OCSForbiddenException $e) {
  99. $this->assertFalse($ok);
  100. }
  101. }
  102. public function dataTestRequestSharedSecret() {
  103. return [
  104. ['token2', 'token1', true, true],
  105. ['token1', 'token2', false, false],
  106. ['token1', 'token2', true, false],
  107. ];
  108. }
  109. /**
  110. * @dataProvider dataTestGetSharedSecret
  111. */
  112. public function testGetSharedSecret(bool $isTrustedServer, bool $isValidToken, bool $ok): void {
  113. $url = 'url';
  114. $token = 'token';
  115. /** @var OCSAuthAPIController | \PHPUnit\Framework\MockObject\MockObject $ocsAuthApi */
  116. $ocsAuthApi = $this->getMockBuilder('OCA\Federation\Controller\OCSAuthAPIController')
  117. ->setConstructorArgs(
  118. [
  119. 'federation',
  120. $this->request,
  121. $this->secureRandom,
  122. $this->jobList,
  123. $this->trustedServers,
  124. $this->dbHandler,
  125. $this->logger,
  126. $this->timeFactory
  127. ]
  128. )->setMethods(['isValidToken'])->getMock();
  129. $this->trustedServers
  130. ->expects($this->any())
  131. ->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
  132. $ocsAuthApi->expects($this->any())
  133. ->method('isValidToken')->with($url, $token)->willReturn($isValidToken);
  134. if ($ok) {
  135. $this->secureRandom->expects($this->once())->method('generate')->with(32)
  136. ->willReturn('secret');
  137. $this->trustedServers->expects($this->once())
  138. ->method('addSharedSecret')->with($url, 'secret');
  139. } else {
  140. $this->secureRandom->expects($this->never())->method('generate');
  141. $this->trustedServers->expects($this->never())->method('addSharedSecret');
  142. }
  143. try {
  144. $result = $ocsAuthApi->getSharedSecret($url, $token);
  145. $this->assertTrue($ok);
  146. $data = $result->getData();
  147. $this->assertSame('secret', $data['sharedSecret']);
  148. } catch (OCSForbiddenException $e) {
  149. $this->assertFalse($ok);
  150. }
  151. }
  152. public function dataTestGetSharedSecret() {
  153. return [
  154. [true, true, true],
  155. [false, true, false],
  156. [true, false, false],
  157. [false, false, false],
  158. ];
  159. }
  160. }