-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathFeatureContext.php
257 lines (223 loc) · 6.37 KB
/
FeatureContext.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2016 David Toledo <[email protected]>
* SPDX-FileCopyrightText: 2016 Sergio Bertolín <[email protected]>
* SPDX-FileCopyrightText: 2016 Thomas Müller <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
require __DIR__ . '/../../vendor/autoload.php';
require __DIR__ . '/WebDav.php';
use Behat\Behat\Context\Context;
use Behat\Gherkin\Node\TableNode;
use GuzzleHttp\Client;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\ServerException;
use GuzzleHttp\RequestOptions;
use PHPUnit\Framework\Assert;
use Psr\Http\Message\ResponseInterface;
/**
* Defines application features from the specific context.
*/
class FeatureContext implements Context {
use WebDav;
protected $regularUser = '123456';
protected $adminUser = ['admin', 'admin'];
/** @var string */
protected $currentUser;
/** @var ResponseInterface */
private $response = null;
/** @var CookieJar */
private $cookieJar;
/** @var string */
protected $baseUrl;
/**
* FeatureContext constructor.
*/
public function __construct() {
$this->cookieJar = new CookieJar();
$this->baseUrl = getenv('TEST_SERVER_URL');
}
/**
* @BeforeScenario
* @AfterScenario
*/
public function cleanUpBetweenTests() {
// TODO: Remove all created tags?
$this->setCurrentUser('admin');
$this->sendingTo('DELETE', '/apps/files_accesscontrol_testing');
$this->assertStatusCode($this->response, 200);
try {
$this->userDeletesFile('test1', 'folder', '/subdir');
} catch (\Exception $e) {
}
try {
$this->userDeletesFile('test1', 'file', '/foobar.txt');
} catch (\Exception $e) {
}
try {
$this->userDeletesFile('test1', 'file', '/definitely.notexe');
} catch (\Exception $e) {
}
try {
$this->emptyTrashbin('test1');
} catch (\Exception $e) {
}
}
/**
* @Given /^user "([^"]*)" creates (global|user) flow with (\d+)$/
*/
public function createFlow(string $user, string $scope, int $statusCode, TableNode $tableNode) {
$this->setCurrentUser($user);
$formData = $tableNode->getRowsHash();
$checks = [];
foreach ($formData as $key => $value) {
if (strpos($key, 'checks-') === 0) {
$checks[] = json_decode($value, true);
unset($formData[$key]);
}
}
$formData['checks'] = $checks;
$formData['events'] = [];
$this->sendingToWith('POST', '/apps/workflowengine/api/v1/workflows/' . $scope, $formData);
Assert::assertSame($statusCode, $this->response->getStatusCode(), 'HTTP status code mismatch');
}
/**
* @Given /^user "([^"]*)" shares file "([^"]*)" with user "([^"]*)"$/
*/
public function userSharesFile(string $sharer, string $file, string $sharee): void {
$this->setCurrentUser($sharer);
$this->sendingToWith('POST', '/apps/files_sharing/api/v1/shares', [
'path' => $file,
'permissions' => 19,
'shareType' => 0,
'shareWith' => $sharee,
]);
}
// ChecksumsContext
/**
* @Then The webdav response should have a status code :statusCode
* @param string $statusCode
* @throws \Exception
*/
public function theWebdavResponseShouldHaveAStatusCode($statusCode) {
if (str_contains($statusCode, '|')) {
$statusCodes = array_map('intval', explode('|', $statusCode));
} else {
$statusCodes = [(int)$statusCode];
}
if (!in_array($this->response->getStatusCode(), $statusCodes, true)) {
throw new \Exception("Expected $statusCode, got ".$this->response->getStatusCode());
}
}
/**
* User management
*/
/**
* @Given /^as user "([^"]*)"$/
* @param string $user
*/
public function setCurrentUser(string $user): void {
$this->currentUser = $user;
}
/**
* @Given /^user "([^"]*)" exists$/
* @param string $user
*/
public function assureUserExists(string $user): void {
try {
$this->userExists($user);
} catch (ClientException $ex) {
$this->createUser($user);
}
$response = $this->userExists($user);
$this->assertStatusCode($response, 200);
}
private function userExists(string $user): ResponseInterface {
$client = new Client();
$options = [
'auth' => ['admin', 'admin'],
'headers' => [
'OCS-APIREQUEST' => 'true',
],
];
return $client->get($this->baseUrl . 'ocs/v2.php/cloud/users/' . $user, $options);
}
private function createUser(string $user): void {
$previous_user = $this->currentUser;
$this->currentUser = 'admin';
$userProvisioningUrl = $this->baseUrl . 'ocs/v2.php/cloud/users';
$client = new Client();
$options = [
'auth' => ['admin', 'admin'],
'form_params' => [
'userid' => $user,
'password' => '123456'
],
'headers' => [
'OCS-APIREQUEST' => 'true',
],
];
$client->post($userProvisioningUrl, $options);
// Quick hack to log in once with the current user
$options2 = [
'auth' => [$user, '123456'],
'headers' => [
'OCS-APIREQUEST' => 'true',
],
];
$client->get($userProvisioningUrl . '/' . $user, $options2);
$this->currentUser = $previous_user;
}
/*
* Requests
*/
/**
* @When /^sending "([^"]*)" to "([^"]*)"$/
* @param string $verb
* @param string $url
*/
public function sendingTo(string $verb, string $url): void {
$this->sendingToWith($verb, $url, null);
}
/**
* @When /^sending "([^"]*)" to "([^"]*)" with$/
* @param string $verb
* @param string $url
* @param array|null $body
* @param array $headers
*/
public function sendingToWith(string $verb, string $url, ?array $body = null, array $headers = []): void {
$fullUrl = $this->baseUrl . 'ocs/v2.php' . $url;
$client = new Client();
$options = [];
if ($this->currentUser === 'admin') {
$options['auth'] = [$this->currentUser, 'admin'];
} else {
$options['auth'] = [$this->currentUser, '123456'];
}
if (is_array($body)) {
$options[RequestOptions::JSON] = $body;
}
$options['headers'] = array_merge($headers, [
'OCS-APIREQUEST' => 'true',
'Accept' => 'application/json',
]);
try {
$this->response = $client->{$verb}($fullUrl, $options);
} catch (ClientException $ex) {
$this->response = $ex->getResponse();
} catch (ServerException $ex) {
$this->response = $ex->getResponse();
}
}
/**
* @param ResponseInterface $response
* @param int $statusCode
*/
protected function assertStatusCode(ResponseInterface $response, int $statusCode): void {
Assert::assertEquals($statusCode, $response->getStatusCode());
}
}