-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from gregurco/validation_and_tests
Validation and tests
- Loading branch information
Showing
3 changed files
with
187 additions
and
33 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 |
---|---|---|
|
@@ -43,7 +43,7 @@ new EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle([ | |
``` | ||
|
||
### Basic configuration | ||
#### With password grant type | ||
#### With default grant type (client) | ||
``` yaml | ||
# app/config/config.yml | ||
|
||
|
@@ -59,13 +59,12 @@ eight_points_guzzle: | |
oauth2: | ||
base_uri: "https://example.com" | ||
token_url: "/oauth/token" | ||
username: "[email protected]" | ||
password: "pa55w0rd" | ||
client_id: "test-client-id" | ||
client_secret: "test-client-secret" # optional | ||
scope: "administration" | ||
``` | ||
#### With client credentials grant type | ||
#### With password grant type | ||
``` yaml | ||
# app/config/config.yml | ||
|
||
|
@@ -82,9 +81,10 @@ eight_points_guzzle: | |
base_uri: "https://example.com" | ||
token_url: "/oauth/token" | ||
client_id: "test-client-id" | ||
client_secret: "test-client-secret" # optional | ||
username: "johndoe" | ||
password: "A3ddj3w" | ||
scope: "administration" | ||
grant_type: "Sainsburys\\Guzzle\\Oauth2\\GrantType\\ClientCredentials" | ||
grant_type: "Sainsburys\\Guzzle\\Oauth2\\GrantType\\PasswordCredentials" | ||
``` | ||
#### With client credentials in body | ||
|
@@ -104,9 +104,7 @@ eight_points_guzzle: | |
base_uri: "https://example.com" | ||
token_url: "/oauth/token" | ||
client_id: "test-client-id" | ||
client_secret: "test-client-secret" # optional | ||
scope: "administration" | ||
grant_type: "Sainsburys\\Guzzle\\Oauth2\\GrantType\\ClientCredentials" | ||
auth_location: "body" | ||
``` | ||
|
@@ -122,8 +120,9 @@ eight_points_guzzle: | |
| password | The resource owner password | for PasswordCredentials grant type | A3ddj3w | | ||
| auth_location | The place where to put client_id and client_secret in auth request. <br/>Default: headers. Allowed values: body, headers. | no | body | | ||
| resource | The App ID URI of the web API (secured resource) | no | https://service.contoso.com/ | | ||
| private_key | Path to private key | for JwtBearer grant type | `"%kernel.root_dir%/path/to/private.key"` | | ||
| scope | One or more scope values indicating which parts of the user's account you wish to access | no | administration | | ||
| grant_type | Grant type class path. Class should implement GrantTypeInterface. <br/> Default: `Sainsburys\\Guzzle\\Oauth2\\GrantType\\PasswordCredentials` | no | `Sainsburys\\Guzzle\\Oauth2\\GrantType\\ClientCredentials` | | ||
| grant_type | Grant type class path. Class should implement GrantTypeInterface. <br/> Default: `Sainsburys\\Guzzle\\Oauth2\\GrantType\\ClientCredentials` | no | `Sainsburys\\Guzzle\\Oauth2\\GrantType\\PasswordCredentials`<br/>`Sainsburys\\Guzzle\\Oauth2\\GrantType\\AuthorizationCode`<br/>`Sainsburys\\Guzzle\\Oauth2\\GrantType\\JwtBearer` | | ||
|
||
See more information about middleware [here][3]. | ||
|
||
|
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 |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
use EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundlePlugin; | ||
use Gregurco\Bundle\GuzzleBundleOAuth2Plugin\GuzzleBundleOAuth2Plugin; | ||
use Sainsburys\Guzzle\Oauth2\GrantType\ClientCredentials; | ||
use Sainsburys\Guzzle\Oauth2\GrantType\GrantTypeInterface; | ||
use Sainsburys\Guzzle\Oauth2\GrantType\JwtBearer; | ||
use Sainsburys\Guzzle\Oauth2\GrantType\PasswordCredentials; | ||
use Sainsburys\Guzzle\Oauth2\GrantType\RefreshToken; | ||
use Sainsburys\Guzzle\Oauth2\Middleware\OAuthMiddleware; | ||
|
@@ -56,8 +58,9 @@ public function testAddConfiguration() | |
'token_url' => null, | ||
'scope' => null, | ||
'resource' => null, | ||
'private_key' => null, | ||
'auth_location' => 'headers', | ||
'grant_type' => PasswordCredentials::class, | ||
'grant_type' => ClientCredentials::class, | ||
], | ||
$node->getDefaultValue() | ||
); | ||
|
@@ -91,14 +94,15 @@ public function testLoadForClient() | |
'enabled' => true, | ||
'base_uri' => 'https://example.com', | ||
'token_url' => '/oauth/token', | ||
'username' => '[email protected]', | ||
'password' => 'pa55w0rd', | ||
'username' => null, | ||
'password' => null, | ||
'client_id' => 'test-client-id', | ||
'client_secret' => '', | ||
'scope' => 'administration', | ||
'resource' => null, | ||
'private_key' => null, | ||
'auth_location' => 'headers', | ||
'grant_type' => PasswordCredentials::class, | ||
'grant_type' => ClientCredentials::class, | ||
], | ||
$container, 'api_payment', $handler | ||
); | ||
|
@@ -110,6 +114,41 @@ public function testLoadForClient() | |
$this->assertCount(3, $clientMiddlewareDefinition->getArguments()); | ||
} | ||
|
||
public function testLoadForClientWithPrivateKey() | ||
{ | ||
$handler = new Definition(); | ||
$container = new ContainerBuilder(); | ||
|
||
$this->plugin->loadForClient( | ||
[ | ||
'enabled' => true, | ||
'base_uri' => 'https://example.com', | ||
'token_url' => '/oauth/token', | ||
'username' => null, | ||
'password' => null, | ||
'client_id' => 'test-client-id', | ||
'client_secret' => '', | ||
'scope' => 'administration', | ||
'resource' => null, | ||
'private_key' => '/path/to/private.key', | ||
'auth_location' => 'headers', | ||
'grant_type' => JwtBearer::class, | ||
], | ||
$container, 'api_payment', $handler | ||
); | ||
|
||
$this->assertTrue($container->hasDefinition('guzzle_bundle_oauth2_plugin.middleware.api_payment')); | ||
$this->assertCount(2, $handler->getMethodCalls()); | ||
|
||
$clientMiddlewareDefinition = $container->getDefinition('guzzle_bundle_oauth2_plugin.middleware.api_payment'); | ||
$this->assertCount(3, $clientMiddlewareDefinition->getArguments()); | ||
|
||
$this->assertTrue($container->hasDefinition('guzzle_bundle_oauth2_plugin.private_key.api_payment')); | ||
$clientMiddlewareDefinition = $container->getDefinition('guzzle_bundle_oauth2_plugin.private_key.api_payment'); | ||
$this->assertCount(1, $clientMiddlewareDefinition->getArguments()); | ||
$this->assertEquals('/path/to/private.key', $clientMiddlewareDefinition->getArgument(0)); | ||
} | ||
|
||
/** | ||
* @dataProvider provideValidConfigurationData | ||
* | ||
|
@@ -141,29 +180,45 @@ public function testAddConfigurationWithData(array $pluginConfiguration) | |
public function provideValidConfigurationData() : array | ||
{ | ||
return [ | ||
'config is empty' => [[]], | ||
'plugin is disabled' => [[ | ||
'enabled' => false, | ||
]], | ||
'plugin is enabled' => [[ | ||
'enabled' => true, | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
]], | ||
'PasswordCredentials in grant_type' => [[ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'username' => 'johndoe', | ||
'password' => 'A3ddj3w', | ||
'grant_type' => PasswordCredentials::class, | ||
]], | ||
'ClientCredentials in grant_type' => [[ | ||
'grant_type' => ClientCredentials::class, | ||
]], | ||
'JwtBearer in grant_type' => [[ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'grant_type' => ClientCredentials::class, | ||
]], | ||
'RefreshToken in grant_type' => [[ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'grant_type' => RefreshToken::class, | ||
]], | ||
'JwtBearer in grant_type' => [[ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'private_key' => '/path/to/private/key', | ||
'grant_type' => JwtBearer::class, | ||
]], | ||
'headers in auth_location' => [[ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'auth_location' => 'headers', | ||
]], | ||
'body in auth_location' => [[ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'auth_location' => 'body', | ||
]], | ||
]; | ||
|
@@ -173,10 +228,12 @@ public function provideValidConfigurationData() : array | |
* @dataProvider provideInvalidConfigurationData | ||
* | ||
* @param array $pluginConfiguration | ||
* @param string $message | ||
*/ | ||
public function testAddConfigurationWithInvalidData(array $pluginConfiguration) | ||
public function testAddConfigurationWithInvalidData(array $pluginConfiguration, string $message) | ||
{ | ||
$this->expectException(InvalidConfigurationException::class); | ||
$this->expectExceptionMessage($message); | ||
|
||
$config = [ | ||
'eight_points_guzzle' => [ | ||
|
@@ -200,15 +257,70 @@ public function testAddConfigurationWithInvalidData(array $pluginConfiguration) | |
public function provideInvalidConfigurationData() : array | ||
{ | ||
return [ | ||
'invalid type in grant_type' => [[ | ||
'grant_type' => true, | ||
]], | ||
'invalid class in grant_type' => [[ | ||
'grant_type' => \stdClass::class, | ||
]], | ||
'invalid grant_type' => [[ | ||
'auth_location' => 'somewhere', | ||
]], | ||
'without base_uri' => [ | ||
'config' => [ | ||
'enabled' => true, | ||
'client_id' => 's6BhdRkqt3', | ||
], | ||
'exception message' => 'base_uri is required', | ||
], | ||
'without client_id' => [ | ||
'config' => [ | ||
'enabled' => true, | ||
'base_uri' => 'https://example.com', | ||
], | ||
'exception message' => 'client_id is required', | ||
], | ||
'invalid type in grant_type' => [ | ||
'config' => [ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'grant_type' => true, | ||
], | ||
'exception message' => sprintf('Use instance of %s in grant_type', GrantTypeInterface::class), | ||
], | ||
'invalid class in grant_type' => [ | ||
'config' => [ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'grant_type' => \stdClass::class, | ||
], | ||
'exception message' => sprintf('Use instance of %s in grant_type', GrantTypeInterface::class), | ||
], | ||
'invalid auth_location' => [ | ||
'config' => [ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'auth_location' => 'somewhere', | ||
], | ||
'exception message' => 'Invalid auth_location "somewhere". Allowed values: headers, body.', | ||
], | ||
'PasswordCredentials grant type without username' => [ | ||
'config' => [ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'password' => 'A3ddj3w', | ||
'grant_type' => PasswordCredentials::class, | ||
], | ||
'exception message' => 'username and password are required', | ||
], | ||
'PasswordCredentials grant type without password' => [ | ||
'config' => [ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'username' => 'johndoe', | ||
'grant_type' => PasswordCredentials::class, | ||
], | ||
'exception message' => 'username and password are required', | ||
], | ||
'JwtBearer grant type without private_key' => [ | ||
'config' => [ | ||
'base_uri' => 'https://example.com', | ||
'client_id' => 's6BhdRkqt3', | ||
'grant_type' => JwtBearer::class, | ||
], | ||
'exception message' => 'private_key is required', | ||
], | ||
]; | ||
} | ||
} |