GetSharedSecretTest.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. private GetSharedSecret $getSharedSecret;
  68. protected function setUp(): void {
  69. parent::setUp();
  70. $this->httpClientService = $this->createMock(IClientService::class);
  71. $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
  72. $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
  73. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  74. $this->trustedServers = $this->getMockBuilder(TrustedServers::class)
  75. ->disableOriginalConstructor()->getMock();
  76. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  77. $this->response = $this->getMockBuilder(IResponse::class)->getMock();
  78. $this->discoverService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
  79. $this->timeFactory = $this->createMock(ITimeFactory::class);
  80. $this->discoverService->expects($this->any())->method('discover')->willReturn([]);
  81. $this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
  82. $this->getSharedSecret = new GetSharedSecret(
  83. $this->httpClientService,
  84. $this->urlGenerator,
  85. $this->jobList,
  86. $this->trustedServers,
  87. $this->logger,
  88. $this->discoverService,
  89. $this->timeFactory
  90. );
  91. }
  92. /**
  93. * @dataProvider dataTestExecute
  94. *
  95. * @param bool $isTrustedServer
  96. * @param bool $retainBackgroundJob
  97. */
  98. public function testExecute(bool $isTrustedServer, bool $retainBackgroundJob): void {
  99. /** @var GetSharedSecret |\PHPUnit\Framework\MockObject\MockObject $getSharedSecret */
  100. $getSharedSecret = $this->getMockBuilder(GetSharedSecret::class)
  101. ->setConstructorArgs(
  102. [
  103. $this->httpClientService,
  104. $this->urlGenerator,
  105. $this->jobList,
  106. $this->trustedServers,
  107. $this->logger,
  108. $this->discoverService,
  109. $this->timeFactory
  110. ]
  111. )->setMethods(['parentStart'])->getMock();
  112. $this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
  113. $this->trustedServers->expects($this->once())->method('isTrustedServer')
  114. ->with('url')->willReturn($isTrustedServer);
  115. if ($isTrustedServer) {
  116. $getSharedSecret->expects($this->once())->method('parentStart');
  117. } else {
  118. $getSharedSecret->expects($this->never())->method('parentStart');
  119. }
  120. $this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
  121. $this->jobList->expects($this->once())->method('remove');
  122. $this->timeFactory->method('getTime')->willReturn(42);
  123. if ($retainBackgroundJob) {
  124. $this->jobList->expects($this->once())
  125. ->method('add')
  126. ->with(
  127. GetSharedSecret::class,
  128. [
  129. 'url' => 'url',
  130. 'token' => 'token',
  131. 'created' => 42,
  132. ]
  133. );
  134. } else {
  135. $this->jobList->expects($this->never())->method('add');
  136. }
  137. $getSharedSecret->start($this->jobList);
  138. }
  139. public function dataTestExecute() {
  140. return [
  141. [true, true],
  142. [true, false],
  143. [false, false],
  144. ];
  145. }
  146. /**
  147. * @dataProvider dataTestRun
  148. *
  149. * @param int $statusCode
  150. */
  151. public function testRun($statusCode) {
  152. $target = 'targetURL';
  153. $source = 'sourceURL';
  154. $token = 'token';
  155. $argument = ['url' => $target, 'token' => $token];
  156. $this->timeFactory->method('getTime')
  157. ->willReturn(42);
  158. $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
  159. ->willReturn($source);
  160. $this->httpClient->expects($this->once())->method('get')
  161. ->with(
  162. $target . '/ocs/v2.php/apps/federation/api/v1/shared-secret',
  163. [
  164. 'query' =>
  165. [
  166. 'url' => $source,
  167. 'token' => $token,
  168. 'format' => 'json',
  169. ],
  170. 'timeout' => 3,
  171. 'connect_timeout' => 3,
  172. ]
  173. )->willReturn($this->response);
  174. $this->response->expects($this->once())->method('getStatusCode')
  175. ->willReturn($statusCode);
  176. if ($statusCode === Http::STATUS_OK) {
  177. $this->response->expects($this->once())->method('getBody')
  178. ->willReturn('{"ocs":{"data":{"sharedSecret":"secret"}}}');
  179. $this->trustedServers->expects($this->once())->method('addSharedSecret')
  180. ->with($target, 'secret');
  181. } else {
  182. $this->trustedServers->expects($this->never())->method('addSharedSecret');
  183. }
  184. $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
  185. if (
  186. $statusCode !== Http::STATUS_OK
  187. && $statusCode !== Http::STATUS_FORBIDDEN
  188. ) {
  189. $this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
  190. } else {
  191. $this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob'));
  192. }
  193. }
  194. public function dataTestRun() {
  195. return [
  196. [Http::STATUS_OK],
  197. [Http::STATUS_FORBIDDEN],
  198. [Http::STATUS_CONFLICT],
  199. ];
  200. }
  201. public function testRunExpired() {
  202. $target = 'targetURL';
  203. $source = 'sourceURL';
  204. $token = 'token';
  205. $created = 42;
  206. $argument = [
  207. 'url' => $target,
  208. 'token' => $token,
  209. 'created' => $created,
  210. ];
  211. $this->urlGenerator->expects($this->once())
  212. ->method('getAbsoluteURL')
  213. ->with('/')
  214. ->willReturn($source);
  215. $this->timeFactory->method('getTime')
  216. ->willReturn($created + 2592000 + 1);
  217. $this->trustedServers->expects($this->once())
  218. ->method('setServerStatus')
  219. ->with(
  220. $target,
  221. TrustedServers::STATUS_FAILURE
  222. );
  223. $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
  224. }
  225. public function testRunConnectionError() {
  226. $target = 'targetURL';
  227. $source = 'sourceURL';
  228. $token = 'token';
  229. $argument = ['url' => $target, 'token' => $token];
  230. $this->timeFactory->method('getTime')
  231. ->willReturn(42);
  232. $this->urlGenerator
  233. ->expects($this->once())
  234. ->method('getAbsoluteURL')
  235. ->with('/')
  236. ->willReturn($source);
  237. $this->httpClient->expects($this->once())->method('get')
  238. ->with(
  239. $target . '/ocs/v2.php/apps/federation/api/v1/shared-secret',
  240. [
  241. 'query' =>
  242. [
  243. 'url' => $source,
  244. 'token' => $token,
  245. 'format' => 'json',
  246. ],
  247. 'timeout' => 3,
  248. 'connect_timeout' => 3,
  249. ]
  250. )->willThrowException($this->createMock(ConnectException::class));
  251. $this->trustedServers->expects($this->never())->method('addSharedSecret');
  252. $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
  253. $this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
  254. }
  255. }