GetSharedSecretTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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\BackgroundJob;
  28. use GuzzleHttp\Exception\ConnectException;
  29. use OCA\Federation\BackgroundJob\GetSharedSecret;
  30. use OCA\Federation\TrustedServers;
  31. use OCA\Files_Sharing\Tests\TestCase;
  32. use OCP\AppFramework\Http;
  33. use OCP\AppFramework\Utility\ITimeFactory;
  34. use OCP\BackgroundJob\IJobList;
  35. use OCP\Http\Client\IClient;
  36. use OCP\Http\Client\IClientService;
  37. use OCP\Http\Client\IResponse;
  38. use OCP\IURLGenerator;
  39. use OCP\OCS\IDiscoveryService;
  40. use Psr\Log\LoggerInterface;
  41. /**
  42. * Class GetSharedSecretTest
  43. *
  44. * @group DB
  45. *
  46. * @package OCA\Federation\Tests\BackgroundJob
  47. */
  48. class GetSharedSecretTest extends TestCase {
  49. /** @var \PHPUnit\Framework\MockObject\MockObject|IClient */
  50. private $httpClient;
  51. /** @var \PHPUnit\Framework\MockObject\MockObject|IClientService */
  52. private $httpClientService;
  53. /** @var \PHPUnit\Framework\MockObject\MockObject|IJobList */
  54. private $jobList;
  55. /** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */
  56. private $urlGenerator;
  57. /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */
  58. private $trustedServers;
  59. /** @var \PHPUnit\Framework\MockObject\MockObject|LoggerInterface */
  60. private $logger;
  61. /** @var \PHPUnit\Framework\MockObject\MockObject|IResponse */
  62. private $response;
  63. /** @var \PHPUnit\Framework\MockObject\MockObject|IDiscoveryService */
  64. private $discoverService;
  65. /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
  66. private $timeFactory;
  67. /** @var GetSharedSecret */
  68. private $getSharedSecret;
  69. protected function setUp(): void {
  70. parent::setUp();
  71. $this->httpClientService = $this->createMock(IClientService::class);
  72. $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
  73. $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
  74. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  75. $this->trustedServers = $this->getMockBuilder(TrustedServers::class)
  76. ->disableOriginalConstructor()->getMock();
  77. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  78. $this->response = $this->getMockBuilder(IResponse::class)->getMock();
  79. $this->discoverService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
  80. $this->timeFactory = $this->createMock(ITimeFactory::class);
  81. $this->discoverService->expects($this->any())->method('discover')->willReturn([]);
  82. $this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
  83. $this->getSharedSecret = new GetSharedSecret(
  84. $this->httpClientService,
  85. $this->urlGenerator,
  86. $this->jobList,
  87. $this->trustedServers,
  88. $this->logger,
  89. $this->discoverService,
  90. $this->timeFactory
  91. );
  92. }
  93. /**
  94. * @dataProvider dataTestExecute
  95. *
  96. * @param bool $isTrustedServer
  97. * @param bool $retainBackgroundJob
  98. */
  99. public function testExecute($isTrustedServer, $retainBackgroundJob) {
  100. /** @var GetSharedSecret |\PHPUnit\Framework\MockObject\MockObject $getSharedSecret */
  101. $getSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\GetSharedSecret')
  102. ->setConstructorArgs(
  103. [
  104. $this->httpClientService,
  105. $this->urlGenerator,
  106. $this->jobList,
  107. $this->trustedServers,
  108. $this->logger,
  109. $this->discoverService,
  110. $this->timeFactory
  111. ]
  112. )->setMethods(['parentExecute'])->getMock();
  113. $this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
  114. $this->trustedServers->expects($this->once())->method('isTrustedServer')
  115. ->with('url')->willReturn($isTrustedServer);
  116. if ($isTrustedServer) {
  117. $getSharedSecret->expects($this->once())->method('parentExecute');
  118. } else {
  119. $getSharedSecret->expects($this->never())->method('parentExecute');
  120. }
  121. $this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
  122. $this->jobList->expects($this->once())->method('remove');
  123. $this->timeFactory->method('getTime')->willReturn(42);
  124. if ($retainBackgroundJob) {
  125. $this->jobList->expects($this->once())
  126. ->method('add')
  127. ->with(
  128. GetSharedSecret::class,
  129. [
  130. 'url' => 'url',
  131. 'token' => 'token',
  132. 'created' => 42,
  133. ]
  134. );
  135. } else {
  136. $this->jobList->expects($this->never())->method('add');
  137. }
  138. $getSharedSecret->execute($this->jobList);
  139. }
  140. public function dataTestExecute() {
  141. return [
  142. [true, true],
  143. [true, false],
  144. [false, false],
  145. ];
  146. }
  147. /**
  148. * @dataProvider dataTestRun
  149. *
  150. * @param int $statusCode
  151. */
  152. public function testRun($statusCode) {
  153. $target = 'targetURL';
  154. $source = 'sourceURL';
  155. $token = 'token';
  156. $argument = ['url' => $target, 'token' => $token];
  157. $this->timeFactory->method('getTime')
  158. ->willReturn(42);
  159. $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
  160. ->willReturn($source);
  161. $this->httpClient->expects($this->once())->method('get')
  162. ->with(
  163. $target . '/ocs/v2.php/apps/federation/api/v1/shared-secret',
  164. [
  165. 'query' =>
  166. [
  167. 'url' => $source,
  168. 'token' => $token,
  169. 'format' => 'json',
  170. ],
  171. 'timeout' => 3,
  172. 'connect_timeout' => 3,
  173. ]
  174. )->willReturn($this->response);
  175. $this->response->expects($this->once())->method('getStatusCode')
  176. ->willReturn($statusCode);
  177. if ($statusCode === Http::STATUS_OK) {
  178. $this->response->expects($this->once())->method('getBody')
  179. ->willReturn('{"ocs":{"data":{"sharedSecret":"secret"}}}');
  180. $this->trustedServers->expects($this->once())->method('addSharedSecret')
  181. ->with($target, 'secret');
  182. } else {
  183. $this->trustedServers->expects($this->never())->method('addSharedSecret');
  184. }
  185. $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
  186. if (
  187. $statusCode !== Http::STATUS_OK
  188. && $statusCode !== Http::STATUS_FORBIDDEN
  189. ) {
  190. $this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
  191. } else {
  192. $this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob'));
  193. }
  194. }
  195. public function dataTestRun() {
  196. return [
  197. [Http::STATUS_OK],
  198. [Http::STATUS_FORBIDDEN],
  199. [Http::STATUS_CONFLICT],
  200. ];
  201. }
  202. public function testRunExpired() {
  203. $target = 'targetURL';
  204. $source = 'sourceURL';
  205. $token = 'token';
  206. $created = 42;
  207. $argument = [
  208. 'url' => $target,
  209. 'token' => $token,
  210. 'created' => $created,
  211. ];
  212. $this->urlGenerator->expects($this->once())
  213. ->method('getAbsoluteURL')
  214. ->with('/')
  215. ->willReturn($source);
  216. $this->timeFactory->method('getTime')
  217. ->willReturn($created + 2592000 + 1);
  218. $this->trustedServers->expects($this->once())
  219. ->method('setServerStatus')
  220. ->with(
  221. $target,
  222. TrustedServers::STATUS_FAILURE
  223. );
  224. $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
  225. }
  226. public function testRunConnectionError() {
  227. $target = 'targetURL';
  228. $source = 'sourceURL';
  229. $token = 'token';
  230. $argument = ['url' => $target, 'token' => $token];
  231. $this->timeFactory->method('getTime')
  232. ->willReturn(42);
  233. $this->urlGenerator
  234. ->expects($this->once())
  235. ->method('getAbsoluteURL')
  236. ->with('/')
  237. ->willReturn($source);
  238. $this->httpClient->expects($this->once())->method('get')
  239. ->with(
  240. $target . '/ocs/v2.php/apps/federation/api/v1/shared-secret',
  241. [
  242. 'query' =>
  243. [
  244. 'url' => $source,
  245. 'token' => $token,
  246. 'format' => 'json',
  247. ],
  248. 'timeout' => 3,
  249. 'connect_timeout' => 3,
  250. ]
  251. )->willThrowException($this->createMock(ConnectException::class));
  252. $this->trustedServers->expects($this->never())->method('addSharedSecret');
  253. $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
  254. $this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
  255. }
  256. }