From 5d7e0d67ccfc4839c70082d480e9b673c232ee55 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Mon, 6 May 2013 15:36:59 -0700 Subject: [PATCH] Updated tests for custom expires in --- tests/authorization/AuthServerTest.php | 41 ++++++++++++++++ .../ClientCredentialsGrantTest.php | 42 ++++++++++++++++ tests/authorization/PasswordGrantTest.php | 48 +++++++++++++++++++ tests/authorization/RefreshTokenTest.php | 43 +++++++++++++++++ 4 files changed, 174 insertions(+) diff --git a/tests/authorization/AuthServerTest.php b/tests/authorization/AuthServerTest.php index 5ffa92fb..50b03147 100644 --- a/tests/authorization/AuthServerTest.php +++ b/tests/authorization/AuthServerTest.php @@ -413,6 +413,47 @@ class Authorization_Server_test extends PHPUnit_Framework_TestCase $this->assertEquals(time()+$a->getExpiresIn(), $v['expires']); } + 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); + + $a = $this->returnDefault(); + $grant = new OAuth2\Grant\AuthCode($a); + $grant->setExpiresIn(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 OAuth2\Util\Request(array(), $_POST); + $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->getExpiresIn(), $v['expires_in']); + $this->assertNotEquals(time()+$a->getExpiresIn(), $v['expires']); + $this->assertEquals(30, $v['expires_in']); + $this->assertEquals(time()+30, $v['expires']); + } + public function test_issueAccessToken_HTTP_auth() { $this->client->shouldReceive('getClient')->andReturn(array( diff --git a/tests/authorization/ClientCredentialsGrantTest.php b/tests/authorization/ClientCredentialsGrantTest.php index 327030d9..679c54aa 100644 --- a/tests/authorization/ClientCredentialsGrantTest.php +++ b/tests/authorization/ClientCredentialsGrantTest.php @@ -280,6 +280,48 @@ class Client_Credentials_Grant_Test extends PHPUnit_Framework_TestCase $this->assertEquals(time()+$a->getExpiresIn(), $v['expires']); } + function test_issueAccessToken_clientCredentialsGrant_customExpiresIn() + { + $this->client->shouldReceive('getClient')->andReturn(array( + 'client_id' => 1234, + 'client_secret' => 5678, + 'redirect_uri' => 'http://foo/redirect', + 'name' => 'Example Client' + )); + + $this->client->shouldReceive('validateRefreshToken')->andReturn(1); + + $this->session->shouldReceive('validateAuthCode')->andReturn(1); + $this->session->shouldReceive('createSession')->andReturn(1); + $this->session->shouldReceive('deleteSession')->andReturn(null); + $this->session->shouldReceive('associateAccessToken')->andReturn(1); + + $a = $this->returnDefault(); + $grant = new OAuth2\Grant\ClientCredentials($a); + $grant->setExpiresIn(30); + $a->addGrantType($grant); + $a->requireScopeParam(false); + + $_POST['grant_type'] = 'client_credentials'; + $_POST['client_id'] = 1234; + $_POST['client_secret'] = 5678; + + $request = new OAuth2\Util\Request(array(), $_POST); + $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->getExpiresIn(), $v['expires_in']); + $this->assertNotEquals(time()+$a->getExpiresIn(), $v['expires']); + $this->assertEquals(30, $v['expires_in']); + $this->assertEquals(time()+30, $v['expires']); + } + function test_issueAccessToken_clientCredentialsGrant_withRefreshToken() { $this->client->shouldReceive('getClient')->andReturn(array( diff --git a/tests/authorization/PasswordGrantTest.php b/tests/authorization/PasswordGrantTest.php index bb54d808..aee3005f 100644 --- a/tests/authorization/PasswordGrantTest.php +++ b/tests/authorization/PasswordGrantTest.php @@ -461,6 +461,54 @@ class Password_Grant_Test extends PHPUnit_Framework_TestCase $this->assertEquals(time()+$a->getExpiresIn(), $v['expires']); } + function test_issueAccessToken_passwordGrant_customExpiresIn() + { + $this->client->shouldReceive('getClient')->andReturn(array( + 'client_id' => 1234, + 'client_secret' => 5678, + 'redirect_uri' => 'http://foo/redirect', + 'name' => 'Example Client' + )); + + $this->client->shouldReceive('validateRefreshToken')->andReturn(1); + + $this->session->shouldReceive('validateAuthCode')->andReturn(1); + $this->session->shouldReceive('createSession')->andReturn(1); + $this->session->shouldReceive('deleteSession')->andReturn(null); + $this->session->shouldReceive('updateRefreshToken')->andReturn(null); + $this->session->shouldReceive('associateAccessToken')->andReturn(1); + + $testCredentials = function($u, $p) { return 1; }; + + $a = $this->returnDefault(); + $pgrant = new OAuth2\Grant\Password($a); + $pgrant->setVerifyCredentialsCallback($testCredentials); + $pgrant->setExpiresIn(30); + $a->addGrantType($pgrant); + $a->requireScopeParam(false); + + $_POST['grant_type'] = 'password'; + $_POST['client_id'] = 1234; + $_POST['client_secret'] = 5678; + $_POST['username'] = 'foo'; + $_POST['password'] = 'bar'; + + $request = new OAuth2\Util\Request(array(), $_POST); + $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->getExpiresIn(), $v['expires_in']); + $this->assertNotEquals(time()+$a->getExpiresIn(), $v['expires']); + $this->assertEquals(30, $v['expires_in']); + $this->assertEquals(time()+30, $v['expires']); + } + function test_issueAccessToken_passwordGrant_withRefreshToken() { $this->client->shouldReceive('getClient')->andReturn(array( diff --git a/tests/authorization/RefreshTokenTest.php b/tests/authorization/RefreshTokenTest.php index 188eb6fb..b309638b 100644 --- a/tests/authorization/RefreshTokenTest.php +++ b/tests/authorization/RefreshTokenTest.php @@ -240,4 +240,47 @@ class Refresh_Token_test extends PHPUnit_Framework_TestCase $this->assertEquals($a->getExpiresIn(), $v['expires_in']); $this->assertEquals(time()+$a->getExpiresIn(), $v['expires']); } + + public function test_issueAccessToken_refreshTokenGrant_customExpiresIn() + { + $this->client->shouldReceive('getClient')->andReturn(array( + 'client_id' => 1234, + 'client_secret' => 5678, + 'redirect_uri' => 'http://foo/redirect', + 'name' => 'Example Client' + )); + + $this->session->shouldReceive('validateRefreshToken')->andReturn(1); + $this->session->shouldReceive('validateAuthCode')->andReturn(1); + $this->session->shouldReceive('updateSession')->andReturn(null); + $this->session->shouldReceive('updateRefreshToken')->andReturn(null); + $this->session->shouldReceive('getAccessToken')->andReturn(null); + $this->session->shouldReceive('getScopes')->andReturn(array('id' => 1)); + $this->session->shouldReceive('associateAccessToken')->andReturn(1); + $this->session->shouldReceive('associateRefreshToken')->andReturn(1); + $this->session->shouldReceive('associateScope')->andReturn(null); + + $a = $this->returnDefault(); + $grant = new OAuth2\Grant\RefreshToken($a); + $grant->setExpiresIn(30); + $a->addGrantType($grant); + + $v = $a->issueAccessToken(array( + 'grant_type' => 'refresh_token', + 'client_id' => 1234, + 'client_secret' => 5678, + 'refresh_token' => 'abcdef', + )); + + $this->assertArrayHasKey('access_token', $v); + $this->assertArrayHasKey('token_type', $v); + $this->assertArrayHasKey('expires', $v); + $this->assertArrayHasKey('expires_in', $v); + $this->assertArrayHasKey('refresh_token', $v); + + $this->assertNotEquals($a->getExpiresIn(), $v['expires_in']); + $this->assertNotEquals(time()+$a->getExpiresIn(), $v['expires']); + $this->assertEquals(30, $v['expires_in']); + $this->assertEquals(time()+30, $v['expires']); + } } \ No newline at end of file