1
0

RequestSharedSecretTest.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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. 'attempt' => 1,
  127. ]
  128. );
  129. } else {
  130. $this->jobList->expects($this->never())->method('add');
  131. }
  132. $requestSharedSecret->start($this->jobList);
  133. }
  134. public function dataTestStart() {
  135. return [
  136. [true, true],
  137. [true, false],
  138. [false, false],
  139. ];
  140. }
  141. /**
  142. * @dataProvider dataTestRun
  143. *
  144. * @param int $statusCode
  145. */
  146. public function testRun(int $statusCode, int $attempt = 0): void {
  147. $target = 'targetURL';
  148. $source = 'sourceURL';
  149. $token = 'token';
  150. $argument = ['url' => $target, 'token' => $token, 'attempt' => $attempt];
  151. $this->timeFactory->method('getTime')->willReturn(42);
  152. $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
  153. ->willReturn($source);
  154. $this->httpClient->expects($this->once())->method('post')
  155. ->with(
  156. $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret',
  157. [
  158. 'body' =>
  159. [
  160. 'url' => $source,
  161. 'token' => $token,
  162. 'format' => 'json',
  163. ],
  164. 'timeout' => 3,
  165. 'connect_timeout' => 3,
  166. ]
  167. )->willReturn($this->response);
  168. $this->response->expects($this->once())->method('getStatusCode')
  169. ->willReturn($statusCode);
  170. $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
  171. if (
  172. $statusCode !== Http::STATUS_OK
  173. && ($statusCode !== Http::STATUS_FORBIDDEN || $attempt < 5)
  174. ) {
  175. $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  176. } else {
  177. $this->assertFalse($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  178. }
  179. }
  180. public function dataTestRun() {
  181. return [
  182. [Http::STATUS_OK],
  183. [Http::STATUS_FORBIDDEN, 5],
  184. [Http::STATUS_FORBIDDEN],
  185. [Http::STATUS_CONFLICT],
  186. ];
  187. }
  188. public function testRunExpired() {
  189. $target = 'targetURL';
  190. $source = 'sourceURL';
  191. $token = 'token';
  192. $created = 42;
  193. $argument = [
  194. 'url' => $target,
  195. 'token' => $token,
  196. 'created' => $created,
  197. ];
  198. $this->urlGenerator->expects($this->once())
  199. ->method('getAbsoluteURL')
  200. ->with('/')
  201. ->willReturn($source);
  202. $this->timeFactory->method('getTime')
  203. ->willReturn($created + 2592000 + 1);
  204. $this->trustedServers->expects($this->once())
  205. ->method('setServerStatus')
  206. ->with(
  207. $target,
  208. TrustedServers::STATUS_FAILURE
  209. );
  210. $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
  211. }
  212. public function testRunConnectionError() {
  213. $target = 'targetURL';
  214. $source = 'sourceURL';
  215. $token = 'token';
  216. $argument = ['url' => $target, 'token' => $token];
  217. $this->timeFactory->method('getTime')->willReturn(42);
  218. $this->urlGenerator
  219. ->expects($this->once())
  220. ->method('getAbsoluteURL')
  221. ->with('/')
  222. ->willReturn($source);
  223. $this->httpClient
  224. ->expects($this->once())
  225. ->method('post')
  226. ->with(
  227. $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret',
  228. [
  229. 'body' =>
  230. [
  231. 'url' => $source,
  232. 'token' => $token,
  233. 'format' => 'json',
  234. ],
  235. 'timeout' => 3,
  236. 'connect_timeout' => 3,
  237. ]
  238. )->willThrowException($this->createMock(ConnectException::class));
  239. $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
  240. $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  241. }
  242. }