Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. ])->andReturn($response);
  80. $this->assertSame([
  81. 'access_token' => 'fake_access_token',
  82. 'refresh_token' => 'fake_refresh_token',
  83. 'expires_in' => 123456,
  84. ], $provider->tokenFromCode('fake_code'));
  85. }
  86. public function test_it_can_get_user_by_token()
  87. {
  88. $config = [
  89. 'client_id' => 'fake_client_id',
  90. 'client_secret' => 'fake_client_secret',
  91. ];
  92. $provider = new OAuthTestProviderStub($config);
  93. $user = $provider->userFromToken('fake_access_token');
  94. $this->assertSame('foo', $user->getId());
  95. $this->assertSame(['id' => 'foo'], $user->getRaw());
  96. $this->assertSame('fake_access_token', $user->getAccessToken());
  97. }
  98. public function test_it_can_get_user_by_code()
  99. {
  100. $config = [
  101. 'client_id' => 'fake_client_id',
  102. 'client_secret' => 'fake_client_secret',
  103. ];
  104. $provider = new OAuthTestProviderStub($config);
  105. $response = m::mock(\Psr\Http\Message\ResponseInterface::class);
  106. $response->shouldReceive('getBody')->andReturn($response);
  107. $response->shouldReceive('getContents')->andReturn([
  108. 'access_token' => 'fake_access_token',
  109. 'refresh_token' => 'fake_refresh_token',
  110. 'expires_in' => 123456,
  111. ]);
  112. $provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
  113. 'form_params' => [
  114. 'client_id' => 'fake_client_id',
  115. 'client_secret' => 'fake_client_secret',
  116. 'code' => 'fake_code',
  117. 'redirect_uri' => null,
  118. ],
  119. ])->andReturn($response);
  120. $this->assertSame([
  121. 'access_token' => 'fake_access_token',
  122. 'refresh_token' => 'fake_refresh_token',
  123. 'expires_in' => 123456,
  124. ], $provider->tokenFromCode('fake_code'));
  125. $user = $provider->userFromCode('fake_code');
  126. $tokenResponse = [
  127. 'access_token' => 'fake_access_token',
  128. 'refresh_token' => 'fake_refresh_token',
  129. 'expires_in' => 123456,
  130. ];
  131. $this->assertSame('foo', $user->getId());
  132. $this->assertSame($tokenResponse, $user->getTokenResponse());
  133. $this->assertSame('fake_access_token', $user->getAccessToken());
  134. $this->assertSame('fake_refresh_token', $user->getRefreshToken());
  135. }
  136. }
  137. class OAuthTestProviderStub extends Base
  138. {
  139. public $http;
  140. protected array $scopes = ['info'];
  141. protected int $encodingType = PHP_QUERY_RFC3986;
  142. protected function getAuthUrl(): string
  143. {
  144. $url = 'http://auth.url';
  145. return $this->buildAuthUrlFromBase($url);
  146. }
  147. protected function getTokenUrl(): string
  148. {
  149. return 'http://token.url';
  150. }
  151. protected function getUserByToken(string $token): array
  152. {
  153. return ['id' => 'foo'];
  154. }
  155. protected function mapUserToObject(array $user): User
  156. {
  157. return new User(['id' => $user['id']]);
  158. }
  159. /**
  160. * Get a fresh instance of the Guzzle HTTP client.
  161. *
  162. * @return \GuzzleHttp\Client
  163. */
  164. public function getHttpClient(): \GuzzleHttp\Client
  165. {
  166. if ($this->http) {
  167. return $this->http;
  168. }
  169. return $this->http = m::mock(\GuzzleHttp\Client::class);
  170. }
  171. }