oauth2-server/tests/Grant/ClientCredentialsTest.php

202 lines
7.2 KiB
PHP
Raw Normal View History

2014-01-17 16:06:57 +05:30
<?php
namespace LeagueTests\Grant;
use League\OAuth2\Server\Grant\ClientCredentialsGrant;
2014-05-02 19:44:12 +05:30
use League\OAuth2\Server\Entity\ScopeEntity;
use League\OAuth2\Server\Entity\ClientEntity;
2014-05-02 21:51:53 +05:30
use League\OAuth2\Server\AuthorizationServer;
2014-01-17 16:06:57 +05:30
use League\OAuth2\Server\Grant\ClientException;
use Mockery as M;
class ClientCredentialsGrantTest extends \PHPUnit_Framework_TestCase
2014-01-17 16:06:57 +05:30
{
function testCompleteFlowMissingClientId()
{
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidRequestException');
2014-01-17 16:06:57 +05:30
$_POST['grant_type'] = 'client_credentials';
2014-05-02 21:51:53 +05:30
$server = new AuthorizationServer;
$grant = new ClientCredentialsGrant;
2014-01-17 16:06:57 +05:30
$server->addGrantType($grant);
$server->issueAccessToken();
}
function testCompleteFlowMissingClientSecret()
{
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidRequestException');
2014-01-17 16:06:57 +05:30
$_POST = [
'grant_type' => 'client_credentials',
'client_id' => 'testapp'
];
2014-05-02 21:51:53 +05:30
$server = new AuthorizationServer;
$grant = new ClientCredentialsGrant;
2014-01-17 16:06:57 +05:30
$server->addGrantType($grant);
$server->issueAccessToken();
}
function testCompleteFlowInvalidClient()
{
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidClientException');
2014-01-17 16:06:57 +05:30
$_POST = [
'grant_type' => 'client_credentials',
'client_id' => 'testapp',
'client_secret' => 'foobar'
];
2014-05-02 21:51:53 +05:30
$server = new AuthorizationServer;
$grant = new ClientCredentialsGrant;
2014-01-17 16:06:57 +05:30
$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 testCompleteFlowInvalidScope()
{
$this->setExpectedException('League\OAuth2\Server\Exception\InvalidScopeException');
2014-01-17 16:06:57 +05:30
$_POST = [
'grant_type' => 'client_credentials',
'client_id' => 'testapp',
'client_secret' => 'foobar',
'scope' => 'foo'
];
2014-05-02 21:51:53 +05:30
$server = new AuthorizationServer;
$grant = new ClientCredentialsGrant;
2014-01-17 16:06:57 +05:30
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
$clientStorage->shouldReceive('setServer');
$clientStorage->shouldReceive('get')->andReturn(
2014-05-02 21:51:53 +05:30
(new ClientEntity($server))->setId('testapp')
2014-01-17 16:06:57 +05:30
);
$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 testCompleteFlowNoScopes()
{
$_POST = [
'grant_type' => 'client_credentials',
'client_id' => 'testapp',
'client_secret' => 'foobar'
];
2014-05-02 21:51:53 +05:30
$server = new AuthorizationServer;
$grant = new ClientCredentialsGrant;
2014-01-17 16:06:57 +05:30
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
$clientStorage->shouldReceive('setServer');
$clientStorage->shouldReceive('get')->andReturn(
2014-05-02 21:51:53 +05:30
(new ClientEntity($server))->setId('testapp')
2014-01-17 16:06:57 +05:30
);
$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');
// $scopeStorage->shouldReceive('get')->andReturn(
2014-05-02 21:51:53 +05:30
// // (new ScopeEntity($server))->setId('foo')
2014-01-17 16:06:57 +05:30
// );
$server->setClientStorage($clientStorage);
$server->setScopeStorage($scopeStorage);
$server->setSessionStorage($sessionStorage);
$server->setAccessTokenStorage($accessTokenStorage);
$server->addGrantType($grant);
$server->issueAccessToken();
}
function testCompleteFlow()
{
$_POST = [
'grant_type' => 'client_credentials',
'client_id' => 'testapp',
'client_secret' => 'foobar',
'scope' => 'foo'
];
2014-05-02 21:51:53 +05:30
$server = new AuthorizationServer;
$grant = new ClientCredentialsGrant;
2014-01-17 16:06:57 +05:30
$clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface');
$clientStorage->shouldReceive('setServer');
$clientStorage->shouldReceive('get')->andReturn(
2014-05-02 21:51:53 +05:30
(new ClientEntity($server))->setId('testapp')
2014-01-17 16:06:57 +05:30
);
$sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface');
$sessionStorage->shouldReceive('setServer');
$sessionStorage->shouldReceive('create')->andreturn(123);
$sessionStorage->shouldReceive('getScopes')->shouldReceive('getScopes')->andReturn([
2014-05-02 21:51:53 +05:30
(new ScopeEntity($server))->setId('foo')
2014-01-17 16:06:57 +05:30
]);
$sessionStorage->shouldReceive('associateScope');
$accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface');
$accessTokenStorage->shouldReceive('setServer');
$accessTokenStorage->shouldReceive('create');
$accessTokenStorage->shouldReceive('getScopes')->andReturn([
2014-05-02 21:51:53 +05:30
(new ScopeEntity($server))->setId('foo')
2014-01-17 16:06:57 +05:30
]);
$accessTokenStorage->shouldReceive('associateScope');
$scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
$scopeStorage->shouldReceive('setServer');
$scopeStorage->shouldReceive('get')->andReturn(
2014-05-02 21:51:53 +05:30
(new ScopeEntity($server))->setId('foo')
2014-01-17 16:06:57 +05:30
);
$server->setClientStorage($clientStorage);
$server->setScopeStorage($scopeStorage);
$server->setSessionStorage($sessionStorage);
$server->setAccessTokenStorage($accessTokenStorage);
$server->addGrantType($grant);
$server->issueAccessToken();
}
}