Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hafezdivandari committed Dec 29, 2024
1 parent cfd7713 commit 8c7aec5
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 0 deletions.
32 changes: 32 additions & 0 deletions tests/Grant/AuthCodeGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\RequestAccessTokenEvent;
use League\OAuth2\Server\RequestEvent;
use League\OAuth2\Server\RequestRefreshTokenEvent;
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
use LeagueTests\Stubs\AccessTokenEntity;
Expand Down Expand Up @@ -635,6 +638,27 @@ public function testRespondToAccessTokenRequest(): void
$grant->setEncryptionKey($this->cryptStub->getKey());
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));

$accessTokenEventEmitted = false;
$refreshTokenEventEmitted = false;

$grant->getListenerRegistry()->subscribeTo(
RequestEvent::ACCESS_TOKEN_ISSUED,
function ($event) use (&$accessTokenEventEmitted): void {
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);

$accessTokenEventEmitted = true;
}
);

$grant->getListenerRegistry()->subscribeTo(
RequestEvent::REFRESH_TOKEN_ISSUED,
function ($event) use (&$refreshTokenEventEmitted): void {
self::assertInstanceOf(RequestRefreshTokenEvent::class, $event);

$refreshTokenEventEmitted = true;
}
);

$request = new ServerRequest(
[],
[],
Expand Down Expand Up @@ -665,6 +689,14 @@ public function testRespondToAccessTokenRequest(): void
$response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new DateInterval('PT10M'));

self::assertInstanceOf(RefreshTokenEntityInterface::class, $response->getRefreshToken());

if (!$accessTokenEventEmitted) {
self::fail('Access token issued event is not emitted.');
}

if (!$refreshTokenEventEmitted) {
self::fail('Refresh token issued event is not emitted.');
}
}

public function testRespondToAccessTokenRequestWithDefaultRedirectUri(): void
Expand Down
17 changes: 17 additions & 0 deletions tests/Grant/ClientCredentialsGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\RequestAccessTokenEvent;
use League\OAuth2\Server\RequestEvent;
use LeagueTests\Stubs\AccessTokenEntity;
use LeagueTests\Stubs\ClientEntity;
use LeagueTests\Stubs\ScopeEntity;
Expand Down Expand Up @@ -53,6 +55,17 @@ public function testRespondToRequest(): void
$grant->setDefaultScope(self::DEFAULT_SCOPE);
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));

$accessTokenEventEmitted = false;

$grant->getListenerRegistry()->subscribeTo(
RequestEvent::ACCESS_TOKEN_ISSUED,
function ($event) use (&$accessTokenEventEmitted): void {
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);

$accessTokenEventEmitted = true;
}
);

$serverRequest = (new ServerRequest())->withParsedBody([
'client_id' => 'foo',
'client_secret' => 'bar',
Expand All @@ -64,5 +77,9 @@ public function testRespondToRequest(): void
$response = $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));

self::assertNotEmpty($response->getAccessToken()->getIdentifier());

if (!$accessTokenEventEmitted) {
self::fail('Access token issued event is not emitted.');
}
}
}
32 changes: 32 additions & 0 deletions tests/Grant/DeviceCodeGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
use League\OAuth2\Server\Repositories\DeviceCodeRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\RequestAccessTokenEvent;
use League\OAuth2\Server\RequestEvent;
use League\OAuth2\Server\RequestRefreshTokenEvent;
use LeagueTests\Stubs\AccessTokenEntity;
use LeagueTests\Stubs\ClientEntity;
use LeagueTests\Stubs\DeviceCodeEntity;
Expand Down Expand Up @@ -380,6 +383,27 @@ public function testRespondToAccessTokenRequest(): void

$grant->completeDeviceAuthorizationRequest($deviceCodeEntity->getIdentifier(), 'baz', true);

$accessTokenEventEmitted = false;
$refreshTokenEventEmitted = false;

$grant->getListenerRegistry()->subscribeTo(
RequestEvent::ACCESS_TOKEN_ISSUED,
function ($event) use (&$accessTokenEventEmitted): void {
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);

$accessTokenEventEmitted = true;
}
);

$grant->getListenerRegistry()->subscribeTo(
RequestEvent::REFRESH_TOKEN_ISSUED,
function ($event) use (&$refreshTokenEventEmitted): void {
self::assertInstanceOf(RequestRefreshTokenEvent::class, $event);

$refreshTokenEventEmitted = true;
}
);

$serverRequest = (new ServerRequest())->withParsedBody([
'grant_type' => 'urn:ietf:params:oauth:grant-type:device_code',
'device_code' => $deviceCodeEntity->getIdentifier(),
Expand All @@ -391,6 +415,14 @@ public function testRespondToAccessTokenRequest(): void

$this::assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken());
$this::assertSame([$scope], $responseType->getAccessToken()->getScopes());

if (!$accessTokenEventEmitted) {
self::fail('Access token issued event is not emitted.');
}

if (!$refreshTokenEventEmitted) {
self::fail('Refresh token issued event is not emitted.');
}
}

public function testRespondToRequestMissingClient(): void
Expand Down
17 changes: 17 additions & 0 deletions tests/Grant/ImplicitGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\RequestAccessTokenEvent;
use League\OAuth2\Server\RequestEvent;
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
use LeagueTests\Stubs\AccessTokenEntity;
Expand Down Expand Up @@ -272,7 +274,22 @@ public function testCompleteAuthorizationRequest(): void
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
$grant->setScopeRepository($scopeRepositoryMock);

$accessTokenEventEmitted = false;

$grant->getListenerRegistry()->subscribeTo(
RequestEvent::ACCESS_TOKEN_ISSUED,
function ($event) use (&$accessTokenEventEmitted): void {
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);

$accessTokenEventEmitted = true;
}
);

self::assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest));

if (!$accessTokenEventEmitted) {
// self::fail('Access token issued event is not emitted.'); // TODO: next major release
}
}

public function testCompleteAuthorizationRequestDenied(): void
Expand Down
32 changes: 32 additions & 0 deletions tests/Grant/PasswordGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
use League\OAuth2\Server\RequestAccessTokenEvent;
use League\OAuth2\Server\RequestEvent;
use League\OAuth2\Server\RequestRefreshTokenEvent;
use LeagueTests\Stubs\AccessTokenEntity;
use LeagueTests\Stubs\ClientEntity;
use LeagueTests\Stubs\RefreshTokenEntity;
Expand Down Expand Up @@ -69,6 +72,27 @@ public function testRespondToRequest(): void
$grant->setDefaultScope(self::DEFAULT_SCOPE);
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));

$accessTokenEventEmitted = false;
$refreshTokenEventEmitted = false;

$grant->getListenerRegistry()->subscribeTo(
RequestEvent::ACCESS_TOKEN_ISSUED,
function ($event) use (&$accessTokenEventEmitted): void {
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);

$accessTokenEventEmitted = true;
}
);

$grant->getListenerRegistry()->subscribeTo(
RequestEvent::REFRESH_TOKEN_ISSUED,
function ($event) use (&$refreshTokenEventEmitted): void {
self::assertInstanceOf(RequestRefreshTokenEvent::class, $event);

$refreshTokenEventEmitted = true;
}
);

$serverRequest = (new ServerRequest())->withParsedBody([
'client_id' => 'foo',
'client_secret' => 'bar',
Expand All @@ -80,6 +104,14 @@ public function testRespondToRequest(): void
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));

self::assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken());

if (!$accessTokenEventEmitted) {
self::fail('Access token issued event is not emitted.');
}

if (!$refreshTokenEventEmitted) {
self::fail('Refresh token issued event is not emitted.');
}
}

public function testRespondToRequestNullRefreshToken(): void
Expand Down
32 changes: 32 additions & 0 deletions tests/Grant/RefreshTokenGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use League\OAuth2\Server\RequestAccessTokenEvent;
use League\OAuth2\Server\RequestEvent;
use League\OAuth2\Server\RequestRefreshTokenEvent;
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
use LeagueTests\Stubs\AccessTokenEntity;
use LeagueTests\Stubs\ClientEntity;
Expand Down Expand Up @@ -76,6 +79,27 @@ public function testRespondToRequest(): void
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
$grant->revokeRefreshTokens(true);

$accessTokenEventEmitted = false;
$refreshTokenEventEmitted = false;

$grant->getListenerRegistry()->subscribeTo(
RequestEvent::ACCESS_TOKEN_ISSUED,
function ($event) use (&$accessTokenEventEmitted): void {
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);

$accessTokenEventEmitted = true;
}
);

$grant->getListenerRegistry()->subscribeTo(
RequestEvent::REFRESH_TOKEN_ISSUED,
function ($event) use (&$refreshTokenEventEmitted): void {
self::assertInstanceOf(RequestRefreshTokenEvent::class, $event);

$refreshTokenEventEmitted = true;
}
);

$oldRefreshToken = json_encode(
[
'client_id' => 'foo',
Expand Down Expand Up @@ -106,6 +130,14 @@ public function testRespondToRequest(): void
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));

self::assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken());

if (!$accessTokenEventEmitted) {
self::fail('Access token issued event is not emitted.');
}

if (!$refreshTokenEventEmitted) {
self::fail('Refresh token issued event is not emitted.');
}
}

public function testRespondToRequestNullRefreshToken(): void
Expand Down

0 comments on commit 8c7aec5

Please sign in to comment.