Skip to content

Commit

Permalink
Adding unit tests for Credential model. Closes photo#442
Browse files Browse the repository at this point in the history
  • Loading branch information
jmathai committed Jan 13, 2012
1 parent 261d46f commit 1fd74ff
Show file tree
Hide file tree
Showing 7 changed files with 409 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -9,6 +9,7 @@ src/configs/override.ini
src/configs/generated/*
src/html/photos/**/*
src/html/assets/themes/*
src/html/assets/cache
src/plugins/*
src/userdata/**/*
!default
1 change: 1 addition & 0 deletions src/libraries/models/BaseModel.php
Expand Up @@ -18,6 +18,7 @@ public function __construct()
$this->logger = getLogger();
$this->route = getRoute();
$this->session = getSession();
$this->cache = getCache();

// really just for setup when the systems don't yet exist
if(isset($this->config->systems))
Expand Down
47 changes: 36 additions & 11 deletions src/libraries/models/Credential.php
Expand Up @@ -9,15 +9,30 @@ class Credential extends BaseModel
const statusActive = '1';

const nonceCacheKey = 'oauthTimestamps';
private $consumer, $oauthException, $oauthParams, $provider;
public $consumer, $oauthException, $oauthParams, $provider, $sendHeadersOnError = true;

public function __construct()
/**
* Constructor
*/
public function __construct($config = null, $params = null)
{
parent::__construct();
if(isset($params['utility']))
$this->utility = $params['utility'];
else
$this->utility = new Utility;

if(class_exists('OAuthProvider'))
$this->provider = new OAuthProvider($this->getOAuthParameters());
}

/**
* Add an oauth credential for this user
*
* @param string $name Human readable name for this credential
* @param array $params Array of permissions
* @return mixed Credential ID on success, false on failure
*/
public function add($name, $permissions = array('read'))
{
if(!class_exists('OAuthProvider'))
Expand Down Expand Up @@ -46,6 +61,14 @@ public function add($name, $permissions = array('read'))
return false;
}

/**
* Convert an existing token from one type to another.
* Typically used to convert from an authorized request token to an access token
*
* @param string $name Human readable name for this credential
* @param array $params Array of permissions
* @return mixed Credential ID on success, false on failure
*/
public function convertToken($id, $toTokenType)
{
if(!class_exists('OAuthProvider'))
Expand Down Expand Up @@ -79,7 +102,7 @@ public function checkRequest()
catch(OAuthException $e)
{
$this->oauthException = $e;
$this->logger->crit(OAuthProvider::reportProblem($e));
$this->logger->crit(OAuthProvider::reportProblem($e, $this->sendHeadersOnError));
return false;
}
}
Expand Down Expand Up @@ -118,7 +141,7 @@ public function checkTimestampAndNonce($provider)
return false;
}

$cache = getConfig()->get(self::nonceCacheKey);
$cache = $this->cache->get(self::nonceCacheKey);
if(!$cache)
$cache = array();
list($lastTimestamp, $nonces) = each($cache);
Expand Down Expand Up @@ -193,9 +216,8 @@ public function getOAuthParameters()
if($this->oauthParams)
return $this->oauthParams;

$utilityObj = new Utility;
$this->oauthParams = array();
$headers = $utilityObj->getAllHeaders();
$headers = $this->utility->getAllHeaders();
foreach($headers as $name => $header)
{
if(stripos($name, 'authorization') === 0)
Expand Down Expand Up @@ -233,11 +255,14 @@ public function isOAuthRequest()
}
}

function getCredential()
if(!function_exists('getCredential'))
{
static $credential;
if(!$credential)
$credential = new Credential;
function getCredential()
{
static $credential;
if(!$credential)
$credential = new Credential;

return $credential;
return $credential;
}
}
10 changes: 6 additions & 4 deletions src/libraries/models/User.php
Expand Up @@ -130,9 +130,10 @@ public function getUserRecord()

public function isLoggedIn()
{
if(getCredential()->isOAuthRequest())
$credential = new Credential;
if($credential->isOAuthRequest())
{
if(!getCredential()->checkRequest())
if(!$credential->checkRequest())
{
return false;
}
Expand All @@ -146,9 +147,10 @@ public function isLoggedIn()

public function isOwner()
{
if(getCredential()->isOAuthRequest())
$credential = new Credential;
if($credential->isOAuthRequest())
{
if(!getCredential()->checkRequest())
if(!$credential->checkRequest())
{
return false;
}
Expand Down
9 changes: 8 additions & 1 deletion src/tests/helpers/init.php
Expand Up @@ -6,6 +6,10 @@ function arrayToObject($array)
return json_decode(json_encode($array));
}

if(!function_exists('getCache'))
{
function getCache() { return new FauxObject; }
}
if(!function_exists('getConfig'))
{
function getConfig() { return new FauxObject; }
Expand Down Expand Up @@ -44,7 +48,10 @@ function getCredential() { return new FauxObject; }
{
class BaseModel
{
public function __construct() {}
public function __construct()
{
$this->logger = getLogger();
}
public function inject($key, $value)
{
$this->$key = $value;
Expand Down
8 changes: 7 additions & 1 deletion src/tests/libraries/models/AuthenticationTest.php
Expand Up @@ -3,9 +3,15 @@
require_once sprintf('%s/tests/helpers/init.php', $baseDir);
require_once sprintf('%s/libraries/models/Authentication.php', $baseDir);

if(!class_exists('User'))
{
class User {}
}

$_REQUEST['oauth_consumer_key'] = 'foo';
class AuthenticationWrapper extends Authentication
{
public function __construct() { parent::__construct();}
public function __construct() { parent::__construct(); }
public function inject($key, $value)
{
$this->$key = $value;
Expand Down

0 comments on commit 1fd74ff

Please sign in to comment.