From 9ffb14ef3bec5957441fb0156bf9e2eb42070d7e Mon Sep 17 00:00:00 2001 From: Niraj Acharya Date: Wed, 26 Mar 2025 12:41:57 +0545 Subject: [PATCH] adding test for using auth-app token --- tests/acceptance/bootstrap/AuthAppContext.php | 38 +++++++- tests/acceptance/bootstrap/FeatureContext.php | 9 +- .../features/apiAuthApp/tokenUsage.feature | 88 +++++++++++++++++++ 3 files changed, 130 insertions(+), 5 deletions(-) create mode 100644 tests/acceptance/features/apiAuthApp/tokenUsage.feature diff --git a/tests/acceptance/bootstrap/AuthAppContext.php b/tests/acceptance/bootstrap/AuthAppContext.php index 8d0dfda1054..75f76ab28c1 100644 --- a/tests/acceptance/bootstrap/AuthAppContext.php +++ b/tests/acceptance/bootstrap/AuthAppContext.php @@ -33,7 +33,8 @@ require_once 'bootstrap.php'; */ class AuthAppContext implements Context { private FeatureContext $featureContext; - private string $lastCreatedToken; + private array $lastCreatedToken = []; + private bool $usingAuthAppToken = false; /** * @BeforeScenario @@ -49,6 +50,20 @@ class AuthAppContext implements Context { $this->featureContext = BehatHelper::getContext($scope, $environment, 'FeatureContext'); } + /** + * @return array + */ + public function getLastCreatedToken(): array { + return $this->lastCreatedToken; + } + + /** + * @return bool + */ + public function isUsingAuthAppToken(): bool { + return $this->usingAuthAppToken; + } + /** * @When /^user "([^"]*)" (?:creates|tries to create) auth-app token with expiration time "([^"]*)" using the auth-app API$/ * @@ -84,7 +99,10 @@ class AuthAppContext implements Context { ["expiry" => $expiration] ); $this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response); - $this->lastCreatedToken = json_decode($response->getBody()->getContents())->token; + $this->lastCreatedToken = [ + "user" => strtolower($user), + "token" => json_decode($response->getBody()->getContents())->token + ]; } /** @@ -133,7 +151,10 @@ class AuthAppContext implements Context { . "HTTP status code 200 is not the expected value " . $response->getStatusCode(), $response ); - $this->lastCreatedToken = json_decode($response->getBody()->getContents())->token; + $this->lastCreatedToken = [ + "user" => strtolower($impersonatedUser), + "token" => json_decode($response->getBody()->getContents())->token + ]; } /** @@ -256,9 +277,18 @@ class AuthAppContext implements Context { $baseUrl, $user, $password, - $this->lastCreatedToken, + $this->getLastCreatedToken()['token'], ); $this->featureContext->setResponse($deleteResponse); $this->featureContext->pushToLastHttpStatusCodesArray((string)$deleteResponse->getStatusCode()); } + + /** + * @Given using auth-app token + * + * @return void + */ + public function usingAuthAppToken(): void { + $this->usingAuthAppToken = true; + } } diff --git a/tests/acceptance/bootstrap/FeatureContext.php b/tests/acceptance/bootstrap/FeatureContext.php index fee66657d4d..68f85aba105 100644 --- a/tests/acceptance/bootstrap/FeatureContext.php +++ b/tests/acceptance/bootstrap/FeatureContext.php @@ -164,6 +164,7 @@ class FeatureContext extends BehatVariablesContext { public TUSContext $tusContext; public GraphContext $graphContext; public SpacesContext $spacesContext; + public AuthAppContext $authAppContext; public OcmContext $ocmContext; /** @@ -1784,7 +1785,12 @@ class FeatureContext extends BehatVariablesContext { public function getPasswordForUser(?string $userName): string { $userNameNormalized = $this->normalizeUsername($userName); $username = $this->getActualUsername($userNameNormalized); - if ($username === $this->getAdminUsername()) { + if ($this->authAppContext->isUsingAuthAppToken() + && $this->authAppContext->getLastCreatedToken() !== [] + && strtolower($userName) === $this->authAppContext->getLastCreatedToken()["user"] + ) { + return $this->authAppContext->getLastCreatedToken()["token"]; + } elseif ($username === $this->getAdminUsername()) { return $this->getAdminPassword(); } elseif (\array_key_exists($username, $this->createdUsers)) { return (string)$this->createdUsers[$username]['password']; @@ -2670,6 +2676,7 @@ class FeatureContext extends BehatVariablesContext { $this->ocmContext = BehatHelper::getContext($scope, $environment, 'OcmContext'); $this->graphContext = BehatHelper::getContext($scope, $environment, 'GraphContext'); $this->spacesContext = BehatHelper::getContext($scope, $environment, 'SpacesContext'); + $this->authAppContext = BehatHelper::getContext($scope, $environment, 'AuthAppContext'); $scenarioLine = $scope->getScenario()->getLine(); $featureFile = $scope->getFeature()->getFile(); diff --git a/tests/acceptance/features/apiAuthApp/tokenUsage.feature b/tests/acceptance/features/apiAuthApp/tokenUsage.feature new file mode 100644 index 00000000000..f9585f76f61 --- /dev/null +++ b/tests/acceptance/features/apiAuthApp/tokenUsage.feature @@ -0,0 +1,88 @@ +Feature: create auth-app token + As a user + I want to create auth-app Tokens + So that I can let 3rd party apps use my account + + Background: + Given user "Alice" has been created with default attributes + And using auth-app token + + + Scenario: admin creates user using auth-app token + Given user "Admin" has created auth-app token with expiration time "1h" using the auth-app API + When the user "Admin" creates a new user with the following attributes using the Graph API: + | userName | Brian | + | displayName | This is another Brian | + | email | brian@example.com | + | password | brian123 | + | accountEnabled | true | + Then the HTTP status code should be "201" + And the JSON response should contain space called "Alice Hansen" and match + """ + { + "type": "object", + "required": [ + "accountEnabled", + "displayName", + "id", + "mail", + "onPremisesSamAccountName", + "surname", + "userType" + ], + "properties": { + "accountEnabled": { "const": true }, + "displayName": { "const": "This is another Brian" }, + "id": { "pattern": "%user_id_pattern%" }, + "mail": { "const": "brian@example.com" }, + "onPremisesSamAccountName": { "const": "Brian" }, + "surname": { "const":"Brian" }, + "userType": { "const":"Member" } + } + } + """ + And user "Brian" should be able to upload file "filesForUpload/lorem.txt" to "lorem.txt" + + + Scenario: user lists their drives using auth-app token + Given user "Alice" has created auth-app token with expiration time "1h" using the auth-app API + When user "Alice" lists all available spaces via the Graph API + Then the HTTP status code should be "200" + And the JSON response should contain space called "Alice Hansen" and match + """ + { + "type": "object", + "required": [ + "driveType", + "driveAlias", + "name", + "id", + "quota", + "root", + "webUrl" + ], + "properties": { + "name": { "const": "Alice Hansen" }, + "driveType": { "const": "personal" }, + "driveAlias": { "const": "personal/alice" }, + "id": { "pattern": "%space_id_pattern%" }, + "quota": { + "type": "object", + "required": ["state"], + "properties": { + "state": { "const": "normal" } + } + }, + "root": { + "type": "object", + "required": ["webDavUrl"], + "properties": { + "webDavUrl": { "const": "%base_url%/dav/spaces/%space_id%" } + } + }, + "webUrl": { + "const": "%base_url%/f/%space_id%" + } + } + } + """ \ No newline at end of file