OAuthTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. use Mockery as m;
  3. use Overtrue\Socialite\Providers\Base;
  4. use Overtrue\Socialite\User;
  5. use PHPUnit\Framework\TestCase;
  6. class OAuthTest extends TestCase
  7. {
  8. public function tearDown(): void
  9. {
  10. m::close();
  11. }
  12. public function test_it_can_get_auth_url_without_redirect()
  13. {
  14. $config = [
  15. 'client_id' => 'fake_client_id',
  16. 'client_secret' => 'fake_client_secret',
  17. ];
  18. $provider = new OAuthTestProviderStub($config);
  19. $this->assertSame('http://auth.url?client_id=fake_client_id&scope=info&response_type=code', $provider->redirect());
  20. }
  21. public function test_it_can_get_auth_url_with_redirect()
  22. {
  23. // 手动配置
  24. $config = [
  25. 'client_id' => 'fake_client_id',
  26. 'client_secret' => 'fake_client_secret',
  27. ];
  28. $provider = new OAuthTestProviderStub($config);
  29. $this->assertSame('http://auth.url?client_id=fake_client_id&redirect_uri=fake_redirect&scope=info&response_type=code', $provider->redirect('fake_redirect'));
  30. // 用配置属性配置
  31. $config += ['redirect_url' => 'fake_redirect'];
  32. $provider = new OAuthTestProviderStub($config);
  33. $this->assertSame('http://auth.url?client_id=fake_client_id&redirect_uri=fake_redirect&scope=info&response_type=code', $provider->redirect('fake_redirect'));
  34. }
  35. public function test_it_can_get_auth_url_with_scopes()
  36. {
  37. $config = [
  38. 'client_id' => 'fake_client_id',
  39. 'client_secret' => 'fake_client_secret',
  40. ];
  41. $provider = new OAuthTestProviderStub($config);
  42. $url = $provider->scopes(['test_info', 'test_email'])->redirect();
  43. $this->assertSame('http://auth.url?client_id=fake_client_id&scope=test_info%2Ctest_email&response_type=code', $url);
  44. // 切换scope分割符
  45. $url = $provider->scopes(['test_info', 'test_email'])->withScopeSeparator(' ')->redirect();
  46. $this->assertSame('http://auth.url?client_id=fake_client_id&scope=test_info%20test_email&response_type=code', $url);
  47. }
  48. public function test_it_can_get_auth_url_with_state()
  49. {
  50. $config = [
  51. 'client_id' => 'fake_client_id',
  52. 'client_secret' => 'fake_client_secret',
  53. ];
  54. $provider = new OAuthTestProviderStub($config);
  55. $url = $provider->withState(123456)->redirect();
  56. $this->assertSame('http://auth.url?client_id=fake_client_id&scope=info&response_type=code&state=123456', $url);
  57. }
  58. public function test_it_can_get_token()
  59. {
  60. $config = [
  61. 'client_id' => 'fake_client_id',
  62. 'client_secret' => 'fake_client_secret',
  63. ];
  64. $provider = new OAuthTestProviderStub($config);
  65. $response = m::mock(\Psr\Http\Message\ResponseInterface::class);
  66. $response->shouldReceive('getBody')->andReturn($response);
  67. $response->shouldReceive('getContents')->andReturn([
  68. 'access_token' => 'fake_access_token',
  69. 'refresh_token' => 'fake_refresh_token',
  70. 'expires_in' => 123456,
  71. ]);
  72. $provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
  73. 'form_params' => [
  74. 'client_id' => 'fake_client_id',
  75. 'client_secret' => 'fake_client_secret',
  76. 'code' => 'fake_code',
  77. 'redirect_uri' => null,
  78. ],
  79. 'headers' => [
  80. 'Accept' => 'application/json',
  81. ],
  82. ])->andReturn($response);
  83. $this->assertSame([
  84. 'access_token' => 'fake_access_token',
  85. 'refresh_token' => 'fake_refresh_token',
  86. 'expires_in' => 123456,
  87. ], $provider->tokenFromCode('fake_code'));
  88. }
  89. public function test_it_can_get_user_by_token()
  90. {
  91. $config = [
  92. 'client_id' => 'fake_client_id',
  93. 'client_secret' => 'fake_client_secret',
  94. ];
  95. $provider = new OAuthTestProviderStub($config);
  96. $user = $provider->userFromToken('fake_access_token');
  97. $this->assertSame('foo', $user->getId());
  98. $this->assertSame(['id' => 'foo'], $user->getRaw());
  99. $this->assertSame('fake_access_token', $user->getAccessToken());
  100. }
  101. public function test_it_can_get_user_by_code()
  102. {
  103. $config = [
  104. 'client_id' => 'fake_client_id',
  105. 'client_secret' => 'fake_client_secret',
  106. ];
  107. $provider = new OAuthTestProviderStub($config);
  108. $response = m::mock(\Psr\Http\Message\ResponseInterface::class);
  109. $response->shouldReceive('getBody')->andReturn($response);
  110. $response->shouldReceive('getContents')->andReturn([
  111. 'access_token' => 'fake_access_token',
  112. 'refresh_token' => 'fake_refresh_token',
  113. 'expires_in' => 123456,
  114. ]);
  115. $provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
  116. 'form_params' => [
  117. 'client_id' => 'fake_client_id',
  118. 'client_secret' => 'fake_client_secret',
  119. 'code' => 'fake_code',
  120. 'redirect_uri' => null,
  121. ],
  122. 'headers' => [
  123. 'Accept' => 'application/json',
  124. ],
  125. ])->andReturn($response);
  126. $this->assertSame([
  127. 'access_token' => 'fake_access_token',
  128. 'refresh_token' => 'fake_refresh_token',
  129. 'expires_in' => 123456,
  130. ], $provider->tokenFromCode('fake_code'));
  131. $user = $provider->userFromCode('fake_code');
  132. $tokenResponse = [
  133. 'access_token' => 'fake_access_token',
  134. 'refresh_token' => 'fake_refresh_token',
  135. 'expires_in' => 123456,
  136. ];
  137. $this->assertSame('foo', $user->getId());
  138. $this->assertSame($tokenResponse, $user->getTokenResponse());
  139. $this->assertSame('fake_access_token', $user->getAccessToken());
  140. $this->assertSame('fake_refresh_token', $user->getRefreshToken());
  141. }
  142. }
  143. class OAuthTestProviderStub extends Base
  144. {
  145. public $http;
  146. protected array $scopes = ['info'];
  147. protected int $encodingType = PHP_QUERY_RFC3986;
  148. protected function getAuthUrl(): string
  149. {
  150. $url = 'http://auth.url';
  151. return $this->buildAuthUrlFromBase($url);
  152. }
  153. protected function getTokenUrl(): string
  154. {
  155. return 'http://token.url';
  156. }
  157. protected function getUserByToken(string $token): array
  158. {
  159. return ['id' => 'foo'];
  160. }
  161. protected function mapUserToObject(array $user): User
  162. {
  163. return new User(['id' => $user['id']]);
  164. }
  165. /**
  166. * Get a fresh instance of the Guzzle HTTP client.
  167. *
  168. * @return \GuzzleHttp\Client
  169. */
  170. public function getHttpClient(): \GuzzleHttp\Client
  171. {
  172. if ($this->http) {
  173. return $this->http;
  174. }
  175. return $this->http = m::mock(\GuzzleHttp\Client::class);
  176. }
  177. }