Skip to content

Commit 43401c6

Browse files
committed
Merge pull request #5 from stevenmaguire/improve-error-response-support
Add improved error response support and tests
2 parents 754204c + f778262 commit 43401c6

File tree

4 files changed

+83
-26
lines changed

4 files changed

+83
-26
lines changed

CHANGELOG.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,40 @@
11
# Changelog
22
All Notable changes to `oauth2-bitbucket` will be documented in this file
33

4+
## 0.1.2 - 2016-02-06
5+
6+
### Added
7+
- Improved support for error response handling.
8+
9+
### Deprecated
10+
- Nothing
11+
12+
### Fixed
13+
- Nothing
14+
15+
### Removed
16+
- Nothing
17+
18+
### Security
19+
- Nothing
20+
21+
## 0.1.1 - 2015-08-20
22+
23+
### Added
24+
- Changelog
25+
26+
### Deprecated
27+
- Nothing
28+
29+
### Fixed
30+
- Nothing
31+
32+
### Removed
33+
- Nothing
34+
35+
### Security
36+
- Nothing
37+
438
## 0.1.0 - 2015-08-20
539

640
### Added

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
],
2020
"require": {
2121
"php": ">=5.5.0",
22-
"league/oauth2-client": "~1.0"
22+
"league/oauth2-client": "~1.2"
2323
},
2424
"require-dev": {
2525
"phpunit/phpunit": "~4.0",

src/Provider/Bitbucket.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
use League\OAuth2\Client\Provider\AbstractProvider;
44
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
55
use League\OAuth2\Client\Token\AccessToken;
6+
use League\OAuth2\Client\Tool\ArrayAccessorTrait;
67
use League\OAuth2\Client\Tool\BearerAuthorizationTrait;
78
use Psr\Http\Message\ResponseInterface;
89

910
class Bitbucket extends AbstractProvider
1011
{
11-
use BearerAuthorizationTrait;
12+
use ArrayAccessorTrait,
13+
BearerAuthorizationTrait;
1214

1315
/**
1416
* Get authorization url to begin OAuth flow
@@ -65,9 +67,16 @@ protected function getDefaultScopes()
6567
*/
6668
protected function checkResponse(ResponseInterface $response, $data)
6769
{
68-
if (isset($data['error'])) {
69-
throw new IdentityProviderException($data['error_description'], $response->getStatusCode(), $response);
70-
}
70+
$errors = [
71+
'error_description',
72+
'error.message',
73+
];
74+
75+
array_map(function ($error) use ($response, $data) {
76+
if ($message = $this->getValueByKey($data, $error)) {
77+
throw new IdentityProviderException($message, $response->getStatusCode(), $response);
78+
}
79+
}, $errors);
7180
}
7281

7382
/**

test/src/Provider/BitbucketTest.php

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -117,28 +117,42 @@ public function testUserData()
117117
$this->assertEquals($location, $user->toArray()['location']);
118118
}
119119

120-
/**
121-
*
122-
* @expectedException \League\OAuth2\Client\Provider\Exception\IdentityProviderException
123-
*/
124120
public function testUserDataFails()
125121
{
126-
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
127-
$postResponse->shouldReceive('getBody')->andReturn('{"access_token": "mock_access_token","scopes": "account","expires_in": 3600,"refresh_token": "mock_refresh_token","token_type": "bearer"}');
128-
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
129-
130-
$userResponse = m::mock('Psr\Http\Message\ResponseInterface');
131-
$userResponse->shouldReceive('getBody')->andReturn('{"error":"mock_error","error_description": "mock_error_description"}');
132-
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
133-
$userResponse->shouldReceive('getStatusCode')->andReturn(500);
134-
135-
$client = m::mock('GuzzleHttp\ClientInterface');
136-
$client->shouldReceive('send')
137-
->times(2)
138-
->andReturn($postResponse, $userResponse);
139-
$this->provider->setHttpClient($client);
140-
141-
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
142-
$user = $this->provider->getResourceOwner($token);
122+
$errorPayloads = [
123+
'{"error":"mock_error","error_description": "mock_error_description"}',
124+
'{"error":{"message":"mock_error"},"error_description": "mock_error_description"}',
125+
'{"foo":"bar"}'
126+
];
127+
128+
$testPayload = function ($payload) {
129+
$postResponse = m::mock('Psr\Http\Message\ResponseInterface');
130+
$postResponse->shouldReceive('getBody')->andReturn('{"access_token": "mock_access_token","scopes": "account","expires_in": 3600,"refresh_token": "mock_refresh_token","token_type": "bearer"}');
131+
$postResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
132+
133+
$userResponse = m::mock('Psr\Http\Message\ResponseInterface');
134+
$userResponse->shouldReceive('getBody')->andReturn($payload);
135+
$userResponse->shouldReceive('getHeader')->andReturn(['content-type' => 'json']);
136+
$userResponse->shouldReceive('getStatusCode')->andReturn(500);
137+
138+
$client = m::mock('GuzzleHttp\ClientInterface');
139+
$client->shouldReceive('send')
140+
->times(2)
141+
->andReturn($postResponse, $userResponse);
142+
$this->provider->setHttpClient($client);
143+
144+
$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);
145+
146+
try {
147+
$user = $this->provider->getResourceOwner($token);
148+
return false;
149+
} catch (\Exception $e) {
150+
$this->assertInstanceOf('\League\OAuth2\Client\Provider\Exception\IdentityProviderException', $e);
151+
}
152+
153+
return $payload;
154+
};
155+
156+
$this->assertCount(2, array_filter(array_map($testPayload, $errorPayloads)));
143157
}
144158
}

0 commit comments

Comments
 (0)