RequestSharedSecretTest.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OCA\Federation\Tests\BackgroundJob;
  27. use GuzzleHttp\Exception\ConnectException;
  28. use OCA\Federation\BackgroundJob\RequestSharedSecret;
  29. use OCA\Federation\TrustedServers;
  30. use OCP\AppFramework\Http;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\BackgroundJob\IJobList;
  33. use OCP\Http\Client\IClient;
  34. use OCP\Http\Client\IClientService;
  35. use OCP\Http\Client\IResponse;
  36. use OCP\ILogger;
  37. use OCP\IURLGenerator;
  38. use OCP\OCS\IDiscoveryService;
  39. use Test\TestCase;
  40. class RequestSharedSecretTest extends TestCase {
  41. /** @var \PHPUnit\Framework\MockObject\MockObject|IClientService */
  42. private $httpClientService;
  43. /** @var \PHPUnit\Framework\MockObject\MockObject|IClient */
  44. private $httpClient;
  45. /** @var \PHPUnit\Framework\MockObject\MockObject|IJobList */
  46. private $jobList;
  47. /** @var \PHPUnit\Framework\MockObject\MockObject|IURLGenerator */
  48. private $urlGenerator;
  49. /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers */
  50. private $trustedServers;
  51. /** @var \PHPUnit\Framework\MockObject\MockObject|IResponse */
  52. private $response;
  53. /** @var \PHPUnit\Framework\MockObject\MockObject|IDiscoveryService */
  54. private $discoveryService;
  55. /** @var \PHPUnit\Framework\MockObject\MockObject|ILogger */
  56. private $logger;
  57. /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
  58. private $timeFactory;
  59. /** @var RequestSharedSecret */
  60. private $requestSharedSecret;
  61. protected function setUp(): void {
  62. parent::setUp();
  63. $this->httpClientService = $this->createMock(IClientService::class);
  64. $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
  65. $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
  66. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  67. $this->trustedServers = $this->getMockBuilder(TrustedServers::class)
  68. ->disableOriginalConstructor()->getMock();
  69. $this->response = $this->getMockBuilder(IResponse::class)->getMock();
  70. $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
  71. $this->logger = $this->createMock(ILogger::class);
  72. $this->timeFactory = $this->createMock(ITimeFactory::class);
  73. $this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
  74. $this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
  75. $this->requestSharedSecret = new RequestSharedSecret(
  76. $this->httpClientService,
  77. $this->urlGenerator,
  78. $this->jobList,
  79. $this->trustedServers,
  80. $this->discoveryService,
  81. $this->logger,
  82. $this->timeFactory
  83. );
  84. }
  85. /**
  86. * @dataProvider dataTestExecute
  87. *
  88. * @param bool $isTrustedServer
  89. * @param bool $retainBackgroundJob
  90. */
  91. public function testExecute($isTrustedServer, $retainBackgroundJob) {
  92. /** @var RequestSharedSecret |\PHPUnit\Framework\MockObject\MockObject $requestSharedSecret */
  93. $requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret')
  94. ->setConstructorArgs(
  95. [
  96. $this->httpClientService,
  97. $this->urlGenerator,
  98. $this->jobList,
  99. $this->trustedServers,
  100. $this->discoveryService,
  101. $this->logger,
  102. $this->timeFactory
  103. ]
  104. )->setMethods(['parentExecute'])->getMock();
  105. $this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
  106. $this->trustedServers->expects($this->once())->method('isTrustedServer')
  107. ->with('url')->willReturn($isTrustedServer);
  108. if ($isTrustedServer) {
  109. $requestSharedSecret->expects($this->once())->method('parentExecute');
  110. } else {
  111. $requestSharedSecret->expects($this->never())->method('parentExecute');
  112. }
  113. $this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]);
  114. $this->jobList->expects($this->once())->method('remove');
  115. $this->timeFactory->method('getTime')->willReturn(42);
  116. if ($retainBackgroundJob) {
  117. $this->jobList->expects($this->once())
  118. ->method('add')
  119. ->with(
  120. RequestSharedSecret::class,
  121. [
  122. 'url' => 'url',
  123. 'token' => 'token',
  124. 'created' => 42,
  125. ]
  126. );
  127. } else {
  128. $this->jobList->expects($this->never())->method('add');
  129. }
  130. $requestSharedSecret->execute($this->jobList);
  131. }
  132. public function dataTestExecute() {
  133. return [
  134. [true, true],
  135. [true, false],
  136. [false, false],
  137. ];
  138. }
  139. /**
  140. * @dataProvider dataTestRun
  141. *
  142. * @param int $statusCode
  143. */
  144. public function testRun($statusCode) {
  145. $target = 'targetURL';
  146. $source = 'sourceURL';
  147. $token = 'token';
  148. $argument = ['url' => $target, 'token' => $token];
  149. $this->timeFactory->method('getTime')->willReturn(42);
  150. $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
  151. ->willReturn($source);
  152. $this->httpClient->expects($this->once())->method('post')
  153. ->with(
  154. $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret',
  155. [
  156. 'body' =>
  157. [
  158. 'url' => $source,
  159. 'token' => $token,
  160. 'format' => 'json',
  161. ],
  162. 'timeout' => 3,
  163. 'connect_timeout' => 3,
  164. ]
  165. )->willReturn($this->response);
  166. $this->response->expects($this->once())->method('getStatusCode')
  167. ->willReturn($statusCode);
  168. $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
  169. if (
  170. $statusCode !== Http::STATUS_OK
  171. && $statusCode !== Http::STATUS_FORBIDDEN
  172. ) {
  173. $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  174. } else {
  175. $this->assertFalse($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  176. }
  177. }
  178. public function dataTestRun() {
  179. return [
  180. [Http::STATUS_OK],
  181. [Http::STATUS_FORBIDDEN],
  182. [Http::STATUS_CONFLICT],
  183. ];
  184. }
  185. public function testRunExpired() {
  186. $target = 'targetURL';
  187. $source = 'sourceURL';
  188. $token = 'token';
  189. $created = 42;
  190. $argument = [
  191. 'url' => $target,
  192. 'token' => $token,
  193. 'created' => $created,
  194. ];
  195. $this->urlGenerator->expects($this->once())
  196. ->method('getAbsoluteURL')
  197. ->with('/')
  198. ->willReturn($source);
  199. $this->timeFactory->method('getTime')
  200. ->willReturn($created + 2592000 + 1);
  201. $this->trustedServers->expects($this->once())
  202. ->method('setServerStatus')
  203. ->with(
  204. $target,
  205. TrustedServers::STATUS_FAILURE
  206. );
  207. $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
  208. }
  209. public function testRunConnectionError() {
  210. $target = 'targetURL';
  211. $source = 'sourceURL';
  212. $token = 'token';
  213. $argument = ['url' => $target, 'token' => $token];
  214. $this->timeFactory->method('getTime')->willReturn(42);
  215. $this->urlGenerator
  216. ->expects($this->once())
  217. ->method('getAbsoluteURL')
  218. ->with('/')
  219. ->willReturn($source);
  220. $this->httpClient
  221. ->expects($this->once())
  222. ->method('post')
  223. ->with(
  224. $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret',
  225. [
  226. 'body' =>
  227. [
  228. 'url' => $source,
  229. 'token' => $token,
  230. 'format' => 'json',
  231. ],
  232. 'timeout' => 3,
  233. 'connect_timeout' => 3,
  234. ]
  235. )->willThrowException($this->createMock(ConnectException::class));
  236. $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
  237. $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  238. }
  239. }