2014-03-10 01:05:23 +05:30
< ? php
namespace LeagueTests\Grant ;
use League\OAuth2\Server\Grant\Password ;
use League\OAuth2\Server\Grant\RefreshToken ;
use League\OAuth2\Server\Entity\Scope ;
use League\OAuth2\Server\Entity\Client ;
use League\OAuth2\Server\AuthorizationServer ;
use League\OAuth2\Server\Grant\ClientException ;
use Mockery as M ;
class PasswordTest extends \PHPUnit_Framework_TestCase
{
function testCompleteFlowMissingClientId ()
{
2014-04-06 23:43:45 +05:30
$this -> setExpectedException ( 'League\OAuth2\Server\Exception\ClientException' , 'The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the "client_id" parameter.' );
2014-03-10 01:05:23 +05:30
$_POST [ 'grant_type' ] = 'password' ;
$server = new AuthorizationServer ;
$grant = new Password ;
$server -> addGrantType ( $grant );
$server -> issueAccessToken ();
}
function testCompleteFlowMissingClientSecret ()
{
2014-04-06 23:43:45 +05:30
$this -> setExpectedException ( 'League\OAuth2\Server\Exception\ClientException' , 'The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the "client_secret" parameter.' );
2014-03-10 01:05:23 +05:30
$_POST = [
'grant_type' => 'password' ,
'client_id' => 'testapp'
];
$server = new AuthorizationServer ;
$grant = new Password ;
$server -> addGrantType ( $grant );
$server -> issueAccessToken ();
}
function testCompleteFlowInvalidClient ()
{
2014-04-06 23:43:45 +05:30
$this -> setExpectedException ( 'League\OAuth2\Server\Exception\ClientException' , 'Client authentication failed' );
2014-03-10 01:05:23 +05:30
$_POST = [
'grant_type' => 'password' ,
'client_id' => 'testapp' ,
'client_secret' => 'foobar'
];
$server = new AuthorizationServer ;
$grant = new Password ;
$clientStorage = M :: mock ( 'League\OAuth2\Server\Storage\ClientInterface' );
$clientStorage -> shouldReceive ( 'setServer' );
$clientStorage -> shouldReceive ( 'get' ) -> andReturn ( null );
$server -> setClientStorage ( $clientStorage );
$server -> addGrantType ( $grant );
$server -> issueAccessToken ();
}
function testNoUsername ()
{
2014-04-06 23:43:45 +05:30
$this -> setExpectedException ( 'League\OAuth2\Server\Exception\ClientException' , 'The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the "username" parameter.' );
2014-03-10 01:05:23 +05:30
$_POST = [
'grant_type' => 'password' ,
'client_id' => 'testapp' ,
'client_secret' => 'foobar'
];
$server = new AuthorizationServer ;
$grant = new Password ;
$clientStorage = M :: mock ( 'League\OAuth2\Server\Storage\ClientInterface' );
$clientStorage -> shouldReceive ( 'setServer' );
$clientStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Client ( $server )) -> setId ( 'testapp' )
);
$sessionStorage = M :: mock ( 'League\OAuth2\Server\Storage\SessionInterface' );
$sessionStorage -> shouldReceive ( 'setServer' );
$sessionStorage -> shouldReceive ( 'create' );
$sessionStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([]);
$accessTokenStorage = M :: mock ( 'League\OAuth2\Server\Storage\AccessTokenInterface' );
$accessTokenStorage -> shouldReceive ( 'setServer' );
$accessTokenStorage -> shouldReceive ( 'create' );
$accessTokenStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([]);
$scopeStorage = M :: mock ( 'League\OAuth2\Server\Storage\ScopeInterface' );
$scopeStorage -> shouldReceive ( 'setServer' );
$scopeStorage -> shouldReceive ( 'get' ) -> andReturn ( null );
$server -> setClientStorage ( $clientStorage );
$server -> setScopeStorage ( $scopeStorage );
$server -> setSessionStorage ( $sessionStorage );
$server -> setAccessTokenStorage ( $accessTokenStorage );
$server -> addGrantType ( $grant );
$server -> issueAccessToken ();
}
function testNoPassword ()
{
2014-04-06 23:43:45 +05:30
$this -> setExpectedException ( 'League\OAuth2\Server\Exception\ClientException' , 'The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the "password" parameter.' );
2014-03-10 01:05:23 +05:30
$_POST = [
'grant_type' => 'password' ,
'client_id' => 'testapp' ,
'client_secret' => 'foobar' ,
'username' => 'foo'
];
$server = new AuthorizationServer ;
$grant = new Password ;
$clientStorage = M :: mock ( 'League\OAuth2\Server\Storage\ClientInterface' );
$clientStorage -> shouldReceive ( 'setServer' );
$clientStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Client ( $server )) -> setId ( 'testapp' )
);
$sessionStorage = M :: mock ( 'League\OAuth2\Server\Storage\SessionInterface' );
$sessionStorage -> shouldReceive ( 'setServer' );
$sessionStorage -> shouldReceive ( 'create' );
$sessionStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([]);
$accessTokenStorage = M :: mock ( 'League\OAuth2\Server\Storage\AccessTokenInterface' );
$accessTokenStorage -> shouldReceive ( 'setServer' );
$accessTokenStorage -> shouldReceive ( 'create' );
$accessTokenStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([]);
$scopeStorage = M :: mock ( 'League\OAuth2\Server\Storage\ScopeInterface' );
$scopeStorage -> shouldReceive ( 'setServer' );
$scopeStorage -> shouldReceive ( 'get' ) -> andReturn ( null );
$server -> setClientStorage ( $clientStorage );
$server -> setScopeStorage ( $scopeStorage );
$server -> setSessionStorage ( $sessionStorage );
$server -> setAccessTokenStorage ( $accessTokenStorage );
$server -> addGrantType ( $grant );
$server -> issueAccessToken ();
}
function testNoCallable ()
{
2014-04-06 23:43:45 +05:30
$this -> setExpectedException ( 'League\OAuth2\Server\Exception\InvalidGrantTypeException' , 'Null or non-callable callback set on Password grant' );
2014-03-10 01:05:23 +05:30
$_POST = [
'grant_type' => 'password' ,
'client_id' => 'testapp' ,
'client_secret' => 'foobar' ,
'username' => 'foo' ,
'password' => 'foobar'
];
$server = new AuthorizationServer ;
$grant = new Password ;
$clientStorage = M :: mock ( 'League\OAuth2\Server\Storage\ClientInterface' );
$clientStorage -> shouldReceive ( 'setServer' );
$clientStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Client ( $server )) -> setId ( 'testapp' )
);
$sessionStorage = M :: mock ( 'League\OAuth2\Server\Storage\SessionInterface' );
$sessionStorage -> shouldReceive ( 'setServer' );
$sessionStorage -> shouldReceive ( 'create' );
$sessionStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([]);
$accessTokenStorage = M :: mock ( 'League\OAuth2\Server\Storage\AccessTokenInterface' );
$accessTokenStorage -> shouldReceive ( 'setServer' );
$accessTokenStorage -> shouldReceive ( 'create' );
$accessTokenStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([]);
$scopeStorage = M :: mock ( 'League\OAuth2\Server\Storage\ScopeInterface' );
$scopeStorage -> shouldReceive ( 'setServer' );
$scopeStorage -> shouldReceive ( 'get' ) -> andReturn ( null );
$server -> setClientStorage ( $clientStorage );
$server -> setScopeStorage ( $scopeStorage );
$server -> setSessionStorage ( $sessionStorage );
$server -> setAccessTokenStorage ( $accessTokenStorage );
$server -> addGrantType ( $grant );
$server -> issueAccessToken ();
}
function testCompleteFlowInvalidScope ()
{
2014-04-06 23:43:45 +05:30
$this -> setExpectedException ( 'League\OAuth2\Server\Exception\ClientException' , 'The requested scope is invalid, unknown, or malformed. Check the "foo" scope.' );
2014-03-10 01:05:23 +05:30
$_POST = [
'grant_type' => 'password' ,
'client_id' => 'testapp' ,
'client_secret' => 'foobar' ,
2014-04-06 23:43:45 +05:30
'username' => 'foo' ,
'password' => 'foobar' ,
2014-03-10 01:05:23 +05:30
'scope' => 'foo'
];
$server = new AuthorizationServer ;
$grant = new Password ;
$clientStorage = M :: mock ( 'League\OAuth2\Server\Storage\ClientInterface' );
$clientStorage -> shouldReceive ( 'setServer' );
$clientStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Client ( $server )) -> setId ( 'testapp' )
);
$sessionStorage = M :: mock ( 'League\OAuth2\Server\Storage\SessionInterface' );
$sessionStorage -> shouldReceive ( 'setServer' );
$sessionStorage -> shouldReceive ( 'create' );
$sessionStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([]);
$accessTokenStorage = M :: mock ( 'League\OAuth2\Server\Storage\AccessTokenInterface' );
$accessTokenStorage -> shouldReceive ( 'setServer' );
$accessTokenStorage -> shouldReceive ( 'create' );
$accessTokenStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([]);
$scopeStorage = M :: mock ( 'League\OAuth2\Server\Storage\ScopeInterface' );
$scopeStorage -> shouldReceive ( 'setServer' );
$scopeStorage -> shouldReceive ( 'get' ) -> andReturn ( null );
$server -> setClientStorage ( $clientStorage );
$server -> setScopeStorage ( $scopeStorage );
$server -> setSessionStorage ( $sessionStorage );
$server -> setAccessTokenStorage ( $accessTokenStorage );
$grant -> setVerifyCredentialsCallback ( function () {
return 123 ;
});
$server -> addGrantType ( $grant );
$server -> issueAccessToken ();
}
function testCompleteFlowNoScopes ()
{
2014-04-06 23:43:45 +05:30
$this -> setExpectedException ( 'League\OAuth2\Server\Exception\ClientException' , 'The request is missing a required parameter, includes an invalid parameter value, includes a parameter more than once, or is otherwise malformed. Check the "scope" parameter.' );
2014-03-10 01:05:23 +05:30
$_POST = [
'grant_type' => 'password' ,
'client_id' => 'testapp' ,
'client_secret' => 'foobar' ,
'username' => 'username' ,
'password' => 'password'
];
$server = new AuthorizationServer ;
$grant = new Password ;
$clientStorage = M :: mock ( 'League\OAuth2\Server\Storage\ClientInterface' );
$clientStorage -> shouldReceive ( 'setServer' );
$clientStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Client ( $server )) -> setId ( 'testapp' )
);
$sessionStorage = M :: mock ( 'League\OAuth2\Server\Storage\SessionInterface' );
$sessionStorage -> shouldReceive ( 'setServer' );
$sessionStorage -> shouldReceive ( 'create' ) -> andreturn ( 123 );
$sessionStorage -> shouldReceive ( 'getScopes' ) -> shouldReceive ( 'getScopes' ) -> andReturn ([]);
$sessionStorage -> shouldReceive ( 'associateScope' );
$accessTokenStorage = M :: mock ( 'League\OAuth2\Server\Storage\AccessTokenInterface' );
$accessTokenStorage -> shouldReceive ( 'setServer' );
$accessTokenStorage -> shouldReceive ( 'create' );
$accessTokenStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([]);
$accessTokenStorage -> shouldReceive ( 'associateScope' );
$scopeStorage = M :: mock ( 'League\OAuth2\Server\Storage\ScopeInterface' );
$scopeStorage -> shouldReceive ( 'setServer' );
$server -> setClientStorage ( $clientStorage );
$server -> setScopeStorage ( $scopeStorage );
$server -> setSessionStorage ( $sessionStorage );
$server -> setAccessTokenStorage ( $accessTokenStorage );
$server -> requireScopeParam ( true );
$grant -> setVerifyCredentialsCallback ( function () {
return 123 ;
});
$server -> addGrantType ( $grant );
$server -> issueAccessToken ();
}
function testCompleteFlowInvalidCredentials ()
{
2014-04-06 23:43:45 +05:30
$this -> setExpectedException ( 'League\OAuth2\Server\Exception\ClientException' , 'The user credentials were incorrect.' );
2014-03-10 01:05:23 +05:30
$_POST = [
'grant_type' => 'password' ,
'client_id' => 'testapp' ,
'client_secret' => 'foobar' ,
'scope' => 'foo' ,
'username' => 'username' ,
'password' => 'password'
];
$server = new AuthorizationServer ;
$grant = new Password ;
$clientStorage = M :: mock ( 'League\OAuth2\Server\Storage\ClientInterface' );
$clientStorage -> shouldReceive ( 'setServer' );
$clientStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Client ( $server )) -> setId ( 'testapp' )
);
$sessionStorage = M :: mock ( 'League\OAuth2\Server\Storage\SessionInterface' );
$sessionStorage -> shouldReceive ( 'setServer' );
$sessionStorage -> shouldReceive ( 'create' ) -> andreturn ( 123 );
$sessionStorage -> shouldReceive ( 'getScopes' ) -> shouldReceive ( 'getScopes' ) -> andReturn ([
( new Scope ( $server )) -> setId ( 'foo' )
]);
$sessionStorage -> shouldReceive ( 'associateScope' );
$accessTokenStorage = M :: mock ( 'League\OAuth2\Server\Storage\AccessTokenInterface' );
$accessTokenStorage -> shouldReceive ( 'setServer' );
$accessTokenStorage -> shouldReceive ( 'create' );
$accessTokenStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([
( new Scope ( $server )) -> setId ( 'foo' )
]);
$accessTokenStorage -> shouldReceive ( 'associateScope' );
$scopeStorage = M :: mock ( 'League\OAuth2\Server\Storage\ScopeInterface' );
$scopeStorage -> shouldReceive ( 'setServer' );
$scopeStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Scope ( $server )) -> setId ( 'foo' )
);
$server -> setClientStorage ( $clientStorage );
$server -> setScopeStorage ( $scopeStorage );
$server -> setSessionStorage ( $sessionStorage );
$server -> setAccessTokenStorage ( $accessTokenStorage );
$grant -> setVerifyCredentialsCallback ( function () {
return false ;
});
$server -> addGrantType ( $grant );
$server -> issueAccessToken ();
}
function testCompleteFlow ()
{
$_POST = [
'grant_type' => 'password' ,
'client_id' => 'testapp' ,
'client_secret' => 'foobar' ,
'scope' => 'foo' ,
'username' => 'username' ,
'password' => 'password'
];
$server = new AuthorizationServer ;
$grant = new Password ;
$clientStorage = M :: mock ( 'League\OAuth2\Server\Storage\ClientInterface' );
$clientStorage -> shouldReceive ( 'setServer' );
$clientStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Client ( $server )) -> setId ( 'testapp' )
);
$sessionStorage = M :: mock ( 'League\OAuth2\Server\Storage\SessionInterface' );
$sessionStorage -> shouldReceive ( 'setServer' );
$sessionStorage -> shouldReceive ( 'create' ) -> andreturn ( 123 );
$sessionStorage -> shouldReceive ( 'getScopes' ) -> shouldReceive ( 'getScopes' ) -> andReturn ([
( new Scope ( $server )) -> setId ( 'foo' )
]);
$sessionStorage -> shouldReceive ( 'associateScope' );
$accessTokenStorage = M :: mock ( 'League\OAuth2\Server\Storage\AccessTokenInterface' );
$accessTokenStorage -> shouldReceive ( 'setServer' );
$accessTokenStorage -> shouldReceive ( 'create' );
$accessTokenStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([
( new Scope ( $server )) -> setId ( 'foo' )
]);
$accessTokenStorage -> shouldReceive ( 'associateScope' );
$scopeStorage = M :: mock ( 'League\OAuth2\Server\Storage\ScopeInterface' );
$scopeStorage -> shouldReceive ( 'setServer' );
$scopeStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Scope ( $server )) -> setId ( 'foo' )
);
$server -> setClientStorage ( $clientStorage );
$server -> setScopeStorage ( $scopeStorage );
$server -> setSessionStorage ( $sessionStorage );
$server -> setAccessTokenStorage ( $accessTokenStorage );
$grant -> setVerifyCredentialsCallback ( function () {
return 123 ;
});
$server -> addGrantType ( $grant );
$response = $server -> issueAccessToken ();
$this -> assertTrue ( isset ( $response [ 'access_token' ]));
$this -> assertTrue ( isset ( $response [ 'token_type' ]));
$this -> assertTrue ( isset ( $response [ 'expires_in' ]));
$this -> assertTrue ( isset ( $response [ 'expires' ]));
}
function testCompleteFlowRefreshToken ()
{
$_POST = [
'grant_type' => 'password' ,
'client_id' => 'testapp' ,
'client_secret' => 'foobar' ,
'scope' => 'foo' ,
'username' => 'username' ,
'password' => 'password'
];
$server = new AuthorizationServer ;
$grant = new Password ;
$clientStorage = M :: mock ( 'League\OAuth2\Server\Storage\ClientInterface' );
$clientStorage -> shouldReceive ( 'setServer' );
$clientStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Client ( $server )) -> setId ( 'testapp' )
);
$sessionStorage = M :: mock ( 'League\OAuth2\Server\Storage\SessionInterface' );
$sessionStorage -> shouldReceive ( 'setServer' );
$sessionStorage -> shouldReceive ( 'create' ) -> andreturn ( 123 );
$sessionStorage -> shouldReceive ( 'getScopes' ) -> shouldReceive ( 'getScopes' ) -> andReturn ([
( new Scope ( $server )) -> setId ( 'foo' )
]);
$sessionStorage -> shouldReceive ( 'associateScope' );
$accessTokenStorage = M :: mock ( 'League\OAuth2\Server\Storage\AccessTokenInterface' );
$accessTokenStorage -> shouldReceive ( 'setServer' );
$accessTokenStorage -> shouldReceive ( 'create' );
$accessTokenStorage -> shouldReceive ( 'getScopes' ) -> andReturn ([
( new Scope ( $server )) -> setId ( 'foo' )
]);
$accessTokenStorage -> shouldReceive ( 'associateScope' );
$scopeStorage = M :: mock ( 'League\OAuth2\Server\Storage\ScopeInterface' );
$scopeStorage -> shouldReceive ( 'setServer' );
$scopeStorage -> shouldReceive ( 'get' ) -> andReturn (
( new Scope ( $server )) -> setId ( 'foo' )
);
$refreshTokenStorage = M :: mock ( 'League\OAuth2\Server\Storage\RefreshTokenInterface' );
$refreshTokenStorage -> shouldReceive ( 'setServer' );
$refreshTokenStorage -> shouldReceive ( 'create' );
$refreshTokenStorage -> shouldReceive ( 'associateScope' );
$server -> setClientStorage ( $clientStorage );
$server -> setScopeStorage ( $scopeStorage );
$server -> setSessionStorage ( $sessionStorage );
$server -> setAccessTokenStorage ( $accessTokenStorage );
$server -> setRefreshTokenStorage ( $refreshTokenStorage );
$grant -> setVerifyCredentialsCallback ( function () {
return 123 ;
});
$server -> addGrantType ( $grant );
$server -> addGrantType ( new RefreshToken );
$response = $server -> issueAccessToken ();
$this -> assertTrue ( isset ( $response [ 'access_token' ]));
$this -> assertTrue ( isset ( $response [ 'refresh_token' ]));
$this -> assertTrue ( isset ( $response [ 'token_type' ]));
$this -> assertTrue ( isset ( $response [ 'expires_in' ]));
$this -> assertTrue ( isset ( $response [ 'expires' ]));
}
}