diff options
Diffstat (limited to 'test')
| -rw-r--r-- | test/OAuth2/Controller/TokenControllerTest.php | 35 | ||||
| -rw-r--r-- | test/OAuth2/Encryption/JwtTest.php | 8 | ||||
| -rw-r--r-- | test/OAuth2/ServerTest.php | 9 | ||||
| -rw-r--r-- | test/OAuth2/Storage/AccessTokenTest.php | 2 | ||||
| -rw-r--r-- | test/OAuth2/Storage/AuthorizationCodeTest.php | 4 | ||||
| -rw-r--r-- | test/OAuth2/Storage/ClientCredentialsTest.php | 2 | ||||
| -rw-r--r-- | test/OAuth2/Storage/ClientTest.php | 8 | ||||
| -rw-r--r-- | test/OAuth2/Storage/PublicKeyTest.php | 9 | ||||
| -rw-r--r-- | test/OAuth2/Storage/RefreshTokenTest.php | 2 | ||||
| -rw-r--r-- | test/OAuth2/Storage/ScopeTest.php | 28 | ||||
| -rw-r--r-- | test/OAuth2/Storage/UserCredentialsTest.php | 2 | ||||
| -rw-r--r-- | test/bootstrap.php | 5 | ||||
| -rw-r--r-- | test/config/storage.json | 7 | ||||
| -rw-r--r-- | test/lib/OAuth2/Storage/Bootstrap.php | 35 | ||||
| -rw-r--r-- | test/lib/OAuth2/Storage/MockRedisClient.php | 96 |
15 files changed, 116 insertions, 136 deletions
diff --git a/test/OAuth2/Controller/TokenControllerTest.php b/test/OAuth2/Controller/TokenControllerTest.php index 6cd7416..183e392 100644 --- a/test/OAuth2/Controller/TokenControllerTest.php +++ b/test/OAuth2/Controller/TokenControllerTest.php @@ -6,6 +6,7 @@ use OAuth2\Storage\Bootstrap; use OAuth2\Server; use OAuth2\GrantType\AuthorizationCode; use OAuth2\GrantType\ClientCredentials; +use OAuth2\GrantType\UserCredentials; use OAuth2\Scope; use OAuth2\Request\TestRequest; use OAuth2\Response; @@ -52,7 +53,7 @@ class Controller_TokenControllerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($response->getParameter('error_description'), 'Client credentials were not found in the headers or body'); } - public function testNoClientSecret() + public function testNoClientSecretWithConfidentialClient() { // add the test parameters in memory $server = $this->getTestServer(); @@ -65,7 +66,7 @@ class Controller_TokenControllerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($response->getStatusCode(), 400); $this->assertEquals($response->getParameter('error'), 'invalid_client'); - $this->assertEquals($response->getParameter('error_description'), 'The client credentials are invalid'); + $this->assertEquals($response->getParameter('error_description'), 'This client is invalid or must authenticate using a client secret'); } public function testNoClientSecretWithEmptySecret() @@ -132,9 +133,9 @@ class Controller_TokenControllerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($response->getStatusCode(), 200); $this->assertNull($response->getParameter('error')); $this->assertNull($response->getParameter('error_description')); - $this->assertNotNUll($response->getParameter('access_token')); - $this->assertNotNUll($response->getParameter('expires_in')); - $this->assertNotNUll($response->getParameter('token_type')); + $this->assertNotNull($response->getParameter('access_token')); + $this->assertNotNull($response->getParameter('expires_in')); + $this->assertNotNull($response->getParameter('token_type')); } public function testValidClientIdScope() @@ -198,6 +199,30 @@ class Controller_TokenControllerTest extends \PHPUnit_Framework_TestCase $this->assertEquals($response->getParameter('error_description'), 'This application requires you specify a scope parameter'); } + public function testCanReceiveAccessTokenUsingPasswordGrantTypeWithoutClientSecret() + { + // add the test parameters in memory + $storage = Bootstrap::getInstance()->getMemoryStorage(); + $server = new Server($storage); + $server->addGrantType(new UserCredentials($storage)); + + $request = TestRequest::createPost(array( + 'grant_type' => 'password', // valid grant type + 'client_id' => 'Test Client ID For Password Grant', // valid client id + 'username' => 'johndoe', // valid username + 'password' => 'password', // valid password for username + )); + $server->handleTokenRequest($request, $response = new Response()); + + $this->assertTrue($response instanceof Response); + $this->assertEquals(200, $response->getStatusCode(), var_export($response, 1)); + $this->assertNull($response->getParameter('error')); + $this->assertNull($response->getParameter('error_description')); + $this->assertNotNull($response->getParameter('access_token')); + $this->assertNotNull($response->getParameter('expires_in')); + $this->assertNotNull($response->getParameter('token_type')); + } + public function testCreateController() { $storage = Bootstrap::getInstance()->getMemoryStorage(); diff --git a/test/OAuth2/Encryption/JwtTest.php b/test/OAuth2/Encryption/JwtTest.php index b10c61b..20d3128 100644 --- a/test/OAuth2/Encryption/JwtTest.php +++ b/test/OAuth2/Encryption/JwtTest.php @@ -50,6 +50,14 @@ EOD; $this->assertEquals($params, $payload); } + public function testInvalidJwt() + { + $jwtUtil = new Jwt(); + + $this->assertFalse($jwtUtil->decode('goob')); + $this->assertFalse($jwtUtil->decode('go.o.b')); + } + public function provideClientCredentials() { $storage = Bootstrap::getInstance()->getMemoryStorage(); diff --git a/test/OAuth2/ServerTest.php b/test/OAuth2/ServerTest.php index b61f851..8794b42 100644 --- a/test/OAuth2/ServerTest.php +++ b/test/OAuth2/ServerTest.php @@ -253,7 +253,10 @@ class ServerTest extends \PHPUnit_Framework_TestCase { // create mock storage $storage = Bootstrap::getInstance()->getMemoryStorage(); - $server = new Server(array($storage), array('allow_credentials_in_request_body' => false)); + $server = new Server(array($storage), array( + 'allow_credentials_in_request_body' => false, + 'allow_public_clients' => false + )); $server->getTokenController(); $httpBasic = $server->getClientAssertionType(); @@ -261,8 +264,10 @@ class ServerTest extends \PHPUnit_Framework_TestCase $prop = $reflection->getProperty('config'); $prop->setAccessible(true); - $config = $prop->getValue($httpBasic); // get the private "storages" property + $config = $prop->getValue($httpBasic); // get the private "config" property + $this->assertEquals($config['allow_credentials_in_request_body'], false); + $this->assertEquals($config['allow_public_clients'], false); } public function testRefreshTokenConfig() diff --git a/test/OAuth2/Storage/AccessTokenTest.php b/test/OAuth2/Storage/AccessTokenTest.php index 17c9cd9..ba54b34 100644 --- a/test/OAuth2/Storage/AccessTokenTest.php +++ b/test/OAuth2/Storage/AccessTokenTest.php @@ -8,7 +8,7 @@ class AccessTokenTest extends BaseTest public function testSetAccessToken(AccessTokenInterface $storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); + $this->markTestSkipped('Skipped Storage'); return; } diff --git a/test/OAuth2/Storage/AuthorizationCodeTest.php b/test/OAuth2/Storage/AuthorizationCodeTest.php index 9f57714..7dacbf4 100644 --- a/test/OAuth2/Storage/AuthorizationCodeTest.php +++ b/test/OAuth2/Storage/AuthorizationCodeTest.php @@ -8,7 +8,7 @@ class AuthorizationCodeTest extends BaseTest public function testGetAuthorizationCode(AuthorizationCodeInterface $storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); + $this->markTestSkipped('Skipped Storage'); return; } @@ -25,7 +25,7 @@ class AuthorizationCodeTest extends BaseTest public function testSetAuthorizationCode(AuthorizationCodeInterface $storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); + $this->markTestSkipped('Skipped Storage'); return; } diff --git a/test/OAuth2/Storage/ClientCredentialsTest.php b/test/OAuth2/Storage/ClientCredentialsTest.php index ecaf6f6..21d6c14 100644 --- a/test/OAuth2/Storage/ClientCredentialsTest.php +++ b/test/OAuth2/Storage/ClientCredentialsTest.php @@ -8,7 +8,7 @@ class ClientCredentialsTest extends BaseTest public function testCheckClientCredentials(ClientCredentialsInterface $storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); + $this->markTestSkipped('Skipped Storage'); return; } diff --git a/test/OAuth2/Storage/ClientTest.php b/test/OAuth2/Storage/ClientTest.php index 4d95c6c..e370e38 100644 --- a/test/OAuth2/Storage/ClientTest.php +++ b/test/OAuth2/Storage/ClientTest.php @@ -8,7 +8,7 @@ class ClientTest extends BaseTest public function testGetClientDetails(ClientInterface $storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); + $this->markTestSkipped('Skipped Storage'); return; } @@ -28,7 +28,7 @@ class ClientTest extends BaseTest public function testCheckRestrictedGrantType(ClientInterface $storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); + $this->markTestSkipped('Skipped Storage'); return; } @@ -46,7 +46,7 @@ class ClientTest extends BaseTest public function testGetAccessToken(ClientInterface $storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); + $this->markTestSkipped('Skipped Storage'); return; } @@ -63,7 +63,7 @@ class ClientTest extends BaseTest public function testSaveClient(ClientInterface $storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); + $this->markTestSkipped('Skipped Storage'); return; } diff --git a/test/OAuth2/Storage/PublicKeyTest.php b/test/OAuth2/Storage/PublicKeyTest.php index d08c9c0..0e2bfd6 100644 --- a/test/OAuth2/Storage/PublicKeyTest.php +++ b/test/OAuth2/Storage/PublicKeyTest.php @@ -7,8 +7,13 @@ class PublicKeyTest extends BaseTest /** @dataProvider provideStorage */ public function testSetAccessToken($storage) { - if (is_null($storage) || !$storage instanceof PublicKeyInterface) { - return $this->markTestSkipped('Invalid storage for public key test'); + if (is_null($storage)) { + return $this->markTestSkipped('Skipped Storage'); + } + + if (!$storage instanceof PublicKeyInterface) { + // incompatible storage + return; } $configDir = Bootstrap::getInstance()->getConfigDir(); diff --git a/test/OAuth2/Storage/RefreshTokenTest.php b/test/OAuth2/Storage/RefreshTokenTest.php index 7777bb9..a356cc5 100644 --- a/test/OAuth2/Storage/RefreshTokenTest.php +++ b/test/OAuth2/Storage/RefreshTokenTest.php @@ -8,7 +8,7 @@ class RefreshTokenTest extends BaseTest public function testSetRefreshToken(RefreshTokenInterface $storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); + $this->markTestSkipped('Skipped Storage'); return; } diff --git a/test/OAuth2/Storage/ScopeTest.php b/test/OAuth2/Storage/ScopeTest.php index a34ce54..83d9b6f 100644 --- a/test/OAuth2/Storage/ScopeTest.php +++ b/test/OAuth2/Storage/ScopeTest.php @@ -10,14 +10,11 @@ class ScopeTest extends BaseTest public function testScopeExists($storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); - - return; + return $this->markTestSkipped('Skipped Storage'); } if (!$storage instanceof ScopeInterface) { - $this->markTestSkipped('Skipping incompatible storage'); - + // incompatible storage return; } @@ -32,14 +29,11 @@ class ScopeTest extends BaseTest public function testGetDefaultScope($storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); - - return; + return $this->markTestSkipped('Skipped Storage'); } if (!$storage instanceof ScopeInterface) { - $this->markTestSkipped('Skipping incompatible storage'); - + // incompatible storage return; } @@ -52,14 +46,11 @@ class ScopeTest extends BaseTest public function testClientScopeExists($storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); - - return; + return $this->markTestSkipped('Skipped Storage'); } if (!$storage instanceof ScopeInterface) { - $this->markTestSkipped('Skipping incompatible storage'); - + // incompatible storage return; } @@ -75,14 +66,11 @@ class ScopeTest extends BaseTest public function testGetDefaultClientScope($storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); - - return; + return $this->markTestSkipped('Skipped Storage'); } if (!$storage instanceof ScopeInterface) { - $this->markTestSkipped('Skipping incompatible storage'); - + // incompatible storage return; } diff --git a/test/OAuth2/Storage/UserCredentialsTest.php b/test/OAuth2/Storage/UserCredentialsTest.php index ee1c721..78fa804 100644 --- a/test/OAuth2/Storage/UserCredentialsTest.php +++ b/test/OAuth2/Storage/UserCredentialsTest.php @@ -8,7 +8,7 @@ class UserCredentialsTest extends BaseTest public function testCheckUserCredentials(UserCredentialsInterface $storage = null) { if (is_null($storage)) { - $this->markTestSkipped('Unable to load class Mongo_Client'); + $this->markTestSkipped('Skipped Storage'); return; } diff --git a/test/bootstrap.php b/test/bootstrap.php index a967dea..0a4af07 100644 --- a/test/bootstrap.php +++ b/test/bootstrap.php @@ -5,3 +5,8 @@ OAuth2\Autoloader::register(); // register test classes OAuth2\Autoloader::register(dirname(__FILE__).'/lib'); + +// register vendors if possible +if (file_exists(__DIR__.'/../vendor/autoload.php')) { + require_once(__DIR__.'/../vendor/autoload.php'); +} diff --git a/test/config/storage.json b/test/config/storage.json index f2db151..23a3988 100644 --- a/test/config/storage.json +++ b/test/config/storage.json @@ -48,6 +48,10 @@ "Test Client ID Empty Secret": { "client_secret": "" }, + "Test Client ID For Password Grant": { + "grant_types": "password", + "client_secret": "" + }, "Client ID With User ID": { "client_secret": "TestSecret", "user_id": "brent@brentertainment.com" @@ -63,6 +67,9 @@ }, "testusername": { "password": "testpass" + }, + "johndoe": { + "password": "password" } }, "refresh_tokens" : { diff --git a/test/lib/OAuth2/Storage/Bootstrap.php b/test/lib/OAuth2/Storage/Bootstrap.php index 673a01a..d1e7818 100644 --- a/test/lib/OAuth2/Storage/Bootstrap.php +++ b/test/lib/OAuth2/Storage/Bootstrap.php @@ -8,6 +8,7 @@ class Bootstrap private $mysql; private $sqlite; private $mongo; + private $redis; private $configDir; public function __construct() @@ -45,7 +46,16 @@ class Bootstrap public function getRedisStorage() { - return new Redis(new MockRedisClient()); + if (!$this->redis) { + if (class_exists('Predis\Client')) { + $redis = new \Predis\Client(); + $redis->flushdb(); + $this->redis = new Redis($redis); + $this->createRedisDb($this->redis); + } + } + + return $this->redis; } public function getMysqlPdo() @@ -159,6 +169,29 @@ class Bootstrap $db->oauth_users->insert(array('username' => "testuser", 'password' => "password")); } + private function createRedisDb(Redis $storage) + { + $storage->setClientDetails("oauth_test_client", "testpass", "http://example.com", 'implicit password'); + $storage->setAccessToken("testtoken", "Some Client", '', time() + 1000); + $storage->setAuthorizationCode("testcode", "Some Client", '', '', time() + 1000); + $storage->setUser("testuser", "password"); + + $storage->setScope('supportedscope1 supportedscope2 supportedscope3 supportedscope4'); + $storage->setScope('defaultscope1 defaultscope2', null, 'default'); + + $storage->setScope('clientscope1 clientscope2', 'Test Client ID'); + $storage->setScope('clientscope1 clientscope2', 'Test Client ID', 'default'); + + $storage->setScope('clientscope1 clientscope2 clientscope3', 'Test Client ID 2'); + $storage->setScope('clientscope1 clientscope2', 'Test Client ID 2', 'default'); + + $storage->setScope('clientscope1 clientscope2', 'Test Default Scope Client ID'); + $storage->setScope('clientscope1 clientscope2', 'Test Default Scope Client ID', 'default'); + + $storage->setScope('clientscope1 clientscope2 clientscope3', 'Test Default Scope Client ID 2'); + $storage->setScope('clientscope3', 'Test Default Scope Client ID 2', 'default'); + } + public function removeMongoDb(\MongoDB $db) { $db->drop(); diff --git a/test/lib/OAuth2/Storage/MockRedisClient.php b/test/lib/OAuth2/Storage/MockRedisClient.php deleted file mode 100644 index dbfe264..0000000 --- a/test/lib/OAuth2/Storage/MockRedisClient.php +++ /dev/null @@ -1,96 +0,0 @@ -<?php - -namespace OAuth2\Storage; - -class MockRedisClient -{ - public function __construct() - { - $data = array( - 'oauth_clients:oauth_test_client' => array( - 'client_id' => 'oauth_test_client', - 'client_secret' => "testpass", - 'redirect_uri' => '', - 'grant_types' => 'implicit password', - ), - 'oauth_clients:Test Client ID' => array( - 'client_id' => 'Test Client ID', - 'client_secret' => "TestSecret" - ), - 'oauth_clients:Test Client ID 2' => array( - 'client_id' => 'Test Client ID 2', - 'client_secret' => "TestSecret", - ), - 'oauth_clients:Test Default Scope Client ID' => array( - 'client_id' => 'Test Default Scope Client ID', - 'client_secret' => "TestSecret", - ), - 'oauth_clients:Test Default Scope Client ID 2' => array( - 'client_id' => 'Test Default Scope Client ID 2', - 'client_secret' => "TestSecret", - ), - 'oauth_access_tokens:testtoken' => array( - 'access_token' => 'testtoken', - 'client_id' => "Some Client", - 'user_id' => '', - 'expires' => 0, - 'scope' => '' - ), - 'oauth_authorization_codes:testcode' => array( - 'client_id' => "Some Client", - 'authorization_code' => 'testcode', - 'user_id' => '', - 'redirect_uri' => '', - 'expires' => 0, - 'scope' => '' - ), - 'oauth_users:testuser' => array( - 'user_id' => 'testuser', - 'password' => "password", - 'first_name' => '', - 'last_name' => '' - ), - 'oauth_scopes:supported:global' => 'supportedscope1 supportedscope2 supportedscope3 supportedscope4', - 'oauth_scopes:default:global' => 'defaultscope1 defaultscope2', - 'oauth_scopes:supported:Test Client ID' => 'clientscope1 clientscope2', - 'oauth_scopes:default:Test Client ID' => 'clientscope1 clientscope2', - 'oauth_scopes:supported:Test Client ID 2' => 'clientscope3', - 'oauth_scopes:default:Test Client ID 2' => 'clientscope3', - 'oauth_scopes:supported:Test Default Scope Client ID' => 'clientscope1 clientscope2', - 'oauth_scopes:default:Test Default Scope Client ID' => 'clientscope1 clientscope2', - 'oauth_scopes:supported:Test Default Scope Client ID 2' => 'clientscope3', - 'oauth_scopes:default:Test Default Scope Client ID 2' => 'clientscope3', - ); - foreach ($data as $name => $val) { - $data[$name] = json_encode($val); - } - $this->data = $data; - } - - public function get($key) - { - return isset($this->data[$key]) ? $this->data[$key] : null; - } - - public function set ($key, $value) - { - $this->data[$key] = $value; - - return true; - } - - public function setex($key, $expires, $value) - { - - $this->data[$key] = $value; - - return true; - } - - public function expire($key) - { - unset($this->data[$key]); - - return true; - } -} |
