更新
This commit is contained in:
243
vendor/overtrue/socialite/tests/OAuthTest.php
vendored
Normal file
243
vendor/overtrue/socialite/tests/OAuthTest.php
vendored
Normal file
@@ -0,0 +1,243 @@
|
||||
<?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\User;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class OAuthTest extends TestCase
|
||||
{
|
||||
public function tearDown()
|
||||
{
|
||||
m::close();
|
||||
}
|
||||
|
||||
public function testAbstractProviderBackwardCompatible()
|
||||
{
|
||||
$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');
|
||||
|
||||
$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());
|
||||
}
|
||||
|
||||
public function testRedirectGeneratesTheProperSymfonyRedirectResponse()
|
||||
{
|
||||
$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();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
|
||||
$this->assertSame('http://auth.url', $response->getTargetUrl());
|
||||
}
|
||||
|
||||
public function testRedirectUrl()
|
||||
{
|
||||
$request = Request::create('foo', 'GET', ['state' => str_repeat('A', 40), 'code' => 'code']);
|
||||
$request->setSession($session = m::mock('Symfony\Component\HttpFoundation\Session\SessionInterface'));
|
||||
|
||||
$provider = new OAuthTwoTestProviderStub(
|
||||
$request, [
|
||||
'client_id' => 'client_id',
|
||||
'client_secret' => 'client_secret',
|
||||
]
|
||||
);
|
||||
$this->assertNull($provider->getRedirectUrl());
|
||||
|
||||
$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());
|
||||
}
|
||||
|
||||
public function testUserReturnsAUserInstanceForTheAuthenticatedRequest()
|
||||
{
|
||||
$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('{"access_token":"access_token"}');
|
||||
$user = $provider->user();
|
||||
|
||||
$this->assertInstanceOf('Overtrue\Socialite\User', $user);
|
||||
$this->assertSame('foo', $user->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* @expectedException \Overtrue\Socialite\InvalidStateException
|
||||
*/
|
||||
public function testExceptionIsThrownIfStateIsInvalid()
|
||||
{
|
||||
$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();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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();
|
||||
}
|
||||
|
||||
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('OAuthTwoTest', $provider->getName());
|
||||
}
|
||||
}
|
||||
|
||||
class OAuthTwoTestProviderStub extends AbstractProvider
|
||||
{
|
||||
public $http;
|
||||
|
||||
protected function getAuthUrl($state)
|
||||
{
|
||||
return 'http://auth.url';
|
||||
}
|
||||
|
||||
protected function getTokenUrl()
|
||||
{
|
||||
return 'http://token.url';
|
||||
}
|
||||
|
||||
protected function getUserByToken(AccessTokenInterface $token)
|
||||
{
|
||||
return ['id' => 'foo'];
|
||||
}
|
||||
|
||||
protected function mapUserToObject(array $user)
|
||||
{
|
||||
return new User(['id' => $user['id']]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a fresh instance of the Guzzle HTTP client.
|
||||
*
|
||||
* @return \GuzzleHttp\Client
|
||||
*/
|
||||
protected function getHttpClient()
|
||||
{
|
||||
if ($this->http) {
|
||||
return $this->http;
|
||||
}
|
||||
|
||||
return $this->http = m::mock('StdClass');
|
||||
}
|
||||
}
|
||||
60
vendor/overtrue/socialite/tests/Providers/WeWorkProviderTest.php
vendored
Normal file
60
vendor/overtrue/socialite/tests/Providers/WeWorkProviderTest.php
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
<?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 Overtrue\Socialite\Providers\WeWorkProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class WeWorkProviderTest extends TestCase
|
||||
{
|
||||
public function testQrConnect()
|
||||
{
|
||||
$response = (new WeWorkProvider(Request::create('foo'), [
|
||||
'client_id' => 'ww100000a5f2191',
|
||||
'client_secret' => 'client_secret',
|
||||
'redirect' => 'http://www.oa.com',
|
||||
]))
|
||||
->setAgentId('1000000')
|
||||
->stateless()
|
||||
->redirect();
|
||||
|
||||
$this->assertSame('https://open.work.weixin.qq.com/wwopen/sso/qrConnect?appid=ww100000a5f2191&agentid=1000000&redirect_uri=http%3A%2F%2Fwww.oa.com', $response->getTargetUrl());
|
||||
}
|
||||
|
||||
public function testOAuthWithAgentId()
|
||||
{
|
||||
$response = (new WeWorkProvider(Request::create('foo'), [
|
||||
'client_id' => 'CORPID',
|
||||
'client_secret' => 'client_secret',
|
||||
'redirect' => 'REDIRECT_URI',
|
||||
]))
|
||||
->scopes(['snsapi_base'])
|
||||
->setAgentId('1000000')
|
||||
->stateless()
|
||||
->redirect();
|
||||
|
||||
$this->assertSame('https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base&agentid=1000000#wechat_redirect', $response->getTargetUrl());
|
||||
}
|
||||
|
||||
public function testOAuthWithoutAgentId()
|
||||
{
|
||||
$response = (new WeWorkProvider(Request::create('foo'), [
|
||||
'client_id' => 'CORPID',
|
||||
'client_secret' => 'client_secret',
|
||||
'redirect' => 'REDIRECT_URI',
|
||||
]))
|
||||
->scopes(['snsapi_base'])
|
||||
->stateless()
|
||||
->redirect();
|
||||
|
||||
$this->assertSame('https://open.weixin.qq.com/connect/oauth2/authorize?appid=CORPID&redirect_uri=REDIRECT_URI&response_type=code&scope=snsapi_base#wechat_redirect', $response->getTargetUrl());
|
||||
}
|
||||
}
|
||||
45
vendor/overtrue/socialite/tests/UserTest.php
vendored
Normal file
45
vendor/overtrue/socialite/tests/UserTest.php
vendored
Normal file
@@ -0,0 +1,45 @@
|
||||
<?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 Overtrue\Socialite\AccessToken;
|
||||
use Overtrue\Socialite\User;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class UserTest extends TestCase
|
||||
{
|
||||
public function testJsonserialize()
|
||||
{
|
||||
$this->assertSame('[]', json_encode(new User([])));
|
||||
|
||||
$this->assertSame('{"token":"mock-token"}', json_encode(new User(['token' => new AccessToken(['access_token' => 'mock-token'])])));
|
||||
}
|
||||
|
||||
public function test_it_can_get_refresh_token()
|
||||
{
|
||||
$user = new User([
|
||||
'access_token' => 'mock-token',
|
||||
'refresh_token' => 'fake_refresh',
|
||||
]);
|
||||
|
||||
// 能通过用 User 对象获取 refresh token
|
||||
$this->assertSame('fake_refresh', $user->getRefreshToken());
|
||||
// json 序列化只有 token 字段
|
||||
$this->assertSame('{"access_token":"mock-token","refresh_token":"fake_refresh"}', json_encode($user));
|
||||
|
||||
$user = new User([]);
|
||||
$user->setToken(new AccessToken([
|
||||
'access_token' => 'mock-token',
|
||||
'refresh_token' => 'fake_refresh',
|
||||
]));
|
||||
$this->assertSame('fake_refresh', $user->getRefreshToken());
|
||||
$this->assertSame('{"token":"mock-token","access_token":"mock-token","refresh_token":"fake_refresh"}', json_encode($user));
|
||||
}
|
||||
}
|
||||
137
vendor/overtrue/socialite/tests/WechatProviderTest.php
vendored
Normal file
137
vendor/overtrue/socialite/tests/WechatProviderTest.php
vendored
Normal file
@@ -0,0 +1,137 @@
|
||||
<?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 Overtrue\Socialite\Providers\WeChatProvider as RealWeChatProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
class WechatProviderTest extends TestCase
|
||||
{
|
||||
public function testWeChatProviderHasCorrectlyRedirectResponse()
|
||||
{
|
||||
$response = (new WeChatProvider(Request::create('foo'), [
|
||||
'client_id' => 'client_id',
|
||||
'client_secret' => 'client_secret',
|
||||
'redirect' => 'http://localhost/socialite/callback.php',
|
||||
]))->redirect();
|
||||
|
||||
$this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response);
|
||||
$this->assertStringStartsWith('https://open.weixin.qq.com/connect/qrconnect', $response->getTargetUrl());
|
||||
$this->assertRegExp('/redirect_uri=http%3A%2F%2Flocalhost%2Fsocialite%2Fcallback.php/', $response->getTargetUrl());
|
||||
}
|
||||
|
||||
public function testWeChatProviderTokenUrlAndRequestFields()
|
||||
{
|
||||
$provider = new WeChatProvider(Request::create('foo'), [
|
||||
'client_id' => 'client_id',
|
||||
'client_secret' => 'client_secret',
|
||||
'redirect' => 'http://localhost/socialite/callback.php',
|
||||
]);
|
||||
|
||||
$this->assertSame('https://api.weixin.qq.com/sns/oauth2/access_token', $provider->tokenUrl());
|
||||
$this->assertSame([
|
||||
'appid' => 'client_id',
|
||||
'secret' => 'client_secret',
|
||||
'code' => 'iloveyou',
|
||||
'grant_type' => 'authorization_code',
|
||||
], $provider->tokenFields('iloveyou'));
|
||||
|
||||
$this->assertSame([
|
||||
'appid' => 'client_id',
|
||||
'redirect_uri' => 'http://localhost/socialite/callback.php',
|
||||
'response_type' => 'code',
|
||||
'scope' => 'snsapi_login',
|
||||
'state' => 'wechat-state',
|
||||
'connect_redirect' => 1,
|
||||
], $provider->codeFields('wechat-state'));
|
||||
}
|
||||
|
||||
public function testOpenPlatformComponent()
|
||||
{
|
||||
$provider = new WeChatProvider(Request::create('foo'), [
|
||||
'client_id' => 'client_id',
|
||||
'client_secret' => null,
|
||||
'redirect' => 'redirect-url',
|
||||
]);
|
||||
$provider->component(new WeChatComponent());
|
||||
$this->assertSame([
|
||||
'appid' => 'client_id',
|
||||
'redirect_uri' => 'redirect-url',
|
||||
'response_type' => 'code',
|
||||
'scope' => 'snsapi_base',
|
||||
'state' => 'state',
|
||||
'connect_redirect' => 1,
|
||||
'component_appid' => 'component-app-id',
|
||||
], $provider->codeFields('state'));
|
||||
|
||||
$this->assertSame([
|
||||
'appid' => 'client_id',
|
||||
'component_appid' => 'component-app-id',
|
||||
'component_access_token' => 'token',
|
||||
'code' => 'simcode',
|
||||
'grant_type' => 'authorization_code',
|
||||
], $provider->tokenFields('simcode'));
|
||||
|
||||
$this->assertSame('https://api.weixin.qq.com/sns/oauth2/component/access_token', $provider->tokenUrl());
|
||||
}
|
||||
|
||||
public function testOpenPlatformComponentWithCustomParameters()
|
||||
{
|
||||
$provider = new WeChatProvider(Request::create('foo'), [
|
||||
'client_id' => 'client_id',
|
||||
'client_secret' => null,
|
||||
'redirect' => 'redirect-url',
|
||||
]);
|
||||
$provider->component(new WeChatComponent());
|
||||
$provider->with(['foo' => 'bar']);
|
||||
|
||||
$fields = $provider->codeFields('wechat-state');
|
||||
|
||||
$this->assertArrayHasKey('foo', $fields);
|
||||
$this->assertSame('bar', $fields['foo']);
|
||||
}
|
||||
}
|
||||
|
||||
trait ProviderTrait
|
||||
{
|
||||
public function tokenUrl()
|
||||
{
|
||||
return $this->getTokenUrl();
|
||||
}
|
||||
|
||||
public function tokenFields($code)
|
||||
{
|
||||
return $this->getTokenFields($code);
|
||||
}
|
||||
|
||||
public function codeFields($state = null)
|
||||
{
|
||||
return $this->getCodeFields($state);
|
||||
}
|
||||
}
|
||||
|
||||
class WeChatProvider extends RealWeChatProvider
|
||||
{
|
||||
use ProviderTrait;
|
||||
}
|
||||
|
||||
class WeChatComponent implements \Overtrue\Socialite\WeChatComponentInterface
|
||||
{
|
||||
public function getAppId()
|
||||
{
|
||||
return 'component-app-id';
|
||||
}
|
||||
|
||||
public function getToken()
|
||||
{
|
||||
return 'token';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user