Skip to content

Commit

Permalink
Fix share attribute related tests + code style
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Petry <vincent@nextcloud.com>
  • Loading branch information
PVince81 committed May 31, 2022
1 parent e0414dc commit 29c2cf3
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 66 deletions.
1 change: 1 addition & 0 deletions apps/dav/lib/DAV/ViewOnlyPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class ViewOnlyPlugin extends ServerPlugin {
*/
public function __construct(ILogger $logger) {
$this->logger = $logger;
$this->server = null;
}

/**
Expand Down
43 changes: 25 additions & 18 deletions apps/files_sharing/lib/Controller/ShareAPIController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
namespace OCA\Files_Sharing\Controller;

use OC\Files\FileInfo;
use OCA\DAV\DAV\ViewOnlyPlugin;
use OCA\Files_Sharing\Exceptions\SharingRightsException;
use OCA\Files_Sharing\External\Storage;
use OCA\Files\Helper;
Expand Down Expand Up @@ -442,6 +441,7 @@ public function deleteShare(string $id): DataResponse {
* @param string $sendPasswordByTalk
* @param string $expireDate
* @param string $label
* @param string $attributes
*
* @return DataResponse
* @throws NotFoundException
Expand All @@ -462,7 +462,8 @@ public function createShare(
string $sendPasswordByTalk = null,
string $expireDate = '',
string $note = '',
string $label = ''
string $label = '',
string $attributes = null
): DataResponse {
$share = $this->shareManager->newShare();

Expand Down Expand Up @@ -680,7 +681,7 @@ public function createShare(
$share->setNote($note);
}

$share = $this->setShareAttributes($share, $this->request->getParam('attributes', null));
$share = $this->setShareAttributes($share, $attributes);

try {
$share = $this->shareManager->createShare($share);
Expand Down Expand Up @@ -1043,6 +1044,7 @@ private function hasPermission(int $permissionsSet, int $permissionsToCheck): bo
* @param string $note
* @param string $label
* @param string $hideDownload
* @param string $attributes
* @return DataResponse
* @throws LockedException
* @throws NotFoundException
Expand All @@ -1059,7 +1061,8 @@ public function updateShare(
string $expireDate = null,
string $note = null,
string $label = null,
string $hideDownload = null
string $hideDownload = null,
string $attributes = null
): DataResponse {
try {
$share = $this->getShareById($id);
Expand All @@ -1077,8 +1080,6 @@ public function updateShare(
throw new OCSForbiddenException('You are not allowed to edit incoming shares');
}

$shareAttributes = $this->request->getParam('attributes', null);

if (
$permissions === null &&
$password === null &&
Expand All @@ -1088,7 +1089,7 @@ public function updateShare(
$note === null &&
$label === null &&
$hideDownload === null &&
$shareAttributes === null
$attributes === null
) {
throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given'));
}
Expand Down Expand Up @@ -1227,7 +1228,7 @@ public function updateShare(
}
}

$share = $this->setShareAttributes($share, $shareAttributes);
$share = $this->setShareAttributes($share, $attributes);

try {
$share = $this->shareManager->updateShare($share);
Expand Down Expand Up @@ -1848,18 +1849,24 @@ private function mergeFormattedShares(array &$shares, array $newShares) {

/**
* @param IShare $share
* @param string[][]|null $formattedShareAttributes
* @param string|null $attributesString
* @return IShare modified share
*/
private function setShareAttributes(IShare $share, $formattedShareAttributes) {
$newShareAttributes = $this->shareManager->newShare()->newAttributes();
if ($formattedShareAttributes !== null) {
foreach ($formattedShareAttributes as $formattedAttr) {
$newShareAttributes->setAttribute(
$formattedAttr['scope'],
$formattedAttr['key'],
(bool) \json_decode($formattedAttr['enabled'])
);
private function setShareAttributes(IShare $share, ?string $attributesString) {
$newShareAttributes = null;
if ($attributesString !== null) {
$newShareAttributes = $this->shareManager->newShare()->newAttributes();
$formattedShareAttributes = \json_decode($attributesString, true);
if (is_array($formattedShareAttributes)) {
foreach ($formattedShareAttributes as $formattedAttr) {
$newShareAttributes->setAttribute(
$formattedAttr['scope'],
$formattedAttr['key'],
is_string($formattedAttr['enabled']) ? (bool) \json_decode($formattedAttr['enabled']) : $formattedAttr['enabled']
);
}
} else {
throw new OCSBadRequestException('Invalid share attributes provided: \"' . $attributesString . '\"');
}
}
$share->setAttributes($newShareAttributes);
Expand Down
11 changes: 9 additions & 2 deletions apps/files_sharing/tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -950,9 +950,13 @@ public function testUpdateShare() {
->setShareType(IShare::TYPE_USER)
->setPermissions(19)
->setAttributes($this->shareManager->newShare()->newAttributes());

$this->assertNotNull($share1->getAttributes());
$share1 = $this->shareManager->createShare($share1);
$this->assertNull($share1->getAttributes());
$this->assertEquals(19, $share1->getPermissions());
$this->assertEquals(null, $share1->getAttributes());
// attributes get cleared when empty
$this->assertNull($share1->getAttributes());

$share2 = $this->shareManager->newShare();
$share2->setNode($node1)
Expand All @@ -964,7 +968,10 @@ public function testUpdateShare() {

// update permissions
$ocs = $this->createOCS(self::TEST_FILES_SHARING_API_USER1);
$ocs->updateShare($share1->getId(), 1);
$ocs->updateShare(
$share1->getId(), 1, null, null, null, null, null, null, null,
'[{"scope": "app1", "key": "attr1", "enabled": true}]'
);
$ocs->cleanup();

$share1 = $this->shareManager->getShareById('ocinternal:' . $share1->getId());
Expand Down
19 changes: 9 additions & 10 deletions apps/files_sharing/tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ protected function setUp(): void {

$this->application = new Application([]);

// FIXME: how to mock this one ??
$symfonyDispatcher = $this->createMock(SymfonyDispatcher::class);
$symfonyDispatcher = new SymfonyDispatcher();
$this->eventDispatcher = new EventDispatcher(
$symfonyDispatcher,
$this->createMock(IServerContainer::class),
Expand Down Expand Up @@ -133,9 +132,9 @@ public function providesDataForCanGet() {
*/
public function testCheckDirectCanBeDownloaded($path, $userFolder, $run) {
$user = $this->createMock(IUser::class);
$user->method("getUID")->willReturn("test");
$this->userSession->method("getUser")->willReturn($user);
$this->userSession->method("isLoggedIn")->willReturn(true);
$user->method('getUID')->willReturn('test');
$this->userSession->method('getUser')->willReturn($user);
$this->userSession->method('isLoggedIn')->willReturn(true);
$this->rootFolder->method('getUserFolder')->willReturn($userFolder);

// Simulate direct download of file
Expand Down Expand Up @@ -210,11 +209,11 @@ public function providesDataForCanZip() {
*/
public function testCheckZipCanBeDownloaded($dir, $files, $userFolder, $run) {
$user = $this->createMock(IUser::class);
$user->method("getUID")->willReturn("test");
$this->userSession->method("getUser")->willReturn($user);
$this->userSession->method("isLoggedIn")->willReturn(true);
$user->method('getUID')->willReturn('test');
$this->userSession->method('getUser')->willReturn($user);
$this->userSession->method('isLoggedIn')->willReturn(true);

$this->rootFolder->method('getUserFolder')->with("test")->willReturn($userFolder);
$this->rootFolder->method('getUserFolder')->with('test')->willReturn($userFolder);

// Simulate zip download of folder folder
$event = new GenericEvent(null, ['dir' => $dir, 'files' => $files, 'run' => true]);
Expand All @@ -225,7 +224,7 @@ public function testCheckZipCanBeDownloaded($dir, $files, $userFolder, $run) {
}

public function testCheckFileUserNotFound() {
$this->userSession->method("isLoggedIn")->willReturn(false);
$this->userSession->method('isLoggedIn')->willReturn(false);

// Simulate zip download of folder folder
$event = new GenericEvent(null, ['dir' => '/test', 'files' => ['test.txt'], 'run' => true]);
Expand Down
34 changes: 25 additions & 9 deletions apps/files_sharing/tests/Controller/ShareAPIControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
use OCP\IUserManager;
use OCP\Lock\LockedException;
use OCP\Share\Exceptions\GenericShareException;
use OCP\Share\IAttributes as IShareAttributes;
use OCP\Share\IManager;
use OCP\Share\IShare;
use Test\TestCase;
Expand Down Expand Up @@ -125,10 +126,6 @@ protected function setUp(): void {
$this->shareManager
->expects($this->any())
->method('shareProviderExists')->willReturn(true);
$this->shareManager
->expects($this->any())
->method('newShare')
->willReturn($this->newShare());
$this->groupManager = $this->createMock(IGroupManager::class);
$this->userManager = $this->createMock(IUserManager::class);
$this->request = $this->createMock(IRequest::class);
Expand Down Expand Up @@ -201,11 +198,9 @@ private function newShare() {
private function mockShareAttributes() {
$formattedShareAttributes = [
[
[
'scope' => 'permissions',
'key' => 'download',
'enabled' => true
]
'scope' => 'permissions',
'key' => 'download',
'enabled' => true
]
];

Expand Down Expand Up @@ -641,6 +636,7 @@ public function dataGetShare() {
'can_edit' => false,
'can_delete' => false,
'status' => [],
'attributes' => null,
];
$data[] = [$share, $expected];

Expand Down Expand Up @@ -692,6 +688,7 @@ public function dataGetShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
];
$data[] = [$share, $expected];

Expand Down Expand Up @@ -749,6 +746,7 @@ public function dataGetShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
];
$data[] = [$share, $expected];

Expand Down Expand Up @@ -3808,6 +3806,7 @@ public function dataFormatShare() {
'can_edit' => false,
'can_delete' => false,
'status' => [],
'attributes' => '[{"scope":"permissions","key":"download","enabled":true}]',
], $share, [], false
];
// User backend up
Expand Down Expand Up @@ -3845,6 +3844,7 @@ public function dataFormatShare() {
'can_edit' => false,
'can_delete' => false,
'status' => [],
'attributes' => '[{"scope":"permissions","key":"download","enabled":true}]',
], $share, [
['owner', $owner],
['initiator', $initiator],
Expand Down Expand Up @@ -3898,6 +3898,7 @@ public function dataFormatShare() {
'can_edit' => false,
'can_delete' => false,
'status' => [],
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -3947,6 +3948,7 @@ public function dataFormatShare() {
'can_edit' => true,
'can_delete' => true,
'status' => [],
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -3996,6 +3998,7 @@ public function dataFormatShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4042,6 +4045,7 @@ public function dataFormatShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4095,6 +4099,7 @@ public function dataFormatShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4148,6 +4153,7 @@ public function dataFormatShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4195,6 +4201,7 @@ public function dataFormatShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4242,6 +4249,7 @@ public function dataFormatShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4292,6 +4300,7 @@ public function dataFormatShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4339,6 +4348,7 @@ public function dataFormatShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4386,6 +4396,7 @@ public function dataFormatShare() {
'hide_download' => 0,
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4450,6 +4461,7 @@ public function dataFormatShare() {
'can_edit' => false,
'can_delete' => false,
'password_expiration_time' => null,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4500,6 +4512,7 @@ public function dataFormatShare() {
'can_edit' => false,
'can_delete' => false,
'password_expiration_time' => null,
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4549,6 +4562,7 @@ public function dataFormatShare() {
'can_edit' => true,
'can_delete' => true,
'status' => [],
'attributes' => null,
], $share, [], false
];

Expand Down Expand Up @@ -4700,6 +4714,7 @@ public function dataFormatRoomShare() {
'label' => '',
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, false, []
];

Expand Down Expand Up @@ -4746,6 +4761,7 @@ public function dataFormatRoomShare() {
'label' => '',
'can_edit' => false,
'can_delete' => false,
'attributes' => null,
], $share, true, [
'share_with_displayname' => 'recipientRoomName'
]
Expand Down
Loading

0 comments on commit 29c2cf3

Please sign in to comment.