DiscoveryManagerTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. <?php
  2. /**
  3. * @author Björn Schießle <bjoern@schiessle.org>
  4. * @author Lukas Reschke <lukas@statuscode.ch>
  5. *
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OCA\FederatedFileSharing\Tests;
  23. use OCA\FederatedFileSharing\DiscoveryManager;
  24. use OCP\Http\Client\IClient;
  25. use OCP\Http\Client\IClientService;
  26. use OCP\ICache;
  27. use OCP\ICacheFactory;
  28. class DiscoveryManagerTest extends \Test\TestCase {
  29. /** @var ICache */
  30. private $cache;
  31. /** @var IClient */
  32. private $client;
  33. /** @var DiscoveryManager */
  34. private $discoveryManager;
  35. public function setUp() {
  36. parent::setUp();
  37. $this->cache = $this->getMock('\OCP\ICache');
  38. /** @var ICacheFactory $cacheFactory */
  39. $cacheFactory = $this->getMockBuilder('\OCP\ICacheFactory')
  40. ->disableOriginalConstructor()->getMock();
  41. $cacheFactory
  42. ->expects($this->once())
  43. ->method('create')
  44. ->with('ocs-discovery')
  45. ->willReturn($this->cache);
  46. $this->client = $this->getMockBuilder('\OCP\Http\Client\IClient')
  47. ->disableOriginalConstructor()->getMock();
  48. /** @var IClientService $clientService */
  49. $clientService = $this->getMockBuilder('\OCP\Http\Client\IClientService')
  50. ->disableOriginalConstructor()->getMock();
  51. $clientService
  52. ->expects($this->once())
  53. ->method('newClient')
  54. ->willReturn($this->client);
  55. $this->discoveryManager = new DiscoveryManager(
  56. $cacheFactory,
  57. $clientService
  58. );
  59. }
  60. public function testWithMalformedFormattedEndpointCached() {
  61. $response = $this->getMock('\OCP\Http\Client\IResponse');
  62. $response
  63. ->expects($this->once())
  64. ->method('getStatusCode')
  65. ->willReturn(200);
  66. $response
  67. ->expects($this->once())
  68. ->method('getBody')
  69. ->willReturn('CertainlyNotJson');
  70. $this->client
  71. ->expects($this->once())
  72. ->method('get')
  73. ->with('https://myhost.com/ocs-provider/', [
  74. 'timeout' => 10,
  75. 'connect_timeout' => 10,
  76. ])
  77. ->willReturn($response);
  78. $this->cache
  79. ->expects($this->at(0))
  80. ->method('get')
  81. ->with('https://myhost.com')
  82. ->willReturn(null);
  83. $this->cache
  84. ->expects($this->at(1))
  85. ->method('set')
  86. ->with('https://myhost.com', '{"webdav":"\/public.php\/webdav","share":"\/ocs\/v1.php\/cloud\/shares"}');
  87. $this->cache
  88. ->expects($this->at(2))
  89. ->method('get')
  90. ->with('https://myhost.com')
  91. ->willReturn('{"webdav":"\/public.php\/webdav","share":"\/ocs\/v1.php\/cloud\/shares"}');
  92. $this->assertSame('/public.php/webdav', $this->discoveryManager->getWebDavEndpoint('https://myhost.com'));
  93. $this->assertSame('/ocs/v1.php/cloud/shares', $this->discoveryManager->getShareEndpoint('https://myhost.com'));
  94. }
  95. public function testGetWebDavEndpointWithValidFormattedEndpointAndNotCached() {
  96. $response = $this->getMock('\OCP\Http\Client\IResponse');
  97. $response
  98. ->expects($this->once())
  99. ->method('getStatusCode')
  100. ->willReturn(200);
  101. $response
  102. ->expects($this->once())
  103. ->method('getBody')
  104. ->willReturn('{"version":2,"services":{"PRIVATE_DATA":{"version":1,"endpoints":{"store":"\/ocs\/v2.php\/privatedata\/setattribute","read":"\/ocs\/v2.php\/privatedata\/getattribute","delete":"\/ocs\/v2.php\/privatedata\/deleteattribute"}},"SHARING":{"version":1,"endpoints":{"share":"\/ocs\/v2.php\/apps\/files_sharing\/api\/v1\/shares"}},"FEDERATED_SHARING":{"version":1,"endpoints":{"share":"\/ocs\/v2.php\/cloud\/shares","webdav":"\/public.php\/MyCustomEndpoint\/"}},"ACTIVITY":{"version":1,"endpoints":{"list":"\/ocs\/v2.php\/cloud\/activity"}},"PROVISIONING":{"version":1,"endpoints":{"user":"\/ocs\/v2.php\/cloud\/users","groups":"\/ocs\/v2.php\/cloud\/groups","apps":"\/ocs\/v2.php\/cloud\/apps"}}}}');
  105. $this->client
  106. ->expects($this->once())
  107. ->method('get')
  108. ->with('https://myhost.com/ocs-provider/', [
  109. 'timeout' => 10,
  110. 'connect_timeout' => 10,
  111. ])
  112. ->willReturn($response);
  113. $expectedResult = '/public.php/MyCustomEndpoint/';
  114. $this->assertSame($expectedResult, $this->discoveryManager->getWebDavEndpoint('https://myhost.com'));
  115. }
  116. public function testGetWebDavEndpointWithValidFormattedEndpointWithoutDataAndNotCached() {
  117. $response = $this->getMock('\OCP\Http\Client\IResponse');
  118. $response
  119. ->expects($this->once())
  120. ->method('getStatusCode')
  121. ->willReturn(200);
  122. $response
  123. ->expects($this->once())
  124. ->method('getBody')
  125. ->willReturn('{"version":2,"PRIVATE_DATA":{"version":1,"endpoints":{"store":"\/ocs\/v2.php\/privatedata\/setattribute","read":"\/ocs\/v2.php\/privatedata\/getattribute","delete":"\/ocs\/v2.php\/privatedata\/deleteattribute"}},"SHARING":{"version":1,"endpoints":{"share":"\/ocs\/v2.php\/apps\/files_sharing\/api\/v1\/shares"}},"FEDERATED_SHARING":{"version":1,"endpoints":{"share":"\/ocs\/v2.php\/cloud\/shares","webdav":"\/public.php\/MyCustomEndpoint\/"}},"ACTIVITY":{"version":1,"endpoints":{"list":"\/ocs\/v2.php\/cloud\/activity"}},"PROVISIONING":{"version":1,"endpoints":{"user":"\/ocs\/v2.php\/cloud\/users","groups":"\/ocs\/v2.php\/cloud\/groups","apps":"\/ocs\/v2.php\/cloud\/apps"}}}');
  126. $this->client
  127. ->expects($this->once())
  128. ->method('get')
  129. ->with('https://myhost.com/ocs-provider/', [
  130. 'timeout' => 10,
  131. 'connect_timeout' => 10,
  132. ])
  133. ->willReturn($response);
  134. $expectedResult = '/public.php/webdav';
  135. $this->assertSame($expectedResult, $this->discoveryManager->getWebDavEndpoint('https://myhost.com'));
  136. }
  137. public function testGetShareEndpointWithValidFormattedEndpointAndNotCached() {
  138. $response = $this->getMock('\OCP\Http\Client\IResponse');
  139. $response
  140. ->expects($this->once())
  141. ->method('getStatusCode')
  142. ->willReturn(200);
  143. $response
  144. ->expects($this->once())
  145. ->method('getBody')
  146. ->willReturn('{"version":2,"services":{"PRIVATE_DATA":{"version":1,"endpoints":{"store":"\/ocs\/v2.php\/privatedata\/setattribute","read":"\/ocs\/v2.php\/privatedata\/getattribute","delete":"\/ocs\/v2.php\/privatedata\/deleteattribute"}},"SHARING":{"version":1,"endpoints":{"share":"\/ocs\/v2.php\/apps\/files_sharing\/api\/v1\/shares"}},"FEDERATED_SHARING":{"version":1,"endpoints":{"share":"\/ocs\/v2.php\/cloud\/MyCustomShareEndpoint","webdav":"\/public.php\/MyCustomEndpoint\/"}},"ACTIVITY":{"version":1,"endpoints":{"list":"\/ocs\/v2.php\/cloud\/activity"}},"PROVISIONING":{"version":1,"endpoints":{"user":"\/ocs\/v2.php\/cloud\/users","groups":"\/ocs\/v2.php\/cloud\/groups","apps":"\/ocs\/v2.php\/cloud\/apps"}}}}');
  147. $this->client
  148. ->expects($this->once())
  149. ->method('get')
  150. ->with('https://myhost.com/ocs-provider/', [
  151. 'timeout' => 10,
  152. 'connect_timeout' => 10,
  153. ])
  154. ->willReturn($response);
  155. $expectedResult = '/ocs/v2.php/cloud/MyCustomShareEndpoint';
  156. $this->assertSame($expectedResult, $this->discoveryManager->getShareEndpoint('https://myhost.com'));
  157. }
  158. public function testWithMaliciousEndpointCached() {
  159. $response = $this->getMock('\OCP\Http\Client\IResponse');
  160. $response
  161. ->expects($this->once())
  162. ->method('getStatusCode')
  163. ->willReturn(200);
  164. $response
  165. ->expects($this->once())
  166. ->method('getBody')
  167. ->willReturn('{"version":2,"services":{"PRIVATE_DATA":{"version":1,"endpoints":{"store":"\/ocs\/v2.php\/privatedata\/setattribute","read":"\/ocs\/v2.php\/privatedata\/getattribute","delete":"\/ocs\/v2.php\/privatedata\/deleteattribute"}},"SHARING":{"version":1,"endpoints":{"share":"\/ocs\/v2.php\/apps\/files_sharing\/api\/v1\/shares"}},"FEDERATED_SHARING":{"version":1,"endpoints":{"share":"\/ocs\/v2.php\/cl@oud\/MyCustomShareEndpoint","webdav":"\/public.php\/MyC:ustomEndpoint\/"}},"ACTIVITY":{"version":1,"endpoints":{"list":"\/ocs\/v2.php\/cloud\/activity"}},"PROVISIONING":{"version":1,"endpoints":{"user":"\/ocs\/v2.php\/cloud\/users","groups":"\/ocs\/v2.php\/cloud\/groups","apps":"\/ocs\/v2.php\/cloud\/apps"}}}}');
  168. $this->client
  169. ->expects($this->once())
  170. ->method('get')
  171. ->with('https://myhost.com/ocs-provider/', [
  172. 'timeout' => 10,
  173. 'connect_timeout' => 10,
  174. ])
  175. ->willReturn($response);
  176. $this->cache
  177. ->expects($this->at(0))
  178. ->method('get')
  179. ->with('https://myhost.com')
  180. ->willReturn(null);
  181. $this->cache
  182. ->expects($this->at(1))
  183. ->method('set')
  184. ->with('https://myhost.com', '{"webdav":"\/public.php\/webdav","share":"\/ocs\/v1.php\/cloud\/shares"}');
  185. $this->cache
  186. ->expects($this->at(2))
  187. ->method('get')
  188. ->with('https://myhost.com')
  189. ->willReturn('{"webdav":"\/public.php\/webdav","share":"\/ocs\/v1.php\/cloud\/shares"}');
  190. $this->assertSame('/public.php/webdav', $this->discoveryManager->getWebDavEndpoint('https://myhost.com'));
  191. $this->assertSame('/ocs/v1.php/cloud/shares', $this->discoveryManager->getShareEndpoint('https://myhost.com'));
  192. }
  193. }