Skip to content

Commit 65cfe9d

Browse files
committed
Add integration tests for user_status API
Signed-off-by: Jonas Meurer <[email protected]>
1 parent 3fe267b commit 65cfe9d

File tree

3 files changed

+112
-0
lines changed

3 files changed

+112
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
Feature: user_status
2+
Background:
3+
Given using api version "2"
4+
And user "user0" exists
5+
And user "user0" has status "dnd"
6+
7+
Scenario: listing recent user statuses with default settings
8+
Then user statuses for "admin" list "user0" with status "dnd"
9+
10+
Scenario: empty recent user statuses with disabled/limited user enumeration
11+
When parameter "shareapi_allow_share_dialog_user_enumeration" of app "core" is set to "no"
12+
Then user statuses for "admin" are empty
13+
When parameter "shareapi_allow_share_dialog_user_enumeration" of app "core" is set to "yes"
14+
When parameter "shareapi_restrict_user_enumeration_to_group" of app "core" is set to "yes"
15+
Then user statuses for "admin" are empty
16+
When parameter "shareapi_restrict_user_enumeration_to_group" of app "core" is set to "no"
17+
When parameter "shareapi_restrict_user_enumeration_to_phone" of app "core" is set to "yes"
18+
Then user statuses for "admin" are empty
19+
When parameter "shareapi_restrict_user_enumeration_to_phone" of app "core" is set to "no"
20+
Then user statuses for "admin" list "user0" with status "dnd"

build/integration/features/bootstrap/CollaborationContext.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626
use Behat\Behat\Context\Context;
2727
use Behat\Gherkin\Node\TableNode;
28+
use GuzzleHttp\Client;
2829
use PHPUnit\Framework\Assert;
2930

3031
require __DIR__ . '/../../vendor/autoload.php';
@@ -70,4 +71,94 @@ protected function resetAppConfigs(): void {
7071
$this->deleteServerConfig('core', 'shareapi_restrict_user_enumeration_full_match');
7172
$this->deleteServerConfig('core', 'shareapi_only_share_with_group_members');
7273
}
74+
75+
/**
76+
* @Given /^user "([^"]*)" has status "([^"]*)"$/
77+
* @param string $user
78+
* @param string $status
79+
*/
80+
public function assureUserHasStatus($user, $status) {
81+
$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/user_status/status";
82+
$client = new Client();
83+
$options = [
84+
'headers' => [
85+
'OCS-APIREQUEST' => 'true',
86+
],
87+
];
88+
if ($user === 'admin') {
89+
$options['auth'] = $this->adminUser;
90+
} else {
91+
$options['auth'] = [$user, $this->regularUser];
92+
}
93+
94+
$options['form_params'] = [
95+
'statusType' => $status
96+
];
97+
98+
$this->response = $client->put($fullUrl, $options);
99+
$this->theHTTPStatusCodeShouldBe(200);
100+
101+
$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/user_status";
102+
unset($options['form_params']);
103+
$this->response = $client->get($fullUrl, $options);
104+
$this->theHTTPStatusCodeShouldBe(200);
105+
106+
$returnedStatus = json_decode(json_encode(simplexml_load_string($this->response->getBody()->getContents())->data), true)['status'];
107+
Assert::assertEquals($status, $returnedStatus);
108+
}
109+
110+
/**
111+
* @param string $user
112+
* @return null|array
113+
*/
114+
public function getStatusList(string $user): ?array {
115+
$fullUrl = $this->baseUrl . "v{$this->apiVersion}.php/apps/user_status/api/v1/statuses";
116+
$client = new Client();
117+
$options = [
118+
'headers' => [
119+
'OCS-APIREQUEST' => 'true',
120+
],
121+
];
122+
if ($user === 'admin') {
123+
$options['auth'] = $this->adminUser;
124+
} else {
125+
$options['auth'] = [$user, $this->regularUser];
126+
}
127+
128+
$this->response = $client->get($fullUrl, $options);
129+
$this->theHTTPStatusCodeShouldBe(200);
130+
131+
$contents = $this->response->getBody()->getContents();
132+
return json_decode(json_encode(simplexml_load_string($contents)->data), true);
133+
}
134+
135+
/**
136+
* @Given /^user statuses for "([^"]*)" list "([^"]*)" with status "([^"]*)"$/
137+
* @param string $user
138+
* @param string $statusUser
139+
* @param string $status
140+
*/
141+
public function assertStatusesList(string $user, string $statusUser, string $status): void {
142+
$statusList = $this->getStatusList($user);
143+
Assert::assertArrayHasKey('element', $statusList, 'Returned status list empty or broken');
144+
if (array_key_exists('userId', $statusList['element'])) {
145+
// If only one user has a status set, the API returns their status directly
146+
Assert::assertArrayHasKey('status', $statusList['element'], 'Returned status list empty or broken');
147+
$filteredStatusList = [ $statusList['element']['userId'] => $statusList['element']['status'] ];
148+
} else {
149+
// If more than one user have their status set, the API returns an array of their statuses
150+
$filteredStatusList = array_column($statusList['element'], 'status', 'userId');
151+
}
152+
Assert::assertArrayHasKey($statusUser, $filteredStatusList, 'User not listed in statuses: ' . $statusUser);
153+
Assert::assertEquals($status, $filteredStatusList[$statusUser]);
154+
}
155+
156+
/**
157+
* @Given /^user statuses for "([^"]*)" are empty$/
158+
* @param string $user
159+
*/
160+
public function assertStatusesEmpty(string $user): void {
161+
$statusList = $this->getStatusList($user);
162+
Assert::assertEmpty($statusList);
163+
}
73164
}

build/integration/features/bootstrap/Provisioning.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* @author Sergio Bertolín <[email protected]>
1414
* @author Thomas Müller <[email protected]>
1515
* @author Vincent Petry <[email protected]>
16+
* @author Jonas Meurer <[email protected]>
1617
*
1718
* @license GNU AGPL version 3 or any later version
1819
*

0 commit comments

Comments
 (0)