Skip to content

Commit 8c7aec5

Browse files
add tests
1 parent cfd7713 commit 8c7aec5

File tree

6 files changed

+162
-0
lines changed

6 files changed

+162
-0
lines changed

tests/Grant/AuthCodeGrantTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
1818
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
1919
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
20+
use League\OAuth2\Server\RequestAccessTokenEvent;
21+
use League\OAuth2\Server\RequestEvent;
22+
use League\OAuth2\Server\RequestRefreshTokenEvent;
2023
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
2124
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
2225
use LeagueTests\Stubs\AccessTokenEntity;
@@ -635,6 +638,27 @@ public function testRespondToAccessTokenRequest(): void
635638
$grant->setEncryptionKey($this->cryptStub->getKey());
636639
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
637640

641+
$accessTokenEventEmitted = false;
642+
$refreshTokenEventEmitted = false;
643+
644+
$grant->getListenerRegistry()->subscribeTo(
645+
RequestEvent::ACCESS_TOKEN_ISSUED,
646+
function ($event) use (&$accessTokenEventEmitted): void {
647+
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);
648+
649+
$accessTokenEventEmitted = true;
650+
}
651+
);
652+
653+
$grant->getListenerRegistry()->subscribeTo(
654+
RequestEvent::REFRESH_TOKEN_ISSUED,
655+
function ($event) use (&$refreshTokenEventEmitted): void {
656+
self::assertInstanceOf(RequestRefreshTokenEvent::class, $event);
657+
658+
$refreshTokenEventEmitted = true;
659+
}
660+
);
661+
638662
$request = new ServerRequest(
639663
[],
640664
[],
@@ -665,6 +689,14 @@ public function testRespondToAccessTokenRequest(): void
665689
$response = $grant->respondToAccessTokenRequest($request, new StubResponseType(), new DateInterval('PT10M'));
666690

667691
self::assertInstanceOf(RefreshTokenEntityInterface::class, $response->getRefreshToken());
692+
693+
if (!$accessTokenEventEmitted) {
694+
self::fail('Access token issued event is not emitted.');
695+
}
696+
697+
if (!$refreshTokenEventEmitted) {
698+
self::fail('Refresh token issued event is not emitted.');
699+
}
668700
}
669701

670702
public function testRespondToAccessTokenRequestWithDefaultRedirectUri(): void

tests/Grant/ClientCredentialsGrantTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
1212
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
1313
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
14+
use League\OAuth2\Server\RequestAccessTokenEvent;
15+
use League\OAuth2\Server\RequestEvent;
1416
use LeagueTests\Stubs\AccessTokenEntity;
1517
use LeagueTests\Stubs\ClientEntity;
1618
use LeagueTests\Stubs\ScopeEntity;
@@ -53,6 +55,17 @@ public function testRespondToRequest(): void
5355
$grant->setDefaultScope(self::DEFAULT_SCOPE);
5456
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
5557

58+
$accessTokenEventEmitted = false;
59+
60+
$grant->getListenerRegistry()->subscribeTo(
61+
RequestEvent::ACCESS_TOKEN_ISSUED,
62+
function ($event) use (&$accessTokenEventEmitted): void {
63+
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);
64+
65+
$accessTokenEventEmitted = true;
66+
}
67+
);
68+
5669
$serverRequest = (new ServerRequest())->withParsedBody([
5770
'client_id' => 'foo',
5871
'client_secret' => 'bar',
@@ -64,5 +77,9 @@ public function testRespondToRequest(): void
6477
$response = $grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));
6578

6679
self::assertNotEmpty($response->getAccessToken()->getIdentifier());
80+
81+
if (!$accessTokenEventEmitted) {
82+
self::fail('Access token issued event is not emitted.');
83+
}
6784
}
6885
}

tests/Grant/DeviceCodeGrantTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use League\OAuth2\Server\Repositories\DeviceCodeRepositoryInterface;
1919
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
2020
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
21+
use League\OAuth2\Server\RequestAccessTokenEvent;
22+
use League\OAuth2\Server\RequestEvent;
23+
use League\OAuth2\Server\RequestRefreshTokenEvent;
2124
use LeagueTests\Stubs\AccessTokenEntity;
2225
use LeagueTests\Stubs\ClientEntity;
2326
use LeagueTests\Stubs\DeviceCodeEntity;
@@ -380,6 +383,27 @@ public function testRespondToAccessTokenRequest(): void
380383

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

386+
$accessTokenEventEmitted = false;
387+
$refreshTokenEventEmitted = false;
388+
389+
$grant->getListenerRegistry()->subscribeTo(
390+
RequestEvent::ACCESS_TOKEN_ISSUED,
391+
function ($event) use (&$accessTokenEventEmitted): void {
392+
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);
393+
394+
$accessTokenEventEmitted = true;
395+
}
396+
);
397+
398+
$grant->getListenerRegistry()->subscribeTo(
399+
RequestEvent::REFRESH_TOKEN_ISSUED,
400+
function ($event) use (&$refreshTokenEventEmitted): void {
401+
self::assertInstanceOf(RequestRefreshTokenEvent::class, $event);
402+
403+
$refreshTokenEventEmitted = true;
404+
}
405+
);
406+
383407
$serverRequest = (new ServerRequest())->withParsedBody([
384408
'grant_type' => 'urn:ietf:params:oauth:grant-type:device_code',
385409
'device_code' => $deviceCodeEntity->getIdentifier(),
@@ -391,6 +415,14 @@ public function testRespondToAccessTokenRequest(): void
391415

392416
$this::assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken());
393417
$this::assertSame([$scope], $responseType->getAccessToken()->getScopes());
418+
419+
if (!$accessTokenEventEmitted) {
420+
self::fail('Access token issued event is not emitted.');
421+
}
422+
423+
if (!$refreshTokenEventEmitted) {
424+
self::fail('Refresh token issued event is not emitted.');
425+
}
394426
}
395427

396428
public function testRespondToRequestMissingClient(): void

tests/Grant/ImplicitGrantTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
1515
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
1616
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
17+
use League\OAuth2\Server\RequestAccessTokenEvent;
18+
use League\OAuth2\Server\RequestEvent;
1719
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
1820
use League\OAuth2\Server\ResponseTypes\RedirectResponse;
1921
use LeagueTests\Stubs\AccessTokenEntity;
@@ -272,7 +274,22 @@ public function testCompleteAuthorizationRequest(): void
272274
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
273275
$grant->setScopeRepository($scopeRepositoryMock);
274276

277+
$accessTokenEventEmitted = false;
278+
279+
$grant->getListenerRegistry()->subscribeTo(
280+
RequestEvent::ACCESS_TOKEN_ISSUED,
281+
function ($event) use (&$accessTokenEventEmitted): void {
282+
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);
283+
284+
$accessTokenEventEmitted = true;
285+
}
286+
);
287+
275288
self::assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest));
289+
290+
if (!$accessTokenEventEmitted) {
291+
// self::fail('Access token issued event is not emitted.'); // TODO: next major release
292+
}
276293
}
277294

278295
public function testCompleteAuthorizationRequestDenied(): void

tests/Grant/PasswordGrantTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
1616
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
1717
use League\OAuth2\Server\Repositories\UserRepositoryInterface;
18+
use League\OAuth2\Server\RequestAccessTokenEvent;
19+
use League\OAuth2\Server\RequestEvent;
20+
use League\OAuth2\Server\RequestRefreshTokenEvent;
1821
use LeagueTests\Stubs\AccessTokenEntity;
1922
use LeagueTests\Stubs\ClientEntity;
2023
use LeagueTests\Stubs\RefreshTokenEntity;
@@ -69,6 +72,27 @@ public function testRespondToRequest(): void
6972
$grant->setDefaultScope(self::DEFAULT_SCOPE);
7073
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
7174

75+
$accessTokenEventEmitted = false;
76+
$refreshTokenEventEmitted = false;
77+
78+
$grant->getListenerRegistry()->subscribeTo(
79+
RequestEvent::ACCESS_TOKEN_ISSUED,
80+
function ($event) use (&$accessTokenEventEmitted): void {
81+
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);
82+
83+
$accessTokenEventEmitted = true;
84+
}
85+
);
86+
87+
$grant->getListenerRegistry()->subscribeTo(
88+
RequestEvent::REFRESH_TOKEN_ISSUED,
89+
function ($event) use (&$refreshTokenEventEmitted): void {
90+
self::assertInstanceOf(RequestRefreshTokenEvent::class, $event);
91+
92+
$refreshTokenEventEmitted = true;
93+
}
94+
);
95+
7296
$serverRequest = (new ServerRequest())->withParsedBody([
7397
'client_id' => 'foo',
7498
'client_secret' => 'bar',
@@ -80,6 +104,14 @@ public function testRespondToRequest(): void
80104
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));
81105

82106
self::assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken());
107+
108+
if (!$accessTokenEventEmitted) {
109+
self::fail('Access token issued event is not emitted.');
110+
}
111+
112+
if (!$refreshTokenEventEmitted) {
113+
self::fail('Refresh token issued event is not emitted.');
114+
}
83115
}
84116

85117
public function testRespondToRequestNullRefreshToken(): void

tests/Grant/RefreshTokenGrantTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
1616
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
1717
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
18+
use League\OAuth2\Server\RequestAccessTokenEvent;
19+
use League\OAuth2\Server\RequestEvent;
20+
use League\OAuth2\Server\RequestRefreshTokenEvent;
1821
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
1922
use LeagueTests\Stubs\AccessTokenEntity;
2023
use LeagueTests\Stubs\ClientEntity;
@@ -76,6 +79,27 @@ public function testRespondToRequest(): void
7679
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
7780
$grant->revokeRefreshTokens(true);
7881

82+
$accessTokenEventEmitted = false;
83+
$refreshTokenEventEmitted = false;
84+
85+
$grant->getListenerRegistry()->subscribeTo(
86+
RequestEvent::ACCESS_TOKEN_ISSUED,
87+
function ($event) use (&$accessTokenEventEmitted): void {
88+
self::assertInstanceOf(RequestAccessTokenEvent::class, $event);
89+
90+
$accessTokenEventEmitted = true;
91+
}
92+
);
93+
94+
$grant->getListenerRegistry()->subscribeTo(
95+
RequestEvent::REFRESH_TOKEN_ISSUED,
96+
function ($event) use (&$refreshTokenEventEmitted): void {
97+
self::assertInstanceOf(RequestRefreshTokenEvent::class, $event);
98+
99+
$refreshTokenEventEmitted = true;
100+
}
101+
);
102+
79103
$oldRefreshToken = json_encode(
80104
[
81105
'client_id' => 'foo',
@@ -106,6 +130,14 @@ public function testRespondToRequest(): void
106130
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new DateInterval('PT5M'));
107131

108132
self::assertInstanceOf(RefreshTokenEntityInterface::class, $responseType->getRefreshToken());
133+
134+
if (!$accessTokenEventEmitted) {
135+
self::fail('Access token issued event is not emitted.');
136+
}
137+
138+
if (!$refreshTokenEventEmitted) {
139+
self::fail('Refresh token issued event is not emitted.');
140+
}
109141
}
110142

111143
public function testRespondToRequestNullRefreshToken(): void

0 commit comments

Comments
 (0)