ExternalShareControllerTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /**
  31. * Class ExternalShareControllerTest
  32. *
  33. * @package OCA\Files_Sharing\Controllers
  34. */
  35. class ExternalShareControllerTest extends \Test\TestCase {
  36. /** @var IRequest */
  37. private $request;
  38. /** @var \OCA\Files_Sharing\External\Manager */
  39. private $externalManager;
  40. /** @var IClientService */
  41. private $clientService;
  42. public function setUp() {
  43. parent::setUp();
  44. $this->request = $this->getMockBuilder('\\OCP\\IRequest')
  45. ->disableOriginalConstructor()->getMock();
  46. $this->externalManager = $this->getMockBuilder('\\OCA\\Files_Sharing\\External\\Manager')
  47. ->disableOriginalConstructor()->getMock();
  48. $this->clientService = $this->getMockBuilder('\\OCP\Http\\Client\\IClientService')
  49. ->disableOriginalConstructor()->getMock();
  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. ->will($this->returnValue(['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->getMockBuilder('\\OCP\\Http\\Client\\IClient')
  85. ->disableOriginalConstructor()->getMock();
  86. $response = $this->getMockBuilder('\\OCP\\Http\\Client\\IResponse')
  87. ->disableOriginalConstructor()->getMock();
  88. $response
  89. ->expects($this->exactly(2))
  90. ->method('getBody')
  91. ->will($this->onConsecutiveCalls('Certainly not a JSON string', '{"installed":true,"maintenance":false,"version":"8.1.0.8","versionstring":"8.1.0","edition":""}'));
  92. $client
  93. ->expects($this->any())
  94. ->method('get')
  95. ->will($this->returnValue($response));
  96. $this->clientService
  97. ->expects($this->exactly(2))
  98. ->method('newClient')
  99. ->will($this->returnValue($client));
  100. $this->assertEquals(new DataResponse('https'), $this->getExternalShareController()->testRemote('owncloud.org'));
  101. }
  102. public function testRemoteWithWorkingHttp() {
  103. $client = $this->getMockBuilder('\\OCP\\Http\\Client\\IClient')
  104. ->disableOriginalConstructor()->getMock();
  105. $response = $this->getMockBuilder('\\OCP\\Http\\Client\\IResponse')
  106. ->disableOriginalConstructor()->getMock();
  107. $client
  108. ->method('get')
  109. ->will($this->returnValue($response));
  110. $response
  111. ->expects($this->exactly(5))
  112. ->method('getBody')
  113. ->will($this->onConsecutiveCalls('Certainly not a JSON string', 'Certainly not a JSON string', 'Certainly not a JSON string', 'Certainly not a JSON string', '{"installed":true,"maintenance":false,"version":"8.1.0.8","versionstring":"8.1.0","edition":""}'));
  114. $this->clientService
  115. ->expects($this->exactly(5))
  116. ->method('newClient')
  117. ->will($this->returnValue($client));
  118. $this->assertEquals(new DataResponse('http'), $this->getExternalShareController()->testRemote('owncloud.org'));
  119. }
  120. public function testRemoteWithInvalidRemote() {
  121. $client = $this->getMockBuilder('\\OCP\\Http\\Client\\IClient')
  122. ->disableOriginalConstructor()->getMock();
  123. $response = $this->getMockBuilder('\\OCP\\Http\\Client\\IResponse')
  124. ->disableOriginalConstructor()->getMock();
  125. $client
  126. ->method('get')
  127. ->will($this->returnValue($response));
  128. $response
  129. ->expects($this->exactly(6))
  130. ->method('getBody')
  131. ->will($this->returnValue('Certainly not a JSON string'));
  132. $this->clientService
  133. ->expects($this->exactly(6))
  134. ->method('newClient')
  135. ->will($this->returnValue($client));
  136. $this->assertEquals(new DataResponse(false), $this->getExternalShareController()->testRemote('owncloud.org'));
  137. }
  138. }