Skip to content

Commit 03889c8

Browse files
committed
test(Sharing): Integration test for no expiration set date for share
- Verify that explicitly sending empty `expireDate` param to server overwrite default and sets not expiry date, if non is enforced. - Update tests to avoid converting empty string to date. Signed-off-by: fenn-cs <fenn25.fn@gmail.com> [skip ci]
1 parent 3f6975a commit 03889c8

File tree

5 files changed

+52
-29
lines changed

5 files changed

+52
-29
lines changed

apps/files_sharing/lib/Controller/ShareAPIController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ protected function formatShare(IShare $share, Node $recipientNode = null): array
233233
$result['share_with'] = $share->getSharedWith();
234234
$result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith();
235235
$result['share_with_displayname_unique'] = $sharedWith !== null ? (
236-
!empty($sharedWith->getSystemEMailAddress()) ? $sharedWith->getSystemEMailAddress() : $sharedWith->getUID()
236+
!empty($sharedWith->getSystemEMailAddress()) ? $sharedWith->getSystemEMailAddress() : $sharedWith->getUID()
237237
) : $share->getSharedWith();
238238
$result['status'] = [];
239239

@@ -332,7 +332,7 @@ protected function formatShare(IShare $share, Node $recipientNode = null): array
332332

333333
$result['attributes'] = null;
334334
if ($attributes = $share->getAttributes()) {
335-
$result['attributes'] = \json_encode($attributes->toArray());
335+
$result['attributes'] = \json_encode($attributes->toArray());
336336
}
337337

338338
return $result;
@@ -1275,8 +1275,8 @@ public function updateShare(
12751275
}
12761276

12771277
if (!$this->hasPermission($newPermissions, Constants::PERMISSION_READ) && (
1278-
$this->hasPermission($newPermissions, Constants::PERMISSION_UPDATE) || $this->hasPermission($newPermissions, Constants::PERMISSION_DELETE)
1279-
)) {
1278+
$this->hasPermission($newPermissions, Constants::PERMISSION_UPDATE) || $this->hasPermission($newPermissions, Constants::PERMISSION_DELETE)
1279+
)) {
12801280
throw new OCSBadRequestException($this->l->t('Share must have READ permission if UPDATE or DELETE permission is set'));
12811281
}
12821282
}

build/integration/features/bootstrap/Sharing.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public function asCreatingAShareWith($user, $body) {
8181
$fd = $body->getRowsHash();
8282
if (array_key_exists('expireDate', $fd)) {
8383
$dateModification = $fd['expireDate'];
84-
$fd['expireDate'] = date('Y-m-d', strtotime($dateModification));
84+
if (!empty($dateModification)) {
85+
$fd['expireDate'] = date('Y-m-d', strtotime($dateModification));
86+
}
8587
}
8688
$options['form_params'] = $fd;
8789
}
@@ -270,13 +272,13 @@ public function updatingLastShare($body) {
270272
}
271273

272274
public function createShare($user,
273-
$path = null,
274-
$shareType = null,
275-
$shareWith = null,
276-
$publicUpload = null,
277-
$password = null,
278-
$permissions = null,
279-
$viewOnly = false) {
275+
$path = null,
276+
$shareType = null,
277+
$shareWith = null,
278+
$publicUpload = null,
279+
$password = null,
280+
$permissions = null,
281+
$viewOnly = false) {
280282
$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/files_sharing/api/v{$this->sharingApiVersion}/shares";
281283
$client = new Client();
282284
$options = [
@@ -328,7 +330,9 @@ public function createShare($user,
328330
public function isFieldInResponse($field, $contentExpected) {
329331
$data = simplexml_load_string($this->response->getBody())->data[0];
330332
if ((string)$field == 'expiration') {
331-
$contentExpected = date('Y-m-d', strtotime($contentExpected)) . " 00:00:00";
333+
if (!empty($contentExpected)) {
334+
$contentExpected = date('Y-m-d', strtotime($contentExpected)) . " 00:00:00";
335+
}
332336
}
333337
if (count($data->element) > 0) {
334338
foreach ($data as $element) {

build/integration/sharing_features/sharing-v1.feature

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,24 @@ Feature: sharing
229229
| url | AN_URL |
230230
| mimetype | httpd/unix-directory |
231231

232+
Scenario: Creating a new share with expiration date removed, when default expiration is set
233+
Given user "user0" exists
234+
And user "user1" exists
235+
And parameter "shareapi_default_expire_date" of app "core" is set to "yes"
236+
And As an "user0"
237+
When creating a share with
238+
| path | welcome.txt |
239+
| shareWith | user1 |
240+
| shareType | 0 |
241+
| expireDate | |
242+
Then the OCS status code should be "100"
243+
And the HTTP status code should be "200"
244+
And Getting info of last share
245+
Then the OCS status code should be "100"
246+
And the HTTP status code should be "200"
247+
And Share fields of last share match with
248+
| expiration ||
249+
232250
Scenario: Creating a new public share, updating its password and getting its info
233251
Given user "user0" exists
234252
And As an "user0"

lib/private/Share20/Manager.php

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ protected function generalCreateChecks(IShare $share, bool $isUpdate = false) {
298298

299299
$isFederatedShare = $share->getNode()->getStorage()->instanceOfStorage('\OCA\Files_Sharing\External\Storage');
300300
$permissions = 0;
301-
301+
302302
$isReshare = $share->getNode()->getOwner() && $share->getNode()->getOwner()->getUID() !== $share->getSharedBy();
303303
if (!$isReshare && $isUpdate) {
304304
// in case of update on owner-less filesystem, we use share owner to improve reshare detection
@@ -406,7 +406,7 @@ protected function validateExpirationDateInternal(IShare $share) {
406406

407407
// If $expirationDate is falsy, noExpirationDate is true and expiration not enforced
408408
// Then skip expiration date validation as null is accepted
409-
if(!($share->getNoExpirationDate() && !$isEnforced)) {
409+
if (!($share->getNoExpirationDate() && !$isEnforced)) {
410410
if ($expirationDate != null) {
411411
$expirationDate->setTimezone($this->dateTimeZone->getTimeZone());
412412
$expirationDate->setTime(0, 0, 0);
@@ -486,11 +486,11 @@ protected function validateExpirationDateLink(IShare $share) {
486486

487487
// If $expirationDate is falsy, noExpirationDate is true and expiration not enforced
488488
// Then skip expiration date validation as null is accepted
489-
if(!($share->getNoExpirationDate() && !$isEnforced)) {
489+
if (!($share->getNoExpirationDate() && !$isEnforced)) {
490490
if ($expirationDate !== null) {
491491
$expirationDate->setTimezone($this->dateTimeZone->getTimeZone());
492492
$expirationDate->setTime(0, 0, 0);
493-
493+
494494
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
495495
$date->setTime(0, 0, 0);
496496
if ($date >= $expirationDate) {
@@ -506,24 +506,24 @@ protected function validateExpirationDateLink(IShare $share) {
506506
} catch (\UnexpectedValueException $e) {
507507
// This is a new share
508508
}
509-
509+
510510
if ($fullId === null && $expirationDate === null && $this->shareApiLinkDefaultExpireDate()) {
511511
$expirationDate = new \DateTime('now', $this->dateTimeZone->getTimeZone());
512512
$expirationDate->setTime(0, 0, 0);
513-
513+
514514
$days = (int)$this->config->getAppValue('core', 'link_defaultExpDays', (string)$this->shareApiLinkDefaultExpireDays());
515515
if ($days > $this->shareApiLinkDefaultExpireDays()) {
516516
$days = $this->shareApiLinkDefaultExpireDays();
517517
}
518518
$expirationDate->add(new \DateInterval('P' . $days . 'D'));
519519
}
520-
520+
521521
// If we enforce the expiration date check that is does not exceed
522522
if ($isEnforced) {
523523
if (empty($expirationDate)) {
524524
throw new \InvalidArgumentException('Expiration date is enforced');
525525
}
526-
526+
527527
$date = new \DateTime('now', $this->dateTimeZone->getTimeZone());
528528
$date->setTime(0, 0, 0);
529529
$date->add(new \DateInterval('P' . $this->shareApiLinkDefaultExpireDays() . 'D'));
@@ -532,7 +532,6 @@ protected function validateExpirationDateLink(IShare $share) {
532532
throw new GenericShareException($message, $message, 404);
533533
}
534534
}
535-
536535
}
537536

538537
$accepted = true;
@@ -907,12 +906,12 @@ public function createShare(IShare $share) {
907906
* @param \DateTime|null $expiration
908907
*/
909908
protected function sendMailNotification(IL10N $l,
910-
$filename,
911-
$link,
912-
$initiator,
913-
$shareWith,
914-
\DateTime $expiration = null,
915-
$note = '') {
909+
$filename,
910+
$link,
911+
$initiator,
912+
$shareWith,
913+
\DateTime $expiration = null,
914+
$note = '') {
916915
$initiatorUser = $this->userManager->get($initiator);
917916
$initiatorDisplayName = ($initiatorUser instanceof IUser) ? $initiatorUser->getDisplayName() : $initiator;
918917

lib/private/Share20/Share.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@
2929
*/
3030
namespace OC\Share20;
3131

32-
use OCP\Files\File;
3332
use OCP\Files\Cache\ICacheEntry;
33+
use OCP\Files\File;
3434
use OCP\Files\FileInfo;
3535
use OCP\Files\IRootFolder;
3636
use OCP\Files\Node;
@@ -102,6 +102,8 @@ class Share implements IShare {
102102
/** @var bool */
103103
private $hideDownload = false;
104104

105+
private bool $noExpirationDate = false;
106+
105107
public function __construct(IRootFolder $rootFolder, IUserManager $userManager) {
106108
$this->rootFolder = $rootFolder;
107109
$this->userManager = $userManager;

0 commit comments

Comments
 (0)