Skip to content

Commit

Permalink
added tests for files_external occ command
Browse files Browse the repository at this point in the history
  • Loading branch information
sakshamgurung authored and kiranparajuli589 committed Dec 17, 2021
1 parent ddbe8cd commit ce6fe0c
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 14 deletions.
1 change: 1 addition & 0 deletions tests/acceptance/config/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,7 @@ default:
- FederationContext:
- OccContext:
- WebDavPropertiesContext:
- PublicWebDavContext:

cliLocalStorage:
paths:
Expand Down
209 changes: 195 additions & 14 deletions tests/acceptance/features/bootstrap/OccContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -201,23 +201,19 @@ public function setExtStorageReadOnlyUsingTheOccCommand(string $mountPoint, bool

/**
* @param string $mountPoint
* @param boolean $setting
* @param boolean $enable
*
* @return void
* @throws Exception
*/
public function setExtStorageSharingUsingTheOccCommand(string $mountPoint, bool $setting = true):void {
public function setExtStorageSharingUsingTheOccCommand(string $mountPoint, bool $enable = true):void {
$command = "files_external:option";

$mountId = $this->featureContext->getStorageId($mountPoint);

$key = "enable_sharing";

if ($setting) {
$value = "true";
} else {
$value = "false";
}
$value = $enable ? "true" : "false";

$this->invokingTheCommand(
"$command $mountId $key $value"
Expand Down Expand Up @@ -1231,6 +1227,19 @@ public function theAdminHasSetTheExtStorageToSharing(string $mountPoint):void {
$this->theCommandShouldHaveBeenSuccessful();
}

/**
* @When /^the administrator (enables|disables) sharing for the external storage "([^"]*)" using the occ command$/
*
* @param string $action
* @param string $mountPoint
*
* @return void
* @throws Exception
*/
public function theAdminDisablesSharingForTheExtStorage(string $action, string $mountPoint):void {
$this->setExtStorageSharingUsingTheOccCommand($mountPoint, $action === "enables");
}

/**
* @When the administrator sets the external storage :mountPoint to be never scanned automatically using the occ command
*
Expand Down Expand Up @@ -1460,6 +1469,34 @@ public function addRemoveUserOrGroupToOrFromMount(
);
}

/**
* @param string $mount
* @param string $userOrGroup
*
* @return array
* @throws Exception
*/
public function getListOfApplicableUserOrGroupForMount(string $mount, string $userOrGroup):array {
$validArgs = ["users", "groups"];
if (!\in_array($userOrGroup, $validArgs)) {
throw new Exception(
"Invalid key provided. Expected:" .
\implode(", ", $validArgs) .
"Found: " . $userOrGroup
);
}
$mountId = $this->getMountIdForLocalStorage($mount);
$this->featureContext->runOcc(
[
'files_external:applicable',
$mountId,
'--output=json'
]
);
$commandOutput = \json_decode($this->featureContext->getStdOutOfOccCommand());
return ($userOrGroup === "users") ? $commandOutput->users : $commandOutput->groups;
}

/**
* @param string $action
* @param string $userOrGroup
Expand Down Expand Up @@ -1559,6 +1596,62 @@ public function theAdminAddsRemovesAsTheApplicableUserForMountUsingTheOccCommand
);
}

/**
* @Then /^the following (users|groups) should be listed as applicable for local storage mount "([^"]*)"$/
*
* @param string $usersOrGroups comma separated lists eg: Alice, Brian
* @param string $localStorage
* @param TableNode $applicable
*
* @return void
* @throws Exception
*/
public function theFollowingUsersOrGroupsShouldBeListedAsApplicable(string $usersOrGroups, string $localStorage, TableNode $applicable): void {
$this->featureContext->verifyTableNodeRows(
$applicable,
[],
["users", "groups"]
);
$expectedApplicableList = $applicable->getRowsHash();
$actualApplicableList = $this->getListOfApplicableUserOrGroupForMount($localStorage, $usersOrGroups);
foreach ($expectedApplicableList as $expectedApplicable) {
Assert::assertContains(
$expectedApplicable,
$actualApplicableList,
__METHOD__
. $usersOrGroups
. " not found!\nexpected: "
. $expectedApplicable
. " to be in the list ["
. \implode(", ", $actualApplicableList)
. "]."
);
}
}

/**
* @Then /^the applicable (users|groups) list should be empty for local storage mount "([^"]*)"$/
*
* @param string $usersOrGroups
* @param string $localStorage
*
* @return void
* @throws Exception
*/
public function theApplicableUsersOrGroupsListShouldBeEmptyForLocalStorageMount(string $usersOrGroups, string $localStorage): void {
$actualApplicableList = $this->getListOfApplicableUserOrGroupForMount($localStorage, $usersOrGroups);
Assert::assertEquals(
0,
\count($actualApplicableList),
__METHOD__
. "Expected empty list for applicable "
. $usersOrGroups
. " but found: ["
. \implode(", ", $actualApplicableList)
. "]."
);
}

/**
* @When the administrator removes all from the applicable users and groups for local storage mount :localStorage using the occ command
*
Expand Down Expand Up @@ -1603,6 +1696,35 @@ public function theAdminHasAddedRemovedTheApplicableUserForMountUsingTheOccComma
$mount
);
$this->theCommandShouldHaveBeenSuccessful();
// making plural "users" or "groups"
$userOrGroup = $userOrGroup . "s";
$actualApplicableList = $this->getListOfApplicableUserOrGroupForMount($mount, $userOrGroup);

if ($action === "added") {
Assert::assertContains(
$user,
$actualApplicableList,
__METHOD__
. " The expected applicable "
. $userOrGroup
. " is not present in the actual list of applicable "
. $userOrGroup
. " for mount point: "
. $mount . ".\n"
);
} else {
Assert::assertNotContains(
$user,
$actualApplicableList,
__METHOD__
. " The applicable "
. $userOrGroup
. " is present in the actual list of applicable "
. $userOrGroup
. " for mount point: "
. $mount . ".\n"
);
}
}

/**
Expand Down Expand Up @@ -2261,13 +2383,13 @@ public function administratorHasDeletedLocalStorageFolderUsingTheOccCommand(stri

/**
* @param string $folder
* @param bool $mustExist
*
* @return integer|bool
* @return integer|null
* @throws Exception
*/
public function deleteLocalStorageFolderUsingTheOccCommand(string $folder, bool $mustExist = true) {
public function getMountIdForLocalStorage(string $folder): ?int {
$createdLocalStorage = [];
$mount_id = null;
$this->listLocalStorageMount();
$commandOutput = \json_decode($this->featureContext->getStdOutOfOccCommand());
foreach ($commandOutput as $i) {
Expand All @@ -2278,14 +2400,28 @@ public function deleteLocalStorageFolderUsingTheOccCommand(string $folder, bool
$mount_id = $key;
}
}

return (int) $mount_id;
}

/**
* @param string $folder
* @param bool $mustExist
*
* @return integer|bool
* @throws Exception
*/
public function deleteLocalStorageFolderUsingTheOccCommand(string $folder, bool $mustExist = true) {
$mount_id = $this->getMountIdForLocalStorage($folder);

if (!isset($mount_id)) {
if ($mustExist) {
throw new Exception("Id not found for folder to be deleted");
}
return false;
}
$this->invokingTheCommand('files_external:delete --yes ' . $mount_id);
return (int) $mount_id;
return $mount_id;
}

/**
Expand Down Expand Up @@ -2313,7 +2449,7 @@ public function theAdministratorImportsTheMountFromFileUsingTheOccCommand(string
}

/**
* @When the administrator has exported the local storage mounts using the occ command
* @Given the administrator has exported the local storage mounts using the occ command
*
* @return void
* @throws Exception
Expand All @@ -2323,6 +2459,28 @@ public function theAdministratorHasExportedTheMountsUsingTheOccCommand():void {
$this->theCommandShouldHaveBeenSuccessful();
}

/**
* @Then the command should output configuration for local storage mount :mount
*
* @param string $mount
*
* @return void
* @throws Exception
*/
public function theOutputShouldContainConfigurationForMount(string $mount):void {
$actualConfig = null;

$commandOutput = \json_decode($this->featureContext->getStdOutOfOccCommand());
foreach ($commandOutput as $i) {
if ($mount === \ltrim($i->mount_point, '/')) {
$actualConfig = $i;
break;
}
}

Assert::assertNotNull($actualConfig, 'Configuration for local storage mount ' . $mount . ' not found.');
}

/**
* @When the administrator verifies the mount configuration for local storage :localStorage using the occ command
*
Expand Down Expand Up @@ -3224,6 +3382,30 @@ public function adminHasCreatedAnExternalMountPointWithFollowingConfigUsingTheOc
$this->theCommandShouldHaveBeenSuccessful();
}

/**
* @param string $mountPoint
*
* @return void
* @throws Exception
*/
private function deleteExternalMountPointUsingTheAdmin(string $mountPoint):void {
$mount_id = $this->administratorDeletesFolder($mountPoint);
$this->featureContext->popStorageId($mount_id);
}

/**
* @Given the administrator has deleted external storage with mount point :mountPoint
*
* @param string $mountPoint
*
* @return void
* @throws Exception
*/
public function adminHasDeletedExternalMountPoint(string $mountPoint):void {
$this->deleteExternalMountPointUsingTheAdmin($mountPoint);
$this->theCommandShouldHaveBeenSuccessful();
}

/**
* @When the administrator deletes external storage with mount point :mountPoint
*
Expand All @@ -3233,8 +3415,7 @@ public function adminHasCreatedAnExternalMountPointWithFollowingConfigUsingTheOc
* @throws Exception
*/
public function adminDeletesExternalMountPoint(string $mountPoint):void {
$mount_id = $this->administratorDeletesFolder($mountPoint);
$this->featureContext->popStorageId($mount_id);
$this->deleteExternalMountPointUsingTheAdmin($mountPoint);
}

/**
Expand Down
37 changes: 37 additions & 0 deletions tests/acceptance/features/bootstrap/WebDav.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Exception\BadResponseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Ring\Exception\ConnectException;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;
Expand Down Expand Up @@ -2993,6 +2994,42 @@ public function userHasUploadedAFileWithContentTo(
return $fileId;
}

/**
* @Given the administrator has created a json file with exported config of local storage mount :mount to :destination in temporary storage
*
* @param string $mount
* @param string $destination
*
* @return void
* @throws Exception
* @throws GuzzleException
*/
public function theAdminHasCreatedAJsonFileWithExportedMountConfig(
string $mount,
string $destination
): void {
$actualConfig = null;
$commandOutput = \json_decode(
SetupHelper::runOcc(
['files_external:export'],
$this->getStepLineRef()
)['stdOut']
);

//identifying the correct config and also removing the "mount id" property
foreach ($commandOutput as $i) {
if ($mount === \ltrim($i->mount_point, '/')) {
unset($i->mount_id);
$actualConfig = $i;
break;
}
}

$actualConfig = json_encode($actualConfig);
$this->copyContentToFileInTemporaryStorageOnSystemUnderTest($destination, $actualConfig);
$this->theFileWithContentShouldExistInTheServerRoot(TEMPORARY_STORAGE_DIR_ON_REMOTE_SERVER . "/$destination", $actualConfig);
}

/**
* @Given /^user "([^"]*)" has uploaded the following files with content "([^"]*)"$/
*
Expand Down
Loading

0 comments on commit ce6fe0c

Please sign in to comment.