Skip to content
Browse files

Add comments to the JOAuth unit test.

  • Loading branch information...
1 parent 28f1de1 commit a0d5793cbc4bb1dea63e17cd9254e53e22e8b848 @aaronschmitz aaronschmitz committed with LouisLandry Jul 26, 2012
Showing with 66 additions and 33 deletions.
  1. +66 −33 tests/suites/unit/joomla/oauth/JOauthOauth2clientTest.php
View
99 tests/suites/unit/joomla/oauth/JOauthOauth2clientTest.php
@@ -39,6 +39,7 @@ class JOauth2clientTest extends PHPUnit_Framework_TestCase
* This method is called before a test is executed.
*
* @access protected
+ * @return void
*/
protected function setUp()
{
@@ -53,14 +54,16 @@ protected function setUp()
* This method is called after a test is executed.
*
* @access protected
+ * @return void
*/
protected function tearDown()
{
}
/**
* Tests the auth method
- * @group JOauth2client
+ *
+ * @group JOauth
* @return void
*/
public function testAuth()
@@ -70,6 +73,7 @@ public function testAuth()
$this->object->setOption('scope', array('https://www.googleapis.com/auth/adsense', 'https://www.googleapis.com/auth/calendar'));
$this->object->setOption('redirecturi', 'http://localhost/oauth');
$this->object->setOption('requestparams', array('access_type' => 'offline', 'approval_prompt' => 'auto'));
+ $this->object->setOption('sendheaders', true);
$this->object->auth();
$headers = JResponse::getHeaders();
@@ -87,7 +91,7 @@ public function testAuth()
$this->object->setOption('tokenurl', 'https://accounts.google.com/o/oauth2/token');
$this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
$this->input->set('code', '4/wEr_dK8SDkjfpwmc98KejfiwJP-f4wm.kdowmnr82jvmeisjw94mKFIJE48mcEM');
- $this->client->expects($this->once())->method('request')->will($this->returnCallback('httpTokenCallback'));
+ $this->client->expects($this->once())->method('request')->will($this->returnCallback('httpCallback'));
$result = $this->object->auth();
$this->assertEquals('accessvalue', $result['access_token']);
$this->assertEquals('refreshvalue', $result['refresh_token']);
@@ -97,7 +101,8 @@ public function testAuth()
/**
* Tests the auth method
- * @group JOauth2client
+ *
+ * @group JOauth
* @return void
*/
public function testCreateUrl()
@@ -120,26 +125,29 @@ public function testCreateUrl()
/**
* Tests the auth method
- * @group JOauth2client
+ *
+ * @group JOauth
* @return void
*/
public function testQuery()
{
$token['access_token'] = 'accessvalue';
$token['refresh_token'] = 'refreshvalue';
- $token['created'] = time() - 3600;
+ $token['created'] = time() - 1800;
$token['expires_in'] = 3600;
$this->object->setToken($token);
- $this->client->expects($this->atLeastOnce())->method('request')->will($this->returnCallback('httpQueryCallback'));
- $result = $this->object->query('https://www.googleapis.com/auth/calendar', array('param' => 'value'));
- $this->assertEquals('Lorem ipsum dolor sit amet.', $result->body);
+ $this->client->expects($this->atLeastOnce())->method('request')->will($this->returnCallback('httpCallback'));
+ $result = $this->object->query('https://www.googleapis.com/auth/calendar', array('param' => 'value'), array(), 'post');
+
+ $this->assertEquals($result->body, 'Lorem ipsum dolor sit amet.');
$this->assertEquals(200, $result->code);
}
/**
* Tests the setOption method
- * @group JOauth2client
+ *
+ * @group JOauth
* @return void
*/
public function testSetOption()
@@ -154,7 +162,8 @@ public function testSetOption()
/**
* Tests the getOption method
- * @group JOauth2client
+ *
+ * @group JOauth
* @return void
*/
public function testGetOption()
@@ -169,37 +178,54 @@ public function testGetOption()
/**
* Tests the setToken method
- * @group JOauth2client
+ *
+ * @group JOauth
* @return void
*/
public function testSetToken()
{
- $this->object->setToken('value');
+ $this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA'));
$this->assertThat(
$this->options->get('accesstoken'),
- $this->equalTo('value')
+ $this->equalTo(array('access_token' => 'RANDOM STRING OF DATA'))
+ );
+
+ $this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires_in' => 3600));
+
+ $this->assertThat(
+ $this->options->get('accesstoken'),
+ $this->equalTo(array('access_token' => 'RANDOM STRING OF DATA', 'expires_in' => 3600))
+ );
+
+ $this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires' => 3600));
+
+ $this->assertThat(
+ $this->options->get('accesstoken'),
+ $this->equalTo(array('access_token' => 'RANDOM STRING OF DATA', 'expires_in' => 3600))
);
}
/**
* Tests the getToken method
- * @group JOauth2client
+ *
+ * @group JOauth
* @return void
*/
public function testGetToken()
{
- $this->options->set('accesstoken', 'value');
+ $this->options->set('accesstoken', array('access_token' => 'RANDOM STRING OF DATA'));
$this->assertThat(
$this->object->getToken(),
- $this->equalTo('value')
+ $this->equalTo(array('access_token' => 'RANDOM STRING OF DATA'))
);
}
/**
* Tests the refreshToken method
- * @group JOauth2client
+ *
+ * @group JOauth
* @return void
*/
public function testRefreshToken()
@@ -208,8 +234,10 @@ public function testRefreshToken()
$this->object->setOption('clientid', '01234567891011.apps.googleusercontent.com');
$this->object->setOption('clientsecret', 'jeDs8rKw_jDJW8MMf-ff8ejs');
$this->object->setOption('redirecturi', 'http://localhost/oauth');
+ $this->object->setOption('userefresh', true);
+ $this->object->setToken(array('access_token' => 'RANDOM STRING OF DATA', 'expires' => 3600, 'refresh_token' => ' RANDOM STRING OF DATA'));
- $this->client->expects($this->once())->method('request')->will($this->returnCallback('httpTokenCallback'));
+ $this->client->expects($this->once())->method('request')->will($this->returnCallback('httpCallback'));
$result = $this->object->refreshToken();
$this->assertEquals('accessvalue', $result['access_token']);
$this->assertEquals('refreshvalue', $result['refresh_token']);
@@ -219,25 +247,30 @@ public function testRefreshToken()
}
/**
- * Callback for the use of JHttp to return a token
- */
-function httpTokenCallback($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
-{
- $response->code = 200;
- $response->body = '{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}';
- return $response;
-}
-
-/**
* Callback for the use of JHttp to return a response to an OAuth request
+ *
+ * @param string $method The HTTP method for sending the request.
+ * @param JUri $uri The URI to the resource to request.
+ * @param mixed $data Either an associative array or a string to be sent with the request.
+ * @param array $headers An array of request headers to send with the request.
+ * @param integer $timeout Read timeout in seconds.
+ * @param string $userAgent The optional user agent string to send with the request.
+ *
+ * @return JHttpResponse
*/
-function httpQueryCallback($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
+function httpCallback($method, JUri $uri, $data = null, array $headers = null, $timeout = null, $userAgent = null)
{
- if (isset($data['grant_type']) && $data['grant_type'] == 'refresh_token')
+ if (isset($data['grant_type']))
+ {
+ $response->code = 200;
+ $response->headers = array('Content-Type' => 'application/json');
+ $response->body = '{"access_token":"accessvalue","refresh_token":"refreshvalue","expires_in":3600}';
+ }
+ else
{
- return httpTokenCallback($method, $uri, $data, $headers, $timeout, $userAgent);
+ $response->code = 200;
+ $response->headers = array('Content-Type' => 'text/html');
+ $response->body = 'Lorem ipsum dolor sit amet.';
}
- $response->code = 200;
- $response->body = 'Lorem ipsum dolor sit amet.';
return $response;
}

0 comments on commit a0d5793

Please sign in to comment.
Something went wrong with that request. Please try again.