You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

OCSAuthAPIControllerTest.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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\ILogger;
  35. use OCP\IRequest;
  36. use OCP\Security\ISecureRandom;
  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. /** @var OCSAuthAPIController */
  54. private $ocsAuthApi;
  55. /** @var int simulated timestamp */
  56. private $currentTime = 1234567;
  57. protected function setUp(): void {
  58. parent::setUp();
  59. $this->request = $this->createMock(IRequest::class);
  60. $this->secureRandom = $this->createMock(ISecureRandom::class);
  61. $this->trustedServers = $this->createMock(TrustedServers::class);
  62. $this->dbHandler = $this->createMock(DbHandler::class);
  63. $this->jobList = $this->createMock(JobList::class);
  64. $this->logger = $this->createMock(ILogger::class);
  65. $this->timeFactory = $this->createMock(ITimeFactory::class);
  66. $this->ocsAuthApi = new OCSAuthAPIController(
  67. 'federation',
  68. $this->request,
  69. $this->secureRandom,
  70. $this->jobList,
  71. $this->trustedServers,
  72. $this->dbHandler,
  73. $this->logger,
  74. $this->timeFactory
  75. );
  76. $this->timeFactory->method('getTime')
  77. ->willReturn($this->currentTime);
  78. }
  79. /**
  80. * @dataProvider dataTestRequestSharedSecret
  81. *
  82. * @param string $token
  83. * @param string $localToken
  84. * @param bool $isTrustedServer
  85. * @param bool $ok
  86. */
  87. public function testRequestSharedSecret($token, $localToken, $isTrustedServer, $ok) {
  88. $url = 'url';
  89. $this->trustedServers
  90. ->expects($this->once())
  91. ->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
  92. $this->dbHandler->expects($this->any())
  93. ->method('getToken')->with($url)->willReturn($localToken);
  94. if ($ok) {
  95. $this->jobList->expects($this->once())->method('add')
  96. ->with('OCA\Federation\BackgroundJob\GetSharedSecret', ['url' => $url, 'token' => $token, 'created' => $this->currentTime]);
  97. } else {
  98. $this->jobList->expects($this->never())->method('add');
  99. $this->jobList->expects($this->never())->method('remove');
  100. }
  101. try {
  102. $this->ocsAuthApi->requestSharedSecret($url, $token);
  103. $this->assertTrue($ok);
  104. } catch (OCSForbiddenException $e) {
  105. $this->assertFalse($ok);
  106. }
  107. }
  108. public function dataTestRequestSharedSecret() {
  109. return [
  110. ['token2', 'token1', true, true],
  111. ['token1', 'token2', false, false],
  112. ['token1', 'token2', true, false],
  113. ];
  114. }
  115. /**
  116. * @dataProvider dataTestGetSharedSecret
  117. *
  118. * @param bool $isTrustedServer
  119. * @param bool $isValidToken
  120. * @param bool $ok
  121. */
  122. public function testGetSharedSecret($isTrustedServer, $isValidToken, $ok) {
  123. $url = 'url';
  124. $token = 'token';
  125. /** @var OCSAuthAPIController | \PHPUnit\Framework\MockObject\MockObject $ocsAuthApi */
  126. $ocsAuthApi = $this->getMockBuilder('OCA\Federation\Controller\OCSAuthAPIController')
  127. ->setConstructorArgs(
  128. [
  129. 'federation',
  130. $this->request,
  131. $this->secureRandom,
  132. $this->jobList,
  133. $this->trustedServers,
  134. $this->dbHandler,
  135. $this->logger,
  136. $this->timeFactory
  137. ]
  138. )->setMethods(['isValidToken'])->getMock();
  139. $this->trustedServers
  140. ->expects($this->any())
  141. ->method('isTrustedServer')->with($url)->willReturn($isTrustedServer);
  142. $ocsAuthApi->expects($this->any())
  143. ->method('isValidToken')->with($url, $token)->willReturn($isValidToken);
  144. if ($ok) {
  145. $this->secureRandom->expects($this->once())->method('generate')->with(32)
  146. ->willReturn('secret');
  147. $this->trustedServers->expects($this->once())
  148. ->method('addSharedSecret')->willReturn($url, 'secret');
  149. } else {
  150. $this->secureRandom->expects($this->never())->method('generate');
  151. $this->trustedServers->expects($this->never())->method('addSharedSecret');
  152. }
  153. try {
  154. $result = $ocsAuthApi->getSharedSecret($url, $token);
  155. $this->assertTrue($ok);
  156. $data = $result->getData();
  157. $this->assertSame('secret', $data['sharedSecret']);
  158. } catch (OCSForbiddenException $e) {
  159. $this->assertFalse($ok);
  160. }
  161. }
  162. public function dataTestGetSharedSecret() {
  163. return [
  164. [true, true, true],
  165. [false, true, false],
  166. [true, false, false],
  167. [false, false, false],
  168. ];
  169. }
  170. }