NotificationsTest.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Samuel <faust64@gmail.com>
  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\FederatedFileSharing\Tests;
  27. use OCA\FederatedFileSharing\AddressHandler;
  28. use OCA\FederatedFileSharing\Notifications;
  29. use OCP\BackgroundJob\IJobList;
  30. use OCP\EventDispatcher\IEventDispatcher;
  31. use OCP\Federation\ICloudFederationFactory;
  32. use OCP\Federation\ICloudFederationProviderManager;
  33. use OCP\Http\Client\IClientService;
  34. use OCP\OCS\IDiscoveryService;
  35. use PHPUnit\Framework\MockObject\MockObject;
  36. use Psr\Log\LoggerInterface;
  37. class NotificationsTest extends \Test\TestCase {
  38. /** @var AddressHandler|MockObject */
  39. private $addressHandler;
  40. /** @var IClientService|MockObject*/
  41. private $httpClientService;
  42. /** @var IDiscoveryService|MockObject */
  43. private $discoveryService;
  44. /** @var IJobList|MockObject */
  45. private $jobList;
  46. /** @var ICloudFederationProviderManager|MockObject */
  47. private $cloudFederationProviderManager;
  48. /** @var ICloudFederationFactory|MockObject */
  49. private $cloudFederationFactory;
  50. /** @var IEventDispatcher|MockObject */
  51. private $eventDispatcher;
  52. /** @var LoggerInterface|MockObject */
  53. private $logger;
  54. protected function setUp(): void {
  55. parent::setUp();
  56. $this->jobList = $this->getMockBuilder('OCP\BackgroundJob\IJobList')->getMock();
  57. $this->discoveryService = $this->getMockBuilder(IDiscoveryService::class)->getMock();
  58. $this->httpClientService = $this->getMockBuilder('OCP\Http\Client\IClientService')->getMock();
  59. $this->addressHandler = $this->getMockBuilder('OCA\FederatedFileSharing\AddressHandler')
  60. ->disableOriginalConstructor()->getMock();
  61. $this->logger = $this->createMock(LoggerInterface::class);
  62. $this->cloudFederationProviderManager = $this->createMock(ICloudFederationProviderManager::class);
  63. $this->cloudFederationFactory = $this->createMock(ICloudFederationFactory::class);
  64. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  65. }
  66. /**
  67. * get instance of Notifications class
  68. *
  69. * @param array $mockedMethods methods which should be mocked
  70. * @return Notifications | \PHPUnit\Framework\MockObject\MockObject
  71. */
  72. private function getInstance(array $mockedMethods = []) {
  73. if (empty($mockedMethods)) {
  74. $instance = new Notifications(
  75. $this->addressHandler,
  76. $this->httpClientService,
  77. $this->discoveryService,
  78. $this->jobList,
  79. $this->cloudFederationProviderManager,
  80. $this->cloudFederationFactory,
  81. $this->eventDispatcher,
  82. $this->logger,
  83. );
  84. } else {
  85. $instance = $this->getMockBuilder('OCA\FederatedFileSharing\Notifications')
  86. ->setConstructorArgs(
  87. [
  88. $this->addressHandler,
  89. $this->httpClientService,
  90. $this->discoveryService,
  91. $this->jobList,
  92. $this->cloudFederationProviderManager,
  93. $this->cloudFederationFactory,
  94. $this->eventDispatcher,
  95. $this->logger,
  96. ]
  97. )->setMethods($mockedMethods)->getMock();
  98. }
  99. return $instance;
  100. }
  101. /**
  102. * @dataProvider dataTestSendUpdateToRemote
  103. *
  104. * @param int $try
  105. * @param array $httpRequestResult
  106. * @param bool $expected
  107. */
  108. public function testSendUpdateToRemote($try, $httpRequestResult, $expected) {
  109. $remote = 'http://remote';
  110. $id = 42;
  111. $timestamp = 63576;
  112. $token = 'token';
  113. $action = 'unshare';
  114. $instance = $this->getInstance(['tryHttpPostToShareEndpoint', 'getTimestamp']);
  115. $instance->expects($this->any())->method('getTimestamp')->willReturn($timestamp);
  116. $instance->expects($this->once())->method('tryHttpPostToShareEndpoint')
  117. ->with($remote, '/'.$id.'/unshare', ['token' => $token, 'data1Key' => 'data1Value', 'remoteId' => $id], $action)
  118. ->willReturn($httpRequestResult);
  119. // only add background job on first try
  120. if ($try === 0 && $expected === false) {
  121. $this->jobList->expects($this->once())->method('add')
  122. ->with(
  123. 'OCA\FederatedFileSharing\BackgroundJob\RetryJob',
  124. [
  125. 'remote' => $remote,
  126. 'remoteId' => $id,
  127. 'action' => 'unshare',
  128. 'data' => json_encode(['data1Key' => 'data1Value']),
  129. 'token' => $token,
  130. 'try' => $try,
  131. 'lastRun' => $timestamp
  132. ]
  133. );
  134. } else {
  135. $this->jobList->expects($this->never())->method('add');
  136. }
  137. $this->assertSame($expected,
  138. $instance->sendUpdateToRemote($remote, $id, $token, $action, ['data1Key' => 'data1Value'], $try)
  139. );
  140. }
  141. public function dataTestSendUpdateToRemote() {
  142. return [
  143. // test if background job is added correctly
  144. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  145. [1, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  146. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  147. [1, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  148. // test all combinations of 'statuscode' and 'success'
  149. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], true],
  150. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])], true],
  151. [0, ['success' => true, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 400]]])], false],
  152. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 200]]])], false],
  153. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 100]]])], false],
  154. [0, ['success' => false, 'result' => json_encode(['ocs' => ['meta' => ['statuscode' => 400]]])], false],
  155. ];
  156. }
  157. }