Commit 2d773660 authored by Vincent Petry's avatar Vincent Petry Committed by Thomas Müller
Browse files

Moved sharing related tests to Share2.0 (#26610)

Also moved old ShareTest tests to be integration tests instead, as they
cannot be applied directly as unit tests.
parent 0b91f8d9
......@@ -127,17 +127,19 @@ class DeleteOrphanedSharesJobTest extends \Test\TestCase {
public function testClearShares() {
$this->loginAsUser($this->user1);
$view = new \OC\Files\View('/' . $this->user1 . '/');
$view->mkdir('files/test');
$view->mkdir('files/test/sub');
$folder = \OC::$server->getUserFolder($this->user1);
$testFolder = $folder->newFolder('test');
$sharedFolder = $folder->newFolder('test/sub');
$fileInfo = $view->getFileInfo('files/test/sub');
$fileId = $fileInfo->getId();
$shareManager = \OC::$server->getShareManager();
$share = $shareManager->newShare();
$share->setSharedBy($this->user1);
$share->setShareType(\OCP\Share::SHARE_TYPE_USER);
$share->setNode($sharedFolder);
$share->setSharedWith($this->user2);
$share->setPermissions(\OCP\Constants::PERMISSION_READ);
$this->assertTrue(
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ),
'Failed asserting that user 1 successfully shared "test/sub" with user 2.'
);
$shareManager->createShare($share);
$this->assertCount(1, $this->getShares());
......@@ -145,7 +147,7 @@ class DeleteOrphanedSharesJobTest extends \Test\TestCase {
$this->assertCount(1, $this->getShares(), 'Linked shares not deleted');
$view->unlink('files/test');
$testFolder->delete();
$this->job->run([]);
......
......@@ -131,15 +131,16 @@ class ExpireSharesJobTest extends \Test\TestCase {
public function testExpireLinkShare($addExpiration, $interval, $addInterval, $shouldExpire) {
$this->loginAsUser($this->user1);
$view = new \OC\Files\View('/' . $this->user1 . '/');
$view->mkdir('files/test');
$userFolder = \OC::$server->getUserFolder($this->user1);
$sharedFolder = $userFolder->newFolder('test');
$fileInfo = $view->getFileInfo('files/test');
$this->assertNotNull(
\OCP\Share::shareItem('folder', $fileInfo->getId(), \OCP\Share::SHARE_TYPE_LINK, null, \OCP\Constants::PERMISSION_READ),
'Failed asserting that user 1 successfully shared "test" by link.'
);
$shareManager = \OC::$server->getShareManager();
$share = $shareManager->newShare();
$share->setSharedBy($this->user1);
$share->setShareType(\OCP\Share::SHARE_TYPE_LINK);
$share->setNode($sharedFolder);
$share->setPermissions(\OCP\Constants::PERMISSION_READ);
$shareManager->createShare($share);
$shares = $this->getShares();
$this->assertCount(1, $shares);
......@@ -185,15 +186,17 @@ class ExpireSharesJobTest extends \Test\TestCase {
public function testDoNotExpireOtherShares() {
$this->loginAsUser($this->user1);
$view = new \OC\Files\View('/' . $this->user1 . '/');
$view->mkdir('files/test');
$fileInfo = $view->getFileInfo('files/test');
$this->assertNotNull(
\OCP\Share::shareItem('folder', $fileInfo->getId(), \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ),
'Failed asserting that user 1 successfully shared "test" by link with user2.'
);
$userFolder = \OC::$server->getUserFolder($this->user1);
$sharedFolder = $userFolder->newFolder('test');
$shareManager = \OC::$server->getShareManager();
$share = $shareManager->newShare();
$share->setSharedBy($this->user1);
$share->setSharedWith($this->user2);
$share->setShareType(\OCP\Share::SHARE_TYPE_USER);
$share->setNode($sharedFolder);
$share->setPermissions(\OCP\Constants::PERMISSION_READ);
$shareManager->createShare($share);
$shares = $this->getShares();
$this->assertCount(1, $shares);
......
......@@ -27,6 +27,8 @@
namespace OCA\Files_Sharing\Tests;
use OCP\Share\Exceptions\ShareNotFound;
/**
* Class UpdaterTest
*
......@@ -68,7 +70,7 @@ class UpdaterTest extends TestCase {
* points should be unshared before the folder gets deleted so
* that the mount point doesn't end up at the trash bin
*/
function testDeleteParentFolder() {
public function testDeleteParentFolder() {
$status = \OC_App::isEnabled('files_trashbin');
\OC_App::enable('files_trashbin');
......@@ -77,7 +79,7 @@ class UpdaterTest extends TestCase {
$fileinfo = \OC\Files\Filesystem::getFileInfo($this->folder);
$this->assertTrue($fileinfo instanceof \OC\Files\FileInfo);
$this->share(
$share = $this->share(
\OCP\Share::SHARE_TYPE_USER,
$this->folder,
self::TEST_FILES_SHARING_API_USER1,
......@@ -91,8 +93,8 @@ class UpdaterTest extends TestCase {
// check if user2 can see the shared folder
$this->assertTrue($view->file_exists($this->folder));
$foldersShared = \OCP\Share::getItemsSharedWith('folder');
$this->assertSame(1, count($foldersShared));
// share still exists, no exception
$this->shareManager->getShareById($share->getFullId());
$view->mkdir("localFolder");
$view->file_put_contents("localFolder/localFile.txt", "local file");
......@@ -108,8 +110,14 @@ class UpdaterTest extends TestCase {
$this->loginHelper(self::TEST_FILES_SHARING_API_USER2);
// shared folder should be unshared
$foldersShared = \OCP\Share::getItemsSharedWith('folder');
$this->assertTrue(empty($foldersShared));
$caught = null;
try {
$this->shareManager->getShareById($share->getFullId());
} catch (ShareNotFound $e) {
$caught = $e;
}
$this->assertNotNull($caught);
// trashbin should contain the local file but not the mount point
$rootView = new \OC\Files\View('/' . self::TEST_FILES_SHARING_API_USER2);
......
......@@ -254,6 +254,7 @@ trait Sharing {
* @param string $filename
*/
public function checkSharedFileInResponse($filename){
$filename = ltrim($filename, '/');
PHPUnit_Framework_Assert::assertEquals(True, $this->isFieldInResponse('file_target', "/$filename"));
}
......@@ -263,9 +264,30 @@ trait Sharing {
* @param string $filename
*/
public function checkSharedFileNotInResponse($filename){
$filename = ltrim($filename, '/');
PHPUnit_Framework_Assert::assertEquals(False, $this->isFieldInResponse('file_target', "/$filename"));
}
/**
* @Then /^File "([^"]*)" should be included as path in the response$/
*
* @param string $filename
*/
public function checkSharedFileAsPathInResponse($filename){
$filename = ltrim($filename, '/');
PHPUnit_Framework_Assert::assertEquals(True, $this->isFieldInResponse('path', "/$filename"));
}
/**
* @Then /^File "([^"]*)" should not be included as path in the response$/
*
* @param string $filename
*/
public function checkSharedFileAsPathNotInResponse($filename){
$filename = ltrim($filename, '/');
PHPUnit_Framework_Assert::assertEquals(False, $this->isFieldInResponse('path', "/$filename"));
}
/**
* @Then /^User "([^"]*)" should be included in the response$/
*
......@@ -384,6 +406,14 @@ trait Sharing {
}
}
/**
* @Then /^the response contains ([0-9]+) entries$/
*/
public function checkingTheResponseEntriesCount($count){
$actualCount = count($this->response->xml()->data[0]);
PHPUnit_Framework_Assert::assertEquals($count, $actualCount);
}
/**
* @Then /^Share fields of last share match with$/
* @param \Behat\Gherkin\Node\TableNode|null $body
......
......@@ -901,6 +901,66 @@ Feature: sharing
Then the OCS status code should be "100"
And the HTTP status code should be "200"
Scenario: sharing subfolder when parent already shared
Given As an "admin"
Given user "user0" exists
Given user "user1" exists
And group "sharing-group" exists
And user "user0" created a folder "/test"
And user "user0" created a folder "/test/sub"
And file "/test" of user "user0" is shared with group "sharing-group"
And As an "user0"
When sending "POST" to "/apps/files_sharing/api/v1/shares" with
| path | /test/sub |
| shareWith | user1 |
| shareType | 0 |
Then the OCS status code should be "100"
And the HTTP status code should be "200"
And as "user1" the folder "/sub" exists
Scenario: sharing subfolder when parent already shared with group of sharer
Given As an "admin"
Given user "user0" exists
Given user "user1" exists
And group "sharing-group" exists
And user "user0" belongs to group "sharing-group"
And user "user0" created a folder "/test"
And user "user0" created a folder "/test/sub"
And file "/test" of user "user0" is shared with group "sharing-group"
And As an "user0"
When sending "POST" to "/apps/files_sharing/api/v1/shares" with
| path | /test/sub |
| shareWith | user1 |
| shareType | 0 |
Then the OCS status code should be "100"
And the HTTP status code should be "200"
And as "user1" the folder "/sub" exists
Scenario: sharing subfolder of already shared folder, GET result is correct
Given As an "admin"
Given user "user0" exists
Given user "user1" exists
Given user "user2" exists
Given user "user3" exists
Given user "user4" exists
And user "user0" created a folder "/folder1"
And file "/folder1" of user "user0" is shared with user "user1"
And file "/folder1" of user "user0" is shared with user "user2"
And user "user0" created a folder "/folder1/folder2"
And file "/folder1/folder2" of user "user0" is shared with user "user3"
And file "/folder1/folder2" of user "user0" is shared with user "user4"
And As an "user0"
When sending "GET" to "/apps/files_sharing/api/v1/shares"
Then the OCS status code should be "100"
And the HTTP status code should be "200"
And the response contains 4 entries
And File "/folder1" should be included as path in the response
And File "/folder1/folder2" should be included as path in the response
And sending "GET" to "/apps/files_sharing/api/v1/shares?path=/folder1/folder2"
And the response contains 2 entries
And File "/folder1" should not be included as path in the response
And File "/folder1/folder2" should be included as path in the response
Scenario: unshare from self
Given As an "admin"
And user "user0" exists
......
......@@ -1524,9 +1524,7 @@ class ViewTest extends TestCase {
private function createTestMovableMountPoints($mountPoints) {
$mounts = [];
foreach ($mountPoints as $mountPoint) {
$storage = $this->getMockBuilder('\OC\Files\Storage\Temporary')
->setMethods([])
->getMock();
$storage = new Temporary();
$mounts[] = $this->getMockBuilder('\Test\TestMoveableMountPoint')
->setMethods(['moveMount'])
......@@ -1599,6 +1597,8 @@ class ViewTest extends TestCase {
public function testMoveMountPointIntoSharedFolder() {
$this->loginAsUser($this->user);
$userFolder = \OC::$server->getUserFolder($this->user);
list($mount1) = $this->createTestMovableMountPoints([
$this->user . '/files/mount1',
]);
......@@ -1611,15 +1611,23 @@ class ViewTest extends TestCase {
$view->mkdir('shareddir/sub');
$view->mkdir('shareddir/sub2');
$fileId = $view->getFileInfo('shareddir')->getId();
$userObject = \OC::$server->getUserManager()->createUser('test2', 'IHateNonMockableStaticClasses');
$this->assertTrue(\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, 'test2', \OCP\Constants::PERMISSION_READ));
$sharedFolder = \OC::$server->getUserFolder($this->user)->get('shareddir');
$shareManager = \OC::$server->getShareManager();
$share = $shareManager->newShare();
$share->setSharedBy($this->user);
$share->setShareType(\OCP\Share::SHARE_TYPE_USER);
$share->setNode($sharedFolder);
$share->setSharedWith('test2');
$share->setPermissions(\OCP\Constants::PERMISSION_READ);
$share = $shareManager->createShare($share);
$this->assertFalse($view->rename('mount1', 'shareddir'), 'Cannot overwrite shared folder');
$this->assertFalse($view->rename('mount1', 'shareddir/sub'), 'Cannot move mount point into shared folder');
$this->assertFalse($view->rename('mount1', 'shareddir/sub/sub2'), 'Cannot move mount point into shared subfolder');
$this->assertTrue(\OCP\Share::unshare('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, 'test2'));
$shareManager->deleteShare($share);
$userObject->delete();
}
......
......@@ -410,46 +410,6 @@ class ShareTest extends \Test\TestCase {
$this->assertSame(\OCP\Share::SHARE_TYPE_USER, $share['share_type']);
}
public function testGetShareFromOutsideFilesFolder() {
\OC_User::setUserId($this->user1);
$view = new \OC\Files\View('/' . $this->user1 . '/');
$view->mkdir('files/test');
$view->mkdir('files/test/sub');
$view->mkdir('files_trashbin');
$view->mkdir('files_trashbin/files');
$fileInfo = $view->getFileInfo('files/test/sub');
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
$fileId = $fileInfo->getId();
$this->assertTrue(
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ),
'Failed asserting that user 1 successfully shared "test/sub" with user 2.'
);
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
$this->assertNotEmpty($result);
$result = \OCP\Share::getItemSharedWithUser('folder', $fileId, $this->user2);
$this->assertNotEmpty($result);
$result = \OCP\Share::getItemsSharedWithUser('folder', $this->user2);
$this->assertNotEmpty($result);
// move to trash (keeps file id)
$view->rename('files/test', 'files_trashbin/files/test');
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
$this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
$result = \OCP\Share::getItemSharedWithUser('folder', $fileId, $this->user2);
$this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
$result = \OCP\Share::getItemsSharedWithUser('folder', $this->user2);
$this->assertEmpty($result, 'Share must not be returned for files outside of "files"');
}
public function testSetExpireDateInPast() {
\OC_User::setUserId($this->user1);
$this->shareUserOneTestFileWithUserTwo();
......@@ -517,75 +477,6 @@ class ShareTest extends \Test\TestCase {
}
public function testSharingAFolderThatIsSharedWithAGroupOfTheOwner() {
\OC_User::setUserId($this->user1);
$view = new \OC\Files\View('/' . $this->user1 . '/');
$view->mkdir('files/test');
$view->mkdir('files/test/sub1');
$view->mkdir('files/test/sub1/sub2');
$fileInfo = $view->getFileInfo('files/test/sub1');
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
$fileId = $fileInfo->getId();
$this->assertTrue(
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_GROUP, $this->group1, \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_CREATE),
'Failed asserting that user 1 successfully shared "test/sub1" with group 1.'
);
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
$this->assertNotEmpty($result);
$this->assertEquals(\OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_CREATE, $result['permissions']);
$fileInfo = $view->getFileInfo('files/test/sub1/sub2');
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
$fileId = $fileInfo->getId();
$this->assertTrue(
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user4, \OCP\Constants::PERMISSION_READ),
'Failed asserting that user 1 successfully shared "test/sub1/sub2" with user 4.'
);
$result = \OCP\Share::getItemShared('folder', $fileId, Backend::FORMAT_SOURCE);
$this->assertNotEmpty($result);
$this->assertEquals(\OCP\Constants::PERMISSION_READ, $result['permissions']);
}
public function testSharingAFileInsideAFolderThatIsAlreadyShared() {
\OC_User::setUserId($this->user1);
$view = new \OC\Files\View('/' . $this->user1 . '/');
$view->mkdir('files/test');
$view->mkdir('files/test/sub1');
$view->file_put_contents('files/test/sub1/file.txt', 'abc');
$folderInfo = $view->getFileInfo('files/test/sub1');
$this->assertInstanceOf('\OC\Files\FileInfo', $folderInfo);
$folderId = $folderInfo->getId();
$fileInfo = $view->getFileInfo('files/test/sub1/file.txt');
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
$fileId = $fileInfo->getId();
$this->assertTrue(
\OCP\Share::shareItem('folder', $folderId, \OCP\Share::SHARE_TYPE_GROUP, $this->group2, \OCP\Constants::PERMISSION_READ + \OCP\Constants::PERMISSION_UPDATE),
'Failed asserting that user 1 successfully shared "test/sub1" with group 2.'
);
$this->assertTrue(
\OCP\Share::shareItem('file', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_READ),
'Failed asserting that user 1 successfully shared "test/sub1/file.txt" with user 2.'
);
$result = \OCP\Share::getItemsSharedWithUser('file', $this->user2);
$this->assertCount(2, $result);
foreach ($result as $share) {
$itemName = substr($share['path'], strrpos($share['path'], '/'));
$this->assertSame($itemName, $share['file_target'], 'Asserting that the file_target is the last segment of the path');
$this->assertSame($share['item_target'], '/' . $share['item_source'], 'Asserting that the item is the item that was shared');
}
}
protected function shareUserOneTestFileWithGroupOne() {
\OC_User::setUserId($this->user1);
$this->assertTrue(
......@@ -1543,97 +1434,29 @@ class ShareTest extends \Test\TestCase {
$config->deleteAppValue('core', 'shareapi_expire_after_n_days');
}
/**
* Test case for #17560
*/
public function testAccesToSharedSubFolder() {
\OC_User::setUserId($this->user1);
$view = new \OC\Files\View('/' . $this->user1 . '/');
$view->mkdir('files/folder1');
$fileInfo = $view->getFileInfo('files/folder1');
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
$fileId = $fileInfo->getId();
$this->assertTrue(
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_ALL),
'Failed asserting that user 1 successfully shared "test" with user 2.'
);
$this->assertTrue(
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user3, \OCP\Constants::PERMISSION_ALL),
'Failed asserting that user 1 successfully shared "test" with user 3.'
);
$view->mkdir('files/folder1/folder2');
$fileInfo = $view->getFileInfo('files/folder1/folder2');
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
$fileId = $fileInfo->getId();
$this->assertTrue(
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user4, \OCP\Constants::PERMISSION_ALL),
'Failed asserting that user 1 successfully shared "test" with user 4.'
);
$res = \OCP\Share::getItemShared(
'folder',
$fileId,
\OCP\Share::FORMAT_NONE,
null,
true
);
$this->assertCount(3, $res);
$this->assertTrue(
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user5, \OCP\Constants::PERMISSION_ALL),
'Failed asserting that user 1 successfully shared "test" with user 5.'
);
$res = \OCP\Share::getItemShared(
'folder',
$fileId,
\OCP\Share::FORMAT_NONE,
null,
true
);
$this->assertCount(4, $res);
}
public function testShareWithSelfError() {
\OC_User::setUserId($this->user1);
$view = new \OC\Files\View('/' . $this->user1 . '/');
$view->mkdir('files/folder1');
$fileInfo = $view->getFileInfo('files/folder1');
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
$fileId = $fileInfo->getId();
try {
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user1, \OCP\Constants::PERMISSION_ALL);
\OCP\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_USER, $this->user1, \OCP\Constants::PERMISSION_ALL);
$this->fail();
} catch (\Exception $e) {
$this->assertEquals('Sharing /folder1 failed, because you can not share with yourself', $e->getMessage());
$this->assertEquals('Sharing test.txt failed, because you can not share with yourself', $e->getMessage());
}
}
public function testShareWithOwnerError() {
\OC_User::setUserId($this->user1);
$view = new \OC\Files\View('/' . $this->user1 . '/');
$view->mkdir('files/folder1');
$fileInfo = $view->getFileInfo('files/folder1');
$this->assertInstanceOf('\OC\Files\FileInfo', $fileInfo);
$fileId = $fileInfo->getId();
$this->assertTrue(
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_ALL),
\OCP\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_USER, $this->user2, \OCP\Constants::PERMISSION_ALL),
'Failed asserting that user 1 successfully shared "test" with user 2.'
);
\OC_User::setUserId($this->user2);
try {
\OCP\Share::shareItem('folder', $fileId, \OCP\Share::SHARE_TYPE_USER, $this->user1, \OCP\Constants::PERMISSION_ALL);
\OCP\Share::shareItem('test', 'test.txt', \OCP\Share::SHARE_TYPE_USER, $this->user1, \OCP\Constants::PERMISSION_ALL);
$this->fail();
} catch (\Exception $e) {
$this->assertEquals('Sharing failed, because the user ' . $this->user1 . ' is the original sharer', $e->getMessage());
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment