From 13098d9f32fc4b9aa561d89d0589651639295345 Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Tue, 2 Nov 2021 11:56:05 +0100 Subject: [PATCH 1/4] API tests for archiver endpoint (single file) --- tests/acceptance/config/behat.yml | 17 +++ .../features/apiArchiver/downloadById.feature | 20 +++ .../features/bootstrap/ArchiverContext.php | 119 ++++++++++++++++++ 3 files changed, 156 insertions(+) create mode 100644 tests/acceptance/features/apiArchiver/downloadById.feature create mode 100644 tests/acceptance/features/bootstrap/ArchiverContext.php diff --git a/tests/acceptance/config/behat.yml b/tests/acceptance/config/behat.yml index e3ba6e95a1..016de39b49 100644 --- a/tests/acceptance/config/behat.yml +++ b/tests/acceptance/config/behat.yml @@ -48,6 +48,23 @@ default: - TrashbinContext: - WebDavPropertiesContext: + apiArchiver: + paths: + - '%paths.base%/../features/apiArchiver' + contexts: + - ArchiverContext: + - OccContext: + - FeatureContext: &common_feature_context_params + baseUrl: http://localhost:8080 + adminUsername: admin + adminPassword: admin + regularUserPassword: 123456 + ocPath: apps/testing/api/v1/occ + - CapabilitiesContext: + - ChecksumContext: + - FilesVersionsContext: + - PublicWebDavContext: + extensions: jarnaiz\JUnitFormatter\JUnitFormatterExtension: filename: report.xml diff --git a/tests/acceptance/features/apiArchiver/downloadById.feature b/tests/acceptance/features/apiArchiver/downloadById.feature new file mode 100644 index 0000000000..656fe7dd45 --- /dev/null +++ b/tests/acceptance/features/apiArchiver/downloadById.feature @@ -0,0 +1,20 @@ +@api @skipOnOcV10 +Feature: download multiple resources bundled into an archive + As a user + I want to be able to download multiple items at once + So that I don't have to execute repetitive tasks + + As a developer + I want to be able to use the resource ID to download multiple items at once + So that I don't have to know the full path of the resource + + Background: + Given user "Alice" has been created with default attributes and without skeleton files + + Scenario: download a single file + 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 + Then the HTTP status code should be "200" + And the downloaded archive should contain these files: + | name | content | + | textfile0.txt | some data | diff --git a/tests/acceptance/features/bootstrap/ArchiverContext.php b/tests/acceptance/features/bootstrap/ArchiverContext.php new file mode 100644 index 0000000000..336cb7a546 --- /dev/null +++ b/tests/acceptance/features/bootstrap/ArchiverContext.php @@ -0,0 +1,119 @@ + + * @copyright Copyright (c) 2021 Artur Neumann artur@jankaritech.com + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, + * as published by the Free Software Foundation; + * either version 3 of the License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see + * + */ + +use Behat\Behat\Context\Context; +use Behat\Behat\Hook\Scope\BeforeScenarioScope; +use Behat\Gherkin\Node\TableNode; +use TestHelpers\HttpRequestHelper; +use TestHelpers\SetupHelper; +use wapmorgan\UnifiedArchive\UnifiedArchive; +use PHPUnit\Framework\Assert; + +require_once 'bootstrap.php'; + +/** + * Context for Archiver specific steps + */ +class ArchiverContext implements Context { + + /** + * @var FeatureContext + */ + private $featureContext; + + /** + * @BeforeScenario + * + * @param BeforeScenarioScope $scope + * + * @return void + * + * @throws Exception + */ + public function setUpScenario(BeforeScenarioScope $scope): void { + // Get the environment + $environment = $scope->getEnvironment(); + // Get all the contexts you need in this context + $this->featureContext = $environment->getContext('FeatureContext'); + SetupHelper::init( + $this->featureContext->getAdminUsername(), + $this->featureContext->getAdminPassword(), + $this->featureContext->getBaseUrl(), + $this->featureContext->getOcPath() + ); + } + + /** + * @When user :user downloads the archive of :resourceId using the resource id + * + * @param string $user + * @param string $resource + * + * @return void + * + * @throws \GuzzleHttp\Exception\GuzzleException + */ + public function userDownloadsTheArchiveOfUsingTheResourceId(string $user, string $resource): void { + $resourceId = $this->featureContext->getFileIdForPath($user, $resource); + $user = $this->featureContext->getActualUsername($user); + $this->featureContext->setResponse( + HttpRequestHelper::get( + $this->featureContext->getBaseUrl() . '/archiver?id=' . $resourceId, + '', + $user, + $this->featureContext->getPasswordForUser($user) + ) + ); + } + + /** + * @Then the downloaded archive should contain these files: + * + * @param TableNode $expectedFiles + * + * @return void + * + * @throws Exception + */ + public function theDownloadedArchiveShouldContainTheseFiles(TableNode $expectedFiles) { + $this->featureContext->verifyTableNodeColumns($expectedFiles, ['name', 'content']); + $tempFile = \tempnam(\sys_get_temp_dir(), 'OcAcceptanceTests_'); + \unlink($tempFile); // we only need the name + $tempFile = $tempFile . '.tar'; // it needs the extension + \file_put_contents($tempFile, $this->featureContext->getResponse()->getBody()->getContents()); + $archive = UnifiedArchive::open($tempFile); + foreach ($expectedFiles->getHash() as $expectedFile) { + Assert::assertTrue( + $archive->hasFile($expectedFile['name']), + __METHOD__ . + " archive does not contain '" . $expectedFile['name'] . "'" + ); + Assert::assertEquals( + $expectedFile['content'], + $archive->getFileContent($expectedFile['name']), + __METHOD__ . + " content of '" . $expectedFile['name'] . "' not as expected" + ); + } + \unlink($tempFile); + } +} From 916f0a6225ff9f1c7a496de8d67b6e1babab87c7 Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Tue, 9 Nov 2021 15:02:13 +0545 Subject: [PATCH 2/4] delete `hasFile` check this is not needed as the lib checks the file existance on `getFileContent` and gives a nice eror message if it does not exist --- tests/acceptance/features/bootstrap/ArchiverContext.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/acceptance/features/bootstrap/ArchiverContext.php b/tests/acceptance/features/bootstrap/ArchiverContext.php index 336cb7a546..4b737f302e 100644 --- a/tests/acceptance/features/bootstrap/ArchiverContext.php +++ b/tests/acceptance/features/bootstrap/ArchiverContext.php @@ -102,11 +102,6 @@ class ArchiverContext implements Context { \file_put_contents($tempFile, $this->featureContext->getResponse()->getBody()->getContents()); $archive = UnifiedArchive::open($tempFile); foreach ($expectedFiles->getHash() as $expectedFile) { - Assert::assertTrue( - $archive->hasFile($expectedFile['name']), - __METHOD__ . - " archive does not contain '" . $expectedFile['name'] . "'" - ); Assert::assertEquals( $expectedFile['content'], $archive->getFileContent($expectedFile['name']), From c97cbf9b98a280d449328b39260e34035afe739d Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Tue, 9 Nov 2021 15:04:05 +0545 Subject: [PATCH 3/4] test downloading a folder --- .../features/apiArchiver/downloadById.feature | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tests/acceptance/features/apiArchiver/downloadById.feature b/tests/acceptance/features/apiArchiver/downloadById.feature index 656fe7dd45..75dba81893 100644 --- a/tests/acceptance/features/apiArchiver/downloadById.feature +++ b/tests/acceptance/features/apiArchiver/downloadById.feature @@ -18,3 +18,14 @@ Feature: download multiple resources bundled into an archive And the downloaded archive should contain these files: | name | content | | textfile0.txt | some data | + + Scenario: download a single folder + 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 "more data" to "/my_data/an_other_file.txt" + When user "Alice" downloads the archive of "/my_data" using the resource id + Then the HTTP status code should be "200" + And the downloaded archive should contain these files: + | name | content | + | my_data/textfile0.txt | some data | + | my_data/an_other_file.txt | more data | From 23cb8f25a72c1b3fb396a68de0d16b3e2acec175 Mon Sep 17 00:00:00 2001 From: Artur Neumann Date: Tue, 9 Nov 2021 15:05:32 +0545 Subject: [PATCH 4/4] run apiArchiver tests in drone --- .drone.star | 1 + 1 file changed, 1 insertion(+) diff --git a/.drone.star b/.drone.star index 32b11b4e8b..cdef01deca 100644 --- a/.drone.star +++ b/.drone.star @@ -237,6 +237,7 @@ def testPipelines(ctx): pipelines = [ localApiTests(ctx, "ocis", "apiAccountsHashDifficulty", "default"), localApiTests(ctx, "ocis", "apiSpaces", "default"), + localApiTests(ctx, "ocis", "apiArchiver", "default"), ] if "skip" not in config["apiTests"] or not config["apiTests"]["skip"]: