1
0
mirror of https://github.com/owncloud/ocis.git synced 2025-04-18 23:44:07 +03:00

test: fix archive tests

This commit is contained in:
Saw-jan 2024-08-20 17:57:42 +05:45
parent 085a1a4b40
commit d45efeacca
No known key found for this signature in database
GPG Key ID: 8CB52C9ABF046F4A
4 changed files with 94 additions and 50 deletions

View File

@ -13,11 +13,6 @@ The expected failures in this file are from features in the owncloud/ocis repo.
- [apiArchiver/downloadByPath.feature:171](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiArchiver/downloadByPath.feature#L171) - [apiArchiver/downloadByPath.feature:171](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiArchiver/downloadByPath.feature#L171)
- [apiArchiver/downloadByPath.feature:172](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiArchiver/downloadByPath.feature#L172) - [apiArchiver/downloadByPath.feature:172](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiArchiver/downloadByPath.feature#L172)
#### [Downloaded /Shares tar contains resource (files|folder) with leading / in Response](https://github.com/owncloud/ocis/issues/4636)
- [apiArchiver/downloadById.feature:173](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiArchiver/downloadById.feature#L173)
- [apiArchiver/downloadById.feature:174](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiArchiver/downloadById.feature#L174)
#### [Shared mount folder gets deleted when overwritten by a file from personal space](https://github.com/owncloud/ocis/issues/7208) #### [Shared mount folder gets deleted when overwritten by a file from personal space](https://github.com/owncloud/ocis/issues/7208)
- [apiSpacesShares/copySpaces.feature:696](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/copySpaces.feature#L696) - [apiSpacesShares/copySpaces.feature:696](https://github.com/owncloud/ocis/blob/master/tests/acceptance/features/apiSpacesShares/copySpaces.feature#L696)

View File

@ -13,7 +13,7 @@ Feature: download multiple resources bundled into an archive
Scenario Outline: download a single file Scenario Outline: download a single file
Given user "Alice" has uploaded file with content "some data" to "/textfile0.txt" Given user "Alice" has uploaded file with content "some data" to "/textfile0.txt"
When user "Alice" downloads the archive of "/textfile0.txt" using the resource id and setting these headers When user "Alice" downloads the <archive-type> archive of "/textfile0.txt" using the resource id and setting these headers:
| header | value | | header | value |
| User-Agent | <user-agent> | | User-Agent | <user-agent> |
Then the HTTP status code should be "200" Then the HTTP status code should be "200"
@ -22,7 +22,7 @@ Feature: download multiple resources bundled into an archive
| textfile0.txt | some data | | textfile0.txt | some data |
Examples: Examples:
| user-agent | archive-type | | user-agent | archive-type |
| Linux | zip | | Linux | tar |
| Windows NT | zip | | Windows NT | zip |
@ -30,7 +30,7 @@ Feature: download multiple resources bundled into an archive
Given user "Alice" has created folder "my_data" Given user "Alice" has created folder "my_data"
And user "Alice" has uploaded file with content "some data" to "/my_data/textfile0.txt" And user "Alice" has uploaded file with content "some data" to "/my_data/textfile0.txt"
And user "Alice" has uploaded file with content "more data" to "/my_data/an_other_file.txt" And user "Alice" has uploaded file with content "more data" to "/my_data/an_other_file.txt"
When user "Alice" downloads the archive of "/my_data" using the resource id and setting these headers When user "Alice" downloads the <archive-type> archive of "/my_data" using the resource id and setting these headers:
| header | value | | header | value |
| User-Agent | <user-agent> | | User-Agent | <user-agent> |
Then the HTTP status code should be "200" Then the HTTP status code should be "200"
@ -41,7 +41,7 @@ Feature: download multiple resources bundled into an archive
Examples: Examples:
| user-agent | archive-type | | user-agent | archive-type |
| Linux | zip | | Linux | zip |
| Windows NT | zip | | Windows NT | tar |
Scenario: download multiple files and folders Scenario: download multiple files and folders
@ -158,16 +158,16 @@ Feature: download multiple resources bundled into an archive
| shareType | user | | shareType | user |
| permissionsRole | Viewer | | permissionsRole | Viewer |
And user "Brian" has a share "more_data" synced And user "Brian" has a share "more_data" synced
When user "Brian" downloads the archive of "/Shares" using the resource id and setting these headers When user "Brian" downloads the <archive-type> archive of "/Shares" using the resource id and setting these headers:
| header | value | | header | value |
| User-Agent | <user-agent> | | User-Agent | <user-agent> |
Then the HTTP status code should be "200" Then the HTTP status code should be "200"
And the downloaded <archive-type> archive should contain these files: And the downloaded <archive-type> archive should contain these files:
| name | content | | name | content |
| Shares/textfile0.txt | some data | | textfile0.txt | some data |
| Shares/textfile1.txt | other data | | textfile1.txt | other data |
| Shares/my_data/textfile2.txt | some data | | my_data/textfile2.txt | some data |
| Shares/more_data/an_other_file.txt | more data | | more_data/an_other_file.txt | more data |
Examples: Examples:
| user-agent | archive-type | | user-agent | archive-type |
| Linux | tar | | Linux | tar |

View File

@ -28,6 +28,9 @@ use TestHelpers\HttpRequestHelper;
use TestHelpers\SetupHelper; use TestHelpers\SetupHelper;
use PHPUnit\Framework\Assert; use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use splitbrain\PHPArchive\Tar;
use splitbrain\PHPArchive\Zip;
use splitbrain\PHPArchive\Archive;
require_once 'bootstrap.php'; require_once 'bootstrap.php';
@ -62,6 +65,38 @@ class ArchiverContext implements Context {
); );
} }
/**
* @param string $type
*
* @return Archive
*/
public function getArchiveClass(string $type): Archive {
if ($type === 'zip') {
return new Zip();
} elseif ($type === 'tar') {
return new Tar();
} else {
throw new Exception('Unknown archive type: ' . $type);
}
}
/**
* @param string $dir
*
* @return void
*/
public function removeDir(string $dir): void {
$items = \glob("$dir/*");
foreach ($items as $item) {
if (\is_dir($item)) {
$this->removeDir($item);
} else {
\unlink($item);
}
}
\rmdir($dir);
}
/** /**
* @param string $user * @param string $user
* @param string $resource * @param string $resource
@ -92,9 +127,10 @@ class ArchiverContext implements Context {
} }
/** /**
* @When user :user downloads the archive of :resource using the resource :addressType and setting these headers * @When /^user "([^"]*)" downloads the (zip|tar) archive of "([^"]*)" using the resource (id|ids|path|paths) and setting these headers:$/
* *
* @param string $user * @param string $user
* @param string $archiveType
* @param string $resource * @param string $resource
* @param string $addressType id|path * @param string $addressType id|path
* @param TableNode $headersTable * @param TableNode $headersTable
@ -104,8 +140,9 @@ class ArchiverContext implements Context {
* @throws GuzzleException * @throws GuzzleException
* @throws Exception * @throws Exception
*/ */
public function userDownloadsTheArchive( public function userDownloadsTheZipOrTarArchiveOfResourceUsingResourceIdOrPathAndSettingTheseHeaders(
string $user, string $user,
string $archiveType,
string $resource, string $resource,
string $addressType, string $addressType,
TableNode $headersTable TableNode $headersTable
@ -118,7 +155,7 @@ class ArchiverContext implements Context {
foreach ($headersTable as $row) { foreach ($headersTable as $row) {
$headers[$row['header']] = $row ['value']; $headers[$row['header']] = $row ['value'];
} }
$this->featureContext->setResponse($this->downloadArchive($user, $resource, $addressType, null, $headers)); $this->featureContext->setResponse($this->downloadArchive($user, $resource, $addressType, $archiveType, null, $headers));
} }
/** /**
@ -140,13 +177,14 @@ class ArchiverContext implements Context {
string $owner, string $owner,
string $addressType string $addressType
): void { ): void {
$this->featureContext->setResponse($this->downloadArchive($downloader, $resource, $addressType, $owner)); $this->featureContext->setResponse($this->downloadArchive($downloader, $resource, $addressType, null, $owner));
} }
/** /**
* @param string $downloader * @param string $downloader
* @param string $resource * @param string $resource
* @param string $addressType * @param string $addressType
* @param string|null $archiveType
* @param string|null $owner * @param string|null $owner
* @param array|null $headers * @param array|null $headers
* *
@ -158,12 +196,16 @@ class ArchiverContext implements Context {
string $downloader, string $downloader,
string $resource, string $resource,
string $addressType, string $addressType,
?string $archiveType = null,
?string $owner = null, ?string $owner = null,
?array $headers = null ?array $headers = null
): ResponseInterface { ): ResponseInterface {
$owner = $owner ?? $downloader; $owner = $owner ?? $downloader;
$downloader = $this->featureContext->getActualUsername($downloader); $downloader = $this->featureContext->getActualUsername($downloader);
$queryString = $this->getArchiverQueryString($owner, $resource, $addressType); $queryString = $this->getArchiverQueryString($owner, $resource, $addressType);
if ($archiveType !== null) {
$queryString .= '&output-format=' . $archiveType;
}
return HttpRequestHelper::get( return HttpRequestHelper::get(
$this->featureContext->getBaseUrl() . '/archiver?' . $queryString, $this->featureContext->getBaseUrl() . '/archiver?' . $queryString,
$this->featureContext->getStepLineRef(), $this->featureContext->getStepLineRef(),
@ -220,28 +262,34 @@ class ArchiverContext implements Context {
$this->featureContext->verifyTableNodeColumns($expectedFiles, ['name', 'content']); $this->featureContext->verifyTableNodeColumns($expectedFiles, ['name', 'content']);
$contents = $this->featureContext->getResponse()->getBody()->getContents(); $contents = $this->featureContext->getResponse()->getBody()->getContents();
$tempFile = \tempnam(\sys_get_temp_dir(), 'OcAcceptanceTests_'); $tempFile = \tempnam(\sys_get_temp_dir(), 'OcAcceptanceTests_');
$tempExtractFolder = $tempFile;
\unlink($tempFile); // we only need the name \unlink($tempFile); // we only need the name
$tempFile = $tempFile . '.' . $type; // it needs the extension $tempFile = $tempFile . '.' . $type; // it needs the extension
\file_put_contents($tempFile, $contents); \file_put_contents($tempFile, $contents);
// open the archive // open the archive
$archiveData = new RecursiveIteratorIterator( $tar = $this->getArchiveClass($type);
new PharData($tempFile), $tar->open($tempFile);
RecursiveIteratorIterator::SELF_FIRST $archiveData = $tar->contents();
);
// extract the archive
$tar->open($tempFile);
$tar->extract($tempExtractFolder);
$tar->close();
foreach ($expectedFiles->getHash() as $expectedItem) { foreach ($expectedFiles->getHash() as $expectedItem) {
$expectedPath = trim($expectedItem['name'], "/"); $expectedPath = trim($expectedItem['name'], "/");
$found = false; $found = false;
foreach ($archiveData as $info) { foreach ($archiveData as $info) {
// get only the parent folder path for the given item // get only the parent folder path for the given item
$actualPath = explode(".$type", $info->getPathname())[1]; $actualPath = $info->getPath();
$actualPath = trim($actualPath, "/");
if ($expectedPath === $actualPath) { if ($expectedPath === $actualPath) {
if (!$info->isDir()) { if (!$info->getIsdir()) {
$fileContent = \file_get_contents("$tempExtractFolder/$actualPath");
Assert::assertEquals( Assert::assertEquals(
$expectedItem['content'], $expectedItem['content'],
$info->getContent(), $fileContent,
__METHOD__ . __METHOD__ .
" content of '" . $expectedPath . "' not as expected" " content of '" . $expectedPath . "' not as expected"
); );
@ -255,5 +303,6 @@ class ArchiverContext implements Context {
} }
} }
\unlink($tempFile); \unlink($tempFile);
$this->removeDir($tempExtractFolder);
} }
} }

View File

@ -1,27 +1,27 @@
{ {
"config" : { "config": {
"platform": { "platform": {
"php": "8.2" "php": "8.2"
},
"allow-plugins": {
"composer/package-versions-deprecated": true
}
}, },
"require": { "allow-plugins": {
"behat/behat": "^3.13", "composer/package-versions-deprecated": true
"behat/gherkin": "^4.9",
"behat/mink": "1.7.1",
"friends-of-behat/mink-extension": "^2.7",
"ciaranmcnulty/behat-stepthroughextension" : "dev-master",
"rdx/behat-variables": "^1.2",
"sensiolabs/behat-page-object-extension": "^2.3",
"symfony/translation": "^5.4",
"sabre/xml": "^2.2",
"guzzlehttp/guzzle": "^7.7",
"phpunit/phpunit": "^9.6",
"laminas/laminas-ldap": "^2.15",
"ankitpokhrel/tus-php": "^2.3",
"wapmorgan/unified-archive": "^1.1.10",
"swaggest/json-schema": "^0.12.42"
} }
},
"require": {
"behat/behat": "^3.13",
"behat/gherkin": "^4.9",
"behat/mink": "1.7.1",
"friends-of-behat/mink-extension": "^2.7",
"ciaranmcnulty/behat-stepthroughextension": "dev-master",
"rdx/behat-variables": "^1.2",
"sensiolabs/behat-page-object-extension": "^2.3",
"symfony/translation": "^5.4",
"sabre/xml": "^2.2",
"guzzlehttp/guzzle": "^7.7",
"phpunit/phpunit": "^9.6",
"laminas/laminas-ldap": "^2.15",
"ankitpokhrel/tus-php": "^2.3",
"swaggest/json-schema": "^0.12.42",
"splitbrain/php-archive": "^1.3"
} }
}