ExternalShareControllerTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests\Controllers;
  8. use OCA\Files_Sharing\Controller\ExternalSharesController;
  9. use OCA\Files_Sharing\External\Manager;
  10. use OCP\AppFramework\Http\DataResponse;
  11. use OCP\AppFramework\Http\JSONResponse;
  12. use OCP\Http\Client\IClient;
  13. use OCP\Http\Client\IClientService;
  14. use OCP\Http\Client\IResponse;
  15. use OCP\IConfig;
  16. use OCP\IRequest;
  17. use PHPUnit\Framework\MockObject\MockObject;
  18. /**
  19. * Class ExternalShareControllerTest
  20. *
  21. * @package OCA\Files_Sharing\Controllers
  22. */
  23. class ExternalShareControllerTest extends \Test\TestCase {
  24. /** @var IRequest */
  25. private $request;
  26. /** @var \OCA\Files_Sharing\External\Manager */
  27. private $externalManager;
  28. /** @var IConfig|MockObject */
  29. private $config;
  30. /** @var IClientService */
  31. private $clientService;
  32. protected function setUp(): void {
  33. parent::setUp();
  34. $this->request = $this->createMock(IRequest::class);
  35. $this->externalManager = $this->createMock(Manager::class);
  36. $this->clientService = $this->createMock(IClientService::class);
  37. $this->config = $this->createMock(IConfig::class);
  38. }
  39. /**
  40. * @return ExternalSharesController
  41. */
  42. public function getExternalShareController() {
  43. return new ExternalSharesController(
  44. 'files_sharing',
  45. $this->request,
  46. $this->externalManager,
  47. $this->clientService,
  48. $this->config,
  49. );
  50. }
  51. public function testIndex() {
  52. $this->externalManager
  53. ->expects($this->once())
  54. ->method('getOpenShares')
  55. ->willReturn(['MyDummyArray']);
  56. $this->assertEquals(new JSONResponse(['MyDummyArray']), $this->getExternalShareController()->index());
  57. }
  58. public function testCreate() {
  59. $this->externalManager
  60. ->expects($this->once())
  61. ->method('acceptShare')
  62. ->with(4);
  63. $this->assertEquals(new JSONResponse(), $this->getExternalShareController()->create(4));
  64. }
  65. public function testDestroy() {
  66. $this->externalManager
  67. ->expects($this->once())
  68. ->method('declineShare')
  69. ->with(4);
  70. $this->assertEquals(new JSONResponse(), $this->getExternalShareController()->destroy(4));
  71. }
  72. public function testRemoteWithValidHttps() {
  73. $client = $this->createMock(IClient::class);
  74. $response = $this->createMock(IResponse::class);
  75. $response
  76. ->expects($this->exactly(2))
  77. ->method('getBody')
  78. ->willReturnOnConsecutiveCalls(
  79. 'Certainly not a JSON string',
  80. '{"installed":true,"maintenance":false,"version":"8.1.0.8","versionstring":"8.1.0","edition":""}'
  81. );
  82. $client
  83. ->expects($this->any())
  84. ->method('get')
  85. ->willReturn($response);
  86. $this->clientService
  87. ->expects($this->exactly(2))
  88. ->method('newClient')
  89. ->willReturn($client);
  90. $this->assertEquals(new DataResponse('https'), $this->getExternalShareController()->testRemote('nextcloud.com'));
  91. }
  92. public function testRemoteWithWorkingHttp() {
  93. $client = $this->createMock(IClient::class);
  94. $response = $this->createMock(IResponse::class);
  95. $client
  96. ->method('get')
  97. ->willReturn($response);
  98. $response
  99. ->expects($this->exactly(5))
  100. ->method('getBody')
  101. ->willReturnOnConsecutiveCalls(
  102. 'Certainly not a JSON string',
  103. 'Certainly not a JSON string',
  104. 'Certainly not a JSON string',
  105. 'Certainly not a JSON string',
  106. '{"installed":true,"maintenance":false,"version":"8.1.0.8","versionstring":"8.1.0","edition":""}'
  107. );
  108. $this->clientService
  109. ->expects($this->exactly(5))
  110. ->method('newClient')
  111. ->willReturn($client);
  112. $this->assertEquals(new DataResponse('http'), $this->getExternalShareController()->testRemote('nextcloud.com'));
  113. }
  114. public function testRemoteWithInvalidRemote() {
  115. $client = $this->createMock(IClient::class);
  116. $response = $this->createMock(IResponse::class);
  117. $client
  118. ->expects($this->exactly(6))
  119. ->method('get')
  120. ->willReturn($response);
  121. $response
  122. ->expects($this->exactly(6))
  123. ->method('getBody')
  124. ->willReturn('Certainly not a JSON string');
  125. $this->clientService
  126. ->expects($this->exactly(6))
  127. ->method('newClient')
  128. ->willReturn($client);
  129. $this->assertEquals(new DataResponse(false), $this->getExternalShareController()->testRemote('nextcloud.com'));
  130. }
  131. public function dataRemoteWithInvalidRemoteURLs(): array {
  132. return [
  133. ['nextcloud.com?query'],
  134. ['nextcloud.com/#anchor'],
  135. ['nextcloud.com/;tomcat'],
  136. ];
  137. }
  138. /**
  139. * @dataProvider dataRemoteWithInvalidRemoteURLs
  140. * @param string $remote
  141. */
  142. public function testRemoteWithInvalidRemoteURLs(string $remote) {
  143. $this->clientService
  144. ->expects($this->never())
  145. ->method('newClient');
  146. $this->assertEquals(new DataResponse(false), $this->getExternalShareController()->testRemote($remote));
  147. }
  148. }