Skip to content

Commit

Permalink
Adds testing for claims
Browse files Browse the repository at this point in the history
  • Loading branch information
skroczek committed Jun 5, 2020
1 parent d2888f1 commit 3630fc7
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 1 deletion.
4 changes: 3 additions & 1 deletion tests/Grant/AbstractGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use League\OAuth2\Server\RequestTypes\AuthorizationRequest;
use LeagueTests\Stubs\AccessTokenEntity;
use LeagueTests\Stubs\AuthCodeEntity;
use LeagueTests\Stubs\ClaimEntity;
use LeagueTests\Stubs\ClientEntity;
use LeagueTests\Stubs\RefreshTokenEntity;
use LeagueTests\Stubs\ScopeEntity;
Expand Down Expand Up @@ -360,7 +361,8 @@ public function testIssueAccessToken()
new DateInterval('PT1H'),
new ClientEntity(),
123,
[new ScopeEntity()]
[new ScopeEntity()],
[new ClaimEntity('private', 'claim')]
);
$this->assertInstanceOf(AccessTokenEntityInterface::class, $accessToken);
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Grant/ClientCredentialsGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use League\OAuth2\Server\Entities\AccessTokenEntityInterface;
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClaimRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
use LeagueTests\Stubs\AccessTokenEntity;
Expand Down Expand Up @@ -43,10 +44,14 @@ public function testRespondToRequest()
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scope);
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);

$claimRepositoryMock = $this->getMockBuilder(ClaimRepositoryInterface::class)->getMock();
$claimRepositoryMock->method('getClaims')->willReturn([]);

$grant = new ClientCredentialsGrant();
$grant->setClientRepository($clientRepositoryMock);
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
$grant->setScopeRepository($scopeRepositoryMock);
$grant->setClaimRepository($claimRepositoryMock);
$grant->setDefaultScope(self::DEFAULT_SCOPE);
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));

Expand Down
5 changes: 5 additions & 0 deletions tests/Grant/ImplicitGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\OAuth2\Server\Exception\UniqueTokenIdentifierConstraintViolationException;
use League\OAuth2\Server\Grant\ImplicitGrant;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClaimRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
Expand Down Expand Up @@ -282,10 +283,14 @@ public function testAccessTokenRepositoryUniqueConstraintCheck()
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);

$claimRepositoryMock = $this->getMockBuilder(ClaimRepositoryInterface::class)->getMock();
$claimRepositoryMock->method('getClaims')->willReturn([]);

$grant = new ImplicitGrant(new \DateInterval('PT10M'));
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
$grant->setScopeRepository($scopeRepositoryMock);
$grant->setClaimRepository($claimRepositoryMock);

$this->assertInstanceOf(RedirectResponse::class, $grant->completeAuthorizationRequest($authRequest));
}
Expand Down
5 changes: 5 additions & 0 deletions tests/Grant/PasswordGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use League\OAuth2\Server\Grant\PasswordGrant;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClaimRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
Expand Down Expand Up @@ -57,10 +58,14 @@ public function testRespondToRequest()
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scope);
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0);

$claimRepositoryMock = $this->getMockBuilder(ClaimRepositoryInterface::class)->getMock();
$claimRepositoryMock->method('getClaims')->willReturn([]);

$grant = new PasswordGrant($userRepositoryMock, $refreshTokenRepositoryMock);
$grant->setClientRepository($clientRepositoryMock);
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
$grant->setScopeRepository($scopeRepositoryMock);
$grant->setClaimRepository($claimRepositoryMock);
$grant->setDefaultScope(self::DEFAULT_SCOPE);
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));

Expand Down
5 changes: 5 additions & 0 deletions tests/Grant/RefreshTokenGrantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface;
use League\OAuth2\Server\Grant\RefreshTokenGrant;
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ClaimRepositoryInterface;
use League\OAuth2\Server\Repositories\ClientRepositoryInterface;
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface;
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface;
Expand Down Expand Up @@ -52,6 +53,9 @@ public function testRespondToRequest()
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock();
$scopeRepositoryMock->method('getScopeEntityByIdentifier')->willReturn($scopeEntity);

$claimRepositoryMock = $this->getMockBuilder(ClaimRepositoryInterface::class)->getMock();
$claimRepositoryMock->method('getClaims')->willReturn([]);

$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock();
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity());
$accessTokenRepositoryMock->expects($this->once())->method('persistNewAccessToken')->willReturnSelf();
Expand All @@ -63,6 +67,7 @@ public function testRespondToRequest()
$grant = new RefreshTokenGrant($refreshTokenRepositoryMock);
$grant->setClientRepository($clientRepositoryMock);
$grant->setScopeRepository($scopeRepositoryMock);
$grant->setClaimRepository($claimRepositoryMock);
$grant->setAccessTokenRepository($accessTokenRepositoryMock);
$grant->setEncryptionKey($this->cryptStub->getKey());
$grant->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));
Expand Down
4 changes: 4 additions & 0 deletions tests/ResponseTypes/BearerResponseTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface;
use League\OAuth2\Server\ResponseTypes\BearerTokenResponse;
use LeagueTests\Stubs\AccessTokenEntity;
use LeagueTests\Stubs\ClaimEntity;
use LeagueTests\Stubs\ClientEntity;
use LeagueTests\Stubs\RefreshTokenEntity;
use LeagueTests\Stubs\ScopeEntity;
Expand All @@ -32,11 +33,14 @@ public function testGenerateHttpResponse()
$scope = new ScopeEntity();
$scope->setIdentifier('basic');

$claim = new ClaimEntity('_private', 'claim');

$accessToken = new AccessTokenEntity();
$accessToken->setIdentifier('abcdef');
$accessToken->setExpiryDateTime((new DateTimeImmutable())->add(new DateInterval('PT1H')));
$accessToken->setClient($client);
$accessToken->addScope($scope);
$accessToken->addClaim($claim);
$accessToken->setPrivateKey(new CryptKey('file://' . __DIR__ . '/../Stubs/private.key'));

$refreshToken = new RefreshTokenEntity();
Expand Down
35 changes: 35 additions & 0 deletions tests/Stubs/ClaimEntity.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php


namespace LeagueTests\Stubs;


use League\OAuth2\Server\Entities\ClaimEntityInterface;

class ClaimEntity implements ClaimEntityInterface
{

private $name;
private $value;

public function __construct($name, $value)
{
$this->name = $name;
$this->value = $value;
}

public function getName()
{
return $this->name;
}

public function getValue()
{
return $this->value;
}

public function jsonSerialize()
{
return ['name' => $this->name, 'value' => $this->value];
}
}

0 comments on commit 3630fc7

Please sign in to comment.