Skip to content

Commit

Permalink
[TASK] Update Configuration Unit Test
Browse files Browse the repository at this point in the history
  • Loading branch information
calien666 committed Apr 8, 2024
1 parent a6ba606 commit 79ab2d2
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 17 deletions.
44 changes: 38 additions & 6 deletions Classes/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,44 @@ public function __construct()
$this->privateKeyFile = $configuration['privateKeyFile'];
$this->publicKeyFile = $configuration['publicKeyFile'];
$this->loginPage = (int)$configuration['loginPage'];
$this->authEndpoint = $configuration['authEndpoint'] ?: $this->authEndpoint;
$this->tokenEndpoint = $configuration['tokenEndpoint'] ?: $this->tokenEndpoint;
$this->resourceEndpoint = $configuration['resourceEndpoint'] ?: $this->resourceEndpoint;
$this->accessTokenLifetime = DateInterval::createFromDateString($configuration['accessTokenLifetime']) ?: DateInterval::createFromDateString('1 hour');
$this->refreshTokenLifetime = DateInterval::createFromDateString($configuration['refreshTokenLifetime']) ?: DateInterval::createFromDateString('1 month');
$this->authorizationCodeLifetime = DateInterval::createFromDateString($configuration['authorizationCodeLifetime']) ?: DateInterval::createFromDateString('10 minutes');
$this->authEndpoint = $configuration['authEndpoint'] ?? $this->authEndpoint;
$this->tokenEndpoint = $configuration['tokenEndpoint'] ?? $this->tokenEndpoint;
$this->resourceEndpoint = $configuration['resourceEndpoint'] ?? $this->resourceEndpoint;

$accessInterval = false;
if (isset($configuration['accessTokenLifetime'])) {
$accessInterval = DateInterval::createFromDateString($configuration['accessTokenLifetime']);
if ($accessInterval === false) {
throw new \InvalidArgumentException(
'Invalid Access Token lifetime. See https://www.php.net/manual/de/datetime.formats.php#datetime.formats.relative for valid formats',
1712606345346
);
}
}
$this->accessTokenLifetime = $accessInterval ?: DateInterval::createFromDateString('1 hour');
$refreshInterval = false;
if (isset($configuration['refreshTokenLifetime'])) {
$refreshInterval = DateInterval::createFromDateString($configuration['refreshTokenLifetime']);
if ($refreshInterval === false) {
throw new \InvalidArgumentException(
'Invalid Refresh Token lifetime. See https://www.php.net/manual/de/datetime.formats.php#datetime.formats.relative for valid formats',
1712606543156
);
}
}
$this->refreshTokenLifetime = $refreshInterval ?: DateInterval::createFromDateString('1 month');

$authorizationCodeInterval = false;
if (isset($configuration['authorizationCodeLifetime'])) {
$authorizationCodeInterval = DateInterval::createFromDateString($configuration['authorizationCodeLifetime']);
if ($authorizationCodeInterval === false) {
throw new \InvalidArgumentException(
'Invalid Authorization Code lifetime. See https://www.php.net/manual/de/datetime.formats.php#datetime.formats.relative for valid formats',
1712606546362
);
}
}
$this->authorizationCodeLifetime = $authorizationCodeInterval ?: DateInterval::createFromDateString('10 minutes');
}

/**
Expand Down
62 changes: 52 additions & 10 deletions Tests/Unit/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,67 @@ protected function tearDown(): void
GeneralUtility::purgeInstances();
}

public function validConfigurationDataProvider(): \Generator
{
yield 'Basic configuration' => [
'configurationArrayToTest' => [
'privateKeyFile' => 'private.key',
'publicKeyFile' => 'public.key',
'loginPage' => '10',
'authEndpoint' => '/oauth/authorize',
'tokenEndpoint' => '/oauth/token',
'resourceEndpoint' => '/oauth/identity',
'accessTokenLifetime' => '1 hour',
'refreshTokenLifetime' => '1 month',
'authorizationCodeLifetime' => '10 minutes',
],
'expectedPrivateKeyString' => 'private.key',
'expectedPublicKeyString' => 'public.key',
'expectedLoginPage' => 10,
'expectedAuthEndpoint' => '/oauth/authorize',
'expectedTokenEndpoint' => '/oauth/token',
'expectedResourceEndpoint' => '/oauth/identity',
'expectedAccessTokenLifetimeType' => \DateInterval::class,
'expectedRefreshTokenLifetimeType' => \DateInterval::class,
'expectedAuthorizationCodeLifetimeType' => \DateInterval::class,
];
}
/**
* @test
* @param array<string, string> $configurationArrayToTest
* @param class-string $expectedAccessTokenLifetimeType
* @param class-string $expectedRefreshTokenLifetimeType
* @param class-string $expectedAuthorizationCodeLifetimeType
* @dataProvider validConfigurationDataProvider
*/
public function acceptsValidConfiguration(): void
{
public function acceptsValidConfiguration(
array $configurationArrayToTest,
string $expectedPrivateKeyString,
string $expectedPublicKeyString,
int $expectedLoginPage,
string $expectedAuthEndpoint,
string $expectedTokenEndpoint,
string $expectedResourceEndpoint,
string $expectedAccessTokenLifetimeType,
string $expectedRefreshTokenLifetimeType,
string $expectedAuthorizationCodeLifetimeType
): void {
/** @var TProphecy $extensionConfiguration */
$extensionConfiguration = $this->prophesize(ExtensionConfiguration::class);
$extensionConfiguration->get('oauth2_server')->willReturn([
'privateKeyFile' => 'private.key',
'publicKeyFile' => 'public.key',
'loginPage' => '10',
]);
$extensionConfiguration->get('oauth2_server')->willReturn($configurationArrayToTest);
GeneralUtility::addInstance(ExtensionConfiguration::class, $extensionConfiguration->reveal());

$configuration = new Configuration();

self::assertStringEndsWith('private.key', $configuration->getPrivateKeyFile());
self::assertStringEndsWith('public.key', $configuration->getPublicKeyFile());
self::assertEquals(10, $configuration->getLoginPage());
self::assertStringEndsWith($expectedPrivateKeyString, $configuration->getPrivateKeyFile());
self::assertStringEndsWith($expectedPublicKeyString, $configuration->getPublicKeyFile());
self::assertEquals($expectedLoginPage, $configuration->getLoginPage());
self::assertEquals($expectedAuthEndpoint, $configuration->getAuthEndpoint());
self::assertEquals($expectedTokenEndpoint, $configuration->getTokenEndpoint());
self::assertEquals($expectedResourceEndpoint, $configuration->getResourceEndpoint());
self::assertInstanceOf($expectedAccessTokenLifetimeType, $configuration->getAccessTokenLifetime());
self::assertInstanceOf($expectedRefreshTokenLifetimeType, $configuration->getRefreshTokenLifetime());
self::assertInstanceOf($expectedAuthorizationCodeLifetimeType, $configuration->getAuthorizationCodeLifetime());
}

/**
Expand Down
2 changes: 1 addition & 1 deletion ext_conf_template.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ authEndpoint = /oauth/authorize
# cat=OauthServer/Endpoints/tokenEndpoint; type=string; label=Token Endpoint
tokenEndpoint = /oauth/token
# cat=OauthServer/Endpoints/resourceEndpoint; type=string; label=Resource Server Endpoint
resourceEndpoint = /oauth/indentity
resourceEndpoint = /oauth/identity
# cat=OauthServer/Lifetime/accessToken; type=string; label=Access Token Lifetime
accessTokenLifetime = 1 hour
# cat=OauthServer/Lifetime/refreshToken; type=string; label=Refresh Token Lifetime
Expand Down

0 comments on commit 79ab2d2

Please sign in to comment.