Skip to content

Commit

Permalink
add test for downloading space
Browse files Browse the repository at this point in the history
  • Loading branch information
Salipa-Gurung committed Jul 19, 2023
1 parent cbc6e7e commit 3b31e9a
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 10 deletions.
1 change: 1 addition & 0 deletions tests/acceptance/config/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ default:
- SpacesTUSContext:
- GraphContext:
- OcisConfigContext:
- ArchiverContext:

apiSpacesShares:
paths:
Expand Down
22 changes: 22 additions & 0 deletions tests/acceptance/features/apiSpaces/spaceDownload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
@api
Feature: Download space
As a user
I want to be able to download space
So that I can store it in my local storage


Scenario: user downloads a space
Given user "Alice" has been created with default attributes and without skeleton files
And using spaces DAV path
And the administrator has assigned the role "Space Admin" to user "Alice" using the Graph API
And user "Alice" has created a space "project-space" with the default quota using the GraphApi
And user "Alice" has uploaded a file inside space "project-space" with content "some data" to "file1.txt"
And user "Alice" has uploaded a file inside space "project-space" with content "other data" to "file2.txt"
And user "Alice" has uploaded a file inside space "project-space" with content "more data" to "file3.txt"
When user "Alice" downloads the space "project-space" using the WebDAV API
Then the HTTP status code should be "200"
And the downloaded "tar" archive should contain these files:
| name | content |
| file1.txt | some data |
| file2.txt | other data |
| file3.txt | more data |
42 changes: 32 additions & 10 deletions tests/acceptance/features/bootstrap/ArchiverContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
use GuzzleHttp\Exception\GuzzleException;
use TestHelpers\HttpRequestHelper;
use TestHelpers\SetupHelper;
use wapmorgan\UnifiedArchive\UnifiedArchive;
use PHPUnit\Framework\Assert;
use \Psr\Http\Message\ResponseInterface;

Expand Down Expand Up @@ -219,18 +218,41 @@ public function userDownloadsTheArchiveOfTheseItems(
*/
public function theDownloadedArchiveShouldContainTheseFiles(string $type, TableNode $expectedFiles):void {
$this->featureContext->verifyTableNodeColumns($expectedFiles, ['name', 'content']);
$contents = $this->featureContext->getResponse()->getBody()->getContents();
$tempFile = \tempnam(\sys_get_temp_dir(), 'OcAcceptanceTests_');
\unlink($tempFile); // we only need the name
$tempFile = $tempFile . '.' . $type; // it needs the extension
\file_put_contents($tempFile, $this->featureContext->getResponse()->getBody()->getContents());
$archive = UnifiedArchive::open($tempFile);
foreach ($expectedFiles->getHash() as $expectedFile) {
Assert::assertEquals(
$expectedFile['content'],
$archive->getFileContent($expectedFile['name']),
__METHOD__ .
" content of '" . $expectedFile['name'] . "' not as expected"
);
\file_put_contents($tempFile, $contents);

// open the archive
$archiveData = new RecursiveIteratorIterator(
new PharData($tempFile),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($expectedFiles->getHash() as $expectedItem) {
$expectedPath = trim($expectedItem['name'], "/");
$found = false;
foreach ($archiveData as $info) {
// get only the parent folder path for the given item
$actualPath = explode(".$type", $info->getPathname())[1];
$actualPath = trim($actualPath, "/");

if ($expectedPath === $actualPath) {
if (!$info->isDir()) {
Assert::assertEquals(
$expectedItem['content'],
$info->getContent(),
__METHOD__ .
" content of '" . $expectedPath . "' not as expected"
);
}
$found = true;
break;
}
}
if (!$found) {
Assert::fail("Resource '" . $expectedPath . "' is not in the downloaded archive.");
}
}
\unlink($tempFile);
}
Expand Down
23 changes: 23 additions & 0 deletions tests/acceptance/features/bootstrap/SpacesContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -3240,4 +3240,27 @@ public function theUserShouldHaveSpaceWithRecipient(
}
Assert::assertTrue($foundRoleInResponse, "the response does not contain the $recipientType $recipient");
}

/**
* @When user :user downloads the space :spaceName using the WebDAV API
*
* @param string $user
* @param string $spaceName
*
* @return void
*
* @throws GuzzleException
*/
public function userDownloadsTheSpaceUsingTheWebdavApi(string $user, string $spaceName):void {
$space = $this->getSpaceByName($user, $spaceName);
$url = $this->featureContext->getBaseUrl() . '/archiver?id=' . $space["id"];
$this->featureContext->setResponse(
HttpRequestHelper::get(
$url,
'',
$user,
$this->featureContext->getPasswordForUser($user),
)
);
}
}

0 comments on commit 3b31e9a

Please sign in to comment.