Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Unit tests for federated share expiration date
Signed-off-by: Vincent Petry <[email protected]>
  • Loading branch information
PVince81 committed Apr 15, 2021
commit 2650da70caa73c8bf3119edebb37e91d67f3a214
29 changes: 22 additions & 7 deletions apps/federatedfilesharing/tests/FederatedShareProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,17 @@ protected function tearDown(): void {
parent::tearDown();
}

public function testCreate() {
public function dataTestCreate() {
return [
[null, null],
[new \DateTime('2020-03-01T01:02:03'), '2020-03-01 01:02:03'],
];
}

/**
* @dataProvider dataTestCreate
*/
public function testCreate($expirationDate, $expectedDataDate) {
$share = $this->shareManager->newShare();

/** @var File|MockObject $node */
Expand All @@ -158,6 +168,7 @@ public function testCreate() {
->setShareOwner('shareOwner')
->setPermissions(19)
->setShareType(IShare::TYPE_REMOTE)
->setExpirationDate($expirationDate)
->setNode($node);

$this->tokenHandler->method('generateToken')->willReturn('token');
Expand Down Expand Up @@ -209,6 +220,7 @@ public function testCreate() {
'permissions' => 19,
'accepted' => 0,
'token' => 'token',
'expiration' => $expectedDataDate,
];
foreach (array_keys($expected) as $key) {
$this->assertEquals($expected[$key], $data[$key], "Assert that value for key '$key' is the same");
Expand All @@ -223,6 +235,7 @@ public function testCreate() {
$this->assertEquals(42, $share->getNodeId());
$this->assertEquals(19, $share->getPermissions());
$this->assertEquals('token', $share->getToken());
$this->assertEquals($expirationDate, $share->getExpirationDate());
}

public function testCreateCouldNotFindServer() {
Expand Down Expand Up @@ -442,10 +455,9 @@ public function testCreateAlreadyShared() {
}

/**
* @dataProvider datatTestUpdate
*
* @dataProvider dataTestUpdate
*/
public function testUpdate($owner, $sharedBy) {
public function testUpdate($owner, $sharedBy, $expirationDate) {
$this->provider = $this->getMockBuilder('OCA\FederatedFileSharing\FederatedShareProvider')
->setConstructorArgs(
[
Expand Down Expand Up @@ -478,6 +490,7 @@ public function testUpdate($owner, $sharedBy) {
->setShareOwner($owner)
->setPermissions(19)
->setShareType(IShare::TYPE_REMOTE)
->setExpirationDate(new \DateTime('2019-02-01T01:02:03'))
->setNode($node);

$this->tokenHandler->method('generateToken')->willReturn('token');
Expand Down Expand Up @@ -512,17 +525,19 @@ public function testUpdate($owner, $sharedBy) {
$share = $this->provider->create($share);

$share->setPermissions(1);
$share->setExpirationDate($expirationDate);
$this->provider->update($share);

$share = $this->provider->getShareById($share->getId());

$this->assertEquals(1, $share->getPermissions());
$this->assertEquals($expirationDate, $share->getExpirationDate());
}

public function datatTestUpdate() {
public function dataTestUpdate() {
return [
['sharedBy', 'shareOwner'],
['shareOwner', 'shareOwner']
['sharedBy', 'shareOwner', new \DateTime('2020-03-01T01:02:03')],
['shareOwner', 'shareOwner', null],
];
}

Expand Down
7 changes: 7 additions & 0 deletions apps/files_sharing/tests/CapabilitiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,4 +285,11 @@ public function testFederatedSharingNoOutgoing() {
$this->assertArrayHasKey('federation', $result);
$this->assertFalse($result['federation']['outgoing']);
}

public function testFederatedSharingExpirationDate() {
$result = $this->getResults([]);
$this->assertArrayHasKey('federation', $result);
$this->assertEquals(['enabled' => true], $result['federation']['expire_date']);
$this->assertEquals(['enabled' => true], $result['federation']['expire_date_supported']);
}
}
192 changes: 191 additions & 1 deletion apps/files_sharing/tests/Controller/ShareAPIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,7 @@ public function dataGetShares() {
->setSharedBy('initiator')
->setShareOwner('currentUser')
->setPermissions(\OCP\Constants::PERMISSION_READ)
->setExpirationDate(new \DateTime('2000-01-01T01:02:03'))
->setNode($file1)
->setId(815);

Expand All @@ -1009,6 +1010,7 @@ public function dataGetShares() {
->setSharedBy('initiator')
->setShareOwner('currentUser')
->setPermissions(\OCP\Constants::PERMISSION_READ)
->setExpirationDate(new \DateTime('2000-01-02T01:02:03'))
->setNode($file1)
->setId(816);

Expand Down Expand Up @@ -2173,6 +2175,146 @@ public function testCreateShareInvalidExpireDate() {
$ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_LINK, null, 'false', '', null, 'a1b2d3');
}

public function testCreateShareRemote() {
$share = $this->newShare();
$this->shareManager->method('newShare')->willReturn($share);

/** @var \OCA\Files_Sharing\Controller\ShareAPIController $ocs */
$ocs = $this->getMockBuilder(ShareAPIController::class)
->setConstructorArgs([
$this->appName,
$this->request,
$this->shareManager,
$this->groupManager,
$this->userManager,
$this->rootFolder,
$this->urlGenerator,
$this->currentUser,
$this->l,
$this->config,
$this->appManager,
$this->serverContainer,
$this->userStatusManager,
$this->previewManager,
])->setMethods(['formatShare'])
->getMock();

$userFolder = $this->getMockBuilder(Folder::class)->getMock();
$this->rootFolder->expects($this->once())
->method('getUserFolder')
->with('currentUser')
->willReturn($userFolder);

$path = $this->getMockBuilder(File::class)->getMock();
$storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage')
->with('OCA\Files_Sharing\External\Storage')
->willReturn(false);
$path->method('getStorage')->willReturn($storage);
$userFolder->expects($this->once())
->method('get')
->with('valid-path')
->willReturn($path);

$this->userManager->method('userExists')->with('validUser')->willReturn(true);

$path->expects($this->once())
->method('lock')
->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);

$this->shareManager->method('createShare')
->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
return $share->getNode() === $path &&
$share->getPermissions() === (
\OCP\Constants::PERMISSION_ALL &
~\OCP\Constants::PERMISSION_DELETE &
~\OCP\Constants::PERMISSION_CREATE
) &&
$share->getShareType() === IShare::TYPE_REMOTE &&
$share->getSharedWith() === '[email protected]' &&
$share->getSharedBy() === 'currentUser';
}))
->willReturnArgument(0);

$this->shareManager->method('outgoingServer2ServerSharesAllowed')->willReturn(true);

$expected = new DataResponse([]);
$result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_REMOTE, '[email protected]');

$this->assertInstanceOf(get_class($expected), $result);
$this->assertEquals($expected->getData(), $result->getData());
}

public function testCreateShareRemoteGroup() {
$share = $this->newShare();
$this->shareManager->method('newShare')->willReturn($share);

/** @var \OCA\Files_Sharing\Controller\ShareAPIController $ocs */
$ocs = $this->getMockBuilder(ShareAPIController::class)
->setConstructorArgs([
$this->appName,
$this->request,
$this->shareManager,
$this->groupManager,
$this->userManager,
$this->rootFolder,
$this->urlGenerator,
$this->currentUser,
$this->l,
$this->config,
$this->appManager,
$this->serverContainer,
$this->userStatusManager,
$this->previewManager,
])->setMethods(['formatShare'])
->getMock();

$userFolder = $this->getMockBuilder(Folder::class)->getMock();
$this->rootFolder->expects($this->once())
->method('getUserFolder')
->with('currentUser')
->willReturn($userFolder);

$path = $this->getMockBuilder(File::class)->getMock();
$storage = $this->createMock(Storage::class);
$storage->method('instanceOfStorage')
->with('OCA\Files_Sharing\External\Storage')
->willReturn(false);
$path->method('getStorage')->willReturn($storage);
$userFolder->expects($this->once())
->method('get')
->with('valid-path')
->willReturn($path);

$this->userManager->method('userExists')->with('validUser')->willReturn(true);

$path->expects($this->once())
->method('lock')
->with(\OCP\Lock\ILockingProvider::LOCK_SHARED);

$this->shareManager->method('createShare')
->with($this->callback(function (\OCP\Share\IShare $share) use ($path) {
return $share->getNode() === $path &&
$share->getPermissions() === (
\OCP\Constants::PERMISSION_ALL &
~\OCP\Constants::PERMISSION_DELETE &
~\OCP\Constants::PERMISSION_CREATE
) &&
$share->getShareType() === IShare::TYPE_REMOTE_GROUP &&
$share->getSharedWith() === '[email protected]' &&
$share->getSharedBy() === 'currentUser';
}))
->willReturnArgument(0);

$this->shareManager->method('outgoingServer2ServerGroupSharesAllowed')->willReturn(true);

$expected = new DataResponse([]);
$result = $ocs->createShare('valid-path', \OCP\Constants::PERMISSION_ALL, IShare::TYPE_REMOTE_GROUP, '[email protected]');

$this->assertInstanceOf(get_class($expected), $result);
$this->assertEquals($expected->getData(), $result->getData());
}

public function testCreateShareRoom() {
$ocs = $this->mockFormatShare();

Expand Down Expand Up @@ -3841,6 +3983,7 @@ public function dataFormatShare() {
->setPermissions(\OCP\Constants::PERMISSION_READ)
->setNode($folder)
->setShareTime(new \DateTime('2000-01-01T00:01:02'))
->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
->setTarget('myTarget')
->setNote('personal note')
->setId(42);
Expand All @@ -3854,7 +3997,54 @@ public function dataFormatShare() {
'permissions' => 1,
'stime' => 946684862,
'parent' => null,
'expiration' => null,
'expiration' => '2001-02-03 00:00:00',
'token' => null,
'uid_file_owner' => 'owner',
'displayname_file_owner' => 'owner',
'note' => 'personal note',
'label' => null,
'path' => 'folder',
'item_type' => 'folder',
'storage_id' => 'storageId',
'storage' => 100,
'item_source' => 2,
'file_source' => 2,
'file_parent' => 1,
'file_target' => 'myTarget',
'share_with' => '[email protected]',
'share_with_displayname' => 'foobar',
'mail_send' => 0,
'mimetype' => 'myFolderMimeType',
'has_preview' => false,
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
], $share, [], false
];

$share = \OC::$server->getShareManager()->newShare();
$share->setShareType(IShare::TYPE_REMOTE_GROUP)
->setSharedBy('initiator')
->setSharedWith('[email protected]')
->setShareOwner('owner')
->setPermissions(\OCP\Constants::PERMISSION_READ)
->setNode($folder)
->setShareTime(new \DateTime('2000-01-01T00:01:02'))
->setExpirationDate(new \DateTime('2001-02-03T04:05:06'))
->setTarget('myTarget')
->setNote('personal note')
->setId(42);

$result[] = [
[
'id' => 42,
'share_type' => IShare::TYPE_REMOTE_GROUP,
'uid_owner' => 'initiator',
'displayname_owner' => 'initiator',
'permissions' => 1,
'stime' => 946684862,
'parent' => null,
'expiration' => '2001-02-03 00:00:00',
'token' => null,
'uid_file_owner' => 'owner',
'displayname_file_owner' => 'owner',
Expand Down