From 1cf0a35ba3f14b3bb02e1685849638a948ccc661 Mon Sep 17 00:00:00 2001 From: amrita Date: Mon, 16 Sep 2024 10:02:37 +0545 Subject: [PATCH] add test for sending request to check file info with invalid file id --- .../TestHelpers/CollaborationHelper.php | 67 +++++++++++++++++++ tests/acceptance/TestHelpers/EmailHelper.php | 14 ++++ tests/acceptance/TestHelpers/GraphHelper.php | 14 ++++ tests/acceptance/TestHelpers/OcmHelper.php | 16 ++++- .../bootstrap/CollaborationContext.php | 60 ++++++++++++++--- .../apiCollaboration/checkFileInfo.feature | 11 +++ 6 files changed, 171 insertions(+), 11 deletions(-) create mode 100644 tests/acceptance/TestHelpers/CollaborationHelper.php diff --git a/tests/acceptance/TestHelpers/CollaborationHelper.php b/tests/acceptance/TestHelpers/CollaborationHelper.php new file mode 100644 index 0000000000..a0b6b9b9bc --- /dev/null +++ b/tests/acceptance/TestHelpers/CollaborationHelper.php @@ -0,0 +1,67 @@ + + * @copyright Copyright (c) 2024 Amrita Shrestha amrita@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 + * + */ + +namespace TestHelpers; + +use GuzzleHttp\Exception\GuzzleException; +use Psr\Http\Message\ResponseInterface; + +/** + * A helper class for managing wopi requests + */ +class CollaborationHelper { + + /** + * @param string $fileId + * @param string $app + * @param string $username + * @param string $password + * @param string $baseUrl + * @param string $xRequestId + * @param string|null $viewMode + * + * @return ResponseInterface + * @throws GuzzleException + */ + public static function sendPOSTRequestToAppOpen( + string $fileId, + string $app, + string $username, + string $password, + string $baseUrl, + string $xRequestId, + ?string $viewMode = null, + ): ResponseInterface { + $url = $baseUrl . "/app/open?app_name=$app&file_id=$fileId"; + if ($viewMode) { + $url .= "&view_mode=$viewMode"; + } + + return HttpRequestHelper::post( + $url, + $xRequestId, + $username, + $password, + ['Content-Type' => 'application/json'] + ); + } +} diff --git a/tests/acceptance/TestHelpers/EmailHelper.php b/tests/acceptance/TestHelpers/EmailHelper.php index 02ffe4f758..bc09e81314 100644 --- a/tests/acceptance/TestHelpers/EmailHelper.php +++ b/tests/acceptance/TestHelpers/EmailHelper.php @@ -4,6 +4,20 @@ * * @author Prajwol Amatya * @copyright Copyright (c) 2023 Prajwol Amatya prajwol@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 + * */ namespace TestHelpers; diff --git a/tests/acceptance/TestHelpers/GraphHelper.php b/tests/acceptance/TestHelpers/GraphHelper.php index e3efbf54f5..e02e8e6344 100644 --- a/tests/acceptance/TestHelpers/GraphHelper.php +++ b/tests/acceptance/TestHelpers/GraphHelper.php @@ -4,6 +4,20 @@ * * @author Kiran Parajuli * @copyright Copyright (c) 2022 Kiran Parajuli kiran@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 + * */ namespace TestHelpers; diff --git a/tests/acceptance/TestHelpers/OcmHelper.php b/tests/acceptance/TestHelpers/OcmHelper.php index 9387a285d4..5432d293f7 100644 --- a/tests/acceptance/TestHelpers/OcmHelper.php +++ b/tests/acceptance/TestHelpers/OcmHelper.php @@ -4,6 +4,20 @@ * * @author Viktor Scharf * @copyright Copyright (c) 2024 Viktor Scharf + * + * 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 + * */ namespace TestHelpers; @@ -23,7 +37,7 @@ class OcmHelper { 'Content-Type' => 'application/json', ]; } - + /** * @param string $baseUrl * @param string $path diff --git a/tests/acceptance/bootstrap/CollaborationContext.php b/tests/acceptance/bootstrap/CollaborationContext.php index 5e0a5e9abc..871083d8e7 100644 --- a/tests/acceptance/bootstrap/CollaborationContext.php +++ b/tests/acceptance/bootstrap/CollaborationContext.php @@ -24,6 +24,8 @@ use Behat\Behat\Context\Context; use Behat\Behat\Hook\Scope\BeforeScenarioScope; use GuzzleHttp\Exception\GuzzleException; use TestHelpers\HttpRequestHelper; +use TestHelpers\WebDavHelper; +use TestHelpers\CollaborationHelper; /** * steps needed to re-configure oCIS server @@ -66,19 +68,15 @@ class CollaborationContext implements Context { */ public function userChecksTheInformationOfFileOfSpaceUsingOffice(string $user, string $file, string $space, string $app, string $viewMode = null): void { $fileId = $this->spacesContext->getFileId($user, $space, $file); - $url = $this->featureContext->getBaseUrl() . "/app/open?app_name=$app&file_id=$fileId"; - - if ($viewMode) { - $url .= "&view_mode=$viewMode"; - } - $response = \json_decode( - HttpRequestHelper::post( - $url, - $this->featureContext->getStepLineRef(), + CollaborationHelper::sendPOSTRequestToAppOpen( + $fileId, + $app, $this->featureContext->getActualUsername($user), $this->featureContext->getPasswordForUser($user), - ['Content-Type' => 'application/json'] + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef(), + $viewMode )->getBody()->getContents() ); @@ -119,4 +117,46 @@ class CollaborationContext implements Context { ) ); } + + /** + * @When user :user tries to check the information of file :file of space :space using office :app with invalid file-id + * + * @param string $user + * @param string $file + * @param string $space + * @param string $app + * + * @return void + * + * @throws GuzzleException + * @throws JsonException + */ + public function userTriesToCheckTheInformationOfFileOfSpaceUsingOfficeWithInvalidFileId(string $user, string $file, string $space, string $app): void { + $response = \json_decode( + CollaborationHelper::sendPOSTRequestToAppOpen( + $this->spacesContext->getFileId($user, $space, $file), + $app, + $this->featureContext->getActualUsername($user), + $this->featureContext->getPasswordForUser($user), + $this->featureContext->getBaseUrl(), + $this->featureContext->getStepLineRef() + )->getBody()->getContents() + ); + $accessToken = $response->form_parameters->access_token; + + // Extract the WOPISrc from the app_url + $parsedUrl = parse_url($response->app_url); + parse_str($parsedUrl['query'], $queryParams); + $wopiSrc = $queryParams['WOPISrc']; + $position = strpos($wopiSrc, '/files/') + \strlen('/files/'); + + // Extract the base URL up to and including '/files/' + $fullUrl = substr($wopiSrc, 0, $position) . WebDavHelper::generateUUIDv4(); + $this->featureContext->setResponse( + HttpRequestHelper::get( + $fullUrl . "?access_token=$accessToken", + $this->featureContext->getStepLineRef() + ) + ); + } } diff --git a/tests/acceptance/features/apiCollaboration/checkFileInfo.feature b/tests/acceptance/features/apiCollaboration/checkFileInfo.feature index e243dd259d..f881aa34e8 100644 --- a/tests/acceptance/features/apiCollaboration/checkFileInfo.feature +++ b/tests/acceptance/features/apiCollaboration/checkFileInfo.feature @@ -588,3 +588,14 @@ Feature: check file info with different wopi apps | view | true | false | false | | read | false | false | false | | write | false | true | true | + + + Scenario Outline: try to get file info using invalid file id with office suites + Given user "Alice" has uploaded file with content "hello world" to "/textfile0.txt" + When user "Alice" tries to check the information of file "textfile0.txt" of space "Personal" using office "" with invalid file-id + Then the HTTP status code should be "401" + Examples: + | office-suites | + | Collabora | + | FakeOffice | + | OnlyOffice |