Skip to content

Commit 3630fc7

Browse files
committed
Adds testing for claims
1 parent d2888f1 commit 3630fc7

File tree

7 files changed

+62
-1
lines changed

7 files changed

+62
-1
lines changed

tests/Grant/AbstractGrantTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
1818
use LeagueTests\Stubs\AccessTokenEntity;
1919
use LeagueTests\Stubs\AuthCodeEntity;
20+
use LeagueTests\Stubs\ClaimEntity;
2021
use LeagueTests\Stubs\ClientEntity;
2122
use LeagueTests\Stubs\RefreshTokenEntity;
2223
use LeagueTests\Stubs\ScopeEntity;
@@ -360,7 +361,8 @@ public function testIssueAccessToken()
360361
new DateInterval('PT1H'),
361362
new ClientEntity(),
362363
123,
363-
[new ScopeEntity()]
364+
[new ScopeEntity()],
365+
[new ClaimEntity('private', 'claim')]
364366
);
365367
$this->assertInstanceOf(AccessTokenEntityInterface::class, $accessToken);
366368
}

tests/Grant/ClientCredentialsGrantTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
99
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
1010
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
11+
use League\OAuth2\Server\Repositories\ClaimRepositoryInterface;
1112
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
1213
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
1314
use LeagueTests\Stubs\AccessTokenEntity;
@@ -43,10 +44,14 @@ public function testRespondToRequest()
4344
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scope);
4445
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
4546

47+
$claimRepositoryMock = $this->getMockBuilder(ClaimRepositoryInterface::class)->getMock();
48+
$claimRepositoryMock->method('getClaims')->willReturn([]);
49+
4650
$grant = new ClientCredentialsGrant();
4751
$grant->setClientRepository($clientRepositoryMock);
4852
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
4953
$grant->setScopeRepository($scopeRepositoryMock);
54+
$grant->setClaimRepository($claimRepositoryMock);
5055
$grant->setDefaultScope(self::DEFAULT_SCOPE);
5156
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
5257

tests/Grant/ImplicitGrantTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
1010
use League\OAuth2\Server\Grant\ImplicitGrant;
1111
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
12+
use League\OAuth2\Server\Repositories\ClaimRepositoryInterface;
1213
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
1314
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
1415
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
@@ -282,10 +283,14 @@ public function testAccessTokenRepositoryUniqueConstraintCheck()
282283
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
283284
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
284285

286+
$claimRepositoryMock = $this->getMockBuilder(ClaimRepositoryInterface::class)->getMock();
287+
$claimRepositoryMock->method('getClaims')->willReturn([]);
288+
285289
$grant = new ImplicitGrant(new \DateInterval('PT10M'));
286290
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
287291
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
288292
$grant->setScopeRepository($scopeRepositoryMock);
293+
$grant->setClaimRepository($claimRepositoryMock);
289294

290295
$this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest));
291296
}

tests/Grant/PasswordGrantTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
1010
use League\OAuth2\Server\Grant\PasswordGrant;
1111
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
12+
use League\OAuth2\Server\Repositories\ClaimRepositoryInterface;
1213
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
1314
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
1415
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
@@ -57,10 +58,14 @@ public function testRespondToRequest()
5758
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scope);
5859
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);
5960

61+
$claimRepositoryMock = $this->getMockBuilder(ClaimRepositoryInterface::class)->getMock();
62+
$claimRepositoryMock->method('getClaims')->willReturn([]);
63+
6064
$grant = new PasswordGrant($userRepositoryMock, $refreshTokenRepositoryMock);
6165
$grant->setClientRepository($clientRepositoryMock);
6266
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
6367
$grant->setScopeRepository($scopeRepositoryMock);
68+
$grant->setClaimRepository($claimRepositoryMock);
6469
$grant->setDefaultScope(self::DEFAULT_SCOPE);
6570
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
6671

tests/Grant/RefreshTokenGrantTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
1010
use League\OAuth2\Server\Grant\RefreshTokenGrant;
1111
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
12+
use League\OAuth2\Server\Repositories\ClaimRepositoryInterface;
1213
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
1314
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
1415
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
@@ -52,6 +53,9 @@ public function testRespondToRequest()
5253
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
5354
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);
5455

56+
$claimRepositoryMock = $this->getMockBuilder(ClaimRepositoryInterface::class)->getMock();
57+
$claimRepositoryMock->method('getClaims')->willReturn([]);
58+
5559
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
5660
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
5761
$accessTokenRepositoryMock->expects($this->once())->method('persistNewAccessToken')->willReturnSelf();
@@ -63,6 +67,7 @@ public function testRespondToRequest()
6367
$grant = new RefreshTokenGrant($refreshTokenRepositoryMock);
6468
$grant->setClientRepository($clientRepositoryMock);
6569
$grant->setScopeRepository($scopeRepositoryMock);
70+
$grant->setClaimRepository($claimRepositoryMock);
6671
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
6772
$grant->setEncryptionKey($this->cryptStub->getKey());
6873
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));

tests/ResponseTypes/BearerResponseTypeTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
1313
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
1414
use LeagueTests\Stubs\AccessTokenEntity;
15+
use LeagueTests\Stubs\ClaimEntity;
1516
use LeagueTests\Stubs\ClientEntity;
1617
use LeagueTests\Stubs\RefreshTokenEntity;
1718
use LeagueTests\Stubs\ScopeEntity;
@@ -32,11 +33,14 @@ public function testGenerateHttpResponse()
3233
$scope = new ScopeEntity();
3334
$scope->setIdentifier('basic');
3435

36+
$claim = new ClaimEntity('_private', 'claim');
37+
3538
$accessToken = new AccessTokenEntity();
3639
$accessToken->setIdentifier('abcdef');
3740
$accessToken->setExpiryDateTime((new DateTimeImmutable())->add(new DateInterval('PT1H')));
3841
$accessToken->setClient($client);
3942
$accessToken->addScope($scope);
43+
$accessToken->addClaim($claim);
4044
$accessToken->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
4145

4246
$refreshToken = new RefreshTokenEntity();

tests/Stubs/ClaimEntity.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
4+
namespace LeagueTests\Stubs;
5+
6+
7+
use League\OAuth2\Server\Entities\ClaimEntityInterface;
8+
9+
class ClaimEntity implements ClaimEntityInterface
10+
{
11+
12+
private $name;
13+
private $value;
14+
15+
public function __construct($name, $value)
16+
{
17+
$this->name = $name;
18+
$this->value = $value;
19+
}
20+
21+
public function getName()
22+
{
23+
return $this->name;
24+
}
25+
26+
public function getValue()
27+
{
28+
return $this->value;
29+
}
30+
31+
public function jsonSerialize()
32+
{
33+
return ['name' => $this->name, 'value' => $this->value];
34+
}
35+
}

0 commit comments

Comments
 (0)