From ea713590b384af95a70926bad7e0224837053feb Mon Sep 17 00:00:00 2001 From: Jonas Meurer Date: Tue, 19 Oct 2021 10:56:07 +0200 Subject: [PATCH] Add integration tests for user_status API Signed-off-by: Jonas Meurer --- .../user_status.feature | 29 +++++++ .../bootstrap/CollaborationContext.php | 82 +++++++++++++++++++ .../features/bootstrap/Provisioning.php | 1 + 3 files changed, 112 insertions(+) create mode 100644 build/integration/collaboration_features/user_status.feature diff --git a/build/integration/collaboration_features/user_status.feature b/build/integration/collaboration_features/user_status.feature new file mode 100644 index 0000000000000..cfb2583a2a7e8 --- /dev/null +++ b/build/integration/collaboration_features/user_status.feature @@ -0,0 +1,29 @@ +Feature: user_status + Background: + Given using api version "2" + And user "user0" exists + And user "user0" has status "dnd" + + Scenario: listing recent user statuses with default settings + When user "user0" has status "dnd" + Then user statuses for "admin" list "user0" with status "dnd" + + Scenario: empty recent user statuses with disabled user enumeration + Given As an "admin" + When parameter "shareapi_allow_share_dialog_user_enumeration" of app "core" is set to "no" + And user "user0" has status "dnd" + Then user statuses for "admin" are empty + + Scenario: empty recent user statuses with user enumeration limited to common groups + Given As an "admin" + When parameter "shareapi_allow_share_dialog_user_enumeration" of app "core" is set to "yes" + And parameter "shareapi_restrict_user_enumeration_to_group" of app "core" is set to "yes" + And user "user0" has status "dnd" + Then user statuses for "admin" are empty + + Scenario: empty recent user statuses with user enumeration limited to phone matches + Given As an "admin" + When parameter "shareapi_restrict_user_enumeration_to_group" of app "core" is set to "no" + And parameter "shareapi_restrict_user_enumeration_to_phone" of app "core" is set to "yes" + And user "user0" has status "dnd" + Then user statuses for "admin" are empty diff --git a/build/integration/features/bootstrap/CollaborationContext.php b/build/integration/features/bootstrap/CollaborationContext.php index 17eb820c77dbc..856f07c76a4b1 100644 --- a/build/integration/features/bootstrap/CollaborationContext.php +++ b/build/integration/features/bootstrap/CollaborationContext.php @@ -25,6 +25,7 @@ */ use Behat\Behat\Context\Context; use Behat\Gherkin\Node\TableNode; +use GuzzleHttp\Client; use PHPUnit\Framework\Assert; require __DIR__ . '/../../vendor/autoload.php'; @@ -70,4 +71,85 @@ protected function resetAppConfigs(): void { $this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match'); $this->deleteServerConfig('core', 'shareapi_only_share_with_group_members'); } + + /** + * @Given /^user "([^"]*)" has status "([^"]*)"$/ + * @param string $user + * @param string $status + */ + public function assureUserHasStatus($user, $status) { + $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/user_status/status"; + $client = new Client(); + $options = [ + 'headers' => [ + 'OCS-APIREQUEST' => 'true', + ], + ]; + if ($user === 'admin') { + $options['auth'] = $this->adminUser; + } else { + $options['auth'] = [$user, $this->regularUser]; + } + + $options['form_params'] = [ + 'statusType' => $status + ]; + + $this->response = $client->put($fullUrl, $options); + $this->theHTTPStatusCodeShouldBe(200); + + $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/user_status"; + unset($options['form_params']); + $this->response = $client->get($fullUrl, $options); + $this->theHTTPStatusCodeShouldBe(200); + + $returnedStatus = json_decode(json_encode(simplexml_load_string($this->response->getBody()->getContents())->data), true)['status']; + Assert::assertEquals($status, $returnedStatus); + } + + /** + * @param string $user + * @return null|array + */ + public function getStatusList(string $user): ?array { + $fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/statuses"; + $client = new Client(); + $options = [ + 'headers' => [ + 'OCS-APIREQUEST' => 'true', + ], + ]; + if ($user === 'admin') { + $options['auth'] = $this->adminUser; + } else { + $options['auth'] = [$user, $this->regularUser]; + } + + $this->response = $client->get($fullUrl, $options); + $this->theHTTPStatusCodeShouldBe(200); + + $contents = $this->response->getBody()->getContents(); + return json_decode(json_encode(simplexml_load_string($contents)->data), true); + } + + /** + * @Given /^user statuses for "([^"]*)" list "([^"]*)" with status "([^"]*)"$/ + * @param string $user + * @param string $statusUser + * @param string $status + */ + public function assertStatusesList(string $user, string $statusUser, string $status): void { + $statusList = $this->getStatusList($user); + Assert::assertArrayHasKey('element', $statusList); + Assert::assertEquals($status, array_column($statusList['element'], 'status', 'userId')[$statusUser]); + } + + /** + * @Given /^user statuses for "([^"]*)" are empty$/ + * @param string $user + */ + public function assertStatusesEmpty(string $user): void { + $statusList = $this->getStatusList($user); + Assert::assertEmpty($statusList); + } } diff --git a/build/integration/features/bootstrap/Provisioning.php b/build/integration/features/bootstrap/Provisioning.php index e51339c081e02..1b01a636918aa 100644 --- a/build/integration/features/bootstrap/Provisioning.php +++ b/build/integration/features/bootstrap/Provisioning.php @@ -13,6 +13,7 @@ * @author Sergio Bertolín * @author Thomas Müller * @author Vincent Petry + * @author Jonas Meurer * * @license GNU AGPL version 3 or any later version *