GetSharedSecretTest.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Federation\Tests\BackgroundJob;
  25. use OCA\Federation\BackgroundJob\GetSharedSecret;
  26. use OCA\Files_Sharing\Tests\TestCase;
  27. use OCA\Federation\DbHandler;
  28. use OCA\Federation\TrustedServers;
  29. use OCP\AppFramework\Http;
  30. use OCP\BackgroundJob\IJobList;
  31. use OCP\Http\Client\IClient;
  32. use OCP\Http\Client\IResponse;
  33. use OCP\ILogger;
  34. use OCP\IURLGenerator;
  35. /**
  36. * Class GetSharedSecretTest
  37. *
  38. * @group DB
  39. *
  40. * @package OCA\Federation\Tests\BackgroundJob
  41. */
  42. class GetSharedSecretTest extends TestCase {
  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 | DbHandler */
  52. private $dbHandler;
  53. /** @var \PHPUnit_Framework_MockObject_MockObject | ILogger */
  54. private $logger;
  55. /** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */
  56. private $response;
  57. /** @var GetSharedSecret */
  58. private $getSharedSecret;
  59. public function setUp() {
  60. parent::setUp();
  61. $this->httpClient = $this->getMock('OCP\Http\Client\IClient');
  62. $this->jobList = $this->getMock('OCP\BackgroundJob\IJobList');
  63. $this->urlGenerator = $this->getMock('OCP\IURLGenerator');
  64. $this->trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
  65. ->disableOriginalConstructor()->getMock();
  66. $this->dbHandler = $this->getMockBuilder('OCA\Federation\DbHandler')
  67. ->disableOriginalConstructor()->getMock();
  68. $this->logger = $this->getMock('OCP\ILogger');
  69. $this->response = $this->getMock('OCP\Http\Client\IResponse');
  70. $this->getSharedSecret = new GetSharedSecret(
  71. $this->httpClient,
  72. $this->urlGenerator,
  73. $this->jobList,
  74. $this->trustedServers,
  75. $this->logger,
  76. $this->dbHandler
  77. );
  78. }
  79. /**
  80. * @dataProvider dataTestExecute
  81. *
  82. * @param bool $isTrustedServer
  83. * @param bool $retainBackgroundJob
  84. */
  85. public function testExecute($isTrustedServer, $retainBackgroundJob) {
  86. /** @var GetSharedSecret |\PHPUnit_Framework_MockObject_MockObject $getSharedSecret */
  87. $getSharedSecret = $this->getMockBuilder('OCA\Federation\BackgroundJob\GetSharedSecret')
  88. ->setConstructorArgs(
  89. [
  90. $this->httpClient,
  91. $this->urlGenerator,
  92. $this->jobList,
  93. $this->trustedServers,
  94. $this->logger,
  95. $this->dbHandler
  96. ]
  97. )->setMethods(['parentExecute'])->getMock();
  98. $this->invokePrivate($getSharedSecret, 'argument', [['url' => 'url']]);
  99. $this->trustedServers->expects($this->once())->method('isTrustedServer')
  100. ->with('url')->willReturn($isTrustedServer);
  101. if ($isTrustedServer) {
  102. $getSharedSecret->expects($this->once())->method('parentExecute');
  103. } else {
  104. $getSharedSecret->expects($this->never())->method('parentExecute');
  105. }
  106. $this->invokePrivate($getSharedSecret, 'retainJob', [$retainBackgroundJob]);
  107. if ($retainBackgroundJob) {
  108. $this->jobList->expects($this->never())->method('remove');
  109. } else {
  110. $this->jobList->expects($this->once())->method('remove');
  111. }
  112. $getSharedSecret->execute($this->jobList);
  113. }
  114. public function dataTestExecute() {
  115. return [
  116. [true, true],
  117. [true, false],
  118. [false, false],
  119. ];
  120. }
  121. /**
  122. * @dataProvider dataTestRun
  123. *
  124. * @param int $statusCode
  125. */
  126. public function testRun($statusCode) {
  127. $target = 'targetURL';
  128. $source = 'sourceURL';
  129. $token = 'token';
  130. $argument = ['url' => $target, 'token' => $token];
  131. $this->urlGenerator->expects($this->once())->method('getAbsoluteURL')->with('/')
  132. ->willReturn($source);
  133. $this->httpClient->expects($this->once())->method('get')
  134. ->with(
  135. $target . '/ocs/v2.php/apps/federation/api/v1/shared-secret?format=json',
  136. [
  137. 'query' =>
  138. [
  139. 'url' => $source,
  140. 'token' => $token
  141. ],
  142. 'timeout' => 3,
  143. 'connect_timeout' => 3,
  144. ]
  145. )->willReturn($this->response);
  146. $this->response->expects($this->once())->method('getStatusCode')
  147. ->willReturn($statusCode);
  148. if (
  149. $statusCode !== Http::STATUS_OK
  150. && $statusCode !== Http::STATUS_FORBIDDEN
  151. ) {
  152. $this->dbHandler->expects($this->never())->method('addToken');
  153. } else {
  154. $this->dbHandler->expects($this->once())->method('addToken')->with($target, '');
  155. }
  156. if ($statusCode === Http::STATUS_OK) {
  157. $this->response->expects($this->once())->method('getBody')
  158. ->willReturn('{"ocs":{"data":{"sharedSecret":"secret"}}}');
  159. $this->trustedServers->expects($this->once())->method('addSharedSecret')
  160. ->with($target, 'secret');
  161. } else {
  162. $this->trustedServers->expects($this->never())->method('addSharedSecret');
  163. }
  164. $this->invokePrivate($this->getSharedSecret, 'run', [$argument]);
  165. if (
  166. $statusCode !== Http::STATUS_OK
  167. && $statusCode !== Http::STATUS_FORBIDDEN
  168. ) {
  169. $this->assertTrue($this->invokePrivate($this->getSharedSecret, 'retainJob'));
  170. } else {
  171. $this->assertFalse($this->invokePrivate($this->getSharedSecret, 'retainJob'));
  172. }
  173. }
  174. public function dataTestRun() {
  175. return [
  176. [Http::STATUS_OK],
  177. [Http::STATUS_FORBIDDEN],
  178. [Http::STATUS_CONFLICT],
  179. ];
  180. }
  181. }