From 558839faa61620aa37de68b89fa81f37d93b146b Mon Sep 17 00:00:00 2001 From: Niraj Acharya Date: Tue, 26 Nov 2024 17:13:14 +0545 Subject: [PATCH] adding test for creating auth token for an app using cli --- tests/acceptance/bootstrap/AuthAppContext.php | 2 +- tests/acceptance/bootstrap/CliContext.php | 39 ++++++++++++ tests/acceptance/config/behat.yml | 1 + .../features/apiAuthApp/token.feature | 59 ++++++++++++------- .../features/cliCommands/authAppToken.feature | 16 +++++ 5 files changed, 96 insertions(+), 21 deletions(-) create mode 100644 tests/acceptance/features/cliCommands/authAppToken.feature diff --git a/tests/acceptance/bootstrap/AuthAppContext.php b/tests/acceptance/bootstrap/AuthAppContext.php index ad95dc02dbf..ce562b6c255 100644 --- a/tests/acceptance/bootstrap/AuthAppContext.php +++ b/tests/acceptance/bootstrap/AuthAppContext.php @@ -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 diff --git a/tests/acceptance/bootstrap/CliContext.php b/tests/acceptance/bootstrap/CliContext.php index 7e4fe4a4d70..64da475b466 100644 --- a/tests/acceptance/bootstrap/CliContext.php +++ b/tests/acceptance/bootstrap/CliContext.php @@ -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 diff --git a/tests/acceptance/config/behat.yml b/tests/acceptance/config/behat.yml index 04a57217c77..57948c74c69 100644 --- a/tests/acceptance/config/behat.yml +++ b/tests/acceptance/config/behat.yml @@ -431,6 +431,7 @@ default: contexts: - FeatureContext: *common_feature_context_params - AuthAppContext: + - CliContext: cliCommands: paths: diff --git a/tests/acceptance/features/apiAuthApp/token.feature b/tests/acceptance/features/apiAuthApp/token.feature index 53de35f2188..a4b3d92b661 100644 --- a/tests/acceptance/features/apiAuthApp/token.feature +++ b/tests/acceptance/features/apiAuthApp/token.feature @@ -1,13 +1,13 @@ -Feature: create auth token +Feature: create auth-app token As a user - I want to create App Tokens + I want to create auth-app Tokens So that I can use 3rd party apps Background: Given user "Alice" has been created with default attributes - Scenario: user creates app token + Scenario: user creates auth-app token When user "Alice" creates app token with expiration time "72h" using the auth-app API Then the HTTP status code should be "200" And the JSON data of the response should match @@ -22,7 +22,6 @@ Feature: create auth token ], "properties": { "token": { - "type": "string", "pattern": "^[a-zA-Z0-9]{16}$" }, "label": { @@ -34,8 +33,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 @@ -46,22 +45,42 @@ 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": { + "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": { + "pattern": "^\\$2a\\$11\\$[A-Za-z0-9./]{53}$" + }, + "label": { + "const": "Generated via CLI" + } + } } - } + ] } } """ diff --git a/tests/acceptance/features/cliCommands/authAppToken.feature b/tests/acceptance/features/cliCommands/authAppToken.feature new file mode 100644 index 00000000000..5e4c8037ab4 --- /dev/null +++ b/tests/acceptance/features/cliCommands/authAppToken.feature @@ -0,0 +1,16 @@ +@env-config +Feature: create auth-app token + As an admin + I want to create auth-app Tokens + So that I can use 3rd party apps + + + Scenario: creates auth-app token via CLI + 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 + 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 contain "App token created for Alice" \ No newline at end of file