From baf06a66be2849d2fc2b4e0995372e2aac06af19 Mon Sep 17 00:00:00 2001 From: japatel Date: Tue, 13 Jan 2015 23:57:56 -0600 Subject: [PATCH] Updated OAuthToken to throw proper error on not receiving access token - Updated Samples, Tests --- lib/PayPal/Auth/OAuthTokenCredential.php | 6 +- .../Test/Auth/OAuthTokenCredentialTest.php | 110 +++++++++++++++++- .../Test/Cache/AuthorizationCacheTest.php | 51 +++++++- .../PayPal/Test/Handler/OauthHandlerTest.php | 73 ++++++++++++ 4 files changed, 229 insertions(+), 11 deletions(-) create mode 100644 tests/PayPal/Test/Handler/OauthHandlerTest.php diff --git a/lib/PayPal/Auth/OAuthTokenCredential.php b/lib/PayPal/Auth/OAuthTokenCredential.php index ef720cd..f38cd6f 100644 --- a/lib/PayPal/Auth/OAuthTokenCredential.php +++ b/lib/PayPal/Auth/OAuthTokenCredential.php @@ -8,6 +8,7 @@ use PayPal\Core\PayPalHttpConfig; use PayPal\Core\PayPalHttpConnection; use PayPal\Core\PayPalLoggingManager; use PayPal\Exception\PayPalConfigurationException; +use PayPal\Exception\PayPalConnectionException; use PayPal\Handler\IPayPalHandler; use PayPal\Rest\ApiContext; @@ -202,7 +203,7 @@ class OAuthTokenCredential extends PayPalResourceModel * @throws PayPalConfigurationException * @throws \PayPal\Exception\PayPalConnectionException */ - private function getToken($config, $clientId, $clientSecret, $payload) + protected function getToken($config, $clientId, $clientSecret, $payload) { $httpConfig = new PayPalHttpConfig(null, 'POST', $config); @@ -247,8 +248,9 @@ class OAuthTokenCredential extends PayPalResourceModel $this->accessToken = null; $this->tokenExpiresIn = null; $this->logger->warning( - "Could not generate new Access token. Invalid response from server: " . $response + "Could not generate new Access token. Invalid response from server: " ); + throw new PayPalConnectionException(null, "Could not generate new Access token. Invalid response from server: "); } else { $this->accessToken = $response["access_token"]; $this->tokenExpiresIn = $response["expires_in"]; diff --git a/tests/PayPal/Test/Auth/OAuthTokenCredentialTest.php b/tests/PayPal/Test/Auth/OAuthTokenCredentialTest.php index b68fa7a..8a6ea68 100644 --- a/tests/PayPal/Test/Auth/OAuthTokenCredentialTest.php +++ b/tests/PayPal/Test/Auth/OAuthTokenCredentialTest.php @@ -1,13 +1,16 @@ assertEquals(Constants::CLIENT_ID, $cred->getClientId()); + $this->assertEquals(Constants::CLIENT_SECRET, $cred->getClientSecret()); $config = PayPalConfigManager::getInstance()->getConfigHashmap(); $token = $cred->getAccessToken($config); $this->assertNotNull($token); @@ -35,4 +40,103 @@ class OAuthTokenCredentialTest extends PHPUnit_Framework_TestCase $cred = new OAuthTokenCredential('dummy', 'secret'); $this->assertNull($cred->getAccessToken(PayPalConfigManager::getInstance()->getConfigHashmap())); } + + public function testGetAccessTokenUnit() + { + $config = array( + 'mode' => 'sandbox', + 'cache.enabled' => true, + 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE + ); + //{"clientId":{"clientId":"clientId","accessToken":"accessToken","tokenCreateTime":1421204091,"tokenExpiresIn":288000000}} + AuthorizationCache::push($config, 'clientId', 'accessToken', 1421204091, 288000000); + $cred = new OAuthTokenCredential('clientId', 'clientSecret'); + $apiContext = new ApiContext($cred); + $apiContext->setConfig($config); + $this->assertEquals('clientId', $cred->getClientId()); + $this->assertEquals('clientSecret', $cred->getClientSecret()); + $result = $cred->getAccessToken($config); + $this->assertNotNull($result); + } + + public function testGetAccessTokenUnitMock() + { + $config = array( + 'mode' => 'sandbox' + ); + /** @var OAuthTokenCredential $auth */ + $auth = $this->getMockBuilder('\PayPal\Auth\OAuthTokenCredential') + ->setConstructorArgs(array('clientId', 'clientSecret')) + ->setMethods(array('getToken')) + ->getMock(); + + $auth->expects($this->any()) + ->method('getToken') + ->will($this->returnValue( + array('refresh_token' => 'refresh_token_value') + )); + $response = $auth->getRefreshToken($config, 'auth_value'); + $this->assertNotNull($response); + $this->assertEquals('refresh_token_value', $response); + + } + + public function testUpdateAccessTokenUnitMock() + { + $config = array( + 'mode' => 'sandbox' + ); + /** @var OAuthTokenCredential $auth */ + $auth = $this->getMockBuilder('\PayPal\Auth\OAuthTokenCredential') + ->setConstructorArgs(array('clientId', 'clientSecret')) + ->setMethods(array('getToken')) + ->getMock(); + + $auth->expects($this->any()) + ->method('getToken') + ->will($this->returnValue( + array( + 'access_token' => 'accessToken', + 'expires_in' => 280 + ) + )); + + $response = $auth->updateAccessToken($config); + $this->assertNotNull($response); + $this->assertEquals('accessToken', $response); + + $response = $auth->updateAccessToken($config, 'refresh_token'); + $this->assertNotNull($response); + $this->assertEquals('accessToken', $response); + + } + + /** + * @expectedException \PayPal\Exception\PayPalConnectionException + * @expectedExceptionMessage Could not generate new Access token. Invalid response from server: + */ + public function testUpdateAccessTokenNullReturnUnitMock() + { + $config = array( + 'mode' => 'sandbox' + ); + /** @var OAuthTokenCredential $auth */ + $auth = $this->getMockBuilder('\PayPal\Auth\OAuthTokenCredential') + ->setConstructorArgs(array('clientId', 'clientSecret')) + ->setMethods(array('getToken')) + ->getMock(); + + $auth->expects($this->any()) + ->method('getToken') + ->will($this->returnValue( + array( + ) + )); + + $response = $auth->updateAccessToken($config); + $this->assertNotNull($response); + $this->assertEquals('accessToken', $response); + + } + } diff --git a/tests/PayPal/Test/Cache/AuthorizationCacheTest.php b/tests/PayPal/Test/Cache/AuthorizationCacheTest.php index b3b4b0c..8df913b 100644 --- a/tests/PayPal/Test/Cache/AuthorizationCacheTest.php +++ b/tests/PayPal/Test/Cache/AuthorizationCacheTest.php @@ -1,6 +1,7 @@ 'true'), true), array(array('cache.enabled' => true), true), - array(array(), false), - array(null, false) ); } public static function CachePathProvider() { return array( - array(array('cache.FileName' => 'temp.cache'), 'temp.cache'), - array(array(), 'auth.cache'), - array(null, 'auth.cache') + array(array('cache.FileName' => 'temp.cache'), 'temp.cache') ); } @@ -65,4 +62,46 @@ class AuthorizationCacheTest extends \PHPUnit_Framework_TestCase $this->assertContains($expected, $result); } + public function testCacheDisabled() + { + // 'cache.enabled' => true, + AuthorizationCache::push(array('cache.enabled' => false), 'clientId', 'accessToken', 'tokenCreateTime', 'tokenExpiresIn'); + AuthorizationCache::pull(array('cache.enabled' => false), 'clientId'); + } + + public function testCachePush() + { + AuthorizationCache::push(array('cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE), 'clientId', 'accessToken', 'tokenCreateTime', 'tokenExpiresIn'); + $contents = file_get_contents(AuthorizationCacheTest::CACHE_FILE); + $tokens = json_decode($contents, true); + $this->assertNotNull($contents); + $this->assertEquals('clientId', $tokens['clientId']['clientId']); + $this->assertEquals('accessToken', $tokens['clientId']['accessToken']); + $this->assertEquals('tokenCreateTime', $tokens['clientId']['tokenCreateTime']); + $this->assertEquals('tokenExpiresIn', $tokens['clientId']['tokenExpiresIn']); + + } + + public function testCachePullNonExisting() + { + $result = AuthorizationCache::pull(array('cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE), 'clientIdUndefined'); + $this->assertNull($result); + } + + /** + * @depends testCachePush + */ + public function testCachePull() + { + $result = AuthorizationCache::pull(array('cache.enabled' => true, 'cache.FileName' => AuthorizationCacheTest::CACHE_FILE), 'clientId'); + $this->assertNotNull($result); + $this->assertTrue(is_array($result)); + $this->assertEquals('clientId', $result['clientId']); + $this->assertEquals('accessToken', $result['accessToken']); + $this->assertEquals('tokenCreateTime', $result['tokenCreateTime']); + $this->assertEquals('tokenExpiresIn', $result['tokenExpiresIn']); + + unlink(AuthorizationCacheTest::CACHE_FILE); + } + } diff --git a/tests/PayPal/Test/Handler/OauthHandlerTest.php b/tests/PayPal/Test/Handler/OauthHandlerTest.php new file mode 100644 index 0000000..546629e --- /dev/null +++ b/tests/PayPal/Test/Handler/OauthHandlerTest.php @@ -0,0 +1,73 @@ +apiContext = new ApiContext( + new OAuthTokenCredential( + 'clientId', + 'clientSecret' + ) + ); + + } + + public function modeProvider() + { + return array( + array( array('mode' => 'sandbox') ), + array( array('mode' => 'live')), + array( array( 'mode' => 'sandbox','oauth.EndPoint' => 'http://localhost/')), + array( array('mode' => 'sandbox','service.EndPoint' => 'http://service.localhost/')) + ); + } + + + /** + * @dataProvider modeProvider + * @param $configs + */ + public function testGetEndpoint($configs) + { + $config = $configs + array( + 'cache.enabled' => true, + 'http.headers.header1' => 'header1value' + ); + $this->apiContext->setConfig($config); + $this->httpConfig = new PayPalHttpConfig(null, 'POST', $config); + $this->handler = new OauthHandler($this->apiContext); + $this->handler->handle($this->httpConfig, null, $this->config); + } + + +}