2016-02-23 03:19:46 +05:30
|
|
|
<?php
|
2019-09-18 04:44:05 +05:30
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
namespace api\tests\functional\oauth;
|
2016-02-23 03:19:46 +05:30
|
|
|
|
2019-02-21 01:28:52 +05:30
|
|
|
use api\tests\functional\_steps\OauthSteps;
|
2016-02-23 03:19:46 +05:30
|
|
|
|
2022-12-10 08:26:19 +05:30
|
|
|
final class AccessTokenCest {
|
2016-02-23 03:19:46 +05:30
|
|
|
|
2022-12-10 08:26:19 +05:30
|
|
|
public function successfullyIssueTokenWithUrlEncodedBody(OauthSteps $I): void {
|
2019-09-18 04:44:05 +05:30
|
|
|
$I->wantTo('complete oauth flow and obtain access_token');
|
|
|
|
$authCode = $I->obtainAuthCode();
|
|
|
|
$I->sendPOST('/api/oauth2/v1/token', [
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'code' => $authCode,
|
|
|
|
'client_id' => 'ely',
|
|
|
|
'client_secret' => 'ZuM1vGchJz-9_UZ5HC3H3Z9Hg5PzdbkM',
|
|
|
|
'redirect_uri' => 'http://ely.by',
|
2016-02-23 03:19:46 +05:30
|
|
|
]);
|
|
|
|
$I->canSeeResponseCodeIs(200);
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'token_type' => 'Bearer',
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseJsonMatchesJsonPath('$.access_token');
|
2019-12-13 18:30:51 +05:30
|
|
|
$I->cantSeeResponseJsonMatchesJsonPath('$.expires_in');
|
2019-09-18 04:44:05 +05:30
|
|
|
$I->cantSeeResponseJsonMatchesJsonPath('$.refresh_token');
|
2016-02-23 03:19:46 +05:30
|
|
|
}
|
|
|
|
|
2022-12-10 08:26:19 +05:30
|
|
|
public function successfullyIssueOfflineTokenWithJsonEncodedBody(OauthSteps $I): void {
|
2019-09-18 04:44:05 +05:30
|
|
|
$I->wantTo('complete oauth flow with offline_access scope and obtain access_token and refresh_token');
|
|
|
|
$authCode = $I->obtainAuthCode(['offline_access']);
|
2022-12-10 08:26:19 +05:30
|
|
|
$I->haveHttpHeader('Content-Type', 'application/json');
|
2024-12-02 15:40:55 +05:30
|
|
|
// @phpstan-ignore argument.type (it does accept string an we need it to ensure, that JSON passes)
|
2022-12-10 08:26:19 +05:30
|
|
|
$I->sendPOST('/api/oauth2/v1/token', json_encode([
|
2019-09-18 04:44:05 +05:30
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'code' => $authCode,
|
|
|
|
'client_id' => 'ely',
|
|
|
|
'client_secret' => 'ZuM1vGchJz-9_UZ5HC3H3Z9Hg5PzdbkM',
|
|
|
|
'redirect_uri' => 'http://ely.by',
|
2022-12-10 08:26:19 +05:30
|
|
|
]));
|
2016-02-23 03:19:46 +05:30
|
|
|
$I->canSeeResponseCodeIs(200);
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'token_type' => 'Bearer',
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseJsonMatchesJsonPath('$.access_token');
|
|
|
|
$I->canSeeResponseJsonMatchesJsonPath('$.expires_in');
|
2019-09-18 04:44:05 +05:30
|
|
|
$I->canSeeResponseJsonMatchesJsonPath('$.refresh_token');
|
2016-02-23 03:19:46 +05:30
|
|
|
}
|
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function callEndpointWithByEmptyRequest(OauthSteps $I): void {
|
2019-09-18 04:44:05 +05:30
|
|
|
$I->wantTo('check behavior on on request without any params');
|
|
|
|
$I->sendPOST('/api/oauth2/v1/token');
|
|
|
|
$I->canSeeResponseCodeIs(400);
|
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'error' => 'unsupported_grant_type',
|
|
|
|
'message' => 'The authorization grant type is not supported by the authorization server.',
|
|
|
|
]);
|
|
|
|
}
|
2016-02-23 03:19:46 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function issueTokenByPassingInvalidAuthCode(OauthSteps $I): void {
|
2019-09-18 04:44:05 +05:30
|
|
|
$I->wantTo('check behavior on passing invalid auth code');
|
|
|
|
$I->sendPOST('/api/oauth2/v1/token', [
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'code' => 'wrong-auth-code',
|
|
|
|
'client_id' => 'ely',
|
|
|
|
'client_secret' => 'ZuM1vGchJz-9_UZ5HC3H3Z9Hg5PzdbkM',
|
|
|
|
'redirect_uri' => 'http://ely.by',
|
|
|
|
]);
|
|
|
|
$I->canSeeResponseCodeIs(400);
|
|
|
|
$I->canSeeResponseContainsJson([
|
2024-12-02 15:40:55 +05:30
|
|
|
'error' => 'invalid_grant',
|
|
|
|
'message' => 'The provided authorization grant (e.g., authorization code, resource owner credentials) or refresh token '
|
|
|
|
. 'is invalid, expired, revoked, does not match the redirection URI used in the authorization request, '
|
|
|
|
. 'or was issued to another client.',
|
2019-09-18 04:44:05 +05:30
|
|
|
]);
|
|
|
|
}
|
2016-02-23 03:19:46 +05:30
|
|
|
|
2024-12-02 15:40:55 +05:30
|
|
|
public function issueTokenByPassingInvalidRedirectUri(OauthSteps $I): void {
|
2019-09-18 04:44:05 +05:30
|
|
|
$I->wantTo('check behavior on passing invalid redirect_uri');
|
|
|
|
$authCode = $I->obtainAuthCode();
|
|
|
|
$I->sendPOST('/api/oauth2/v1/token', [
|
|
|
|
'grant_type' => 'authorization_code',
|
|
|
|
'code' => $authCode,
|
|
|
|
'client_id' => 'ely',
|
|
|
|
'client_secret' => 'ZuM1vGchJz-9_UZ5HC3H3Z9Hg5PzdbkM',
|
|
|
|
'redirect_uri' => 'http://some-other.domain',
|
|
|
|
]);
|
2019-09-22 02:47:21 +05:30
|
|
|
$I->canSeeResponseCodeIs(401);
|
2019-09-18 04:44:05 +05:30
|
|
|
$I->canSeeResponseContainsJson([
|
|
|
|
'error' => 'invalid_client',
|
2019-09-22 02:47:21 +05:30
|
|
|
'message' => 'Client authentication failed',
|
2019-09-18 04:44:05 +05:30
|
|
|
]);
|
2016-02-23 03:19:46 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|