ExternalShareControllerTest.php 5.1 KB

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