-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathAppConfigurationContext.php
More file actions
589 lines (551 loc) · 16.5 KB
/
AppConfigurationContext.php
File metadata and controls
589 lines (551 loc) · 16.5 KB
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<?php declare(strict_types=1);
/**
* ownCloud
*
* @author Joas Schilling <coding@schilljs.com>
* @author Sergio Bertolin <sbertolin@owncloud.com>
* @author Phillip Davis <phil@jankaritech.com>
* @copyright Copyright (c) 2018, ownCloud GmbH
*
* 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 <http://www.gnu.org/licenses/>
*
*/
namespace Tests\Acceptance;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use GuzzleHttp\Exception\GuzzleException;
use PHPUnit\Framework\Assert;
use TestHelpers\AppConfigHelper;
use TestHelpers\OcsApiHelper;
use Behat\Gherkin\Node\TableNode;
use Behat\Behat\Context\Context;
/**
* AppConfiguration trait
*/
class AppConfigurationContext implements Context {
private FeatureContext $featureContext;
/**
* @When /^the administrator sets parameter "([^"]*)" of app "([^"]*)" to ((?:'[^']*')|(?:"[^"]*"))$/
*
* @param string $parameter
* @param string $app
* @param string $value
*
* @return void
* @throws Exception
*/
public function adminSetsServerParameterToUsingAPI(
string $parameter,
string $app,
string $value
):void {
// The capturing group of the regex always includes the quotes at each
// end of the captured string, so trim them.
$value = \trim($value, $value[0]);
$this->modifyAppConfig($app, $parameter, $value);
}
/**
* @Given /^parameter "([^"]*)" of app "([^"]*)" has been set to ((?:'[^']*')|(?:"[^"]*"))$/
*
* @param string $parameter
* @param string $app
* @param string $value
*
* @return void
* @throws Exception
*/
public function serverParameterHasBeenSetTo(string $parameter, string $app, string $value):void {
// The capturing group of the regex always includes the quotes at each
// end of the captured string, so trim them.
$value = \trim($value, $value[0]);
$this->modifyAppConfig($app, $parameter, $value);
$this->featureContext->clearStatusCodeArrays();
}
/**
* @Then the capabilities setting of :capabilitiesApp path :capabilitiesPath should be :expectedValue
* @Given the capabilities setting of :capabilitiesApp path :capabilitiesPath has been confirmed to be :expectedValue
*
* @param string $capabilitiesApp the "app" name in the capabilities response
* @param string $capabilitiesPath the path to the element
* @param string $expectedValue
*
* @return void
* @throws Exception
*/
public function theCapabilitiesSettingOfAppParameterShouldBe(
string $capabilitiesApp,
string $capabilitiesPath,
string $expectedValue
):void {
$this->theAdministratorGetsCapabilitiesCheckResponse();
$actualValue = $this->getAppParameter($capabilitiesApp, $capabilitiesPath);
Assert::assertEquals(
$expectedValue,
$actualValue,
__METHOD__
. " $capabilitiesApp path $capabilitiesPath should be $expectedValue but is $actualValue"
);
}
/**
* @param string $capabilitiesApp the "app" name in the capabilities response
* @param string $capabilitiesPath the path to the element
*
* @return string
* @throws Exception
*/
public function getAppParameter(string $capabilitiesApp, string $capabilitiesPath):string {
return $this->getParameterValueFromXml(
$this->getCapabilitiesXml(__METHOD__),
$capabilitiesApp,
$capabilitiesPath
);
}
/**
* @When user :username retrieves the capabilities using the capabilities API
*
* @param string $username
*
* @return void
* @throws GuzzleException
* @throws JsonException
*/
public function userRetrievesCapabilities(string $username):void {
$user = $this->featureContext->getActualUsername($username);
$this->userGetsCapabilities($user, true);
}
/**
*
* @param string $username
* @param boolean $formatJson // this parameter if true formats the response in json
*
* @return void
* @throws GuzzleException
* @throws JsonException
*/
public function userGetsCapabilities(string $username, ?bool $formatJson = false):void {
$user = $this->featureContext->getActualUsername($username);
$password = $this->featureContext->getPasswordForUser($user);
$this->featureContext->setResponse(
OcsApiHelper::sendRequest(
$this->featureContext->getBaseUrl(),
$user,
$password,
'GET',
($formatJson) ? '/cloud/capabilities?format=json' : '/cloud/capabilities',
$this->featureContext->getStepLineRef(),
[],
$this->featureContext->getOcsApiVersion()
)
);
}
/**
* @Given user :username has retrieved the capabilities
*
* @param string $username
*
* @return void
* @throws Exception
*/
public function userGetsCapabilitiesCheckResponse(string $username):void {
$this->userGetsCapabilities($username);
$statusCode = $this->featureContext->getResponse()->getStatusCode();
if ($statusCode !== 200) {
throw new \Exception(
__METHOD__
. " user $username returned unexpected status $statusCode"
);
}
}
/**
* @When the user retrieves the capabilities using the capabilities API
*
* @return void
*/
public function theUserGetsCapabilities():void {
$this->userGetsCapabilities($this->featureContext->getCurrentUser());
}
/**
* @Given the user has retrieved the capabilities
*
* @return void
* @throws Exception
*/
public function theUserGetsCapabilitiesCheckResponse():void {
$this->userGetsCapabilitiesCheckResponse($this->featureContext->getCurrentUser());
}
/**
* @return string
* @throws Exception
*/
public function getAdminUsernameForCapabilitiesCheck():string {
return $this->featureContext->getAdminUsername();
}
/**
* @When the administrator retrieves the capabilities using the capabilities API
*
* @return void
*/
public function theAdministratorGetsCapabilities():void {
$user = $this->getAdminUsernameForCapabilitiesCheck();
$this->userGetsCapabilities($user, true);
}
/**
* @Given the administrator has retrieved the capabilities
*
* @return void
* @throws Exception
*/
public function theAdministratorGetsCapabilitiesCheckResponse():void {
$this->userGetsCapabilitiesCheckResponse($this->getAdminUsernameForCapabilitiesCheck());
}
/**
* @param string $exceptionText text to put at the front of exception messages
*
* @return SimpleXMLElement latest retrieved capabilities in XML format
* @throws Exception
*/
public function getCapabilitiesXml(string $exceptionText = ''): SimpleXMLElement {
if ($exceptionText === '') {
$exceptionText = __METHOD__;
}
return $this->featureContext->getResponseXml(null, $exceptionText)->data->capabilities;
}
/**
* @param string $exceptionText text to put at the front of exception messages
*
* @return SimpleXMLElement latest retrieved version data in XML format
* @throws Exception
*/
public function getVersionXml(string $exceptionText = ''): SimpleXMLElement {
if ($exceptionText === '') {
$exceptionText = __METHOD__;
}
return $this->featureContext->getResponseXml(null, $exceptionText)->data->version;
}
/**
* @param SimpleXMLElement $xml of the capabilities
* @param string $capabilitiesApp the "app" name in the capabilities response
* @param string $capabilitiesPath the path to the element
*
* @return string
*/
public function getParameterValueFromXml(
SimpleXMLElement $xml,
string $capabilitiesApp,
string $capabilitiesPath
):string {
$path_to_element = \explode('@@@', $capabilitiesPath);
$answeredValue = $xml->{$capabilitiesApp};
foreach ($path_to_element as $element) {
$nameIndexParts = \explode('[', $element);
if (isset($nameIndexParts[1])) {
// This part of the path should be something like "some_element[1]"
// Separately extract the name and the index
$name = $nameIndexParts[0];
$index = (int) \explode(']', $nameIndexParts[1])[0];
// and use those to construct the reference into the next XML level
$answeredValue = $answeredValue->{$name}[$index];
} else {
if ($element !== "") {
$answeredValue = $answeredValue->{$element};
}
}
}
return (string) $answeredValue;
}
/**
* @param SimpleXMLElement $xml of the capabilities
* @param string $capabilitiesApp the "app" name in the capabilities response
* @param string $capabilitiesPath the path to the element
*
* @return boolean
*/
public function parameterValueExistsInXml(
SimpleXMLElement $xml,
string $capabilitiesApp,
string $capabilitiesPath
):bool {
$path_to_element = \explode('@@@', $capabilitiesPath);
$answeredValue = $xml->{$capabilitiesApp};
foreach ($path_to_element as $element) {
$nameIndexParts = \explode('[', $element);
if (isset($nameIndexParts[1])) {
// This part of the path should be something like "some_element[1]"
// Separately extract the name and the index
$name = $nameIndexParts[0];
$index = (int) \explode(']', $nameIndexParts[1])[0];
// and use those to construct the reference into the next XML level
if (isset($answeredValue->{$name}[$index])) {
$answeredValue = $answeredValue->{$name}[$index];
} else {
// The path ends at this level
return false;
}
} else {
if (isset($answeredValue->{$element})) {
$answeredValue = $answeredValue->{$element};
} else {
// The path ends at this level
return false;
}
}
}
return true;
}
/**
* @param string $app
* @param string $parameter
* @param string $value
*
* @return void
* @throws Exception
*/
public function modifyAppConfig(string $app, string $parameter, string $value):void {
AppConfigHelper::modifyAppConfig(
$this->featureContext->getBaseUrl(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$app,
$parameter,
$value,
$this->featureContext->getStepLineRef(),
$this->featureContext->getOcsApiVersion()
);
}
/**
* @param array $appParameterValues
*
* @return void
* @throws Exception
*/
public function modifyAppConfigs(array $appParameterValues):void {
AppConfigHelper::modifyAppConfigs(
$this->featureContext->getBaseUrl(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$appParameterValues,
$this->featureContext->getStepLineRef(),
$this->featureContext->getOcsApiVersion()
);
}
/**
* @When the administrator adds url :url as trusted server using the testing API
*
* @param string $url
*
* @return void
* @throws GuzzleException
*/
public function theAdministratorAddsUrlAsTrustedServerUsingTheTestingApi(string $url):void {
$adminUser = $this->featureContext->getAdminUsername();
$response = OcsApiHelper::sendRequest(
$this->featureContext->getBaseUrl(),
$adminUser,
$this->featureContext->getAdminPassword(),
'POST',
"/apps/testing/api/v1/trustedservers",
$this->featureContext->getStepLineRef(),
['url' => $this->featureContext->substituteInLineCodes($url)]
);
$this->featureContext->setResponse($response);
$this->featureContext->pushToLastStatusCodesArrays();
}
/**
* Return text that contains the details of the URL, including any differences due to inline codes
*
* @param string $url
*
* @return string
*/
private function getUrlStringForMessage(string $url):string {
$text = $url;
$expectedUrl = $this->featureContext->substituteInLineCodes($url);
if ($expectedUrl !== $url) {
$text .= " ($expectedUrl)";
}
return $text;
}
/**
* @param string $url
*
* @return string
*/
private function getNotTrustedServerMessage(string $url):string {
return
"URL "
. $this->getUrlStringForMessage($url)
. " is not a trusted server but should be";
}
/**
* @Then url :url should be a trusted server
*
* @param string $url
*
* @return void
* @throws Exception
*/
public function urlShouldBeATrustedServer(string $url):void {
$trustedServers = $this->featureContext->getTrustedServers();
foreach ($trustedServers as $server => $id) {
if ($server === $this->featureContext->substituteInLineCodes($url)) {
return;
}
}
Assert::fail($this->getNotTrustedServerMessage($url));
}
/**
* @Then the trusted server list should include these urls:
*
* @param TableNode $table
*
* @return void
* @throws Exception
*/
public function theTrustedServerListShouldIncludeTheseUrls(TableNode $table):void {
$trustedServers = $this->featureContext->getTrustedServers();
$expected = $table->getColumnsHash();
foreach ($expected as $server) {
$found = false;
foreach ($trustedServers as $url => $id) {
if ($url === $this->featureContext->substituteInLineCodes($server['url'])) {
$found = true;
break;
}
}
if (!$found) {
Assert::fail($this->getNotTrustedServerMessage($server['url']));
}
}
}
/**
* @Given the administrator has added url :url as trusted server
*
* @param string $url
*
* @return void
* @throws Exception
* @throws GuzzleException
*/
public function theAdministratorHasAddedUrlAsTrustedServer(string $url):void {
$this->theAdministratorAddsUrlAsTrustedServerUsingTheTestingApi($url);
$status = $this->featureContext->getResponse()->getStatusCode();
if ($status !== 201) {
throw new \Exception(
__METHOD__ .
" Could not add trusted server " . $this->getUrlStringForMessage($url)
. ". The request failed with status $status"
);
}
}
/**
* @When the administrator deletes url :url from trusted servers using the testing API
*
* @param string $url
*
* @return void
* @throws GuzzleException
*/
public function theAdministratorDeletesUrlFromTrustedServersUsingTheTestingApi(string $url):void {
$adminUser = $this->featureContext->getAdminUsername();
$response = OcsApiHelper::sendRequest(
$this->featureContext->getBaseUrl(),
$adminUser,
$this->featureContext->getAdminPassword(),
'DELETE',
"/apps/testing/api/v1/trustedservers",
$this->featureContext->getStepLineRef(),
['url' => $this->featureContext->substituteInLineCodes($url)]
);
$this->featureContext->setResponse($response);
}
/**
* @Then url :url should not be a trusted server
*
* @param string $url
*
* @return void
* @throws Exception
*/
public function urlShouldNotBeATrustedServer(string $url):void {
$trustedServers = $this->featureContext->getTrustedServers();
foreach ($trustedServers as $server => $id) {
if ($server === $this->featureContext->substituteInLineCodes($url)) {
Assert::fail(
"URL " . $this->getUrlStringForMessage($url)
. " is a trusted server but is not expected to be"
);
}
}
}
/**
* @When the administrator deletes all trusted servers using the testing API
*
* @return void
* @throws GuzzleException
*/
public function theAdministratorDeletesAllTrustedServersUsingTheTestingApi():void {
$adminUser = $this->featureContext->getAdminUsername();
$response = OcsApiHelper::sendRequest(
$this->featureContext->getBaseUrl(),
$adminUser,
$this->featureContext->getAdminPassword(),
'DELETE',
"/apps/testing/api/v1/trustedservers/all",
$this->featureContext->getStepLineRef()
);
$this->featureContext->setResponse($response);
}
/**
* @Given the trusted server list is cleared
*
* @return void
* @throws Exception
*/
public function theTrustedServerListIsCleared():void {
$this->theAdministratorDeletesAllTrustedServersUsingTheTestingApi();
$statusCode = $this->featureContext->getResponse()->getStatusCode();
if ($statusCode !== 204) {
$contents = $this->featureContext->getResponse()->getBody()->getContents();
throw new \Exception(
__METHOD__
. " Failed to clear all trusted servers" . $contents
);
}
}
/**
* @Then the trusted server list should be empty
*
* @return void
* @throws Exception
*/
public function theTrustedServerListShouldBeEmpty():void {
$trustedServers = $this->featureContext->getTrustedServers();
Assert::assertEmpty(
$trustedServers,
__METHOD__ . " Trusted server list is not empty"
);
}
/**
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return void
*/
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');
}
}