-
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
a265fcb
commit 46eab2e
Showing
5 changed files
with
316 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
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,94 @@ | ||
<?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/> | ||
* | ||
*/ | ||
|
||
namespace TestHelpers; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
/** | ||
* A helper class for managing Auth App API requests | ||
*/ | ||
class AuthAppHelper { | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public static function getAuthAppEndpoint() { | ||
return "/auth-app/tokens"; | ||
} | ||
|
||
/** | ||
* @param string $baseUrl | ||
* @param string $user | ||
* @param string $password | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public static function listAllAppAuthToken(string $baseUrl, string $user, string $password) : ResponseInterface { | ||
$url = $baseUrl . self::getAuthAppEndpoint(); | ||
return HttpRequestHelper::sendRequest( | ||
$url, | ||
null, | ||
"GET", | ||
$user, | ||
$password, | ||
); | ||
} | ||
|
||
/** | ||
* @param string $baseUrl | ||
* @param string $user | ||
* @param string $password | ||
* @param string $expiration | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public static function createAppAuthToken(string $baseUrl, string $user, string $password, string $expiration) : ResponseInterface { | ||
$url = $baseUrl . self::getAuthAppEndpoint() . "?expiry=$expiration"; | ||
return HttpRequestHelper::sendRequest( | ||
$url, | ||
null, | ||
"POST", | ||
$user, | ||
$password, | ||
); | ||
} | ||
|
||
/** | ||
* @param string $baseUrl | ||
* @param string $user | ||
* @param string $password | ||
* @param string $token | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public static function deleteAppAuthToken(string $baseUrl, string $user, string $password, string $token) : ResponseInterface { | ||
$url = $baseUrl . self::getAuthAppEndpoint() . "?token=$token"; | ||
return HttpRequestHelper::sendRequest( | ||
$url, | ||
null, | ||
"DELETE", | ||
$user, | ||
$password, | ||
); | ||
} | ||
} |
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,133 @@ | ||
<?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 TestHelpers\BehatHelper; | ||
use GuzzleHttp\Exception\GuzzleException; | ||
use TestHelpers\AuthAppHelper; | ||
|
||
require_once 'bootstrap.php'; | ||
|
||
/** | ||
* AuthApp context | ||
*/ | ||
class AuthAppContext implements Context { | ||
private FeatureContext $featureContext; | ||
private array $allCreatedTokens = []; | ||
|
||
/** | ||
* @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 | ||
* | ||
* @param string $expiration | ||
* | ||
* @return void | ||
*/ | ||
public function theAdministratorCreatesAppTokenForUserWithExpirationTimeUsingTheApi(string $expiration): void { | ||
$this->featureContext->setResponse( | ||
AuthAppHelper::createAppAuthToken( | ||
$this->featureContext->getBaseUrl(), | ||
$this->featureContext->getAdminUsername(), | ||
$this->featureContext->getAdminPassword(), | ||
$expiration, | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @Given the administrator has created app token with expiration time :expiration using the API | ||
* | ||
* @param string $expiration | ||
* | ||
* @return void | ||
*/ | ||
public function theAdministratorHasCreatedAppTokenWithExpirationTimeUsingTheApi(string $expiration): void { | ||
$response = AuthAppHelper::createAppAuthToken( | ||
$this->featureContext->getBaseUrl(), | ||
$this->featureContext->getAdminUsername(), | ||
$this->featureContext->getAdminPassword(), | ||
$expiration, | ||
); | ||
$this->featureContext->theHTTPStatusCodeShouldBe(200, "", $response); | ||
} | ||
|
||
/** | ||
* @When admin lists all created tokens | ||
* | ||
* @return void | ||
*/ | ||
public function adminListsAllCreatedTokens(): void { | ||
$this->featureContext->setResponse( | ||
AuthAppHelper::listAllAppAuthToken( | ||
$this->featureContext->getBaseUrl(), | ||
$this->featureContext->getAdminUsername(), | ||
$this->featureContext->getAdminPassword(), | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @return void | ||
*/ | ||
public function deleteAllToken() : void { | ||
$response = AuthAppHelper::listAllAppAuthToken( | ||
$this->featureContext->getBaseUrl(), | ||
$this->featureContext->getAdminUsername(), | ||
$this->featureContext->getAdminPassword(), | ||
); | ||
$rawBody = $response->getBody()->getContents(); | ||
$tokens = json_decode($rawBody); | ||
foreach ($tokens as $token) { | ||
AuthAppHelper::deleteAppAuthToken( | ||
$this->featureContext->getBaseUrl(), | ||
$this->featureContext->getAdminUsername(), | ||
$this->featureContext->getAdminPassword(), | ||
$token->token | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @AfterScenario | ||
* | ||
* @return void | ||
* | ||
* @throws Exception|GuzzleException | ||
*/ | ||
public function cleanDataAfterTests(): void { | ||
$this->deleteAllToken(); | ||
} | ||
} |
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,70 @@ | ||
@env-config | ||
Feature: create auth token | ||
As a 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 and without skeleton files | ||
|
||
|
||
Scenario: admin creates app token | ||
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": { | ||
"token": { | ||
"type": "string", | ||
"pattern": "^[a-zA-Z0-9]{16}$" | ||
}, | ||
"label": { | ||
"const": "Generated via API" | ||
} | ||
} | ||
} | ||
""" | ||
|
||
|
||
Scenario: admin lists app token | ||
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": { | ||
"token": { | ||
"type": "string", | ||
"pattern": "^\\$2a\\$11\\$[A-Za-z0-9./]{53}$" | ||
}, | ||
"label": { | ||
"const": "Generated via API" | ||
} | ||
} | ||
} | ||
} | ||
""" |