RequestSharedSecretTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OCA\Federation\Tests\BackgroundJob;
  24. use OCA\Federation\BackgroundJob\RequestSharedSecret;
  25. use OCA\Federation\DbHandler;
  26. use OCA\Federation\TrustedServers;
  27. use OCP\AppFramework\Http;
  28. use OCP\BackgroundJob\IJobList;
  29. use OCP\Http\Client\IClient;
  30. use OCP\Http\Client\IResponse;
  31. use OCP\IURLGenerator;
  32. use Test\TestCase;
  33. class RequestSharedSecretTest extends TestCase {
  34. /** @var \PHPUnit_Framework_MockObject_MockObject | IClient */
  35. private $httpClient;
  36. /** @var \PHPUnit_Framework_MockObject_MockObject | IJobList */
  37. private $jobList;
  38. /** @var \PHPUnit_Framework_MockObject_MockObject | IURLGenerator */
  39. private $urlGenerator;
  40. /** @var \PHPUnit_Framework_MockObject_MockObject | DbHandler */
  41. private $dbHandler;
  42. /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */
  43. private $trustedServers;
  44. /** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */
  45. private $response;
  46. /** @var RequestSharedSecret */
  47. private $requestSharedSecret;
  48. public function setUp() {
  49. parent::setUp();
  50. $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
  51. $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
  52. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)->getMock();
  53. $this->trustedServers = $this->getMockBuilder(TrustedServers::class)
  54. ->disableOriginalConstructor()->getMock();
  55. $this->dbHandler = $this->getMockBuilder(DbHandler::class)
  56. ->disableOriginalConstructor()->getMock();
  57. $this->response = $this->getMockBuilder(IResponse::class)->getMock();
  58. $this->requestSharedSecret = new RequestSharedSecret(
  59. $this->httpClient,
  60. $this->urlGenerator,
  61. $this->jobList,
  62. $this->trustedServers,
  63. $this->dbHandler
  64. );
  65. }
  66. /**
  67. * @dataProvider dataTestExecute
  68. *
  69. * @param bool $isTrustedServer
  70. * @param bool $retainBackgroundJob
  71. */
  72. public function testExecute($isTrustedServer, $retainBackgroundJob) {
  73. /** @var RequestSharedSecret |\PHPUnit_Framework_MockObject_MockObject $requestSharedSecret */
  74. $requestSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\RequestSharedSecret')
  75. ->setConstructorArgs(
  76. [
  77. $this->httpClient,
  78. $this->urlGenerator,
  79. $this->jobList,
  80. $this->trustedServers,
  81. $this->dbHandler
  82. ]
  83. )->setMethods(['parentExecute'])->getMock();
  84. $this->invokePrivate($requestSharedSecret, 'argument', [['url' => 'url']]);
  85. $this->trustedServers->expects($this->once())->method('isTrustedServer')
  86. ->with('url')->willReturn($isTrustedServer);
  87. if ($isTrustedServer) {
  88. $requestSharedSecret->expects($this->once())->method('parentExecute');
  89. } else {
  90. $requestSharedSecret->expects($this->never())->method('parentExecute');
  91. }
  92. $this->invokePrivate($requestSharedSecret, 'retainJob', [$retainBackgroundJob]);
  93. if ($retainBackgroundJob) {
  94. $this->jobList->expects($this->never())->method('remove');
  95. } else {
  96. $this->jobList->expects($this->once())->method('remove');
  97. }
  98. $requestSharedSecret->execute($this->jobList);
  99. }
  100. public function dataTestExecute() {
  101. return [
  102. [true, true],
  103. [true, false],
  104. [false, false],
  105. ];
  106. }
  107. /**
  108. * @dataProvider dataTestRun
  109. *
  110. * @param int $statusCode
  111. */
  112. public function testRun($statusCode) {
  113. $target = 'targetURL';
  114. $source = 'sourceURL';
  115. $token = 'token';
  116. $argument = ['url' => $target, 'token' => $token];
  117. $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
  118. ->willReturn($source);
  119. $this->httpClient->expects($this->once())->method('post')
  120. ->with(
  121. $target . '/ocs/v2.php/apps/federation/api/v1/request-shared-secret?format=json',
  122. [
  123. 'body' =>
  124. [
  125. 'url' => $source,
  126. 'token' => $token
  127. ],
  128. 'timeout' => 3,
  129. 'connect_timeout' => 3,
  130. ]
  131. )->willReturn($this->response);
  132. $this->response->expects($this->once())->method('getStatusCode')
  133. ->willReturn($statusCode);
  134. if (
  135. $statusCode !== Http::STATUS_OK
  136. && $statusCode !== Http::STATUS_FORBIDDEN
  137. ) {
  138. $this->dbHandler->expects($this->never())->method('addToken');
  139. }
  140. if ($statusCode === Http::STATUS_FORBIDDEN) {
  141. $this->dbHandler->expects($this->once())->method('addToken')->with($target, '');
  142. }
  143. $this->invokePrivate($this->requestSharedSecret, 'run', [$argument]);
  144. if (
  145. $statusCode !== Http::STATUS_OK
  146. && $statusCode !== Http::STATUS_FORBIDDEN
  147. ) {
  148. $this->assertTrue($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  149. } else {
  150. $this->assertFalse($this->invokePrivate($this->requestSharedSecret, 'retainJob'));
  151. }
  152. }
  153. public function dataTestRun() {
  154. return [
  155. [Http::STATUS_OK],
  156. [Http::STATUS_FORBIDDEN],
  157. [Http::STATUS_CONFLICT],
  158. ];
  159. }
  160. }