PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/league/oauth1-client/tests/ServerTest.php

https://gitlab.com/techniconline/kmc
PHP | 279 lines | 168 code | 60 blank | 51 comment | 0 complexity | b3d18424c333ade7b39b4834e40b1b8a MD5 | raw file
  1. <?php namespace League\OAuth1\Client\Tests;
  2. /**
  3. * Part of the Sentry package.
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * Licensed under the 3-clause BSD License.
  8. *
  9. * This source file is subject to the 3-clause BSD License that is
  10. * bundled with this package in the LICENSE file. It is also available at
  11. * the following URL: http://www.opensource.org/licenses/BSD-3-Clause
  12. *
  13. * @package Sentry
  14. * @version 2.0.0
  15. * @author Cartalyst LLC
  16. * @license BSD License (3-clause)
  17. * @copyright (c) 2011 - 2013, Cartalyst LLC
  18. * @link http://cartalyst.com
  19. */
  20. use League\OAuth1\Client\Credentials\ClientCredentials;
  21. use Mockery as m;
  22. use PHPUnit_Framework_TestCase;
  23. class ServerTest extends PHPUnit_Framework_TestCase
  24. {
  25. /**
  26. * Setup resources and dependencies.
  27. *
  28. * @return void
  29. */
  30. public static function setUpBeforeClass()
  31. {
  32. require_once __DIR__.'/stubs/ServerStub.php';
  33. }
  34. /**
  35. * Close mockery.
  36. *
  37. * @return void
  38. */
  39. public function tearDown()
  40. {
  41. m::close();
  42. }
  43. public function testCreatingWithArray()
  44. {
  45. $server = new ServerStub($this->getMockClientCredentials());
  46. $credentials = $server->getClientCredentials();
  47. $this->assertInstanceOf('League\OAuth1\Client\Credentials\ClientCredentialsInterface', $credentials);
  48. $this->assertEquals('myidentifier', $credentials->getIdentifier());
  49. $this->assertEquals('mysecret', $credentials->getSecret());
  50. $this->assertEquals('http://app.dev/', $credentials->getCallbackUri());
  51. }
  52. public function testCreatingWithObject()
  53. {
  54. $credentials = new ClientCredentials;
  55. $credentials->setIdentifier('myidentifier');
  56. $credentials->setSecret('mysecret');
  57. $credentials->setCallbackUri('http://app.dev/');
  58. $server = new ServerStub($credentials);
  59. $this->assertEquals($credentials, $server->getClientCredentials());
  60. }
  61. /**
  62. * @expectedException InvalidArgumentException
  63. **/
  64. public function testCreatingWithInvalidInput()
  65. {
  66. $server = new ServerStub(uniqid());
  67. }
  68. public function testGettingTemporaryCredentials()
  69. {
  70. $server = m::mock('League\OAuth1\Client\Tests\ServerStub[createHttpClient]', array($this->getMockClientCredentials()));
  71. $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
  72. $me = $this;
  73. $client->shouldReceive('post')->with('http://www.example.com/temporary', m::on(function($headers) use ($me) {
  74. $me->assertTrue(isset($headers['Authorization']));
  75. // OAuth protocol specifies a strict number of
  76. // headers should be sent, in the correct order.
  77. // We'll validate that here.
  78. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_callback="'.preg_quote('http%3A%2F%2Fapp.dev%2F', '/').'", oauth_signature=".*?"/';
  79. $matches = preg_match($pattern, $headers['Authorization']);
  80. $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
  81. return true;
  82. }))->once()->andReturn($request = m::mock('stdClass'));
  83. $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
  84. $response->shouldReceive('getBody')->andReturn('oauth_token=temporarycredentialsidentifier&oauth_token_secret=temporarycredentialssecret&oauth_callback_confirmed=true');
  85. $credentials = $server->getTemporaryCredentials();
  86. $this->assertInstanceOf('League\OAuth1\Client\Credentials\TemporaryCredentials', $credentials);
  87. $this->assertEquals('temporarycredentialsidentifier', $credentials->getIdentifier());
  88. $this->assertEquals('temporarycredentialssecret', $credentials->getSecret());
  89. }
  90. public function testGettingAuthorizationUrl()
  91. {
  92. $server = new ServerStub($this->getMockClientCredentials());
  93. $expected = 'http://www.example.com/authorize?oauth_token=foo';
  94. $this->assertEquals($expected, $server->getAuthorizationUrl('foo'));
  95. $credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  96. $credentials->shouldReceive('getIdentifier')->andReturn('foo');
  97. $this->assertEquals($expected, $server->getAuthorizationUrl($credentials));
  98. }
  99. /**
  100. * @expectedException InvalidArgumentException
  101. */
  102. public function testGettingTokenCredentialsFailsWithManInTheMiddle()
  103. {
  104. $server = new ServerStub($this->getMockClientCredentials());
  105. $credentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  106. $credentials->shouldReceive('getIdentifier')->andReturn('foo');
  107. $server->getTokenCredentials($credentials, 'bar', 'verifier');
  108. }
  109. public function testGettingTokenCredentials()
  110. {
  111. $server = m::mock('League\OAuth1\Client\Tests\ServerStub[createHttpClient]', array($this->getMockClientCredentials()));
  112. $temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  113. $temporaryCredentials->shouldReceive('getIdentifier')->andReturn('temporarycredentialsidentifier');
  114. $temporaryCredentials->shouldReceive('getSecret')->andReturn('temporarycredentialssecret');
  115. $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
  116. $me = $this;
  117. $client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($headers) use ($me) {
  118. $me->assertTrue(isset($headers['Authorization']));
  119. $me->assertFalse(isset($headers['User-Agent']));
  120. // OAuth protocol specifies a strict number of
  121. // headers should be sent, in the correct order.
  122. // We'll validate that here.
  123. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/';
  124. $matches = preg_match($pattern, $headers['Authorization']);
  125. $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
  126. return true;
  127. }), array('oauth_verifier' => 'myverifiercode'))->once()->andReturn($request = m::mock('stdClass'));
  128. $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
  129. $response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
  130. $credentials = $server->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
  131. $this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials);
  132. $this->assertEquals('tokencredentialsidentifier', $credentials->getIdentifier());
  133. $this->assertEquals('tokencredentialssecret', $credentials->getSecret());
  134. }
  135. public function testGettingTokenCredentialsWithUserAgent()
  136. {
  137. $userAgent = 'FooBar';
  138. $server = m::mock('League\OAuth1\Client\Tests\ServerStub[createHttpClient]', array($this->getMockClientCredentials()));
  139. $temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TemporaryCredentials');
  140. $temporaryCredentials->shouldReceive('getIdentifier')->andReturn('temporarycredentialsidentifier');
  141. $temporaryCredentials->shouldReceive('getSecret')->andReturn('temporarycredentialssecret');
  142. $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
  143. $me = $this;
  144. $client->shouldReceive('post')->with('http://www.example.com/token', m::on(function($headers) use ($me, $userAgent) {
  145. $me->assertTrue(isset($headers['Authorization']));
  146. $me->assertTrue(isset($headers['User-Agent']));
  147. $me->assertEquals($userAgent, $headers['User-Agent']);
  148. // OAuth protocol specifies a strict number of
  149. // headers should be sent, in the correct order.
  150. // We'll validate that here.
  151. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="temporarycredentialsidentifier", oauth_signature=".*?"/';
  152. $matches = preg_match($pattern, $headers['Authorization']);
  153. $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
  154. return true;
  155. }), array('oauth_verifier' => 'myverifiercode'))->once()->andReturn($request = m::mock('stdClass'));
  156. $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
  157. $response->shouldReceive('getBody')->andReturn('oauth_token=tokencredentialsidentifier&oauth_token_secret=tokencredentialssecret');
  158. $credentials = $server->setUserAgent($userAgent)->getTokenCredentials($temporaryCredentials, 'temporarycredentialsidentifier', 'myverifiercode');
  159. $this->assertInstanceOf('League\OAuth1\Client\Credentials\TokenCredentials', $credentials);
  160. $this->assertEquals('tokencredentialsidentifier', $credentials->getIdentifier());
  161. $this->assertEquals('tokencredentialssecret', $credentials->getSecret());
  162. }
  163. public function testGettingUserDetails()
  164. {
  165. $server = m::mock('League\OAuth1\Client\Tests\ServerStub[createHttpClient,protocolHeader]', array($this->getMockClientCredentials()));
  166. $temporaryCredentials = m::mock('League\OAuth1\Client\Credentials\TokenCredentials');
  167. $temporaryCredentials->shouldReceive('getIdentifier')->andReturn('tokencredentialsidentifier');
  168. $temporaryCredentials->shouldReceive('getSecret')->andReturn('tokencredentialssecret');
  169. $server->shouldReceive('createHttpClient')->andReturn($client = m::mock('stdClass'));
  170. $me = $this;
  171. $client->shouldReceive('get')->with('http://www.example.com/user', m::on(function($headers) use ($me) {
  172. $me->assertTrue(isset($headers['Authorization']));
  173. // OAuth protocol specifies a strict number of
  174. // headers should be sent, in the correct order.
  175. // We'll validate that here.
  176. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="tokencredentialsidentifier", oauth_signature=".*?"/';
  177. $matches = preg_match($pattern, $headers['Authorization']);
  178. $me->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
  179. return true;
  180. }))->once()->andReturn($request = m::mock('stdClass'));
  181. $request->shouldReceive('send')->once()->andReturn($response = m::mock('stdClass'));
  182. $response->shouldReceive('json')->once()->andReturn(array('foo' => 'bar', 'id' => 123, 'contact_email' => 'baz@qux.com', 'username' => 'fred'));
  183. $user = $server->getUserDetails($temporaryCredentials);
  184. $this->assertInstanceOf('League\OAuth1\Client\Server\User', $user);
  185. $this->assertEquals('bar', $user->firstName);
  186. $this->assertEquals(123, $server->getUserUid($temporaryCredentials));
  187. $this->assertEquals('baz@qux.com', $server->getUserEmail($temporaryCredentials));
  188. $this->assertEquals('fred', $server->getUserScreenName($temporaryCredentials));
  189. }
  190. public function testGettingHeaders()
  191. {
  192. $server = new ServerStub($this->getMockClientCredentials());
  193. $tokenCredentials = m::mock('League\OAuth1\Client\Credentials\TokenCredentials');
  194. $tokenCredentials->shouldReceive('getIdentifier')->andReturn('mock_identifier');
  195. $tokenCredentials->shouldReceive('getSecret')->andReturn('mock_secret');
  196. // OAuth protocol specifies a strict number of
  197. // headers should be sent, in the correct order.
  198. // We'll validate that here.
  199. $pattern = '/OAuth oauth_consumer_key=".*?", oauth_nonce="[a-zA-Z0-9]+", oauth_signature_method="HMAC-SHA1", oauth_timestamp="\d{10}", oauth_version="1.0", oauth_token="mock_identifier", oauth_signature=".*?"/';
  200. // With a GET request
  201. $headers = $server->getHeaders($tokenCredentials, 'GET', 'http://example.com/');
  202. $this->assertTrue(isset($headers['Authorization']));
  203. $matches = preg_match($pattern, $headers['Authorization']);
  204. $this->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
  205. // With a POST request
  206. $headers = $server->getHeaders($tokenCredentials, 'POST', 'http://example.com/', array('body' => 'params'));
  207. $this->assertTrue(isset($headers['Authorization']));
  208. $matches = preg_match($pattern, $headers['Authorization']);
  209. $this->assertEquals(1, $matches, 'Asserting that the authorization header contains the correct expression.');
  210. }
  211. protected function getMockClientCredentials()
  212. {
  213. return array(
  214. 'identifier' => 'myidentifier',
  215. 'secret' => 'mysecret',
  216. 'callback_uri' => 'http://app.dev/',
  217. );
  218. }
  219. }