1
0

ExternalShareControllerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Joas Schilling <coding@schilljs.com>
  6. * @author Lukas Reschke <lukas@statuscode.ch>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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\Files_Sharing\Tests\Controllers;
  25. use OCA\Files_Sharing\Controller\ExternalSharesController;
  26. use OCA\Files_Sharing\External\Manager;
  27. use OCP\AppFramework\Http\DataResponse;
  28. use OCP\AppFramework\Http\JSONResponse;
  29. use OCP\Http\Client\IClient;
  30. use OCP\Http\Client\IClientService;
  31. use OCP\Http\Client\IResponse;
  32. use OCP\IConfig;
  33. use OCP\IRequest;
  34. use PHPUnit\Framework\MockObject\MockObject;
  35. /**
  36. * Class ExternalShareControllerTest
  37. *
  38. * @package OCA\Files_Sharing\Controllers
  39. */
  40. class ExternalShareControllerTest extends \Test\TestCase {
  41. /** @var IRequest */
  42. private $request;
  43. /** @var \OCA\Files_Sharing\External\Manager */
  44. private $externalManager;
  45. /** @var IConfig|MockObject */
  46. private $config;
  47. /** @var IClientService */
  48. private $clientService;
  49. protected function setUp(): void {
  50. parent::setUp();
  51. $this->request = $this->createMock(IRequest::class);
  52. $this->externalManager = $this->createMock(Manager::class);
  53. $this->clientService = $this->createMock(IClientService::class);
  54. $this->config = $this->createMock(IConfig::class);
  55. }
  56. /**
  57. * @return ExternalSharesController
  58. */
  59. public function getExternalShareController() {
  60. return new ExternalSharesController(
  61. 'files_sharing',
  62. $this->request,
  63. $this->externalManager,
  64. $this->clientService,
  65. $this->config,
  66. );
  67. }
  68. public function testIndex() {
  69. $this->externalManager
  70. ->expects($this->once())
  71. ->method('getOpenShares')
  72. ->willReturn(['MyDummyArray']);
  73. $this->assertEquals(new JSONResponse(['MyDummyArray']), $this->getExternalShareController()->index());
  74. }
  75. public function testCreate() {
  76. $this->externalManager
  77. ->expects($this->once())
  78. ->method('acceptShare')
  79. ->with(4);
  80. $this->assertEquals(new JSONResponse(), $this->getExternalShareController()->create(4));
  81. }
  82. public function testDestroy() {
  83. $this->externalManager
  84. ->expects($this->once())
  85. ->method('declineShare')
  86. ->with(4);
  87. $this->assertEquals(new JSONResponse(), $this->getExternalShareController()->destroy(4));
  88. }
  89. public function testRemoteWithValidHttps() {
  90. $client = $this->createMock(IClient::class);
  91. $response = $this->createMock(IResponse::class);
  92. $response
  93. ->expects($this->exactly(2))
  94. ->method('getBody')
  95. ->willReturnOnConsecutiveCalls(
  96. 'Certainly not a JSON string',
  97. '{"installed":true,"maintenance":false,"version":"8.1.0.8","versionstring":"8.1.0","edition":""}'
  98. );
  99. $client
  100. ->expects($this->any())
  101. ->method('get')
  102. ->willReturn($response);
  103. $this->clientService
  104. ->expects($this->exactly(2))
  105. ->method('newClient')
  106. ->willReturn($client);
  107. $this->assertEquals(new DataResponse('https'), $this->getExternalShareController()->testRemote('nextcloud.com'));
  108. }
  109. public function testRemoteWithWorkingHttp() {
  110. $client = $this->createMock(IClient::class);
  111. $response = $this->createMock(IResponse::class);
  112. $client
  113. ->method('get')
  114. ->willReturn($response);
  115. $response
  116. ->expects($this->exactly(5))
  117. ->method('getBody')
  118. ->willReturnOnConsecutiveCalls(
  119. 'Certainly not a JSON string',
  120. 'Certainly not a JSON string',
  121. 'Certainly not a JSON string',
  122. 'Certainly not a JSON string',
  123. '{"installed":true,"maintenance":false,"version":"8.1.0.8","versionstring":"8.1.0","edition":""}'
  124. );
  125. $this->clientService
  126. ->expects($this->exactly(5))
  127. ->method('newClient')
  128. ->willReturn($client);
  129. $this->assertEquals(new DataResponse('http'), $this->getExternalShareController()->testRemote('nextcloud.com'));
  130. }
  131. public function testRemoteWithInvalidRemote() {
  132. $client = $this->createMock(IClient::class);
  133. $response = $this->createMock(IResponse::class);
  134. $client
  135. ->expects($this->exactly(6))
  136. ->method('get')
  137. ->willReturn($response);
  138. $response
  139. ->expects($this->exactly(6))
  140. ->method('getBody')
  141. ->willReturn('Certainly not a JSON string');
  142. $this->clientService
  143. ->expects($this->exactly(6))
  144. ->method('newClient')
  145. ->willReturn($client);
  146. $this->assertEquals(new DataResponse(false), $this->getExternalShareController()->testRemote('nextcloud.com'));
  147. }
  148. public function dataRemoteWithInvalidRemoteURLs(): array {
  149. return [
  150. ['nextcloud.com?query'],
  151. ['nextcloud.com/#anchor'],
  152. ['nextcloud.com/;tomcat'],
  153. ];
  154. }
  155. /**
  156. * @dataProvider dataRemoteWithInvalidRemoteURLs
  157. * @param string $remote
  158. */
  159. public function testRemoteWithInvalidRemoteURLs(string $remote) {
  160. $this->clientService
  161. ->expects($this->never())
  162. ->method('newClient');
  163. $this->assertEquals(new DataResponse(false), $this->getExternalShareController()->testRemote($remote));
  164. }
  165. }