From 583c21e7dbe40438694940b7a1954f2852012e89 Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Sat, 8 Nov 2014 17:16:17 +0000 Subject: [PATCH] Updated unit tests --- tests/unit/Exception/OAuthExceptionTest.php | 9 ++++ tests/unit/Grant/AuthCodeGrantTest.php | 53 ++++++++++++++++++++- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/tests/unit/Exception/OAuthExceptionTest.php b/tests/unit/Exception/OAuthExceptionTest.php index be2d0e92..7ab5bed1 100644 --- a/tests/unit/Exception/OAuthExceptionTest.php +++ b/tests/unit/Exception/OAuthExceptionTest.php @@ -22,4 +22,13 @@ class OAuthExceptionTest extends \PHPUnit_Framework_TestCase $exception->httpStatusCode = 501; $this->assertSame($exception->getHttpHeaders(), ['HTTP/1.1 501 Not Implemented']); } + + public function testShouldRedirect() + { + $exception = new \League\OAuth2\Server\Exception\OAuthException(); + $exception->redirectUri = 'http://example.com/'; + $exception->errorType = 'Error'; + $this->assertTrue($exception->shouldRedirect()); + $this->assertEquals('http://example.com/?error=Error&message=An+error+occured', $exception->getRedirectUri()); + } } diff --git a/tests/unit/Grant/AuthCodeGrantTest.php b/tests/unit/Grant/AuthCodeGrantTest.php index ad36a011..3b418f02 100644 --- a/tests/unit/Grant/AuthCodeGrantTest.php +++ b/tests/unit/Grant/AuthCodeGrantTest.php @@ -446,6 +446,57 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase $server->issueAccessToken(); } + public function testCompleteFlowExpiredCode() + { + $this->setExpectedException('League\OAuth2\Server\Exception\InvalidRequestException'); + + $_POST = [ + 'grant_type' => 'authorization_code', + 'client_id' => 'testapp', + 'client_secret' => 'foobar', + 'redirect_uri' => 'http://foo/bar', + 'code' => 'foobar' + ]; + + $server = new AuthorizationServer; + $grant = new AuthCodeGrant; + + $clientStorage = M::mock('League\OAuth2\Server\Storage\ClientInterface'); + $clientStorage->shouldReceive('setServer'); + $clientStorage->shouldReceive('get')->andReturn( + (new ClientEntity($server))->hydrate(['id' => 'testapp']) + ); + + $sessionStorage = M::mock('League\OAuth2\Server\Storage\SessionInterface'); + $sessionStorage->shouldReceive('setServer'); + $sessionStorage->shouldReceive('create'); + $sessionStorage->shouldReceive('getScopes')->andReturn([]); + + $accessTokenStorage = M::mock('League\OAuth2\Server\Storage\AccessTokenInterface'); + $accessTokenStorage->shouldReceive('setServer'); + $accessTokenStorage->shouldReceive('create'); + $accessTokenStorage->shouldReceive('getScopes')->andReturn([]); + + $scopeStorage = M::mock('League\OAuth2\Server\Storage\ScopeInterface'); + $scopeStorage->shouldReceive('setServer'); + $scopeStorage->shouldReceive('get')->andReturn(null); + + $authCodeStorage = M::mock('League\OAuth2\Server\Storage\AuthCodeInterface'); + $authCodeStorage->shouldReceive('setServer'); + $authCodeStorage->shouldReceive('get')->andReturn( + (new AuthCodeEntity($server))->setId('foobar')->setExpireTime(time() - 300)->setRedirectUri('http://foo/bar') + ); + + $server->setClientStorage($clientStorage); + $server->setScopeStorage($scopeStorage); + $server->setSessionStorage($sessionStorage); + $server->setAccessTokenStorage($accessTokenStorage); + $server->setAuthCodeStorage($authCodeStorage); + + $server->addGrantType($grant); + $server->issueAccessToken(); + } + public function testCompleteFlowRedirectUriMismatch() { $this->setExpectedException('League\OAuth2\Server\Exception\InvalidRequestException'); @@ -484,7 +535,7 @@ class AuthCodeGrantTest extends \PHPUnit_Framework_TestCase $authCodeStorage = M::mock('League\OAuth2\Server\Storage\AuthCodeInterface'); $authCodeStorage->shouldReceive('setServer'); $authCodeStorage->shouldReceive('get')->andReturn( - (new AuthCodeEntity($server))->setId('foobar')->setRedirectUri('http://fail/face') + (new AuthCodeEntity($server))->setId('foobar')->setExpireTime(time() + 300)->setRedirectUri('http://fail/face') ); $server->setClientStorage($clientStorage);