You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
9.7 KiB

10 years ago
10 years ago
10 years ago
10 years ago
  1. <?php
  2. /**
  3. * @author Joas Schilling <nickvergessen@owncloud.com>
  4. * @author Robin Appelman <icewind@owncloud.com>
  5. *
  6. * @copyright Copyright (c) 2015, 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\Files_Sharing\Tests\External;
  23. use OC\Files\Storage\StorageFactory;
  24. use OCA\Files_Sharing\External\Manager;
  25. use OCA\Files_Sharing\External\MountProvider;
  26. use OCA\Files_Sharing\Tests\TestCase;
  27. use Test\Traits\UserTrait;
  28. class ManagerTest extends TestCase {
  29. use UserTrait;
  30. /** @var Manager **/
  31. private $manager;
  32. /** @var \OC\Files\Mount\Manager */
  33. private $mountManager;
  34. /** @var \PHPUnit_Framework_MockObject_MockObject */
  35. private $httpHelper;
  36. private $uid;
  37. /**
  38. * @var \OCP\IUser
  39. */
  40. private $user;
  41. private $mountProvider;
  42. protected function setUp() {
  43. parent::setUp();
  44. $this->uid = $this->getUniqueID('user');
  45. $this->createUser($this->uid, '');
  46. $this->user = \OC::$server->getUserManager()->get($this->uid);
  47. $this->mountManager = new \OC\Files\Mount\Manager();
  48. $this->httpHelper = $httpHelper = $this->getMockBuilder('\OC\HTTPHelper')->disableOriginalConstructor()->getMock();
  49. /** @var \OC\HTTPHelper $httpHelper */
  50. $this->manager = new Manager(
  51. \OC::$server->getDatabaseConnection(),
  52. $this->mountManager,
  53. new StorageFactory(),
  54. $httpHelper,
  55. \OC::$server->getNotificationManager(),
  56. $this->uid
  57. );
  58. $this->mountProvider = new MountProvider(\OC::$server->getDatabaseConnection(), function() {
  59. return $this->manager;
  60. });
  61. }
  62. private function setupMounts() {
  63. $mounts = $this->mountProvider->getMountsForUser($this->user, new StorageFactory());
  64. foreach ($mounts as $mount) {
  65. $this->mountManager->addMount($mount);
  66. }
  67. }
  68. public function testAddShare() {
  69. $shareData1 = [
  70. 'remote' => 'http://localhost',
  71. 'token' => 'token1',
  72. 'password' => '',
  73. 'name' => '/SharedFolder',
  74. 'owner' => 'foobar',
  75. 'accepted' => false,
  76. 'user' => $this->uid,
  77. ];
  78. $shareData2 = $shareData1;
  79. $shareData2['token'] = 'token2';
  80. $shareData3 = $shareData1;
  81. $shareData3['token'] = 'token3';
  82. // Add a share for "user"
  83. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData1));
  84. $openShares = $this->manager->getOpenShares();
  85. $this->assertCount(1, $openShares);
  86. $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  87. $this->setupMounts();
  88. $this->assertNotMount('SharedFolder');
  89. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  90. // Add a second share for "user" with the same name
  91. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData2));
  92. $openShares = $this->manager->getOpenShares();
  93. $this->assertCount(2, $openShares);
  94. $this->assertExternalShareEntry($shareData1, $openShares[0], 1, '{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  95. // New share falls back to "-1" appendix, because the name is already taken
  96. $this->assertExternalShareEntry($shareData2, $openShares[1], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  97. $this->setupMounts();
  98. $this->assertNotMount('SharedFolder');
  99. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  100. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  101. $this->httpHelper->expects($this->at(0))
  102. ->method('post')
  103. ->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[0]['remote_id']), $this->anything());
  104. // Accept the first share
  105. $this->manager->acceptShare($openShares[0]['id']);
  106. // Check remaining shares - Accepted
  107. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  108. $this->assertCount(1, $acceptedShares);
  109. $shareData1['accepted'] = true;
  110. $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
  111. // Check remaining shares - Open
  112. $openShares = $this->manager->getOpenShares();
  113. $this->assertCount(1, $openShares);
  114. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  115. $this->setupMounts();
  116. $this->assertMount($shareData1['name']);
  117. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  118. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  119. // Add another share for "user" with the same name
  120. $this->assertSame(null, call_user_func_array([$this->manager, 'addShare'], $shareData3));
  121. $openShares = $this->manager->getOpenShares();
  122. $this->assertCount(2, $openShares);
  123. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  124. // New share falls back to the original name (no "-\d", because the name is not taken)
  125. $this->assertExternalShareEntry($shareData3, $openShares[1], 3, '{{TemporaryMountPointName#' . $shareData3['name'] . '}}');
  126. $this->setupMounts();
  127. $this->assertMount($shareData1['name']);
  128. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  129. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  130. $this->httpHelper->expects($this->at(0))
  131. ->method('post')
  132. ->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[1]['remote_id'] . '/decline'), $this->anything());
  133. // Decline the third share
  134. $this->manager->declineShare($openShares[1]['id']);
  135. $this->setupMounts();
  136. $this->assertMount($shareData1['name']);
  137. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  138. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  139. // Check remaining shares - Accepted
  140. $acceptedShares = self::invokePrivate($this->manager, 'getShares', [true]);
  141. $this->assertCount(1, $acceptedShares);
  142. $shareData1['accepted'] = true;
  143. $this->assertExternalShareEntry($shareData1, $acceptedShares[0], 1, $shareData1['name']);
  144. // Check remaining shares - Open
  145. $openShares = $this->manager->getOpenShares();
  146. $this->assertCount(1, $openShares);
  147. $this->assertExternalShareEntry($shareData2, $openShares[0], 2, '{{TemporaryMountPointName#' . $shareData2['name'] . '}}-1');
  148. $this->setupMounts();
  149. $this->assertMount($shareData1['name']);
  150. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  151. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  152. $this->httpHelper->expects($this->at(0))
  153. ->method('post')
  154. ->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $openShares[0]['remote_id'] . '/decline'), $this->anything());
  155. $this->httpHelper->expects($this->at(1))
  156. ->method('post')
  157. ->with($this->stringStartsWith('http://localhost/ocs/v1.php/cloud/shares/' . $acceptedShares[0]['remote_id'] . '/decline'), $this->anything());
  158. $this->manager->removeUserShares($this->uid);
  159. $this->assertEmpty(self::invokePrivate($this->manager, 'getShares', [null]), 'Asserting all shares for the user have been deleted');
  160. $this->mountManager->clear();
  161. self::invokePrivate($this->manager, 'setupMounts');
  162. $this->assertNotMount($shareData1['name']);
  163. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}');
  164. $this->assertNotMount('{{TemporaryMountPointName#' . $shareData1['name'] . '}}-1');
  165. }
  166. /**
  167. * @param array $expected
  168. * @param array $actual
  169. * @param int $share
  170. * @param string $mountPoint
  171. */
  172. protected function assertExternalShareEntry($expected, $actual, $share, $mountPoint) {
  173. $this->assertEquals($expected['remote'], $actual['remote'], 'Asserting remote of a share #' . $share);
  174. $this->assertEquals($expected['token'], $actual['share_token'], 'Asserting token of a share #' . $share);
  175. $this->assertEquals($expected['name'], $actual['name'], 'Asserting name of a share #' . $share);
  176. $this->assertEquals($expected['owner'], $actual['owner'], 'Asserting owner of a share #' . $share);
  177. $this->assertEquals($expected['accepted'], (int) $actual['accepted'], 'Asserting accept of a share #' . $share);
  178. $this->assertEquals($expected['user'], $actual['user'], 'Asserting user of a share #' . $share);
  179. $this->assertEquals($mountPoint, $actual['mountpoint'], 'Asserting mountpoint of a share #' . $share);
  180. }
  181. private function assertMount($mountPoint) {
  182. $mountPoint = rtrim($mountPoint, '/');
  183. $mount = $this->mountManager->find($this->getFullPath($mountPoint));
  184. $this->assertInstanceOf('\OCA\Files_Sharing\External\Mount', $mount);
  185. $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
  186. $this->assertEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
  187. $storage = $mount->getStorage();
  188. $this->assertInstanceOf('\OCA\Files_Sharing\External\Storage', $storage);
  189. }
  190. private function assertNotMount($mountPoint) {
  191. $mountPoint = rtrim($mountPoint, '/');
  192. $mount = $this->mountManager->find($this->getFullPath($mountPoint));
  193. if ($mount) {
  194. $this->assertInstanceOf('\OCP\Files\Mount\IMountPoint', $mount);
  195. $this->assertNotEquals($this->getFullPath($mountPoint), rtrim($mount->getMountPoint(), '/'));
  196. } else {
  197. $this->assertNull($mount);
  198. }
  199. }
  200. private function getFullPath($path) {
  201. return '/' . $this->uid . '/files' . $path;
  202. }
  203. }