Skip to content

Commit 7546e47

Browse files
saw-jannirajacharya2
authored andcommitted
test: allow items check for multiple array items
1 parent 6c00b5d commit 7546e47

File tree

5 files changed

+44
-68
lines changed

5 files changed

+44
-68
lines changed

.drone.star

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ config = {
247247
"apiAuthApp",
248248
],
249249
"skip": False,
250+
"withRemotePhp": [True],
250251
"extraServerEnvironment": {
251252
"OCIS_ADD_RUN_SERVICES": "auth-app",
252253
"PROXY_ENABLE_APP_AUTH": True,

tests/acceptance/TestHelpers/AuthAppHelper.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,10 @@
2828
* A helper class for managing Auth App API requests
2929
*/
3030
class AuthAppHelper {
31-
3231
/**
3332
* @return string
3433
*/
35-
public static function getAuthAppEndpoint():string {
34+
public static function getAuthAppEndpoint(): string {
3635
return "/auth-app/tokens";
3736
}
3837

@@ -43,7 +42,7 @@ public static function getAuthAppEndpoint():string {
4342
*
4443
* @return ResponseInterface
4544
*/
46-
public static function listAllAppAuthToken(string $baseUrl, string $user, string $password) : ResponseInterface {
45+
public static function listAllAppAuthTokensForUser(string $baseUrl, string $user, string $password): ResponseInterface {
4746
$url = $baseUrl . self::getAuthAppEndpoint();
4847
return HttpRequestHelper::sendRequest(
4948
$url,
@@ -62,7 +61,7 @@ public static function listAllAppAuthToken(string $baseUrl, string $user, string
6261
*
6362
* @return ResponseInterface
6463
*/
65-
public static function createAppAuthToken(string $baseUrl, string $user, string $password, string $expiration) : ResponseInterface {
64+
public static function createAppAuthToken(string $baseUrl, string $user, string $password, string $expiration): ResponseInterface {
6665
$url = $baseUrl . self::getAuthAppEndpoint() . "?expiry=$expiration";
6766
return HttpRequestHelper::sendRequest(
6867
$url,
@@ -81,7 +80,7 @@ public static function createAppAuthToken(string $baseUrl, string $user, string
8180
*
8281
* @return ResponseInterface
8382
*/
84-
public static function deleteAppAuthToken(string $baseUrl, string $user, string $password, string $token) : ResponseInterface {
83+
public static function deleteAppAuthToken(string $baseUrl, string $user, string $password, string $token): ResponseInterface {
8584
$url = $baseUrl . self::getAuthAppEndpoint() . "?token=$token";
8685
return HttpRequestHelper::sendRequest(
8786
$url,

tests/acceptance/bootstrap/AuthAppContext.php

Lines changed: 17 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
*/
3434
class AuthAppContext implements Context {
3535
private FeatureContext $featureContext;
36-
private array $allCreatedTokens = [];
3736

3837
/**
3938
* @BeforeScenario
@@ -50,89 +49,56 @@ public function before(BeforeScenarioScope $scope): void {
5049
}
5150

5251
/**
53-
* @When the administrator creates app token with expiration time :expiration using the API
52+
* @When user :user creates app token with expiration time :expiration using the auth-app API
5453
*
54+
* @param string $user
5555
* @param string $expiration
5656
*
5757
* @return void
5858
*/
59-
public function theAdministratorCreatesAppTokenForUserWithExpirationTimeUsingTheApi(string $expiration): void {
59+
public function userCreatesAppTokenWithExpirationTimeUsingTheAuthAppApi(string $user, string $expiration): void {
6060
$this->featureContext->setResponse(
6161
AuthAppHelper::createAppAuthToken(
6262
$this->featureContext->getBaseUrl(),
63-
$this->featureContext->getAdminUsername(),
64-
$this->featureContext->getAdminPassword(),
63+
$this->featureContext->getActualUsername($user),
64+
$this->featureContext->getPasswordForUser($user),
6565
$expiration,
6666
)
6767
);
6868
}
6969

7070
/**
71-
* @Given the administrator has created app token with expiration time :expiration using the API
71+
* @Given user :user has created app token with expiration time :expiration
7272
*
73+
* @param string $user
7374
* @param string $expiration
7475
*
7576
* @return void
7677
*/
77-
public function theAdministratorHasCreatedAppTokenWithExpirationTimeUsingTheApi(string $expiration): void {
78+
public function userHasCreatedAppTokenWithExpirationTime(string $user, string $expiration): void {
7879
$response = AuthAppHelper::createAppAuthToken(
7980
$this->featureContext->getBaseUrl(),
80-
$this->featureContext->getAdminUsername(),
81-
$this->featureContext->getAdminPassword(),
81+
$this->featureContext->getActualUsername($user),
82+
$this->featureContext->getPasswordForUser($user),
8283
$expiration,
8384
);
8485
$this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response);
8586
}
8687

8788
/**
88-
* @When admin lists all created tokens
89+
* @When user :user lists all created tokens using the auth-app API
90+
*
91+
* @param string $user
8992
*
9093
* @return void
9194
*/
92-
public function adminListsAllCreatedTokens(): void {
95+
public function userListsAllCreatedTokensUsingTheAuthAppApi(string $user): void {
9396
$this->featureContext->setResponse(
94-
AuthAppHelper::listAllAppAuthToken(
97+
AuthAppHelper::listAllAppAuthTokensForUser(
9598
$this->featureContext->getBaseUrl(),
96-
$this->featureContext->getAdminUsername(),
97-
$this->featureContext->getAdminPassword(),
99+
$this->featureContext->getActualUsername($user),
100+
$this->featureContext->getPasswordForUser($user),
98101
)
99102
);
100103
}
101-
102-
/**
103-
* @return void
104-
*/
105-
public function deleteAllToken() : void {
106-
$response = AuthAppHelper::listAllAppAuthToken(
107-
$this->featureContext->getBaseUrl(),
108-
$this->featureContext->getAdminUsername(),
109-
$this->featureContext->getAdminPassword(),
110-
);
111-
$this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response);
112-
$rawBody = $response->getBody()->getContents();
113-
$tokens = json_decode($rawBody);
114-
foreach ($tokens as $token) {
115-
$this->featureContext->theHTTPStatusCodeShouldBe(
116-
200,
117-
"",
118-
AuthAppHelper::deleteAppAuthToken(
119-
$this->featureContext->getBaseUrl(),
120-
$this->featureContext->getAdminUsername(),
121-
$this->featureContext->getAdminPassword(),
122-
$token->token
123-
)
124-
);
125-
}
126-
}
127-
128-
/**
129-
* @AfterScenario
130-
*
131-
* @return void
132-
*
133-
* @throws Exception|GuzzleException
134-
*/
135-
public function cleanDataAfterTests(): void {
136-
$this->deleteAllToken();
137-
}
138104
}

tests/acceptance/bootstrap/FeatureContext.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,9 +1132,11 @@ private function validateSchemaArray(JsonSchema $schemaObj): void {
11321132
Assert::fail("'$validator' should be an object not an array");
11331133
}
11341134
Assert::assertFalse($value->allOf || $value->anyOf, "'allOf' and 'anyOf' are not allowed in array");
1135-
Assert::assertNotNull($value->oneOf, "'oneOf' is required to assert more than one elements");
1136-
Assert::assertTrue(\is_array($value->oneOf), "'oneOf' should be an array");
1137-
Assert::assertEquals($schemaObj->maxItems, \count($value->oneOf), "Expected " . $schemaObj->maxItems . " 'oneOf' items but got " . \count($value->oneOf));
1135+
if ($value->oneOf) {
1136+
Assert::assertNotNull($value->oneOf, "'oneOf' is required to assert more than one elements");
1137+
Assert::assertTrue(\is_array($value->oneOf), "'oneOf' should be an array");
1138+
Assert::assertEquals($schemaObj->maxItems, \count($value->oneOf), "Expected " . $schemaObj->maxItems . " 'oneOf' items but got " . \count($value->oneOf));
1139+
}
11381140
}
11391141
Assert::assertTrue(\is_object($value), "'$validator' should be an object when expecting 1 element");
11401142
break;
@@ -1226,7 +1228,7 @@ public function throwJsonSchemaException(JsonSchemaException $e): void {
12261228
$errors = $this->getJsonSchemaErrors($e);
12271229
$messages = ["JSON Schema validation failed:"];
12281230

1229-
$previousPointer = '';
1231+
$previousPointer = null;
12301232
$errorCount = 0;
12311233
foreach ($errors as $error) {
12321234
$expected = $error->constraint;
@@ -1236,6 +1238,9 @@ public function throwJsonSchemaException(JsonSchemaException $e): void {
12361238
$dataPointer = \str_replace("/", ".", \trim($error->getDataPointer(), "/"));
12371239

12381240
$pointer = \str_contains($schemaPointer, "additionalProperties") ? $dataPointer : $schemaPointer;
1241+
if ($pointer === '') {
1242+
$pointer = "{root}";
1243+
}
12391244
if ($pointer === $previousPointer) {
12401245
continue;
12411246
}

tests/acceptance/features/apiAuthApp/token.feature

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
Feature: create auth token
2-
As a admin
2+
As a user
33
I want to create App Tokens
44
So that I can use 3rd party apps
55

6+
Background:
7+
Given user "Alice" has been created with default attributes
68

7-
Scenario: admin creates app token
8-
When the administrator creates app token with expiration time "72h" using the API
9+
10+
Scenario: user creates app token
11+
When user "Alice" creates app token with expiration time "72h" using the auth-app API
912
Then the HTTP status code should be "200"
1013
And the JSON data of the response should match
1114
"""
@@ -30,16 +33,18 @@ Feature: create auth token
3033
"""
3134

3235

33-
Scenario: admin lists app token
34-
Given the administrator has created app token with expiration time "72h" using the API
35-
When admin lists all created tokens
36+
Scenario: user lists app tokens
37+
Given user "Alice" has created app token with expiration time "72h"
38+
And user "Alice" has created app token with expiration time "2h"
39+
When user "Alice" lists all created tokens using the auth-app API
3640
Then the HTTP status code should be "200"
3741
And the JSON data of the response should match
3842
"""
3943
{
4044
"type": "array",
41-
"minItems": 1,
42-
"maxItems": 1,
45+
"minItems": 2,
46+
"maxItems": 2,
47+
"uniqueItems": true,
4348
"items": {
4449
"type": "object",
4550
"required": [

0 commit comments

Comments
 (0)