OAuthTest.php 6.4 KB

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