Permalink
Browse files

Adding unit tests for Credential model. Closes #442

  • Loading branch information...
1 parent 261d46f commit 1fd74ff990bc9084a47ea8f953c3a1450655b818 @jmathai jmathai committed Jan 13, 2012
View
@@ -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
@@ -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))
@@ -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'))
@@ -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'))
@@ -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;
}
}
@@ -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);
@@ -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)
@@ -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;
+ }
}
@@ -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;
}
@@ -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;
}
@@ -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; }
@@ -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;
@@ -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;
Oops, something went wrong.

0 comments on commit 1fd74ff

Please sign in to comment.