This commit is contained in:
2025-11-20 16:34:30 +01:00
parent 15d1a30e88
commit 4e4351e833
631 changed files with 130125 additions and 36318 deletions

View File

@@ -1,12 +1,12 @@
{
"name": "league\/oauth2-github",
"name": "league/oauth2-github",
"description": "Github OAuth 2.0 Client Provider for The PHP League OAuth2-Client",
"license": "MIT",
"authors": [
{
"name": "Steven Maguire",
"email": "stevenmaguire@gmail.com",
"homepage": "https:\/\/github.com\/stevenmaguire"
"homepage": "https://github.com/stevenmaguire"
}
],
"keywords": [
@@ -18,21 +18,21 @@
"github"
],
"require": {
"league\/oauth2-client": "^2.0"
"league/oauth2-client": "^2.0"
},
"require-dev": {
"phpunit\/phpunit": "~4.0",
"mockery\/mockery": "~0.9",
"squizlabs\/php_codesniffer": "~2.0"
"phpunit/phpunit": "~4.0",
"mockery/mockery": "~0.9",
"squizlabs/php_codesniffer": "~2.0"
},
"autoload": {
"psr-4": {
"Pshowsso\\Scope68f5e85e9608b\\League\\OAuth2\\Client\\": "src\/"
"League\\OAuth2\\Client\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Pshowsso\\Scope68f5e85e9608b\\League\\OAuth2\\Client\\Test\\": "test\/src\/"
"League\\OAuth2\\Client\\Test\\": "test/src/"
}
},
"extra": {
@@ -40,4 +40,4 @@
"dev-master": "1.0.x-dev"
}
}
}
}

View File

@@ -1,29 +1,36 @@
<?php
namespace Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Test\Provider;
<?php namespace League\OAuth2\Client\Test\Provider;
use Mockery as m;
class GithubResourceOwnerTest extends \PHPUnit_Framework_TestCase
{
public function testUrlIsNullWithoutDomainOrNickname()
{
$user = new \Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Provider\GithubResourceOwner();
$user = new \League\OAuth2\Client\Provider\GithubResourceOwner;
$url = $user->getUrl();
$this->assertNull($url);
}
public function testUrlIsDomainWithoutNickname()
{
$domain = uniqid();
$user = new \Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Provider\GithubResourceOwner();
$user = new \League\OAuth2\Client\Provider\GithubResourceOwner;
$user->setDomain($domain);
$url = $user->getUrl();
$this->assertEquals($domain, $url);
}
public function testUrlIsNicknameWithoutDomain()
{
$nickname = uniqid();
$user = new \Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Provider\GithubResourceOwner(['login' => $nickname]);
$user = new \League\OAuth2\Client\Provider\GithubResourceOwner(['login' => $nickname]);
$url = $user->getUrl();
$this->assertEquals($nickname, $url);
}
}

View File

@@ -1,25 +1,32 @@
<?php
namespace Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Test\Provider;
<?php namespace League\OAuth2\Client\Test\Provider;
use Mockery as m;
class GithubTest extends \PHPUnit_Framework_TestCase
{
protected $provider;
protected function setUp()
{
$this->provider = new \Pshowsso\Scope68f5e85e9608b\League\OAuth2\Client\Provider\Github(['clientId' => 'mock_client_id', 'clientSecret' => 'mock_secret', 'redirectUri' => 'none']);
$this->provider = new \League\OAuth2\Client\Provider\Github([
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
]);
}
public function tearDown()
{
m::close();
parent::tearDown();
}
public function testAuthorizationUrl()
{
$url = $this->provider->getAuthorizationUrl();
$uri = parse_url($url);
parse_str($uri['query'], $query);
$this->assertArrayHasKey('client_id', $query);
$this->assertArrayHasKey('redirect_uri', $query);
$this->assertArrayHasKey('state', $query);
@@ -28,75 +35,102 @@ class GithubTest extends \PHPUnit_Framework_TestCase
$this->assertArrayHasKey('approval_prompt', $query);
$this->assertNotNull($this->provider->getState());
}
public function testScopes()
{
$options = ['scope' => [uniqid(), uniqid()]];
$options = ['scope' => [uniqid(),uniqid()]];
$url = $this->provider->getAuthorizationUrl($options);
$this->assertContains(urlencode(implode(',', $options['scope'])), $url);
}
public function testGetAuthorizationUrl()
{
$url = $this->provider->getAuthorizationUrl();
$uri = parse_url($url);
$this->assertEquals('/login/oauth/authorize', $uri['path']);
}
public function testGetBaseAccessTokenUrl()
{
$params = [];
$url = $this->provider->getBaseAccessTokenUrl($params);
$uri = parse_url($url);
$this->assertEquals('/login/oauth/access_token', $uri['path']);
}
public function testGetAccessToken()
{
$response = m::mock('Pshowsso\Scope68f5e85e9608b\Psr\Http\Message\ResponseInterface');
$response = m::mock('Psr\Http\Message\ResponseInterface');
$response->shouldReceive('getBody')->andReturn('{"access_token":"mock_access_token", "scope":"repo,gist", "token_type":"bearer"}');
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$response->shouldReceive('getStatusCode')->andReturn(200);
$client = m::mock('Pshowsso\Scope68f5e85e9608b\GuzzleHttp\ClientInterface');
$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')->times(1)->andReturn($response);
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$this->assertEquals('mock_access_token', $token->getToken());
$this->assertNull($token->getExpires());
$this->assertNull($token->getRefreshToken());
$this->assertNull($token->getResourceOwnerId());
}
public function testGithubEnterpriseDomainUrls()
{
$this->provider->domain = 'https://github.company.com';
$response = m::mock('Pshowsso\Scope68f5e85e9608b\Psr\Http\Message\ResponseInterface');
$response = m::mock('Psr\Http\Message\ResponseInterface');
$response->shouldReceive('getBody')->times(1)->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}');
$response->shouldReceive('getHeader')->andReturn(['content-type' => 'application/x-www-form-urlencoded']);
$response->shouldReceive('getStatusCode')->andReturn(200);
$client = m::mock('Pshowsso\Scope68f5e85e9608b\GuzzleHttp\ClientInterface');
$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')->times(1)->andReturn($response);
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$this->assertEquals($this->provider->domain . '/login/oauth/authorize', $this->provider->getBaseAuthorizationUrl());
$this->assertEquals($this->provider->domain . '/login/oauth/access_token', $this->provider->getBaseAccessTokenUrl([]));
$this->assertEquals($this->provider->domain . '/api/v3/user', $this->provider->getResourceOwnerDetailsUrl($token));
$this->assertEquals($this->provider->domain.'/login/oauth/authorize', $this->provider->getBaseAuthorizationUrl());
$this->assertEquals($this->provider->domain.'/login/oauth/access_token', $this->provider->getBaseAccessTokenUrl([]));
$this->assertEquals($this->provider->domain.'/api/v3/user', $this->provider->getResourceOwnerDetailsUrl($token));
//$this->assertEquals($this->provider->domain.'/api/v3/user/emails', $this->provider->urlUserEmails($token));
}
public function testUserData()
{
$userId = rand(1000, 9999);
$userId = rand(1000,9999);
$name = uniqid();
$nickname = uniqid();
$email = uniqid();
$postResponse = m::mock('Pshowsso\Scope68f5e85e9608b\Psr\Http\Message\ResponseInterface');
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}');
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'application/x-www-form-urlencoded']);
$postResponse->shouldReceive('getStatusCode')->andReturn(200);
$userResponse = m::mock('Pshowsso\Scope68f5e85e9608b\Psr\Http\Message\ResponseInterface');
$userResponse->shouldReceive('getBody')->andReturn('{"id": ' . $userId . ', "login": "' . $nickname . '", "name": "' . $name . '", "email": "' . $email . '"}');
$userResponse = m::mock('Psr\Http\Message\ResponseInterface');
$userResponse->shouldReceive('getBody')->andReturn('{"id": '.$userId.', "login": "'.$nickname.'", "name": "'.$name.'", "email": "'.$email.'"}');
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$userResponse->shouldReceive('getStatusCode')->andReturn(200);
$client = m::mock('Pshowsso\Scope68f5e85e9608b\GuzzleHttp\ClientInterface');
$client->shouldReceive('send')->times(2)->andReturn($postResponse, $userResponse);
$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')
->times(2)
->andReturn($postResponse, $userResponse);
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$user = $this->provider->getResourceOwner($token);
$this->assertEquals($userId, $user->getId());
$this->assertEquals($userId, $user->toArray()['id']);
$this->assertEquals($name, $user->getName());
@@ -107,6 +141,7 @@ class GithubTest extends \PHPUnit_Framework_TestCase
$this->assertEquals($email, $user->toArray()['email']);
$this->assertContains($nickname, $user->getUrl());
}
public function testUserEmails()
{
/*
@@ -114,24 +149,24 @@ class GithubTest extends \PHPUnit_Framework_TestCase
$name = uniqid();
$nickname = uniqid();
$email = uniqid();
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn('access_token=mock_access_token&expires=3600&refresh_token=mock_refresh_token&otherKey={1234}');
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'application/x-www-form-urlencoded']);
$userResponse = m::mock('Psr\Http\Message\ResponseInterface');
$userResponse->shouldReceive('getBody')->andReturn('[{"email":"mock_email_1","primary":false,"verified":true},{"email":"mock_email_2","primary":false,"verified":true},{"email":"mock_email_3","primary":true,"verified":true}]');
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')
->times(2)
->andReturn($postResponse, $userResponse);
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
$emails = $this->provider->getUserEmails($token);
$this->assertEquals($userId, $user->getUserId());
$this->assertEquals($name, $user->getName());
$this->assertEquals($nickname, $user->getNickname());
@@ -139,33 +174,41 @@ class GithubTest extends \PHPUnit_Framework_TestCase
$this->assertContains($nickname, $user->getUrl());
*/
}
/**
* @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
**/
public function testExceptionThrownWhenErrorObjectReceived()
{
$status = rand(400, 600);
$postResponse = m::mock('Pshowsso\Scope68f5e85e9608b\Psr\Http\Message\ResponseInterface');
$status = rand(400,600);
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn('{"message": "Validation Failed","errors": [{"resource": "Issue","field": "title","code": "missing_field"}]}');
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn($status);
$client = m::mock('Pshowsso\Scope68f5e85e9608b\GuzzleHttp\ClientInterface');
$client->shouldReceive('send')->times(1)->andReturn($postResponse);
$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')
->times(1)
->andReturn($postResponse);
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
}
/**
* @expectedException League\OAuth2\Client\Provider\Exception\IdentityProviderException
**/
public function testExceptionThrownWhenOAuthErrorReceived()
{
$status = 200;
$postResponse = m::mock('Pshowsso\Scope68f5e85e9608b\Psr\Http\Message\ResponseInterface');
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
$postResponse->shouldReceive('getBody')->andReturn('{"error": "bad_verification_code","error_description": "The code passed is incorrect or expired.","error_uri": "https://developer.github.com/v3/oauth/#bad-verification-code"}');
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
$postResponse->shouldReceive('getStatusCode')->andReturn($status);
$client = m::mock('Pshowsso\Scope68f5e85e9608b\GuzzleHttp\ClientInterface');
$client->shouldReceive('send')->times(1)->andReturn($postResponse);
$client = m::mock('GuzzleHttp\ClientInterface');
$client->shouldReceive('send')
->times(1)
->andReturn($postResponse);
$this->provider->setHttpClient($client);
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
}