OAuthTest.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. /*
  3. * This file is part of the overtrue/socialite.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. use Mockery as m;
  11. use Overtrue\Socialite\AccessTokenInterface;
  12. use Overtrue\Socialite\Providers\AbstractProvider;
  13. use Overtrue\Socialite\User;
  14. use Symfony\Component\HttpFoundation\Request;
  15. class OAuthTest extends PHPUnit_Framework_TestCase
  16. {
  17. public function tearDown()
  18. {
  19. m::close();
  20. }
  21. public function testRedirectGeneratesTheProperSymfonyRedirectResponse()
  22. {
  23. $request = Request::create('foo');
  24. $request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
  25. $session->shouldReceive('put')->once();
  26. $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect');
  27. $response = $provider->redirect();
  28. $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
  29. $this->assertSame('http://auth.url', $response->getTargetUrl());
  30. }
  31. public function testRedirectUrl()
  32. {
  33. $request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
  34. $request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
  35. $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret');
  36. $this->assertNull($provider->getRedirectUrl());
  37. $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri');
  38. $this->assertSame('redirect_uri', $provider->getRedirectUrl());
  39. $provider->setRedirectUrl('overtrue.me');
  40. $this->assertSame('overtrue.me', $provider->getRedirectUrl());
  41. $provider->withRedirectUrl('http://overtrue.me');
  42. $this->assertSame('http://overtrue.me', $provider->getRedirectUrl());
  43. }
  44. public function testUserReturnsAUserInstanceForTheAuthenticatedRequest()
  45. {
  46. $request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
  47. $request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
  48. $session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
  49. $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri');
  50. $provider->http = m::mock('StdClass');
  51. $provider->http->shouldReceive('post')->once()->with('http://token.url', [
  52. 'headers' => ['Accept' => 'application/json'], 'form_params' => ['client_id' => 'client_id', 'client_secret' => 'client_secret', 'code' => 'code', 'redirect_uri' => 'redirect_uri'],
  53. ])->andReturn($response = m::mock('StdClass'));
  54. $response->shouldReceive('getBody')->once()->andReturn('{"access_token":"access_token"}');
  55. $user = $provider->user();
  56. $this->assertInstanceOf('Overtrue\Socialite\User', $user);
  57. $this->assertSame('foo', $user->getId());
  58. }
  59. /**
  60. * @expectedException \Overtrue\Socialite\InvalidStateException
  61. */
  62. public function testExceptionIsThrownIfStateIsInvalid()
  63. {
  64. $request = Request::create('foo', 'GET', ['state' => str_repeat('B', 40), 'code' => 'code']);
  65. $request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
  66. $session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
  67. $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect');
  68. $user = $provider->user();
  69. }
  70. /**
  71. * @expectedException \Overtrue\Socialite\AuthorizeFailedException
  72. * @expectedExceptionMessage Authorize Failed: {"error":"scope is invalid"}
  73. */
  74. public function testExceptionisThrownIfAuthorizeFailed()
  75. {
  76. $request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
  77. $request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
  78. $session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
  79. $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect_uri');
  80. $provider->http = m::mock('StdClass');
  81. $provider->http->shouldReceive('post')->once()->with('http://token.url', [
  82. 'headers' => ['Accept' => 'application/json'], 'form_params' => ['client_id' => 'client_id', 'client_secret' => 'client_secret', 'code' => 'code', 'redirect_uri' => 'redirect_uri'],
  83. ])->andReturn($response = m::mock('StdClass'));
  84. $response->shouldReceive('getBody')->once()->andReturn('{"error":"scope is invalid"}');
  85. $user = $provider->user();
  86. }
  87. /**
  88. * @expectedException \Overtrue\Socialite\InvalidStateException
  89. */
  90. public function testExceptionIsThrownIfStateIsNotSet()
  91. {
  92. $request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']);
  93. $request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
  94. $session->shouldReceive('get')->once()->with('state');
  95. $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect');
  96. $user = $provider->user();
  97. }
  98. public function testDriverName()
  99. {
  100. $request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']);
  101. $provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect');
  102. $this->assertSame('OAuthTwoTest', $provider->getName());
  103. }
  104. }
  105. class OAuthTwoTestProviderStub extends AbstractProvider
  106. {
  107. public $http;
  108. protected function getAuthUrl($state)
  109. {
  110. return 'http://auth.url';
  111. }
  112. protected function getTokenUrl()
  113. {
  114. return 'http://token.url';
  115. }
  116. protected function getUserByToken(AccessTokenInterface $token)
  117. {
  118. return ['id' => 'foo'];
  119. }
  120. protected function mapUserToObject(array $user)
  121. {
  122. return new User(['id' => $user['id']]);
  123. }
  124. /**
  125. * Get a fresh instance of the Guzzle HTTP client.
  126. *
  127. * @return \GuzzleHttp\Client
  128. */
  129. protected function getHttpClient()
  130. {
  131. if ($this->http) {
  132. return $this->http;
  133. }
  134. return $this->http = m::mock('StdClass');
  135. }
  136. }