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