RequestSharedSecretTest.php 7.7 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\IURLGenerator;
  37. use OCP\OCS\IDiscoveryService;
  38. use PHPUnit\Framework\MockObject\MockObject;
  39. use Psr\Log\LoggerInterface;
  40. use Test\TestCase;
  41. class RequestSharedSecretTest extends TestCase {
  42. /** @var MockObject|IClientService */
  43. private $httpClientService;
  44. /** @var MockObject|IClient */
  45. private $httpClient;
  46. /** @var MockObject|IJobList */
  47. private $jobList;
  48. /** @var MockObject|IURLGenerator */
  49. private $urlGenerator;
  50. /** @var MockObject|TrustedServers */
  51. private $trustedServers;
  52. /** @var MockObject|IResponse */
  53. private $response;
  54. /** @var MockObject|IDiscoveryService */
  55. private $discoveryService;
  56. /** @var MockObject|LoggerInterface */
  57. private $logger;
  58. /** @var MockObject|ITimeFactory */
  59. private $timeFactory;
  60. /** @var RequestSharedSecret */
  61. private $requestSharedSecret;
  62. protected function setUp(): void {
  63. parent::setUp();
  64. $this->httpClientService = $this->createMock(IClientService::class);
  65. $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
  66. $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
  67. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  68. $this->trustedServers = $this->getMockBuilder(TrustedServers::class)
  69. ->disableOriginalConstructor()->getMock();
  70. $this->response = $this->getMockBuilder(IResponse::class)->getMock();
  71. $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
  72. $this->logger = $this->createMock(LoggerInterface::class);
  73. $this->timeFactory = $this->createMock(ITimeFactory::class);
  74. $this->discoveryService->expects($this->any())->method('discover')->willReturn([]);
  75. $this->httpClientService->expects($this->any())->method('newClient')->willReturn($this->httpClient);
  76. $this->requestSharedSecret = new RequestSharedSecret(
  77. $this->httpClientService,
  78. $this->urlGenerator,
  79. $this->jobList,
  80. $this->trustedServers,
  81. $this->discoveryService,
  82. $this->logger,
  83. $this->timeFactory
  84. );
  85. }
  86. /**
  87. * @dataProvider dataTestStart
  88. *
  89. * @param bool $isTrustedServer
  90. * @param bool $retainBackgroundJob
  91. */
  92. public function testStart($isTrustedServer, $retainBackgroundJob) {
  93. /** @var RequestSharedSecret |MockObject $requestSharedSecret */
  94. $requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret')
  95. ->setConstructorArgs(
  96. [
  97. $this->httpClientService,
  98. $this->urlGenerator,
  99. $this->jobList,
  100. $this->trustedServers,
  101. $this->discoveryService,
  102. $this->logger,
  103. $this->timeFactory
  104. ]
  105. )->setMethods(['parentStart'])->getMock();
  106. $this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url', 'token' => 'token']]);
  107. $this->trustedServers->expects($this->once())->method('isTrustedServer')
  108. ->with('url')->willReturn($isTrustedServer);
  109. if ($isTrustedServer) {
  110. $requestSharedSecret->expects($this->once())->method('parentStart');
  111. } else {
  112. $requestSharedSecret->expects($this->never())->method('parentStart');
  113. }
  114. $this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]);
  115. $this->jobList->expects($this->once())->method('remove');
  116. $this->timeFactory->method('getTime')->willReturn(42);
  117. if ($retainBackgroundJob) {
  118. $this->jobList->expects($this->once())
  119. ->method('add')
  120. ->with(
  121. RequestSharedSecret::class,
  122. [
  123. 'url' => 'url',
  124. 'token' => 'token',
  125. 'created' => 42,
  126. ]
  127. );
  128. } else {
  129. $this->jobList->expects($this->never())->method('add');
  130. }
  131. $requestSharedSecret->start($this->jobList);
  132. }
  133. public function dataTestStart() {
  134. return [
  135. [true, true],
  136. [true, false],
  137. [false, false],
  138. ];
  139. }
  140. /**
  141. * @dataProvider dataTestRun
  142. *
  143. * @param int $statusCode
  144. */
  145. public function testRun($statusCode) {
  146. $target = 'targetURL';
  147. $source = 'sourceURL';
  148. $token = 'token';
  149. $argument = ['url' => $target, 'token' => $token];
  150. $this->timeFactory->method('getTime')->willReturn(42);
  151. $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
  152. ->willReturn($source);
  153. $this->httpClient->expects($this->once())->method('post')
  154. ->with(
  155. $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret',
  156. [
  157. 'body' =>
  158. [
  159. 'url' => $source,
  160. 'token' => $token,
  161. 'format' => 'json',
  162. ],
  163. 'timeout' => 3,
  164. 'connect_timeout' => 3,
  165. ]
  166. )->willReturn($this->response);
  167. $this->response->expects($this->once())->method('getStatusCode')
  168. ->willReturn($statusCode);
  169. $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
  170. if (
  171. $statusCode !== Http::STATUS_OK
  172. && $statusCode !== Http::STATUS_FORBIDDEN
  173. ) {
  174. $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  175. } else {
  176. $this->assertFalse($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  177. }
  178. }
  179. public function dataTestRun() {
  180. return [
  181. [Http::STATUS_OK],
  182. [Http::STATUS_FORBIDDEN],
  183. [Http::STATUS_CONFLICT],
  184. ];
  185. }
  186. public function testRunExpired() {
  187. $target = 'targetURL';
  188. $source = 'sourceURL';
  189. $token = 'token';
  190. $created = 42;
  191. $argument = [
  192. 'url' => $target,
  193. 'token' => $token,
  194. 'created' => $created,
  195. ];
  196. $this->urlGenerator->expects($this->once())
  197. ->method('getAbsoluteURL')
  198. ->with('/')
  199. ->willReturn($source);
  200. $this->timeFactory->method('getTime')
  201. ->willReturn($created + 2592000 + 1);
  202. $this->trustedServers->expects($this->once())
  203. ->method('setServerStatus')
  204. ->with(
  205. $target,
  206. TrustedServers::STATUS_FAILURE
  207. );
  208. $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
  209. }
  210. public function testRunConnectionError() {
  211. $target = 'targetURL';
  212. $source = 'sourceURL';
  213. $token = 'token';
  214. $argument = ['url' => $target, 'token' => $token];
  215. $this->timeFactory->method('getTime')->willReturn(42);
  216. $this->urlGenerator
  217. ->expects($this->once())
  218. ->method('getAbsoluteURL')
  219. ->with('/')
  220. ->willReturn($source);
  221. $this->httpClient
  222. ->expects($this->once())
  223. ->method('post')
  224. ->with(
  225. $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret',
  226. [
  227. 'body' =>
  228. [
  229. 'url' => $source,
  230. 'token' => $token,
  231. 'format' => 'json',
  232. ],
  233. 'timeout' => 3,
  234. 'connect_timeout' => 3,
  235. ]
  236. )->willThrowException($this->createMock(ConnectException::class));
  237. $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
  238. $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  239. }
  240. }