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 13, 2023
1 parent 6fcb41d commit 1505589
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
24 changes: 24 additions & 0 deletions tests/acceptance/features/apiSpaces/spaceDownload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
@api
Feature: Download space
As a user
I want to be able to download space
So that I can have it in my local storage


Scenario: user downloads a space
Given these users have been created with default attributes and without skeleton files:
| username |
| Alice |
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 "download space" with the default quota using the GraphApi
And user "Alice" has uploaded a file inside space "download space" with content "some data" to "file1.txt"
And user "Alice" has uploaded a file inside space "download space" with content "other data" to "file2.txt"
And user "Alice" has uploaded a file inside space "download space" with content "more data" to "file3.txt"
When user "Alice" downloads the space "download space" using the WebDAV API
Then the HTTP status code should be "200"
And the downloaded "tar" space archive should contain these files:
| name | content |
| file1.txt | some data |
| file2.txt | other data |
| file3.txt | more data |
75 changes: 75 additions & 0 deletions tests/acceptance/features/bootstrap/SpacesContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
use TestHelpers\SetupHelper;
use TestHelpers\GraphHelper;
use PHPUnit\Framework\Assert;
use wapmorgan\UnifiedArchive\Exceptions\NonExistentArchiveFileException;
use wapmorgan\UnifiedArchive\UnifiedArchive;

require_once 'bootstrap.php';

Expand Down Expand Up @@ -3198,4 +3200,77 @@ 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 Exception
* @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),
)
);
}

/**
* @Then the downloaded :type space archive should contain these files:
*
* @param string $type
* @param TableNode $expectedFiles
*
* @return void
*
* @throws NonExistentArchiveFileException
*/
public function theDownloadedSpaceArchiveShouldContainTheseFiles(string $type, TableNode $expectedFiles):void {
$dataVal = $this->featureContext->getResponse()->getBody()->getContents();
$placeValue = [];
$filePos = '';

// Storing position of expected file from response data in the array
foreach ($expectedFiles->getHash() as $file) {
$place = stripos($dataVal, $file['name']);
$placeValue[] = $place;
}

// Traversing through response data to get the file name which first occurred
foreach ($expectedFiles->getHash() as $file) {
$place = stripos($dataVal, $file['name']);
if ($place == min($placeValue)) {
$filePos = $file['name'];
}
}

// Data before first occurred expected file is removed to exclude the data of . folder
$fileData = strstr($dataVal, $filePos);

// Checking the contents of expected file and contents of response data
$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, $fileData);
$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"
);
}
\unlink($tempFile);
}
}

0 comments on commit 1505589

Please sign in to comment.