Skip to content

Commit

Permalink
test: allow items check for multiple array items
Browse files Browse the repository at this point in the history
  • Loading branch information
saw-jan authored and nirajacharya2 committed Dec 10, 2024
1 parent 6b1d859 commit a36ed90
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 68 deletions.
1 change: 1 addition & 0 deletions .drone.star
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ config = {
"apiAuthApp",
],
"skip": False,
"withRemotePhp": [True],
"extraServerEnvironment": {
"OCIS_ADD_RUN_SERVICES": "auth-app",
"PROXY_ENABLE_APP_AUTH": True,
Expand Down
9 changes: 4 additions & 5 deletions tests/acceptance/TestHelpers/AuthAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
* A helper class for managing Auth App API requests
*/
class AuthAppHelper {

/**
* @return string
*/
public static function getAuthAppEndpoint():string {
public static function getAuthAppEndpoint(): string {
return "/auth-app/tokens";
}

Expand All @@ -43,7 +42,7 @@ public static function getAuthAppEndpoint():string {
*
* @return ResponseInterface
*/
public static function listAllAppAuthToken(string $baseUrl, string $user, string $password) : ResponseInterface {
public static function listAllAppAuthTokensForUser(string $baseUrl, string $user, string $password): ResponseInterface {
$url = $baseUrl . self::getAuthAppEndpoint();
return HttpRequestHelper::sendRequest(
$url,
Expand All @@ -62,7 +61,7 @@ public static function listAllAppAuthToken(string $baseUrl, string $user, string
*
* @return ResponseInterface
*/
public static function createAppAuthToken(string $baseUrl, string $user, string $password, string $expiration) : ResponseInterface {
public static function createAppAuthToken(string $baseUrl, string $user, string $password, string $expiration): ResponseInterface {
$url = $baseUrl . self::getAuthAppEndpoint() . "?expiry=$expiration";
return HttpRequestHelper::sendRequest(
$url,
Expand All @@ -81,7 +80,7 @@ public static function createAppAuthToken(string $baseUrl, string $user, string
*
* @return ResponseInterface
*/
public static function deleteAppAuthToken(string $baseUrl, string $user, string $password, string $token) : ResponseInterface {
public static function deleteAppAuthToken(string $baseUrl, string $user, string $password, string $token): ResponseInterface {
$url = $baseUrl . self::getAuthAppEndpoint() . "?token=$token";
return HttpRequestHelper::sendRequest(
$url,
Expand Down
68 changes: 17 additions & 51 deletions tests/acceptance/bootstrap/AuthAppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
*/
class AuthAppContext implements Context {
private FeatureContext $featureContext;
private array $allCreatedTokens = [];

/**
* @BeforeScenario
Expand All @@ -50,89 +49,56 @@ public function before(BeforeScenarioScope $scope): void {
}

/**
* @When the administrator creates app token with expiration time :expiration using the API
* @When user :user creates app token with expiration time :expiration using the API
*
* @param string $user
* @param string $expiration
*
* @return void
*/
public function theAdministratorCreatesAppTokenForUserWithExpirationTimeUsingTheApi(string $expiration): void {
public function userCreatesAppTokenWithExpirationTimeUsingTheApi(string $user, string $expiration): void {
$this->featureContext->setResponse(
AuthAppHelper::createAppAuthToken(
$this->featureContext->getBaseUrl(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$this->featureContext->getActualUsername($user),
$this->featureContext->getPasswordForUser($user),
$expiration,
)
);
}

/**
* @Given the administrator has created app token with expiration time :expiration using the API
* @Given user :user has created app token with expiration time :expiration
*
* @param string $user
* @param string $expiration
*
* @return void
*/
public function theAdministratorHasCreatedAppTokenWithExpirationTimeUsingTheApi(string $expiration): void {
public function userHasCreatedAppTokenWithExpirationTime(string $user, string $expiration): void {
$response = AuthAppHelper::createAppAuthToken(
$this->featureContext->getBaseUrl(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$this->featureContext->getActualUsername($user),
$this->featureContext->getPasswordForUser($user),
$expiration,
);
$this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response);
}

/**
* @When admin lists all created tokens
* @When user :user lists all created tokens using auth API
*
* @param string $user
*
* @return void
*/
public function adminListsAllCreatedTokens(): void {
public function userListsAllCreatedTokensUsingAuthApi(string $user): void {
$this->featureContext->setResponse(
AuthAppHelper::listAllAppAuthToken(
AuthAppHelper::listAllAppAuthTokensForUser(
$this->featureContext->getBaseUrl(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$this->featureContext->getActualUsername($user),
$this->featureContext->getPasswordForUser($user),
)
);
}

/**
* @return void
*/
public function deleteAllToken() : void {
$response = AuthAppHelper::listAllAppAuthToken(
$this->featureContext->getBaseUrl(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
);
$this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response);
$rawBody = $response->getBody()->getContents();
$tokens = json_decode($rawBody);
foreach ($tokens as $token) {
$this->featureContext->theHTTPStatusCodeShouldBe(
200,
"",
AuthAppHelper::deleteAppAuthToken(
$this->featureContext->getBaseUrl(),
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
$token->token
)
);
}
}

/**
* @AfterScenario
*
* @return void
*
* @throws Exception|GuzzleException
*/
public function cleanDataAfterTests(): void {
$this->deleteAllToken();
}
}
13 changes: 9 additions & 4 deletions tests/acceptance/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -1132,9 +1132,11 @@ private function validateSchemaArray(JsonSchema $schemaObj): void {
Assert::fail("'$validator' should be an object not an array");
}
Assert::assertFalse($value->allOf || $value->anyOf, "'allOf' and 'anyOf' are not allowed in array");
Assert::assertNotNull($value->oneOf, "'oneOf' is required to assert more than one elements");
Assert::assertTrue(\is_array($value->oneOf), "'oneOf' should be an array");
Assert::assertEquals($schemaObj->maxItems, \count($value->oneOf), "Expected " . $schemaObj->maxItems . " 'oneOf' items but got " . \count($value->oneOf));
if ($value->oneOf) {
Assert::assertNotNull($value->oneOf, "'oneOf' is required to assert more than one elements");
Assert::assertTrue(\is_array($value->oneOf), "'oneOf' should be an array");
Assert::assertEquals($schemaObj->maxItems, \count($value->oneOf), "Expected " . $schemaObj->maxItems . " 'oneOf' items but got " . \count($value->oneOf));
}
}
Assert::assertTrue(\is_object($value), "'$validator' should be an object when expecting 1 element");
break;
Expand Down Expand Up @@ -1226,7 +1228,7 @@ public function throwJsonSchemaException(JsonSchemaException $e): void {
$errors = $this->getJsonSchemaErrors($e);
$messages = ["JSON Schema validation failed:"];

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

$pointer = \str_contains($schemaPointer, "additionalProperties") ? $dataPointer : $schemaPointer;
if ($pointer === '') {
$pointer = "{root}";
}
if ($pointer === $previousPointer) {
continue;
}
Expand Down
21 changes: 13 additions & 8 deletions tests/acceptance/features/apiAuthApp/token.feature
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
Feature: create auth token
As a admin
As a user
I want to create App Tokens
So that I can use 3rd party apps

Background:
Given user "Alice" has been created with default attributes

Scenario: admin creates app token
When the administrator creates app token with expiration time "72h" using the API

Scenario: user creates app token
When user "Alice" creates app token with expiration time "72h" using the API
Then the HTTP status code should be "200"
And the JSON data of the response should match
"""
Expand All @@ -30,16 +33,18 @@ Feature: create auth token
"""


Scenario: admin lists app token
Given the administrator has created app token with expiration time "72h" using the API
When admin lists all created tokens
Scenario: user lists app tokens
Given user "Alice" has created app token with expiration time "72h"
And user "Alice" has created app token with expiration time "2h"
When user "Alice" lists all created tokens using auth API
Then the HTTP status code should be "200"
And the JSON data of the response should match
"""
{
"type": "array",
"minItems": 1,
"maxItems": 1,
"minItems": 2,
"maxItems": 2,
"uniqueItems": true,
"items": {
"type": "object",
"required": [
Expand Down

0 comments on commit a36ed90

Please sign in to comment.