Skip to content

Commit

Permalink
Merge pull request #1 from arai-ta/fix-token-endpoint-authorization
Browse files Browse the repository at this point in the history
Fix token endpoint authorization
  • Loading branch information
arai-ta authored Jan 28, 2021
2 parents e7b2662 + b2ecab5 commit 71d02f0
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: ci

on: [push]

jobs:
run-phpunit:
runs-on: ubuntu-latest

strategy:
matrix:
php: [7.0, 7.1, 7.2, 7.3, 7.4]

steps:
- uses: actions/checkout@v2

- uses: php-actions/composer@v5
with:
php_version: ${{ matrix.php }}

- name: PHPUnit Tests
uses: php-actions/phpunit@v2
with:
version: 6
php_version: ${{ matrix.php }}
bootstrap: vendor/autoload.php
args: --coverage-text
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
],
"require": {
"php": "^7.0",
"league/oauth2-client": "^2.2"
"league/oauth2-client": ">=2.2.0 <2.4.0"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
Expand Down
43 changes: 43 additions & 0 deletions tests/ChatWorkProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace ChatWork\OAuth2\Client\Test;

require 'vendor/phpunit/phpunit/src/Framework/Assert/Functions.php';

use ChatWork\OAuth2\Client\ChatWorkProvider;
use League\OAuth2\Client\Grant\AuthorizationCode;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;

/**
* @package ChatWork\OAuth2\Client\Test
Expand All @@ -28,4 +32,43 @@ public function getAuthorizationUrl_containsRequiredParameters()

$this->assertNotNull($provider->getState());
}

/**
* @test
*/
public function getAccessToken_willSendClientIdByHeader()
{
$clientId = 'test_client_id';
$clientSecret = 'test_client_secret';
$expectedToken = base64_encode("{$clientId}:{$clientSecret}");

$assert = function (RequestInterface $request) use ($expectedToken) {
parse_str($request->getBody()->getContents(), $param);
assertArrayNotHasKey('client_id', $param, 'request body should not contains "client_id".');
assertArrayNotHasKey('client_secret', $param, 'request body should not contains "client_secret" too.');
assertEquals("Basic {$expectedToken}", $request->getHeader('Authorization')[0], 'client MUST use Basic Authentication');
throw new \Exception('OK'); // stop before send request
};

$this->expectExceptionMessage('OK');

$provider = new ChatWorkProvider(
$clientId,
$clientSecret,
'https://example.com/',
[
'httpClient' => $this->createSpyClient($assert)
]
);

$provider->getAccessToken(new AuthorizationCode(), ['code' => 'authorization_code']);
}

private function createSpyClient(callable $assetion)
{
$stack = \GuzzleHttp\HandlerStack::create();
$stack->push(\GuzzleHttp\Middleware::tap($assetion));
return new \GuzzleHttp\Client(['handler' => $stack]);
}

}

0 comments on commit 71d02f0

Please sign in to comment.