-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add test for creating auth tocken for an app using api
- Loading branch information
1 parent
6c3684a
commit 0aca4b3
Showing
3 changed files
with
173 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} | ||
} | ||
} | ||
""" |