OAuthTestUnitOAuth2ServiceAbstractServiceTest

The Gravity Forms Salesforce Add-On OAuthTest Unit OAuth2 Service AbstractServiceTest class.

Defined (1)

The class is defined in the following location(s).

/lib/PHPoAuthLib/tests/Unit/OAuth2/Service/AbstractServiceTest.php  
  1. class AbstractServiceTest extends \PHPUnit_Framework_TestCase 
  2. /** 
  3. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  4. */ 
  5. public function testConstructCorrectInterface() 
  6. $service = $this->getMockForAbstractClass( 
  7. '\\OAuth\\OAuth2\\Service\\AbstractService',  
  8. array( 
  9. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  10. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  11. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  12. array(),  
  13. ); 
  14.  
  15. $this->assertInstanceOf('\\OAuth\\OAuth2\\Service\\ServiceInterface', $service); 
  16.  
  17. /** 
  18. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  19. */ 
  20. public function testConstructCorrectParent() 
  21. $service = $this->getMockForAbstractClass( 
  22. '\\OAuth\\OAuth2\\Service\\AbstractService',  
  23. array( 
  24. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  25. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  26. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  27. array(),  
  28. ); 
  29.  
  30. $this->assertInstanceOf('\\OAuth\\Common\\Service\\AbstractService', $service); 
  31.  
  32. /** 
  33. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  34. */ 
  35. public function testConstructCorrectParentCustomUri() 
  36. $service = $this->getMockForAbstractClass( 
  37. '\\OAuth\\OAuth2\\Service\\AbstractService',  
  38. array( 
  39. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  40. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  41. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  42. array(),  
  43. $this->getMock('\\OAuth\\Common\\Http\\Uri\\UriInterface'),  
  44. ); 
  45.  
  46. $this->assertInstanceOf('\\OAuth\\Common\\Service\\AbstractService', $service); 
  47.  
  48. /** 
  49. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  50. * @covers OAuth\OAuth2\Service\AbstractService::isValidScope 
  51. */ 
  52. public function testConstructThrowsExceptionOnInvalidScope() 
  53. $this->setExpectedException('\\OAuth\\OAuth2\\Service\\Exception\\InvalidScopeException'); 
  54.  
  55. $service = new Mock( 
  56. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  57. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  58. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  59. array('invalidscope') 
  60. ); 
  61.  
  62. /** 
  63. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  64. * @covers OAuth\OAuth2\Service\AbstractService::getAuthorizationUri 
  65. * @covers OAuth\OAuth2\Service\AbstractService::getAuthorizationEndpoint 
  66. */ 
  67. public function testGetAuthorizationUriWithoutParametersOrScopes() 
  68. $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'); 
  69. $credentials->expects($this->once())->method('getConsumerId')->will($this->returnValue('foo')); 
  70. $credentials->expects($this->once())->method('getCallbackUrl')->will($this->returnValue('bar')); 
  71.  
  72. $service = new Mock( 
  73. $credentials,  
  74. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  75. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  76. ); 
  77.  
  78. $this->assertSame( 
  79. 'http://pieterhordijk.com/auth?type=web_server&client_id=foo&redirect_uri=bar&response_type=code&scope=',  
  80. $service->getAuthorizationUri()->getAbsoluteUri() 
  81. ); 
  82.  
  83. /** 
  84. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  85. * @covers OAuth\OAuth2\Service\AbstractService::getAuthorizationUri 
  86. * @covers OAuth\OAuth2\Service\AbstractService::getAuthorizationEndpoint 
  87. */ 
  88. public function testGetAuthorizationUriWithParametersWithoutScopes() 
  89. $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'); 
  90. $credentials->expects($this->once())->method('getConsumerId')->will($this->returnValue('foo')); 
  91. $credentials->expects($this->once())->method('getCallbackUrl')->will($this->returnValue('bar')); 
  92.  
  93. $service = new Mock( 
  94. $credentials,  
  95. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  96. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  97. ); 
  98.  
  99. $this->assertSame( 
  100. 'http://pieterhordijk.com/auth?foo=bar&baz=beer&type=web_server&client_id=foo&redirect_uri=bar&response_type=code&scope=',  
  101. $service->getAuthorizationUri(array('foo' => 'bar', 'baz' => 'beer'))->getAbsoluteUri() 
  102. ); 
  103.  
  104. /** 
  105. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  106. * @covers OAuth\OAuth2\Service\AbstractService::isValidScope 
  107. * @covers OAuth\OAuth2\Service\AbstractService::getAuthorizationUri 
  108. * @covers OAuth\OAuth2\Service\AbstractService::getAuthorizationEndpoint 
  109. */ 
  110. public function testGetAuthorizationUriWithParametersAndScopes() 
  111. $credentials = $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'); 
  112. $credentials->expects($this->once())->method('getConsumerId')->will($this->returnValue('foo')); 
  113. $credentials->expects($this->once())->method('getCallbackUrl')->will($this->returnValue('bar')); 
  114.  
  115. $service = new Mock( 
  116. $credentials,  
  117. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  118. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'),  
  119. array('mock', 'mock2') 
  120. ); 
  121.  
  122. $this->assertSame( 
  123. 'http://pieterhordijk.com/auth?foo=bar&baz=beer&type=web_server&client_id=foo&redirect_uri=bar&response_type=code&scope=mock+mock2',  
  124. $service->getAuthorizationUri(array('foo' => 'bar', 'baz' => 'beer'))->getAbsoluteUri() 
  125. ); 
  126.  
  127. /** 
  128. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  129. * @covers OAuth\OAuth2\Service\AbstractService::requestAccessToken 
  130. * @covers OAuth\OAuth2\Service\AbstractService::getAccessTokenEndpoint 
  131. * @covers OAuth\OAuth2\Service\AbstractService::getExtraOAuthHeaders 
  132. * @covers OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse 
  133. * @covers OAuth\OAuth2\Service\AbstractService::service 
  134. */ 
  135. public function testRequestAccessToken() 
  136. $service = new Mock( 
  137. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  138. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  139. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  140. ); 
  141.  
  142. $this->assertInstanceof('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->requestAccessToken('code')); 
  143.  
  144. /** 
  145. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  146. * @covers OAuth\OAuth2\Service\AbstractService::request 
  147. * @covers OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath 
  148. */ 
  149. public function testRequestThrowsExceptionWhenTokenIsExpired() 
  150. $tokenExpiration = new \DateTime('26-03-1984 00:00:00'); 
  151.  
  152. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  153. $token->expects($this->any())->method('getEndOfLife')->will($this->returnValue($tokenExpiration->format('U'))); 
  154.  
  155. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  156. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  157.  
  158. $service = new Mock( 
  159. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  160. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  161. $storage 
  162. ); 
  163.  
  164. $this->setExpectedException('\\OAuth\\Common\\Token\\Exception\\ExpiredTokenException', 'Token expired on 03/26/1984 at 12:00:00 AM'); 
  165.  
  166. $service->request('https://pieterhordijk.com/my/awesome/path'); 
  167.  
  168. /** 
  169. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  170. * @covers OAuth\OAuth2\Service\AbstractService::request 
  171. * @covers OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath 
  172. * @covers OAuth\OAuth2\Service\AbstractService::getAuthorizationMethod 
  173. * @covers OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse 
  174. * @covers OAuth\OAuth2\Service\AbstractService::service 
  175. * @covers OAuth\OAuth2\Service\AbstractService::getExtraApiHeaders 
  176. */ 
  177. public function testRequestOauthAuthorizationMethod() 
  178. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  179. $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2)); 
  180.  
  181. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  182. $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); 
  183. $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); 
  184.  
  185. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  186. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  187.  
  188. $service = new Mock( 
  189. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  190. $client,  
  191. $storage 
  192. ); 
  193.  
  194. $headers = $service->request('https://pieterhordijk.com/my/awesome/path'); 
  195.  
  196. $this->assertTrue(array_key_exists('Authorization', $headers)); 
  197. $this->assertTrue(in_array('OAuth foo', $headers, true)); 
  198.  
  199. /** 
  200. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  201. * @covers OAuth\OAuth2\Service\AbstractService::request 
  202. * @covers OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath 
  203. * @covers OAuth\OAuth2\Service\AbstractService::getAuthorizationMethod 
  204. * @covers OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse 
  205. * @covers OAuth\OAuth2\Service\AbstractService::service 
  206. * @covers OAuth\OAuth2\Service\AbstractService::getExtraApiHeaders 
  207. */ 
  208. public function testRequestQueryStringMethod() 
  209. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  210. $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0)); 
  211.  
  212. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  213. $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); 
  214. $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); 
  215.  
  216. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  217. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  218.  
  219. $service = new Mock( 
  220. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  221. $client,  
  222. $storage 
  223. ); 
  224.  
  225. $service->setAuthorizationMethod('querystring'); 
  226.  
  227. $uri = $service->request('https://pieterhordijk.com/my/awesome/path'); 
  228. $absoluteUri = parse_url($uri->getAbsoluteUri()); 
  229.  
  230. $this->assertSame('access_token=foo', $absoluteUri['query']); 
  231.  
  232. /** 
  233. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  234. * @covers OAuth\OAuth2\Service\AbstractService::request 
  235. * @covers OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath 
  236. * @covers OAuth\OAuth2\Service\AbstractService::getAuthorizationMethod 
  237. * @covers OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse 
  238. * @covers OAuth\OAuth2\Service\AbstractService::service 
  239. * @covers OAuth\OAuth2\Service\AbstractService::getExtraApiHeaders 
  240. */ 
  241. public function testRequestQueryStringTwoMethod() 
  242. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  243. $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(0)); 
  244.  
  245. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  246. $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); 
  247. $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); 
  248.  
  249. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  250. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  251.  
  252. $service = new Mock( 
  253. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  254. $client,  
  255. $storage 
  256. ); 
  257.  
  258. $service->setAuthorizationMethod('querystring2'); 
  259.  
  260. $uri = $service->request('https://pieterhordijk.com/my/awesome/path'); 
  261. $absoluteUri = parse_url($uri->getAbsoluteUri()); 
  262.  
  263. $this->assertSame('oauth2_access_token=foo', $absoluteUri['query']); 
  264.  
  265. /** 
  266. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  267. * @covers OAuth\OAuth2\Service\AbstractService::request 
  268. * @covers OAuth\OAuth2\Service\AbstractService::determineRequestUriFromPath 
  269. * @covers OAuth\OAuth2\Service\AbstractService::getAuthorizationMethod 
  270. * @covers OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse 
  271. * @covers OAuth\OAuth2\Service\AbstractService::service 
  272. * @covers OAuth\OAuth2\Service\AbstractService::getExtraApiHeaders 
  273. */ 
  274. public function testRequestBearerMethod() 
  275. $client = $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'); 
  276. $client->expects($this->once())->method('retrieveResponse')->will($this->returnArgument(2)); 
  277.  
  278. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\TokenInterface'); 
  279. $token->expects($this->once())->method('getEndOfLife')->will($this->returnValue(TokenInterface::EOL_NEVER_EXPIRES)); 
  280. $token->expects($this->once())->method('getAccessToken')->will($this->returnValue('foo')); 
  281.  
  282. $storage = $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface'); 
  283. $storage->expects($this->once())->method('retrieveAccessToken')->will($this->returnValue($token)); 
  284.  
  285. $service = new Mock( 
  286. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  287. $client,  
  288. $storage 
  289. ); 
  290.  
  291. $service->setAuthorizationMethod('bearer'); 
  292.  
  293. $headers = $service->request('https://pieterhordijk.com/my/awesome/path'); 
  294.  
  295. $this->assertTrue(array_key_exists('Authorization', $headers)); 
  296. $this->assertTrue(in_array('Bearer foo', $headers, true)); 
  297.  
  298. /** 
  299. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  300. * @covers OAuth\OAuth2\Service\AbstractService::getStorage 
  301. */ 
  302. public function testGetStorage() 
  303. $service = new Mock( 
  304. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  305. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  306. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  307. ); 
  308.  
  309. $this->assertInstanceOf('\\OAuth\\Common\\Storage\\TokenStorageInterface', $service->getStorage()); 
  310.  
  311. /** 
  312. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  313. * @covers OAuth\OAuth2\Service\AbstractService::refreshAccessToken 
  314. * @covers OAuth\OAuth2\Service\AbstractService::getAccessTokenEndpoint 
  315. * @covers OAuth\OAuth2\Service\AbstractService::getExtraOAuthHeaders 
  316. * @covers OAuth\OAuth2\Service\AbstractService::parseAccessTokenResponse 
  317. */ 
  318. public function testRefreshAccessTokenSuccess() 
  319. $service = new Mock( 
  320. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  321. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  322. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  323. ); 
  324.  
  325. $token = $this->getMock('\\OAuth\\OAuth2\\Token\\StdOAuth2Token'); 
  326. $token->expects($this->once())->method('getRefreshToken')->will($this->returnValue('foo')); 
  327.  
  328. $this->assertInstanceOf('\\OAuth\\OAuth2\\Token\\StdOAuth2Token', $service->refreshAccessToken($token)); 
  329.  
  330. /** 
  331. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  332. * @covers OAuth\OAuth2\Service\AbstractService::isValidScope 
  333. */ 
  334. public function testIsValidScopeTrue() 
  335. $service = new Mock( 
  336. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  337. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  338. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  339. ); 
  340.  
  341. $this->assertTrue($service->isValidScope('mock')); 
  342.  
  343. /** 
  344. * @covers OAuth\OAuth2\Service\AbstractService::__construct 
  345. * @covers OAuth\OAuth2\Service\AbstractService::isValidScope 
  346. */ 
  347. public function testIsValidScopeFalse() 
  348. $service = new Mock( 
  349. $this->getMock('\\OAuth\\Common\\Consumer\\CredentialsInterface'),  
  350. $this->getMock('\\OAuth\\Common\\Http\\Client\\ClientInterface'),  
  351. $this->getMock('\\OAuth\\Common\\Storage\\TokenStorageInterface') 
  352. ); 
  353.  
  354. $this->assertFalse($service->isValidScope('invalid'));