Skip to content

Commit

Permalink
adding test for creating auth token for an app using cli
Browse files Browse the repository at this point in the history
  • Loading branch information
nirajacharya2 committed Dec 17, 2024
1 parent 0832c9b commit f25c30b
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 19 deletions.
7 changes: 7 additions & 0 deletions tests/acceptance/TestHelpers/AuthAppHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,11 @@ public static function deleteAppAuthToken(string $baseUrl, string $user, string
$password,
);
}

/**
* @return string
*/
public static function getShortTokenRegex(): string {
return "[a-zA-Z0-9]{16}";
}
}
2 changes: 1 addition & 1 deletion tests/acceptance/bootstrap/AuthAppContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function userCreatesAppTokenWithExpirationTimeUsingTheAuthAppApi(string $
}

/**
* @Given user :user has created app token with expiration time :expiration
* @Given user :user has created app token with expiration time :expiration using the auth-app API
*
* @param string $user
* @param string $expiration
Expand Down
53 changes: 53 additions & 0 deletions tests/acceptance/bootstrap/CliContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,45 @@ public function theAdministratorChecksTheBackupConsistencyUsingTheCli():void {
];
$this->featureContext->setResponse(CliHelper::runCommand($body));
}

/**
* @When the administrator creates app token for user :user with expiration time :expirationTime using the auth-app CLI
*
* @param string $user
* @param string $expirationTime
*
* @return void
*/
public function theAdministratorCreatesAppTokenForUserWithExpirationTimeUsingTheAuthAppCLI(string $user, string $expirationTime): void {
$user = $this->featureContext->getActualUserName($user);
$command = "auth-app create --user-name=$user --expiration=$expirationTime";
$body = [
"command" => $command
];
$this->featureContext->setResponse(CliHelper::runCommand($body));
}

/**
* @Given user :user has created app token with expiration time :expirationTime using the auth-app CLI
*
* @param string $user
* @param string $expirationTime
*
* @return void
*/
public function userHasCreatedAppTokenWithExpirationTimeUsingTheAuthAppCLI(string $user, string $expirationTime): void {
$user = $this->featureContext->getActualUserName($user);
$command = "auth-app create --user-name=$user --expiration=$expirationTime";
$body = [
"command" => $command
];

$response = CliHelper::runCommand($body);
$this->featureContext->theHTTPStatusCodeShouldBe(200, '', $response);
$jsonResponse = $this->featureContext->getJsonDecodedResponse($response);
Assert::assertSame("OK", $jsonResponse["status"]);
Assert::assertSame(0, $jsonResponse["exitCode"], "Expected exit code to be 0, but got " . $jsonResponse["exitCode"]);
}

/**
* @When the administrator removes all the file versions using the CLI
Expand Down Expand Up @@ -237,6 +276,20 @@ public function theCommandOutputShouldContain(string $shouldOrNot, string $outpu
}
}

/**
* @Then the command output should be :output
*
* @param string $output
*
* @return void
*/
public function theCommandOutputShouldBe(string $output): void {
$response = $this->featureContext->getResponse();
$jsonResponse = $this->featureContext->getJsonDecodedResponse($response);
$output = $this->featureContext->substituteInLineCodes($output);
Assert::assertMatchesRegularExpression("/$output/", $jsonResponse["message"]);
}

/**
* @When the administrator lists all the upload sessions
* @When the administrator lists all the upload sessions with flag :flag
Expand Down
10 changes: 9 additions & 1 deletion tests/acceptance/bootstrap/FeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,15 @@ public function substituteInLineCodes(
"getXRequestIdRegex"
],
"parameter" => []
]
],
[
"code" => "%short_app_token_pattern%",
"function" => [
__NAMESPACE__ . '\TestHelpers\AuthAppHelper',
"getShortTokenRegex"
],
"parameter" => []
],
];
if ($user !== null) {
array_push(
Expand Down
1 change: 1 addition & 0 deletions tests/acceptance/config/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ default:
contexts:
- FeatureContext: *common_feature_context_params
- AuthAppContext:
- CliContext:

cliCommands:
paths:
Expand Down
56 changes: 39 additions & 17 deletions tests/acceptance/features/apiAuthApp/token.feature
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Feature: create auth token
"properties": {
"token": {
"type": "string",
"pattern": "^[a-zA-Z0-9]{16}$"
"pattern": "^%short_app_token_pattern%$"
},
"label": {
"const": "Generated via API"
Expand All @@ -34,8 +34,8 @@ Feature: create auth token


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"
Given user "Alice" has created app token with expiration time "72h" using the auth-app API
And user "Alice" has created app token with expiration time "72h" using the auth-app CLI
When user "Alice" lists all created tokens using the auth-app API
Then the HTTP status code should be "200"
And the JSON data of the response should match
Expand All @@ -46,22 +46,44 @@ Feature: create auth token
"maxItems": 2,
"uniqueItems": true,
"items": {
"type": "object",
"required": [
"token",
"expiration_date",
"created_date",
"label"
],
"properties": {
"token": {
"type": "string",
"pattern": "^\\$2a\\$11\\$[A-Za-z0-9./]{53}$"
"oneOf": [
{
"type": "object",
"required": [
"token",
"expiration_date",
"created_date",
"label"
],
"properties": {
"token": {
"type": "string",
"pattern": "^\\$2a\\$11\\$[A-Za-z0-9./]{53}$"
},
"label": {
"const": "Generated via API"
}
}
},
"label": {
"const": "Generated via API"
{
"type": "object",
"required": [
"token",
"expiration_date",
"created_date",
"label"
],
"properties": {
"token": {
"type": "string",
"pattern": "^\\$2a\\$11\\$[A-Za-z0-9./]{53}$"
},
"label": {
"const": "Generated via CLI"
}
}
}
}
]
}
}
"""
18 changes: 18 additions & 0 deletions tests/acceptance/features/cliCommands/authAppToken.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
@env-config
Feature: create auth token
As an admin
I want to create App Tokens
So that I can use 3rd party apps

Background:
Given the following configs have been set:
| config | value |
| OCIS_ADD_RUN_SERVICES | auth-app |
| PROXY_ENABLE_APP_AUTH | true |
And user "Alice" has been created with default attributes


Scenario: creates app token via CLI
When the administrator creates app token for user "Alice" with expiration time "72h" using the auth-app CLI
Then the command should be successful
And the command output should be "App token created for Alice\r\n\stoken: %short_app_token_pattern%"

0 comments on commit f25c30b

Please sign in to comment.