oauth2-server/tests/authorization/AuthServerTest.php

516 lines
17 KiB
PHP
Raw Normal View History

2013-02-05 01:16:37 +05:30
<?php
use \Mockery as m;
class Authorization_Server_test extends PHPUnit_Framework_TestCase
2013-02-05 01:16:37 +05:30
{
private $client;
private $session;
private $scope;
public function setUp()
{
$this->client = M::mock('League\OAuth2\Server\Storage\ClientInterface');
$this->session = M::mock('League\OAuth2\Server\Storage\SessionInterface');
$this->scope = M::mock('League\OAuth2\Server\Storage\ScopeInterface');
2013-02-05 01:16:37 +05:30
}
private function returnDefault()
{
return new League\OAuth2\Server\Authorization($this->client, $this->session, $this->scope);
2013-02-05 01:16:37 +05:30
}
/**
* @expectedException PHPUnit_Framework_Error
*/
public function test__construct_NoStorage()
{
new League\OAuth2\Server\Authorization;
2013-02-05 01:16:37 +05:30
}
public function test__contruct_WithStorage()
{
$this->returnDefault();
2013-02-05 01:16:37 +05:30
}
public function test_getExceptionMessage()
{
$m = League\OAuth2\Server\Authorization::getExceptionMessage('access_denied');
2013-02-05 01:16:37 +05:30
$reflector = new ReflectionClass($this->returnDefault());
$exceptionMessages = $reflector->getProperty('exceptionMessages');
$exceptionMessages->setAccessible(true);
$v = $exceptionMessages->getValue();
$this->assertEquals($v['access_denied'], $m);
}
2013-02-15 21:59:00 +05:30
public function test_getExceptionCode()
{
$this->assertEquals('access_denied', League\OAuth2\Server\Authorization::getExceptionType(2));
2013-02-15 21:59:00 +05:30
}
2013-03-27 19:57:06 +05:30
public function test_getExceptionHttpHeaders()
{
$this->assertEquals(array('HTTP/1.1 401 Unauthorized'), League\OAuth2\Server\Authorization::getExceptionHttpHeaders('access_denied'));
$this->assertEquals(array('HTTP/1.1 500 Internal Server Error'), League\OAuth2\Server\Authorization::getExceptionHttpHeaders('server_error'));
$this->assertEquals(array('HTTP/1.1 501 Not Implemented'), League\OAuth2\Server\Authorization::getExceptionHttpHeaders('unsupported_grant_type'));
$this->assertEquals(array('HTTP/1.1 400 Bad Request'), League\OAuth2\Server\Authorization::getExceptionHttpHeaders('invalid_refresh'));
2013-03-27 19:57:06 +05:30
}
2013-02-05 01:16:37 +05:30
public function test_hasGrantType()
{
$a = $this->returnDefault();
$this->assertFalse($a->hasGrantType('test'));
2013-02-05 01:16:37 +05:30
}
public function test_addGrantType()
{
$a = $this->returnDefault();
$grant = M::mock('League\OAuth2\Server\Grant\GrantTypeInterface');
2013-02-05 15:37:45 +05:30
$grant->shouldReceive('getResponseType')->andReturn('test');
2013-12-06 01:55:55 +05:30
$grant->shouldReceive('setAuthorizationServer')->andReturn($grant);
2013-02-05 01:16:37 +05:30
$a->addGrantType($grant, 'test');
$this->assertTrue($a->hasGrantType('test'));
2013-02-05 01:16:37 +05:30
}
public function test_addGrantType_noIdentifier()
{
$a = $this->returnDefault();
$grant = M::mock('League\OAuth2\Server\Grant\GrantTypeInterface');
2013-02-05 15:37:45 +05:30
$grant->shouldReceive('getIdentifier')->andReturn('test');
$grant->shouldReceive('getResponseType')->andReturn('test');
2013-12-06 01:55:55 +05:30
$grant->shouldReceive('setAuthorizationServer')->andReturn($grant);
2013-02-05 01:16:37 +05:30
$a->addGrantType($grant);
$this->assertTrue($a->hasGrantType('test'));
2013-02-05 01:16:37 +05:30
}
public function test_getScopeDelimeter()
{
$a = $this->returnDefault();
$this->assertEquals(' ', $a->getScopeDelimeter());
2013-02-05 01:16:37 +05:30
}
public function test_setScopeDelimeter()
{
$a = $this->returnDefault();
$a->setScopeDelimeter(',');
$this->assertEquals(',', $a->getScopeDelimeter());
2013-02-05 01:16:37 +05:30
}
public function test_requireScopeParam()
2013-03-04 18:40:00 +05:30
{
$a = $this->returnDefault();
$a->requireScopeParam(false);
2013-03-04 18:40:00 +05:30
$reflector = new ReflectionClass($a);
$requestProperty = $reflector->getProperty('requireScopeParam');
2013-03-04 18:40:00 +05:30
$requestProperty->setAccessible(true);
$v = $requestProperty->getValue($a);
$this->assertFalse($v);
}
2013-03-22 16:37:55 +05:30
public function test_scopeParamRequired()
{
$a = $this->returnDefault();
$a->requireScopeParam(false);
$this->assertFalse($a->scopeParamRequired());
}
2013-03-21 21:55:44 +05:30
public function test_setDefaultScope()
{
$a = $this->returnDefault();
2013-03-21 21:55:44 +05:30
$a->setDefaultScope('test.default');
$reflector = new ReflectionClass($a);
$requestProperty = $reflector->getProperty('defaultScope');
$requestProperty->setAccessible(true);
$v = $requestProperty->getValue($a);
$this->assertEquals('test.default', $v);
}
2013-03-21 21:55:44 +05:30
public function test_getDefaultScope()
{
$a = $this->returnDefault();
$a->setDefaultScope('test.default');
$this->assertEquals('test.default', $a->getDefaultScope());
}
public function test_requireStateParam()
{
$a = $this->returnDefault();
$a->requireStateParam(true);
$reflector = new ReflectionClass($a);
$requestProperty = $reflector->getProperty('requireStateParam');
$requestProperty->setAccessible(true);
$v = $requestProperty->getValue($a);
$this->assertTrue($v);
}
public function test_getAccessTokenTTL()
2013-02-05 01:16:37 +05:30
{
$a = $this->returnDefault();
$a->setAccessTokenTTL(7200);
$this->assertEquals(7200, $a->getAccessTokenTTL());
2013-02-05 01:16:37 +05:30
}
public function test_setAccessTokenTTL()
2013-02-05 01:16:37 +05:30
{
$a = $this->returnDefault();
$a->setScopeDelimeter(';');
$this->assertEquals(';', $a->getScopeDelimeter());
}
public function test_setRequest()
{
$a = $this->returnDefault();
$request = new League\OAuth2\Server\Util\Request();
2013-02-05 01:16:37 +05:30
$a->setRequest($request);
$reflector = new ReflectionClass($a);
$requestProperty = $reflector->getProperty('request');
$requestProperty->setAccessible(true);
$v = $requestProperty->getValue($a);
2013-02-05 01:16:37 +05:30
$this->assertTrue($v instanceof League\OAuth2\Server\Util\RequestInterface);
2013-02-05 01:16:37 +05:30
}
public function test_getRequest()
{
$a = $this->returnDefault();
$request = new League\OAuth2\Server\Util\Request();
2013-02-05 01:16:37 +05:30
$a->setRequest($request);
$v = $a->getRequest();
2013-02-05 01:16:37 +05:30
$this->assertTrue($v instanceof League\OAuth2\Server\Util\RequestInterface);
2013-02-05 01:16:37 +05:30
}
public function test_getStorage()
{
$a = $this->returnDefault();
$this->assertTrue($a->getStorage('session') instanceof League\OAuth2\Server\Storage\SessionInterface);
2013-02-05 01:16:37 +05:30
}
public function test_getGrantType()
{
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$reflector = new ReflectionClass($a);
$method = $reflector->getMethod('getGrantType');
$method->setAccessible(true);
$result = $method->invoke($a, 'authorization_code');
$this->assertTrue($result instanceof League\OAuth2\Server\Grant\GrantTypeInterface);
2013-02-05 01:16:37 +05:30
}
/**
* @expectedException League\OAuth2\Server\Exception\InvalidGrantTypeException
* @expectedExceptionCode 9
*/
public function test_getGrantType_fail()
{
$a = $this->returnDefault();
$a->getGrantType('blah');
}
2013-02-05 01:16:37 +05:30
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-05 01:16:37 +05:30
* @expectedExceptionCode 0
*/
public function test_issueAccessToken_missingGrantType()
{
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$a->issueAccessToken();
2013-02-05 01:16:37 +05:30
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-05 01:16:37 +05:30
* @expectedExceptionCode 7
*/
public function test_issueAccessToken_badGrantType()
{
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$a->issueAccessToken(array('grant_type' => 'foo'));
2013-02-05 01:16:37 +05:30
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-05 01:16:37 +05:30
* @expectedExceptionCode 0
*/
public function test_issueAccessToken_missingClientId()
{
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$a->issueAccessToken(array(
2013-02-05 01:16:37 +05:30
'grant_type' => 'authorization_code'
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-05 01:16:37 +05:30
* @expectedExceptionCode 0
*/
public function test_issueAccessToken_missingClientSecret()
{
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$a->issueAccessToken(array(
2013-02-05 01:16:37 +05:30
'grant_type' => 'authorization_code',
'client_id' => 1234
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-05 01:16:37 +05:30
* @expectedExceptionCode 0
*/
public function test_issueAccessToken_missingRedirectUri()
{
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$a->issueAccessToken(array(
2013-02-05 01:16:37 +05:30
'grant_type' => 'authorization_code',
'client_id' => 1234,
'client_secret' => 5678
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-05 01:16:37 +05:30
* @expectedExceptionCode 8
*/
public function test_issueAccessToken_badClient()
{
2013-02-05 15:37:45 +05:30
$this->client->shouldReceive('getClient')->andReturn(false);
2013-02-05 01:16:37 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$a->issueAccessToken(array(
2013-02-05 01:16:37 +05:30
'grant_type' => 'authorization_code',
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect'
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-05 01:16:37 +05:30
* @expectedExceptionCode 0
*/
public function test_issueAccessToken_missingCode()
{
2013-02-05 15:37:45 +05:30
$this->client->shouldReceive('getClient')->andReturn(array());
2013-02-05 01:16:37 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$a->issueAccessToken(array(
2013-02-05 01:16:37 +05:30
'grant_type' => 'authorization_code',
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect'
));
}
/**
* @expectedException League\OAuth2\Server\Exception\ClientException
2013-02-05 01:16:37 +05:30
* @expectedExceptionCode 9
*/
public function test_issueAccessToken_badCode()
{
2013-02-05 15:37:45 +05:30
$this->client->shouldReceive('getClient')->andReturn(array());
$this->session->shouldReceive('validateAuthCode')->andReturn(false);
2013-02-05 01:16:37 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$a->issueAccessToken(array(
2013-02-05 01:16:37 +05:30
'grant_type' => 'authorization_code',
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'code' => 'foobar'
));
}
public function test_issueAccessToken_passedInput()
{
2013-02-05 15:37:45 +05:30
$this->client->shouldReceive('getClient')->andReturn(array(
2013-02-05 01:16:37 +05:30
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
2013-05-06 23:39:36 +05:30
$this->session->shouldReceive('validateAuthCode')->andReturn(array(
2013-05-11 06:09:29 +05:30
'session_id' => 1,
'authcode_id' => 1
2013-05-06 23:39:36 +05:30
));
2013-02-05 01:16:37 +05:30
$this->session->shouldReceive('updateSession')->andReturn(null);
2013-05-06 23:39:36 +05:30
$this->session->shouldReceive('removeAuthCode')->andReturn(null);
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
$this->session->shouldReceive('associateScope')->andReturn(null);
2013-05-11 06:09:29 +05:30
$this->session->shouldReceive('getAuthCodeScopes')->andReturn(array('scope_id' => 1));
2013-02-05 01:16:37 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$v = $a->issueAccessToken(array(
'grant_type' => 'authorization_code',
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'code' => 'foobar'
));
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
$this->assertEquals($a->getAccessTokenTTL(), $v['expires_in']);
$this->assertEquals(time()+$a->getAccessTokenTTL(), $v['expires']);
2013-02-05 01:16:37 +05:30
}
public function test_issueAccessToken()
{
2013-02-05 15:37:45 +05:30
$this->client->shouldReceive('getClient')->andReturn(array(
2013-02-05 01:16:37 +05:30
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('updateSession')->andReturn(null);
2013-05-06 23:39:36 +05:30
$this->session->shouldReceive('removeAuthCode')->andReturn(null);
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
2013-05-11 06:09:29 +05:30
$this->session->shouldReceive('getAuthCodeScopes')->andReturn(array('scope_id' => 1));
$this->session->shouldReceive('associateScope')->andReturn(null);
2013-02-05 01:16:37 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
2013-02-05 01:16:37 +05:30
$_POST['grant_type'] = 'authorization_code';
$_POST['client_id'] = 1234;
$_POST['client_secret'] = 5678;
$_POST['redirect_uri'] = 'http://foo/redirect';
$_POST['code'] = 'foobar';
$request = new League\OAuth2\Server\Util\Request(array(), $_POST);
2013-02-05 01:16:37 +05:30
$a->setRequest($request);
$v = $a->issueAccessToken();
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
$this->assertEquals($a->getAccessTokenTTL(), $v['expires_in']);
$this->assertEquals(time()+$a->getAccessTokenTTL(), $v['expires']);
2013-02-05 01:16:37 +05:30
}
2013-05-07 04:06:59 +05:30
public function test_issueAccessToken_customExpiresIn()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('updateSession')->andReturn(null);
$this->session->shouldReceive('removeAuthCode')->andReturn(null);
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
2013-05-11 06:09:29 +05:30
$this->session->shouldReceive('getAuthCodeScopes')->andReturn(array('scope_id' => 1));
$this->session->shouldReceive('associateScope')->andReturn(null);
2013-05-07 04:06:59 +05:30
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$grant = new League\OAuth2\Server\Grant\AuthCode();
$grant->setAccessTokenTTL(30);
2013-05-07 04:06:59 +05:30
$a->addGrantType($grant);
$_POST['grant_type'] = 'authorization_code';
$_POST['client_id'] = 1234;
$_POST['client_secret'] = 5678;
$_POST['redirect_uri'] = 'http://foo/redirect';
$_POST['code'] = 'foobar';
$request = new League\OAuth2\Server\Util\Request(array(), $_POST);
2013-05-07 04:06:59 +05:30
$a->setRequest($request);
$v = $a->issueAccessToken();
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
$this->assertNotEquals($a->getAccessTokenTTL(), $v['expires_in']);
$this->assertNotEquals(time()+$a->getAccessTokenTTL(), $v['expires']);
2013-05-07 04:06:59 +05:30
$this->assertEquals(30, $v['expires_in']);
$this->assertEquals(time()+30, $v['expires']);
}
public function test_issueAccessToken_HTTP_auth()
{
$this->client->shouldReceive('getClient')->andReturn(array(
'client_id' => 1234,
'client_secret' => 5678,
'redirect_uri' => 'http://foo/redirect',
'name' => 'Example Client'
));
$this->session->shouldReceive('validateAuthCode')->andReturn(1);
$this->session->shouldReceive('updateSession')->andReturn(null);
2013-05-06 23:39:36 +05:30
$this->session->shouldReceive('removeAuthCode')->andReturn(null);
$this->session->shouldReceive('associateAccessToken')->andReturn(1);
2013-05-11 06:09:29 +05:30
$this->session->shouldReceive('getAuthCodeScopes')->andReturn(array('scope_id' => 1));
$this->session->shouldReceive('associateScope')->andReturn(null);
$a = $this->returnDefault();
2013-12-06 01:55:55 +05:30
$a->addGrantType(new League\OAuth2\Server\Grant\AuthCode());
$_POST['grant_type'] = 'authorization_code';
$_SERVER['PHP_AUTH_USER'] = 1234;
$_SERVER['PHP_AUTH_PW'] = 5678;
$_POST['redirect_uri'] = 'http://foo/redirect';
$_POST['code'] = 'foobar';
$request = new League\OAuth2\Server\Util\Request(array(), $_POST, array(), array(), $_SERVER);
$a->setRequest($request);
$v = $a->issueAccessToken();
$this->assertArrayHasKey('access_token', $v);
$this->assertArrayHasKey('token_type', $v);
$this->assertArrayHasKey('expires', $v);
$this->assertArrayHasKey('expires_in', $v);
$this->assertEquals($a->getAccessTokenTTL(), $v['expires_in']);
$this->assertEquals(time()+$a->getAccessTokenTTL(), $v['expires']);
}
2013-02-05 01:16:37 +05:30
public function tearDown() {
M::close();
}
}