Skip to content

Commit

Permalink
add test for creating auth tocken for an app using api
Browse files Browse the repository at this point in the history
  • Loading branch information
nirajacharya2 committed Nov 26, 2024
1 parent 6c3684a commit 0aca4b3
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
104 changes: 104 additions & 0 deletions tests/acceptance/bootstrap/AuthAppTokenContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php declare(strict_types=1);
/**
* ownCloud
*
* @author Sajan Gurung <[email protected]>
* @copyright Copyright (c) 2024 Sajan Gurung [email protected]
*
* This code is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License,
* as published by the Free Software Foundation;
* either version 3 of the License, or any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>
*
*/

use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Psr\Http\Message\ResponseInterface;
use TestHelpers\HttpRequestHelper;
use TestHelpers\BehatHelper;

require_once 'bootstrap.php';

/**
* CLI context
*/
class AuthAppTokenContext implements Context {
private FeatureContext $featureContext;

/**
* @BeforeScenario
*
* @param BeforeScenarioScope $scope
*
* @return void
*/
public function before(BeforeScenarioScope $scope): void {
// Get the environment
$environment = $scope->getEnvironment();
// Get all the contexts you need in this context
$this->featureContext = BehatHelper::getContext($scope, $environment, 'FeatureContext');
}

/**
* @When the administrator creates app token with expiration time :expiration using the API
*/
public function theAdministratorCreatesAppTokenForUserWithExpirationTimeUsingTheApi($expiration): void
{
$this->featureContext->setResponse(
$this->createAppAuthToken($expiration)
);
}
function createAppAuthToken($expiration) : ResponseInterface {
$url = $this->featureContext->getBaseUrl() . "/auth-app/tokens?expiry=$expiration";
return HttpRequestHelper::sendRequest(
$url,
null,
"POST",
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
null
);
}

function listAppAuthToken() : ResponseInterface {
$url = $this->featureContext->getBaseUrl() . "/auth-app/tokens";
return HttpRequestHelper::sendRequest(
$url,
null,
"GET",
$this->featureContext->getAdminUsername(),
$this->featureContext->getAdminPassword(),
null
);
}

/**
* @Given the administrator has created app token with expiration time :expiration using the API
*/
public function theAdministratorHasCreatedAppTokenWithExpirationTimeUsingTheApi($expiration): void
{
$response = $this->createAppAuthToken($expiration);
$this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response);
}

/**
* @When admin lists all created tokens
*/
public function adminListsAllCreatedTokens(): void
{
$this->featureContext->setResponse(
$this->listAppAuthToken()
);
var_dump($this->featureContext->getResponse()->getBody()->getContents());
}

}
9 changes: 9 additions & 0 deletions tests/acceptance/config/behat.yml
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,15 @@ default:
- TagContext:
- TrashbinContext:
- SpacesTUSContext:
- AuthAppTokenContext:

apiAppAuth:
paths:
- "%paths.base%/../features/apiAppAuth"
context: *common_ldap_suite_context
contexts:
- FeatureContext: *common_feature_context_params
- AuthAppTokenContext:

coreApiMain:
paths:
Expand Down
60 changes: 60 additions & 0 deletions tests/acceptance/features/apiAppAuth/create.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
Feature: create auth token
As a user
I want to
So that I

As a developer
I want to be able to use the resource ID to download multiple items at once
So that I don't have to know the full path of the resource

Background:
Given user "Alice" has been created with default attributes and without skeleton files


Scenario: create
When the administrator 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
"""
{
"type": "object",
"required": [
"token",
"expiration_date",
"created_date",
"label"
],
"properties": {
"label": {
"const": "Generated via API"
}
}
}
"""

Scenario: list
Given the administrator has created app token with expiration time "72h" using the API
When admin lists all created tokens
Then the HTTP status code should be "200"
And the JSON data of the response should match
"""
{
"type": "array",
"minItems": 1,
"maxItems": 1,
"items": {
"type": "object",
"required": [
"token",
"expiration_date",
"created_date",
"label"
],
"properties": {
"label": {
"const": "Generated via API"
}
}
}
}
"""

0 comments on commit 0aca4b3

Please sign in to comment.