fix:更新已知bug,优化代码

This commit is contained in:
Ying
2022-11-28 19:11:12 +08:00
parent f6aee95cfc
commit 9445b206a2
1378 changed files with 53759 additions and 20789 deletions

View File

@@ -1,228 +1,198 @@
<?php
/*
* This file is part of the overtrue/socialite.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
use Mockery as m;
use Overtrue\Socialite\AccessTokenInterface;
use Overtrue\Socialite\Providers\AbstractProvider;
use Overtrue\Socialite\Providers\Base;
use Overtrue\Socialite\User;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Request;
class OAuthTest extends TestCase
{
public function tearDown()
public function tearDown(): void
{
m::close();
}
public function testAbstractProviderBackwardCompatible()
public function test_it_can_get_auth_url_without_redirect()
{
$request = Request::create('foo');
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('put')->once();
$provider = new OAuthTwoTestProviderStub($request, 'client_id', 'client_secret', 'redirect');
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$this->assertSame('client_id', $provider->getConfig()['client_id']);
$this->assertSame('client_secret', $provider->getConfig()['client_secret']);
$this->assertSame('redirect', $provider->getConfig()['redirect']);
$response = $provider->redirect();
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertSame('http://auth.url', $response->getTargetUrl());
$this->assertSame('http://auth.url?client_id=fake_client_id&scope=info&response_type=code', $provider->redirect());
}
public function testRedirectGeneratesTheProperSymfonyRedirectResponse()
public function test_it_can_get_auth_url_with_redirect()
{
$request = Request::create('foo');
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('put')->once();
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]
);
$response = $provider->redirect();
// 手动配置
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
$this->assertSame('http://auth.url', $response->getTargetUrl());
$this->assertSame('http://auth.url?client_id=fake_client_id&redirect_uri=fake_redirect&scope=info&response_type=code', $provider->redirect('fake_redirect'));
// 用配置属性配置
$config += ['redirect_url' => 'fake_redirect'];
$provider = new OAuthTestProviderStub($config);
$this->assertSame('http://auth.url?client_id=fake_client_id&redirect_uri=fake_redirect&scope=info&response_type=code', $provider->redirect('fake_redirect'));
}
public function testRedirectUrl()
public function test_it_can_get_auth_url_with_scopes()
{
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$url = $provider->scopes(['test_info', 'test_email'])->redirect();
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
]
);
$this->assertNull($provider->getRedirectUrl());
$this->assertSame('http://auth.url?client_id=fake_client_id&scope=test_info%2Ctest_email&response_type=code', $url);
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect_uri',
]
);
$this->assertSame('redirect_uri', $provider->getRedirectUrl());
$provider->setRedirectUrl('overtrue.me');
$this->assertSame('overtrue.me', $provider->getRedirectUrl());
$provider->withRedirectUrl('http://overtrue.me');
$this->assertSame('http://overtrue.me', $provider->getRedirectUrl());
// 切换scope分割符
$url = $provider->scopes(['test_info', 'test_email'])->withScopeSeparator(' ')->redirect();
$this->assertSame('http://auth.url?client_id=fake_client_id&scope=test_info%20test_email&response_type=code', $url);
}
public function testUserReturnsAUserInstanceForTheAuthenticatedRequest()
public function test_it_can_get_auth_url_with_state()
{
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$url = $provider->withState(123456)->redirect();
$session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect_uri',
]
);
$provider->http = m::mock('StdClass');
$provider->http->shouldReceive('post')->once()->with(
'http://token.url',
[
'headers' => ['Accept' => 'application/json'],
'form_params' => [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'code' => 'code',
'redirect_uri' => 'redirect_uri',
],
]
)->andReturn($response = m::mock('StdClass'));
$response->shouldReceive('getBody')->once()->andReturn('{"access_token":"access_token"}');
$user = $provider->user();
$this->assertSame('http://auth.url?client_id=fake_client_id&scope=info&response_type=code&state=123456', $url);
}
public function test_it_can_get_token()
{
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$response = m::mock(\Psr\Http\Message\ResponseInterface::class);
$response->shouldReceive('getBody')->andReturn($response);
$response->shouldReceive('__toString')->andReturn(\json_encode([
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
]));
$provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
'form_params' => [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
'code' => 'fake_code',
'redirect_uri' => null,
],
'headers' => [
'Accept' => 'application/json',
],
])->andReturn($response);
$this->assertSame([
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
], $provider->tokenFromCode('fake_code'));
}
public function test_it_can_get_user_by_token()
{
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
$user = $provider->userFromToken('fake_access_token');
$this->assertInstanceOf('Overtrue\Socialite\User', $user);
$this->assertSame('foo', $user->getId());
$this->assertSame(['id' => 'foo'], $user->getRaw());
$this->assertSame('fake_access_token', $user->getAccessToken());
}
/**
* @expectedException \Overtrue\Socialite\InvalidStateException
*/
public function testExceptionIsThrownIfStateIsInvalid()
public function test_it_can_get_user_by_code()
{
$request = Request::create('foo', 'GET', ['state' => str_repeat('B', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]
);
$user = $provider->user();
}
$config = [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
];
$provider = new OAuthTestProviderStub($config);
/**
* @expectedException \Overtrue\Socialite\AuthorizeFailedException
* @expectedExceptionMessage Authorize Failed: {"error":"scope is invalid"}
*/
public function testExceptionisThrownIfAuthorizeFailed()
{
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('get')->once()->with('state')->andReturn(str_repeat('A', 40));
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect_uri',
]
);
$provider->http = m::mock('StdClass');
$provider->http->shouldReceive('post')->once()->with(
'http://token.url',
[
'headers' => ['Accept' => 'application/json'],
'form_params' => [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'code' => 'code',
'redirect_uri' => 'redirect_uri',
],
]
)->andReturn($response = m::mock('StdClass'));
$response->shouldReceive('getBody')->once()->andReturn('{"error":"scope is invalid"}');
$user = $provider->user();
}
$response = m::mock(\Psr\Http\Message\ResponseInterface::class);
$response->shouldReceive('getBody')->andReturn($response);
$response->shouldReceive('__toString')->andReturn(\json_encode([
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
]));
/**
* @expectedException \Overtrue\Socialite\InvalidStateException
*/
public function testExceptionIsThrownIfStateIsNotSet()
{
$request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']);
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
$session->shouldReceive('get')->once()->with('state');
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]
);
$user = $provider->user();
}
$provider->getHttpClient()->shouldReceive('post')->with('http://token.url', [
'form_params' => [
'client_id' => 'fake_client_id',
'client_secret' => 'fake_client_secret',
'code' => 'fake_code',
'redirect_uri' => null,
],
'headers' => [
'Accept' => 'application/json',
],
])->andReturn($response);
public function testDriverName()
{
$request = Request::create('foo', 'GET', ['state' => 'state', 'code' => 'code']);
$provider = new OAuthTwoTestProviderStub(
$request, [
'client_id' => 'client_id',
'client_secret' => 'client_secret',
'redirect' => 'redirect',
]
);
$this->assertSame([
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
], $provider->tokenFromCode('fake_code'));
$this->assertSame('OAuthTwoTest', $provider->getName());
$user = $provider->userFromCode('fake_code');
$tokenResponse = [
'access_token' => 'fake_access_token',
'refresh_token' => 'fake_refresh_token',
'expires_in' => 123456,
];
$this->assertSame('foo', $user->getId());
$this->assertSame($tokenResponse, $user->getTokenResponse());
$this->assertSame('fake_access_token', $user->getAccessToken());
$this->assertSame('fake_refresh_token', $user->getRefreshToken());
}
}
class OAuthTwoTestProviderStub extends AbstractProvider
class OAuthTestProviderStub extends Base
{
public $http;
protected function getAuthUrl($state)
protected array $scopes = ['info'];
protected int $encodingType = PHP_QUERY_RFC3986;
protected function getAuthUrl(): string
{
return 'http://auth.url';
$url = 'http://auth.url';
return $this->buildAuthUrlFromBase($url);
}
protected function getTokenUrl()
protected function getTokenUrl(): string
{
return 'http://token.url';
}
protected function getUserByToken(AccessTokenInterface $token)
protected function getUserByToken(string $token): array
{
return ['id' => 'foo'];
}
protected function mapUserToObject(array $user)
protected function mapUserToObject(array $user): User
{
return new User(['id' => $user['id']]);
}
@@ -232,12 +202,12 @@ class OAuthTwoTestProviderStub extends AbstractProvider
*
* @return \GuzzleHttp\Client
*/
protected function getHttpClient()
public function getHttpClient(): GuzzleHttp\Client
{
if ($this->http) {
return $this->http;
}
return $this->http = m::mock('StdClass');
return $this->http = m::mock(\GuzzleHttp\Client::class);
}
}