Skip to content
This repository was archived by the owner on Mar 23, 2024. It is now read-only.

Commit e8384c6

Browse files
committed
:octocat: update tests
1 parent 3cd5c75 commit e8384c6

11 files changed

+611
-28
lines changed

tests/Providers/Live/OAuthProviderLiveTestAbstract.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use function str_contains;
2020

2121
/**
22-
*
22+
* @property \chillerlan\OAuth\Core\OAuthInterface $provider
2323
*/
2424
abstract class OAuthProviderLiveTestAbstract extends ProviderLiveTestAbstract{
2525

tests/Providers/ProviderUnitTestAbstract.php

+8-5
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ protected function setUp():void{
6666

6767
}
6868

69+
6970
/*
7071
* abstract methods
7172
*/
@@ -75,13 +76,14 @@ protected function setUp():void{
7576
*/
7677
abstract protected function getProviderFQCN():string;
7778

79+
7880
/*
7981
* init provider & dependencies
8082
*/
8183

8284
protected function initOptions():OAuthOptions{
8385
return new OAuthOptions([
84-
'key' => 'testkey',
86+
'key' => 'testclient',
8587
'secret' => 'testsecret',
8688
'callbackURL' => 'https://localhost/callback',
8789
'tokenAutoRefresh' => true,
@@ -107,25 +109,26 @@ protected function initProvider(string $FQCN):OAuthInterface|OAuth1Interface|OAu
107109
return $this->invokeReflection($FQCN, $args);
108110
}
109111

112+
110113
/*
111114
* Reflection utilities
112115
*/
113116

114-
protected function invokeReflection(string $FQCN, array $args = []):object{
117+
final protected function invokeReflection(string $FQCN, array $args = []):object{
115118
$this->reflection = new ReflectionClass($FQCN);
116119

117120
return $this->reflection->newInstanceArgs($args);
118121
}
119122

120-
protected function setReflectionProperty(string $property, mixed $value):void{
123+
final protected function setReflectionProperty(string $property, mixed $value):void{
121124
$this->reflection->getProperty($property)->setValue($this->provider, $value);
122125
}
123126

124-
protected function getReflectionProperty(string $property):mixed{
127+
final protected function getReflectionProperty(string $property):mixed{
125128
return $this->reflection->getProperty($property)->getValue($this->provider);
126129
}
127130

128-
protected function invokeReflectionMethod(string $method, array $args = []):mixed{
131+
final protected function invokeReflectionMethod(string $method, array $args = []):mixed{
129132
return $this->reflection->getMethod($method)->invokeArgs($this->provider, $args);
130133
}
131134

tests/Providers/Unit/BattleNetTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public static function requestTargetProvider():array{
4747
'trailing slash' => ['c/', 'https://eu.api.blizzard.com/c/'],
4848
'full url given' => ['https://oauth.battle.net/other/path/d', 'https://oauth.battle.net/other/path/d'],
4949
'ignore params' => ['https://oauth.battle.net/api/e/?with=param#foo', 'https://oauth.battle.net/api/e/'],
50+
'enforce https' => ['wtf://eu.api.blizzard.com/a/b/c', 'https://eu.api.blizzard.com/a/b/c'],
5051
];
5152
}
5253

tests/Providers/Unit/DeezerTest.php

+84
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,99 @@
1010

1111
namespace chillerlan\OAuthTest\Providers\Unit;
1212

13+
use chillerlan\HTTP\Utils\QueryUtil;
1314
use chillerlan\OAuth\Providers\Deezer;
15+
use chillerlan\OAuth\Providers\ProviderException;
16+
use function implode;
1417

1518
/**
1619
* @property \chillerlan\OAuth\Providers\Deezer $provider
1720
*/
1821
class DeezerTest extends OAuth2ProviderUnitTestAbstract{
1922

23+
protected const TEST_TOKEN = 'access_token=2YotnFZFEjr1zCsicMWpAA&token_type=example&expires=3600&'.
24+
'refresh_token=tGzv3JOkF0XG5Qx2TlKWIA&example_parameter=example_value';
25+
2026
protected function getProviderFQCN():string{
2127
return Deezer::class;
2228
}
2329

30+
public function testParseTokenResponseWithScopes():void{
31+
$this::markTestSkipped('N/A');
32+
}
33+
34+
public function testParseTokenResponseNoDataException():void{
35+
$this->expectException(ProviderException::class);
36+
$this->expectExceptionMessage('unable to parse token response');
37+
38+
$response = $this->responseFactory
39+
->createResponse()
40+
->withBody($this->streamFactory->createStream(''))
41+
;
42+
43+
$this->invokeReflectionMethod('parseTokenResponse', [$response]);
44+
}
45+
46+
public function testParseTokenResponseErrorException():void{
47+
$this->expectException(ProviderException::class);
48+
$this->expectExceptionMessage('error retrieving access token');
49+
50+
// the error variable for deezer is "error_reason" and content type is form-data
51+
$response = $this->responseFactory
52+
->createResponse()
53+
->withBody($this->streamFactory->createStream('error_reason=whatever'))
54+
;
55+
56+
$this->invokeReflectionMethod('parseTokenResponse', [$response]);
57+
}
58+
59+
public function testParseTokenResponseNoTokenException():void{
60+
$this->expectException(ProviderException::class);
61+
$this->expectExceptionMessage('token missing');
62+
63+
$response = $this->responseFactory
64+
->createResponse()
65+
->withBody($this->streamFactory->createStream('foo=bar'))
66+
;
67+
68+
$this->invokeReflectionMethod('parseTokenResponse', [$response]);
69+
}
70+
71+
public function testGetAuthURL():void{
72+
$params = ['response_type' => 'whatever', 'foo' => 'bar']; // response_type shall be overwritten
73+
$scopes = ['scope1', 'scope2', 'scope3'];
74+
75+
$uri = $this->provider->getAuthURL($params, $scopes);
76+
$params = QueryUtil::parse($uri->getQuery());
77+
78+
$this::assertSame($this->getReflectionProperty('authURL'), (string)$uri->withQuery(''));
79+
80+
$this::assertSame($this->options->key, $params['app_id']);
81+
$this::assertSame($this->options->callbackURL, $params['redirect_uri']);
82+
$this::assertSame(implode($this->provider::SCOPE_DELIMITER, $scopes), $params['perms']);
83+
$this::assertSame('bar', $params['foo']);
84+
$this::assertArrayHasKey('state', $params);
85+
86+
}
87+
88+
public function testGetAuthURLRequestParams():void{
89+
$params = ['foo' => 'bar']; // response_type shall be overwritten
90+
$scopes = ['scope1', 'scope2', 'scope3'];
91+
92+
$queryparams = $this->invokeReflectionMethod('getAuthURLRequestParams', [$params, $scopes]);
93+
94+
$this::assertArrayHasKey('app_id', $queryparams);
95+
$this::assertArrayHasKey('redirect_uri', $queryparams);
96+
$this::assertSame(implode($this->provider::SCOPE_DELIMITER, $scopes), $queryparams['perms']);
97+
$this::assertSame('bar', $queryparams['foo']);
98+
}
99+
100+
public function testGetAccessTokenRequestBodyParams():void{
101+
$params = $this->invokeReflectionMethod('getAccessTokenRequestBodyParams', ['*test_code*']);
102+
103+
$this::assertSame('*test_code*', $params['code']);
104+
$this::assertSame($this->options->key, $params['app_id']);
105+
$this::assertSame($this->options->secret, $params['secret']);
106+
}
107+
24108
}

tests/Providers/Unit/GuildWars2Test.php

+4
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@ protected function getProviderFQCN():string{
2121
return GuildWars2::class;
2222
}
2323

24+
public function testGetAuthURL():void{
25+
$this::markTestSkipped('N/A');
26+
}
27+
2428
}

tests/Providers/Unit/MusicBrainzTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace chillerlan\OAuthTest\Providers\Unit;
1212

13+
use chillerlan\OAuth\Core\TokenRefresh;
1314
use chillerlan\OAuth\Providers\MusicBrainz;
1415

1516
/**
@@ -21,4 +22,18 @@ protected function getProviderFQCN():string{
2122
return MusicBrainz::class;
2223
}
2324

25+
public function testGetRefreshAccessTokenRequestBodyParams():void{
26+
27+
if(!$this->provider instanceof TokenRefresh){
28+
$this->markTestSkipped('TokenRefresh N/A');
29+
}
30+
31+
$params = $this->invokeReflectionMethod('getRefreshAccessTokenRequestBodyParams', ['*refresh_token*']);
32+
33+
$this::assertSame('*refresh_token*', $params['refresh_token']);
34+
$this::assertSame($this->options->key, $params['client_id']);
35+
$this::assertSame($this->options->secret, $params['client_secret']);
36+
$this::assertSame('refresh_token', $params['grant_type']);
37+
}
38+
2439
}

tests/Providers/Unit/OAuth1ProviderUnitTestAbstract.php

+129-3
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,145 @@
1111

1212
namespace chillerlan\OAuthTest\Providers\Unit;
1313

14-
use chillerlan\OAuth\Core\{AccessToken, OAuth1Interface, OAuthInterface};
14+
use chillerlan\OAuth\Core\{AccessToken, OAuth1Interface};
15+
use chillerlan\HTTP\Utils\MessageUtil;
1516
use chillerlan\OAuth\Providers\ProviderException;
17+
use function str_starts_with;
1618

1719
/**
1820
* @property \chillerlan\OAuth\Core\OAuth1Interface $provider
1921
*/
2022
abstract class OAuth1ProviderUnitTestAbstract extends OAuthProviderUnitTestAbstract{
2123

22-
protected OAuthInterface|OAuth1Interface $provider;
23-
2424
public function testOAuth1Instance():void{
2525
$this::assertInstanceOf(OAuth1Interface::class, $this->provider);
2626
}
2727

28+
/*
29+
* auth URL
30+
*/
31+
32+
public function testGetRequestTokenRequestParams():void{
33+
$params = $this->invokeReflectionMethod('getRequestTokenRequestParams');
34+
35+
$this::assertSame($this->options->callbackURL, $params['oauth_callback']);
36+
$this::assertSame($this->options->key, $params['oauth_consumer_key']);
37+
$this::assertArrayHasKey('oauth_nonce', $params);
38+
$this::assertSame('HMAC-SHA1', $params['oauth_signature_method']);
39+
$this::assertArrayHasKey('oauth_timestamp', $params);
40+
$this::assertSame('1.0', $params['oauth_version']);
41+
}
42+
43+
public function testSendRequestTokenRequest():void{
44+
$url = 'https://localhost/request_token';
45+
$response = $this->invokeReflectionMethod('sendRequestTokenRequest', [$url]);
46+
$json = MessageUtil::decodeJSON($response);
47+
48+
$this::assertTrue(str_starts_with($json->headers->{'Authorization'}, 'OAuth '));
49+
$this::assertSame('identity', $json->headers->{'Accept-Encoding'});
50+
$this::assertSame('0', $json->headers->{'Content-Length'});
51+
$this::assertSame('POST', $json->request->method);
52+
}
53+
54+
55+
/*
56+
* token response parser
57+
*/
58+
59+
public function testParseTokenResponse():void{
60+
// from https://datatracker.ietf.org/doc/html/rfc5849#section-2.3
61+
$responseBody = 'oauth_token=j49ddk933skd9dks&oauth_token_secret=ll399dj47dskfjdk';
62+
63+
$response = $this->responseFactory
64+
->createResponse()
65+
->withBody($this->streamFactory->createStream($responseBody))
66+
;
67+
68+
/** @var \chillerlan\OAuth\Core\AccessToken $token */
69+
$token = $this->invokeReflectionMethod('parseTokenResponse', [$response]);
70+
71+
$this::assertSame('j49ddk933skd9dks', $token->accessToken);
72+
$this::assertSame('ll399dj47dskfjdk', $token->accessTokenSecret);
73+
}
74+
75+
public function testParseTemporaryCredentialsTokenResponse():void{
76+
// from https://datatracker.ietf.org/doc/html/rfc5849#section-2.1
77+
$responseBody = 'oauth_token=hdk48Djdsa&oauth_token_secret=xyz4992k83j47x0b&oauth_callback_confirmed=true';
78+
79+
$response = $this->responseFactory
80+
->createResponse()
81+
->withBody($this->streamFactory->createStream($responseBody))
82+
;
83+
84+
/** @var \chillerlan\OAuth\Core\AccessToken $token */
85+
$token = $this->invokeReflectionMethod('parseTokenResponse', [$response, true]);
86+
87+
$this::assertSame('hdk48Djdsa', $token->accessToken);
88+
$this::assertSame('xyz4992k83j47x0b', $token->accessTokenSecret);
89+
}
90+
91+
public function testParseTokenResponseNoDataException():void{
92+
$this->expectException(ProviderException::class);
93+
$this->expectExceptionMessage('unable to parse token response');
94+
95+
$response = $this->responseFactory->createResponse();
96+
97+
$this->invokeReflectionMethod('parseTokenResponse', [$response]);
98+
}
99+
100+
public function testParseTokenResponseErrorException():void{
101+
$this->expectException(ProviderException::class);
102+
$this->expectExceptionMessage('error retrieving access token: "whatever"');
103+
104+
$body = $this->streamFactory->createStream('error=whatever');
105+
$response = $this->responseFactory->createResponse()->withBody($body);
106+
107+
$this->invokeReflectionMethod('parseTokenResponse', [$response]);
108+
}
109+
110+
public function testParseTokenResponseNoTokenException():void{
111+
$this->expectException(ProviderException::class);
112+
$this->expectExceptionMessage('invalid token');
113+
114+
$body = $this->streamFactory->createStream('oauth_token=whatever');
115+
$response = $this->responseFactory->createResponse()->withBody($body);
116+
117+
$this->invokeReflectionMethod('parseTokenResponse', [$response]);
118+
}
119+
120+
public function testParseTokenResponseConfirmCallbackException():void{
121+
$this->expectException(ProviderException::class);
122+
$this->expectExceptionMessage('oauth callback unconfirmed');
123+
124+
$body = $this->streamFactory->createStream('oauth_token=whatever&oauth_token_secret=whatever_secret');
125+
$response = $this->responseFactory->createResponse()->withBody($body);
126+
127+
$this->invokeReflectionMethod('parseTokenResponse', [$response, true]);
128+
}
129+
130+
131+
/*
132+
* access token
133+
*/
134+
135+
public function testSendAccessTokenRequest():void{
136+
// we need the request token for the access token request
137+
$requestToken = new AccessToken(['accessToken' => 'hdk48Djdsa', 'accessTokenSecret' => 'xyz4992k83j47x0b']);
138+
$this->provider->storeAccessToken($requestToken);
139+
140+
$response = $this->invokeReflectionMethod('sendAccessTokenRequest', ['*verifier*']);
141+
$json = MessageUtil::decodeJSON($response);
142+
143+
// check if the verifier is set
144+
$this::assertSame('*verifier*', $json->request->params->{'oauth_verifier'});
145+
146+
$this::assertTrue(str_starts_with($json->headers->{'Authorization'}, 'OAuth '));
147+
$this::assertSame('identity', $json->headers->{'Accept-Encoding'});
148+
$this::assertSame('0', $json->headers->{'Content-Length'});
149+
$this::assertSame('POST', $json->request->method);
150+
}
151+
152+
28153
/*
29154
* request authorization
30155
*/
@@ -42,6 +167,7 @@ public function testGetRequestAuthorization():void{
42167
$this::assertStringContainsString('oauth_token="test_token"', $authHeader);
43168
}
44169

170+
45171
/*
46172
* signature
47173
*/

0 commit comments

Comments
 (0)