-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26a4bd4
commit 9a22326
Showing
8 changed files
with
307 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,5 +27,8 @@ | |
"require": { | ||
"php": ">=5.6.4", | ||
"laravel/passport": "^1.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "~5.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit backupGlobals="false" | ||
backupStaticAttributes="false" | ||
bootstrap="vendor/autoload.php" | ||
colors="true" | ||
convertErrorsToExceptions="true" | ||
convertNoticesToExceptions="true" | ||
convertWarningsToExceptions="true" | ||
processIsolation="false" | ||
stopOnFailure="false" | ||
syntaxCheck="false" | ||
> | ||
<testsuites> | ||
<testsuite name="Package Test Suite"> | ||
<directory suffix=".php">./tests/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
|
||
namespace LeagueTests\Grant; | ||
|
||
use Adaojunior\Passport\SocialGrant; | ||
use Adaojunior\Passport\SocialUserResolverInterface; | ||
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; | ||
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; | ||
use League\OAuth2\Server\Repositories\AccessTokenRepositoryInterface; | ||
use League\OAuth2\Server\Repositories\ClientRepositoryInterface; | ||
use League\OAuth2\Server\Repositories\RefreshTokenRepositoryInterface; | ||
use League\OAuth2\Server\Repositories\ScopeRepositoryInterface; | ||
|
||
use LeagueTests\Stubs\AccessTokenEntity; | ||
use LeagueTests\Stubs\ClientEntity; | ||
use LeagueTests\Stubs\RefreshTokenEntity; | ||
use LeagueTests\Stubs\StubResponseType; | ||
use LeagueTests\Stubs\UserEntity; | ||
use Zend\Diactoros\ServerRequest; | ||
|
||
class PasswordGrantTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testGetIdentifier() | ||
{ | ||
$resolverMock = $this->getMockBuilder(SocialUserResolverInterface::class)->getMock(); | ||
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(); | ||
|
||
$grant = new SocialGrant($resolverMock, $refreshTokenRepositoryMock); | ||
$this->assertEquals('social', $grant->getIdentifier()); | ||
} | ||
|
||
public function testRespondToRequest() | ||
{ | ||
$client = new ClientEntity(); | ||
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); | ||
$clientRepositoryMock->method('getClientEntity')->willReturn($client); | ||
|
||
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); | ||
$accessTokenRepositoryMock->method('getNewToken')->willReturn(new AccessTokenEntity()); | ||
$accessTokenRepositoryMock->method('persistNewAccessToken')->willReturnSelf(); | ||
|
||
$resolverMock = $this->getMockBuilder(SocialUserResolverInterface::class)->getMock(); | ||
$userEntity = new UserEntity(); | ||
$resolverMock->method('resolve')->willReturn($userEntity); | ||
|
||
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(); | ||
$refreshTokenRepositoryMock->method('persistNewRefreshToken')->willReturnSelf(); | ||
$refreshTokenRepositoryMock->method('getNewRefreshToken')->willReturn(new RefreshTokenEntity()); | ||
|
||
$scopeRepositoryMock = $this->getMockBuilder(ScopeRepositoryInterface::class)->getMock(); | ||
$scopeRepositoryMock->method('finalizeScopes')->willReturnArgument(0); | ||
|
||
$grant = new SocialGrant($resolverMock, $refreshTokenRepositoryMock); | ||
$grant->setClientRepository($clientRepositoryMock); | ||
$grant->setAccessTokenRepository($accessTokenRepositoryMock); | ||
$grant->setScopeRepository($scopeRepositoryMock); | ||
|
||
$serverRequest = new ServerRequest(); | ||
$serverRequest = $serverRequest->withParsedBody( | ||
[ | ||
'client_id' => 'foo', | ||
'client_secret' => 'bar', | ||
'network' => 'foo', | ||
'access_token' => 'bar', | ||
] | ||
); | ||
|
||
$responseType = new StubResponseType(); | ||
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new \DateInterval('PT5M')); | ||
|
||
$this->assertTrue($responseType->getAccessToken() instanceof AccessTokenEntityInterface); | ||
$this->assertTrue($responseType->getRefreshToken() instanceof RefreshTokenEntityInterface); | ||
} | ||
|
||
/** | ||
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException | ||
*/ | ||
public function testRespondToRequestMissingNetwork() | ||
{ | ||
$client = new ClientEntity(); | ||
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); | ||
$clientRepositoryMock->method('getClientEntity')->willReturn($client); | ||
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); | ||
$resolverMock = $this->getMockBuilder(SocialUserResolverInterface::class)->getMock(); | ||
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(); | ||
$grant = new SocialGrant($resolverMock, $refreshTokenRepositoryMock); | ||
$grant->setClientRepository($clientRepositoryMock); | ||
$grant->setAccessTokenRepository($accessTokenRepositoryMock); | ||
$serverRequest = new ServerRequest(); | ||
$serverRequest = $serverRequest->withParsedBody( | ||
[ | ||
'client_id' => 'foo', | ||
'client_secret' => 'bar', | ||
] | ||
); | ||
$responseType = new StubResponseType(); | ||
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new \DateInterval('PT5M')); | ||
} | ||
|
||
/** | ||
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException | ||
*/ | ||
public function testRespondToRequestMissingAccessToken() | ||
{ | ||
$client = new ClientEntity(); | ||
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); | ||
$clientRepositoryMock->method('getClientEntity')->willReturn($client); | ||
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); | ||
$resolverMock = $this->getMockBuilder(SocialUserResolverInterface::class)->getMock(); | ||
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(); | ||
$grant = new SocialGrant($resolverMock, $refreshTokenRepositoryMock); | ||
$grant->setClientRepository($clientRepositoryMock); | ||
$grant->setAccessTokenRepository($accessTokenRepositoryMock); | ||
$serverRequest = new ServerRequest(); | ||
$serverRequest = $serverRequest->withParsedBody( | ||
[ | ||
'client_id' => 'foo', | ||
'client_secret' => 'bar', | ||
'network' => 'facebook', | ||
] | ||
); | ||
$responseType = new StubResponseType(); | ||
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new \DateInterval('PT5M')); | ||
} | ||
|
||
/** | ||
* @expectedException \League\OAuth2\Server\Exception\OAuthServerException | ||
*/ | ||
public function testRespondToRequestBadCredentials() | ||
{ | ||
$client = new ClientEntity(); | ||
$clientRepositoryMock = $this->getMockBuilder(ClientRepositoryInterface::class)->getMock(); | ||
$clientRepositoryMock->method('getClientEntity')->willReturn($client); | ||
$accessTokenRepositoryMock = $this->getMockBuilder(AccessTokenRepositoryInterface::class)->getMock(); | ||
$resolverMock = $this->getMockBuilder(SocialUserResolverInterface::class)->getMock(); | ||
$resolverMock->method('resolve')->willReturn(null); | ||
$refreshTokenRepositoryMock = $this->getMockBuilder(RefreshTokenRepositoryInterface::class)->getMock(); | ||
$grant = new SocialGrant($resolverMock, $refreshTokenRepositoryMock); | ||
$grant->setClientRepository($clientRepositoryMock); | ||
$grant->setAccessTokenRepository($accessTokenRepositoryMock); | ||
$serverRequest = new ServerRequest(); | ||
$serverRequest = $serverRequest->withParsedBody( | ||
[ | ||
'client_id' => 'foo', | ||
'client_secret' => 'bar', | ||
'network' => 'facebook', | ||
'access_token' => 'ACCESS_TOKEN', | ||
] | ||
); | ||
$responseType = new StubResponseType(); | ||
$grant->respondToAccessTokenRequest($serverRequest, $responseType, new \DateInterval('PT5M')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace LeagueTests\Stubs; | ||
|
||
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; | ||
use League\OAuth2\Server\Entities\Traits\AccessTokenTrait; | ||
use League\OAuth2\Server\Entities\Traits\EntityTrait; | ||
use League\OAuth2\Server\Entities\Traits\TokenEntityTrait; | ||
|
||
class AccessTokenEntity implements AccessTokenEntityInterface | ||
{ | ||
use AccessTokenTrait, TokenEntityTrait, EntityTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
namespace LeagueTests\Stubs; | ||
|
||
use League\OAuth2\Server\Entities\ClientEntityInterface; | ||
use League\OAuth2\Server\Entities\Traits\ClientTrait; | ||
use League\OAuth2\Server\Entities\Traits\EntityTrait; | ||
|
||
class ClientEntity implements ClientEntityInterface | ||
{ | ||
use EntityTrait, ClientTrait; | ||
|
||
public function setRedirectUri($uri) | ||
{ | ||
$this->redirectUri = $uri; | ||
} | ||
|
||
public function setName($name) | ||
{ | ||
$this->name = $name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?php | ||
|
||
namespace LeagueTests\Stubs; | ||
|
||
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; | ||
use League\OAuth2\Server\Entities\Traits\EntityTrait; | ||
use League\OAuth2\Server\Entities\Traits\RefreshTokenTrait; | ||
|
||
class RefreshTokenEntity implements RefreshTokenEntityInterface | ||
{ | ||
use RefreshTokenTrait, EntityTrait; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace LeagueTests\Stubs; | ||
|
||
use League\OAuth2\Server\Entities\AccessTokenEntityInterface; | ||
use League\OAuth2\Server\Entities\RefreshTokenEntityInterface; | ||
use League\OAuth2\Server\Exception\OAuthServerException; | ||
use League\OAuth2\Server\ResponseTypes\AbstractResponseType; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Zend\Diactoros\Response; | ||
|
||
class StubResponseType extends AbstractResponseType | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
public function getAccessToken() | ||
{ | ||
return $this->accessToken; | ||
} | ||
|
||
public function getRefreshToken() | ||
{ | ||
return $this->refreshToken; | ||
} | ||
|
||
/** | ||
* @param \League\OAuth2\Server\Entities\AccessTokenEntityInterface $accessToken | ||
*/ | ||
public function setAccessToken(AccessTokenEntityInterface $accessToken) | ||
{ | ||
$this->accessToken = $accessToken; | ||
} | ||
|
||
/** | ||
* @param \League\OAuth2\Server\Entities\RefreshTokenEntityInterface $refreshToken | ||
*/ | ||
public function setRefreshToken(RefreshTokenEntityInterface $refreshToken) | ||
{ | ||
$this->refreshToken = $refreshToken; | ||
} | ||
|
||
/** | ||
* @param ServerRequestInterface $request | ||
* | ||
* @throws \League\OAuth2\Server\Exception\OAuthServerException | ||
* | ||
* @return \Psr\Http\Message\ServerRequestInterface | ||
*/ | ||
public function validateAccessToken(ServerRequestInterface $request) | ||
{ | ||
if ($request->getHeader('authorization')[0] === 'Basic test') { | ||
return $request->withAttribute('oauth_access_token_id', 'test'); | ||
} | ||
|
||
throw OAuthServerException::accessDenied(); | ||
} | ||
|
||
/** | ||
* @param ResponseInterface $response | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
public function generateHttpResponse(ResponseInterface $response) | ||
{ | ||
return new Response(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace LeagueTests\Stubs; | ||
|
||
use League\OAuth2\Server\Entities\Traits\EntityTrait; | ||
use League\OAuth2\Server\Entities\UserEntityInterface; | ||
|
||
class UserEntity implements UserEntityInterface | ||
{ | ||
use EntityTrait; | ||
|
||
public function __construct() | ||
{ | ||
$this->setIdentifier(123); | ||
} | ||
} |