ALL OF THE TESTS!

This commit is contained in:
Alex Bilbie 2012-08-06 15:32:35 +01:00
parent a9e816f336
commit 30ef11c1d7
2 changed files with 254 additions and 13 deletions

View File

@ -0,0 +1,191 @@
<?php
use Oauth2\Authentication\Database;
class OAuthdb implements Database
{
private $sessions = array();
private $sessions_client_type_id = array();
private $sessions_code = array();
private $session_scopes = array();
private $clients = array(0 => array(
'client_id' => 'test',
'client_secret' => 'test',
'redirect_uri' => 'http://example.com/test',
'name' => 'Test Client'
));
private $scopes = array('test' => array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
));
public function validateClient(
$clientId,
$clientSecret = null,
$redirectUri = null
)
{
if ($clientId !== $this->clients[0]['client_id'])
{
return false;
}
if ($clientSecret !== null && $clientSecret !== $this->clients[0]['client_secret'])
{
return false;
}
if ($redirectUri !== null && $redirectUri !== $this->clients[0]['redirect_uri'])
{
return false;
}
return $this->clients[0];
}
public function newSession(
$clientId,
$redirectUri,
$type = 'user',
$typeId = null,
$authCode = null,
$accessToken = null,
$accessTokenExpire = null,
$stage = 'requested'
)
{
$id = count($this->sessions);
$this->sessions[$id] = array(
'id' => $id,
'client_id' => $clientId,
'redirect_uri' => $redirectUri,
'owner_type' => $type,
'owner_id' => $typeId,
'auth_code' => $authCode,
'access_token' => $accessToken,
'access_token_expire' => $accessTokenExpire,
'stage' => $stage
);
$this->sessions_client_type_id[$clientId . ':' . $type . ':' . $typeId] = $id;
$this->sessions_code[$clientId . ':' . $redirectUri . ':' . $authCode] = $id;
return true;
}
public function updateSession(
$sessionId,
$authCode = null,
$accessToken = null,
$accessTokenExpire = null,
$stage = 'requested'
)
{
$this->sessions[$sessionId]['auth_code'] = $authCode;
$this->sessions[$sessionId]['access_token'] = $accessToken;
$this->sessions[$sessionId]['access_token_expire'] = $accessTokenExpire;
$this->sessions[$sessionId]['stage'] = $stage;
return true;
}
public function deleteSession(
$clientId,
$type,
$typeId
)
{
$key = $clientId . ':' . $type . ':' . $typeId;
if (isset($this->sessions_client_type_id[$key]))
{
unset($this->sessions[$this->sessions_client_type_id[$key]]);
}
return true;
}
public function validateAuthCode(
$clientId,
$redirectUri,
$authCode
)
{
$key = $clientId . ':' . $redirectUri . ':' . $authCode;
if (isset($this->sessions_code[$key]))
{
return $this->sessions[$this->sessions_code[$key]];
}
return false;
}
public function hasSession(
$type,
$typeId,
$clientId
)
{
die('not implemented hasSession');
}
public function getAccessToken($sessionId)
{
die('not implemented getAccessToken');
}
public function removeAuthCode($sessionId)
{
die('not implemented removeAuthCode');
}
public function setAccessToken(
$sessionId,
$accessToken
)
{
die('not implemented setAccessToken');
}
public function addSessionScope(
$sessionId,
$scope
)
{
if ( ! isset($this->session_scopes[$sessionId]))
{
$this->session_scopes[$sessionId] = array();
}
$this->session_scopes[$sessionId][] = $scope;
return true;
}
public function getScope($scope)
{
if ( ! isset($this->scopes[$scope]))
{
return false;
}
return $this->scopes[$scope];
}
public function updateSessionScopeAccessToken(
$sessionId,
$accessToken
)
{
return true;
}
public function accessTokenScopes($accessToken)
{
die('not implemented accessTokenScopes');
}
}

View File

@ -2,11 +2,13 @@
class Server_test extends PHPUnit_Framework_TestCase { class Server_test extends PHPUnit_Framework_TestCase {
function __construct() function setUp()
{ {
$this->oauth = new Oauth2\Authentication\Server(); $this->oauth = new Oauth2\Authentication\Server();
//$this->oauth->registerDbAbstractor($this->oauthdb); require_once('database_mock.php');
$this->oauthdb = new OAuthdb();
$this->oauth->registerDbAbstractor($this->oauthdb);
} }
function test_generateCode() function test_generateCode()
@ -33,28 +35,34 @@ class Server_test extends PHPUnit_Framework_TestCase {
$this->assertEquals('http://example.com/foo#foo=bar', $result3); $this->assertEquals('http://example.com/foo#foo=bar', $result3);
} }
function test_checkClientAuthoriseParams() function test_checkClientAuthoriseParams_GET()
{ {
// Test without passing params
$_GET['client_id'] = 'test'; $_GET['client_id'] = 'test';
$_GET['redirect_uri'] = 'http://example.com/test'; $_GET['redirect_uri'] = 'http://example.com/test';
$_GET['response_type'] = 'code'; $_GET['response_type'] = 'code';
$_GET['scope'] = 'test'; $_GET['scope'] = 'test';
$this->assertEquals(array( $expect = array(
'client_id' => 'test', 'client_id' => 'test',
'redirect_uri' => 'http://example.com/test', 'redirect_uri' => 'http://example.com/test',
'response_type' => 'code', 'response_type' => 'code',
'scopes' => array( 'scopes' => array(
0 => array(
'id' => 1, 'id' => 1,
'scope' => 'test', 'scope' => 'test',
'name' => 'test', 'name' => 'test',
'description' => 'test' 'description' => 'test'
) )
), $this->oauth->checkClientAuthoriseParams()); )
);
$result = $this->oauth->checkClientAuthoriseParams();
// Test with passed params $this->assertEquals($expect, $result);
}
function test_checkClientAuthoriseParams_PassedParams()
{
unset($_GET['client_id']); unset($_GET['client_id']);
unset($_GET['redirect_uri']); unset($_GET['redirect_uri']);
unset($_GET['response_type']); unset($_GET['response_type']);
@ -71,7 +79,7 @@ class Server_test extends PHPUnit_Framework_TestCase {
'client_id' => 'test', 'client_id' => 'test',
'redirect_uri' => 'http://example.com/test', 'redirect_uri' => 'http://example.com/test',
'response_type' => 'code', 'response_type' => 'code',
'scopes' => array(array( 'scopes' => array(0 => array(
'id' => 1, 'id' => 1,
'scope' => 'test', 'scope' => 'test',
'name' => 'test', 'name' => 'test',
@ -81,6 +89,22 @@ class Server_test extends PHPUnit_Framework_TestCase {
} }
function test_newAuthoriseRequest() function test_newAuthoriseRequest()
{
$result = $this->oauth->newAuthoriseRequest('user', '123', array(
'client_id' => 'test',
'redirect_uri' => 'http://example.com/test',
'scopes' => array(array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
));
$this->assertEquals(40, strlen($result));
}
function test_newAuthoriseRequest_isUnique()
{ {
$result1 = $this->oauth->newAuthoriseRequest('user', '123', array( $result1 = $this->oauth->newAuthoriseRequest('user', '123', array(
'client_id' => 'test', 'client_id' => 'test',
@ -104,8 +128,34 @@ class Server_test extends PHPUnit_Framework_TestCase {
)) ))
)); ));
$this->assertEquals(40, strlen($result1));
$this->assertNotEquals($result1, $result2); $this->assertNotEquals($result1, $result2);
} }
function test_issueAccessToken_POST()
{
$auth_code = $this->oauth->newAuthoriseRequest('user', '123', array(
'client_id' => 'test',
'redirect_uri' => 'http://example.com/test',
'scopes' => array(array(
'id' => 1,
'scope' => 'test',
'name' => 'test',
'description' => 'test'
))
));
$_POST['client_id'] = 'test';
$_POST['client_secret'] = 'test';
$_POST['redirect_uri'] = 'http://example.com/test';
$_POST['grant_type'] = 'authorization_code';
$_POST['code'] = $auth_code;
$result = $this->oauth->issueAccessToken();
$this->assertCount(3, $result);
$this->assertArrayHasKey('access_token', $result);
$this->assertArrayHasKey('token_type', $result);
$this->assertArrayHasKey('expires_in', $result);
}
} }