- Refactor AlfaBankConnector to use Saloon's OAuth2 traits - Update authentication methods to use new OAuth2 implementation - Add tests for authorization URL generation, token exchange, and refresh - Replace custom GetTokenRequest with Saloon's built-in OAuth2 requests Co-authored-by: Qwen-Coder <qwen-coder@alibabacloud.com>
106 lines
3.8 KiB
PHP
106 lines
3.8 KiB
PHP
<?php
|
|
|
|
use Apoloz\AlfaBank\Core\AlfaBankConnector;
|
|
use Apoloz\AlfaBank\Core\AlfaConfig;
|
|
use Saloon\Http\Faking\MockResponse;
|
|
use Saloon\Http\OAuth2\GetAccessTokenRequest;
|
|
use Saloon\Http\OAuth2\GetRefreshTokenRequest;
|
|
|
|
test('can generate authorization url', function () {
|
|
$config = new AlfaConfig(
|
|
baseUrl: 'https://sandbox.alfabank.ru',
|
|
certificatePath: __DIR__ . '/../../test_key.pem',
|
|
privateKeyPath: __DIR__ . '/../../test_key.pem',
|
|
clientId: 'test-client-id',
|
|
clientSecret: 'test-client-secret',
|
|
redirectUri: 'https://callback.url'
|
|
);
|
|
$connector = new AlfaBankConnector($config);
|
|
|
|
$url = $connector->getAuthorizationUrl(['beneficiaries']);
|
|
|
|
expect($url)->toContain('https://id-sandbox.alfabank.ru/oidc/authorize')
|
|
->toContain('client_id=test-client-id')
|
|
->toContain('redirect_uri=' . urlencode('https://callback.url'))
|
|
->toContain('scope=beneficiaries');
|
|
});
|
|
|
|
test('can exchange code for token', function () {
|
|
$mockClient = new \Saloon\Http\Faking\MockClient([
|
|
GetAccessTokenRequest::class => MockResponse::make([
|
|
'access_token' => 'new-access-token',
|
|
'refresh_token' => 'new-refresh-token',
|
|
'expires_in' => 3600,
|
|
'token_type' => 'Bearer',
|
|
], 200),
|
|
]);
|
|
|
|
$config = new AlfaConfig(
|
|
baseUrl: 'https://sandbox.alfabank.ru',
|
|
certificatePath: __DIR__ . '/../../test_key.pem',
|
|
privateKeyPath: __DIR__ . '/../../test_key.pem',
|
|
clientId: 'test-client-id',
|
|
clientSecret: 'test-client-secret',
|
|
redirectUri: 'https://callback.url'
|
|
);
|
|
$connector = new AlfaBankConnector($config);
|
|
$connector->withMockClient($mockClient);
|
|
|
|
$authenticator = $connector->getAccessToken('auth-code-123');
|
|
|
|
expect($authenticator->getAccessToken())->toBe('new-access-token')
|
|
->and($authenticator->getRefreshToken())->toBe('new-refresh-token');
|
|
});
|
|
|
|
test('can refresh token', function () {
|
|
$mockClient = new \Saloon\Http\Faking\MockClient([
|
|
GetRefreshTokenRequest::class => MockResponse::make([
|
|
'access_token' => 'refreshed-access-token',
|
|
'refresh_token' => 'refreshed-refresh-token',
|
|
'expires_in' => 3600,
|
|
'token_type' => 'Bearer',
|
|
], 200),
|
|
]);
|
|
|
|
$config = new AlfaConfig(
|
|
baseUrl: 'https://sandbox.alfabank.ru',
|
|
certificatePath: __DIR__ . '/../../test_key.pem',
|
|
privateKeyPath: __DIR__ . '/../../test_key.pem',
|
|
clientId: 'test-client-id',
|
|
clientSecret: 'test-client-secret'
|
|
);
|
|
$connector = new AlfaBankConnector($config);
|
|
$connector->withMockClient($mockClient);
|
|
|
|
$authenticator = $connector->refreshAccessToken('old-refresh-token');
|
|
|
|
expect($authenticator->getAccessToken())->toBe('refreshed-access-token')
|
|
->and($authenticator->getRefreshToken())->toBe('refreshed-refresh-token');
|
|
});
|
|
|
|
test('can login with client credentials', function () {
|
|
$mockClient = new \Saloon\Http\Faking\MockClient([
|
|
\Saloon\Http\OAuth2\GetClientCredentialsTokenRequest::class => MockResponse::make([
|
|
'access_token' => 'cc-access-token',
|
|
'expires_in' => 3600,
|
|
'token_type' => 'Bearer',
|
|
], 200),
|
|
]);
|
|
|
|
$config = new AlfaConfig(
|
|
baseUrl: 'https://sandbox.alfabank.ru',
|
|
certificatePath: __DIR__ . '/../../test_key.pem',
|
|
privateKeyPath: __DIR__ . '/../../test_key.pem',
|
|
clientId: 'test-client-id',
|
|
clientSecret: 'test-client-secret'
|
|
);
|
|
$connector = new AlfaBankConnector($config);
|
|
$connector->withMockClient($mockClient);
|
|
|
|
$connector->login();
|
|
|
|
expect($connector->getAuthenticator())->not->toBeNull()
|
|
->and($connector->getAuthenticator()->getAccessToken())->toBe('cc-access-token');
|
|
});
|
|
|