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

Commit 978abed

Browse files
committed
1 parent f4d49f8 commit 978abed

File tree

8 files changed

+655
-12
lines changed

8 files changed

+655
-12
lines changed

examples/OAuth1Testprovider.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
use chillerlan\OAuth\Core\OAuth1Provider;
1616

17-
/**
18-
*
19-
*/
2017
class OAuth1Testprovider extends OAuth1Provider{
2118

2219
protected $apiURL = 'https://api.example.com';
2320
protected $requestTokenURL = 'https://example.com/oauth/request_token';
2421
protected $authURL = 'https://example.com/oauth/authorize';
2522
protected $accessTokenURL = 'https://example.com/oauth/access_token';
2623
protected $userRevokeURL = 'https://account.example.com/apps/';
24+
protected $endpointMap = TestEndpoints::class;
25+
protected $authHeaders = ['foo' => 'bar'];
26+
protected $apiHeaders = ['foo' => 'bar'];
2727

2828
}

examples/OAuth2Testprovider.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@
1414

1515
use chillerlan\OAuth\Core\{
1616
ClientCredentials, CSRFToken, OAuth2CSRFTokenTrait, OAuth2ClientCredentialsTrait,
17-
OAuth2Provider, OAuth2TokenRefreshTrait, TokenExpires, TokenRefresh,
17+
OAuth2Provider, OAuth2TokenRefreshTrait, TokenExpires, TokenRefresh, AccessTokenForRefresh
1818
};
1919

20-
/**
21-
*
22-
*/
23-
class OAuth2Testprovider extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenExpires, TokenRefresh{
20+
class OAuth2Testprovider extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenExpires, TokenRefresh, AccessTokenForRefresh{
2421
use OAuth2CSRFTokenTrait, OAuth2ClientCredentialsTrait, OAuth2TokenRefreshTrait;
2522

26-
protected $apiURL = 'https://api.example.com/';
27-
protected $authURL = 'https://example.com/oauth2/authorize';
28-
protected $accessTokenURL = 'https://example.com/oauth2/token';
29-
protected $userRevokeURL = 'https://account.example.com/apps/';
23+
protected $apiURL = 'https://api.example.com/';
24+
protected $authURL = 'https://example.com/oauth2/authorize';
25+
protected $accessTokenURL = 'https://example.com/oauth2/token';
26+
protected $userRevokeURL = 'https://account.example.com/apps/';
27+
protected $endpointMap = TestEndpoints::class;
28+
protected $authHeaders = ['foo' => 'bar'];
29+
protected $apiHeaders = ['foo' => 'bar'];
3030

3131
}

examples/TestEndpoints.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Class TestEndpoints
4+
*
5+
* @filesource TestEndpoints.php
6+
* @created 09.09.2018
7+
* @package chillerlan\OAuthExamples
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\OAuthExamples;
14+
15+
use chillerlan\HTTP\MagicAPI\EndpointMap;
16+
17+
class TestEndpoints extends EndpointMap{
18+
19+
protected $test = [
20+
'path' => '/test/%1$s',
21+
'method' => 'GET',
22+
'query' => ['foo'],
23+
'path_elements' => ['id'],
24+
'body' => null,
25+
'headers' => [],
26+
];
27+
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Class GenericOAuth1Test
4+
*
5+
* @filesource GenericOAuth1Test.php
6+
* @created 09.09.2018
7+
* @package chillerlan\OAuthTest\Core
8+
* @author Smiley <smiley@chillerlan.net>
9+
* @copyright 2018 Smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\OAuthTest\Providers;
14+
15+
use chillerlan\OAuthExamples\OAuth1Testprovider;
16+
17+
/**
18+
* @property \chillerlan\OAuthExamples\OAuth1Testprovider $provider
19+
*/
20+
class GenericOAuth1Test extends OAuth1ProviderTestAbstract{
21+
22+
protected $FQN = OAuth1Testprovider::class;
23+
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
/**
3+
* Class GenericOAuth2Test
4+
*
5+
* @filesource GenericOAuth2Test.php
6+
* @created 09.09.2018
7+
* @package chillerlan\OAuthTest\Core
8+
* @author Smiley <smiley@chillerlan.net>
9+
* @copyright 2018 Smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\OAuthTest\Providers;
14+
15+
use chillerlan\OAuthExamples\OAuth2Testprovider;
16+
17+
/**
18+
* @property \chillerlan\OAuthExamples\OAuth2Testprovider $provider
19+
*/
20+
class GenericOAuth2Test extends OAuth2ProviderTestAbstract{
21+
22+
protected $FQN = OAuth2Testprovider::class;
23+
24+
}
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
<?php
2+
/**
3+
* Class OAuth1ProviderTestAbstract
4+
*
5+
* @filesource OAuth1ProviderTestAbstract.php
6+
* @created 09.09.2018
7+
* @package chillerlan\OAuthTest\Providers
8+
* @author smiley <smiley@chillerlan.net>
9+
* @copyright 2018 smiley
10+
* @license MIT
11+
*/
12+
13+
namespace chillerlan\OAuthTest\Providers;
14+
15+
use chillerlan\HTTP\Psr7\{Request, Response};
16+
use chillerlan\HTTP\Psr17;
17+
use chillerlan\OAuth\Core\{AccessToken, OAuth1Interface};
18+
19+
/**
20+
* @property \chillerlan\OAuth\Core\OAuth1Interface $provider
21+
*/
22+
abstract class OAuth1ProviderTestAbstract extends ProviderTestAbstract{
23+
24+
protected $responses = [
25+
'/oauth1/request_token' => 'oauth_token=test_request_token&oauth_token_secret=test_request_token_secret&oauth_callback_confirmed=true',
26+
'/oauth1/access_token' => 'oauth_token=test_access_token&oauth_token_secret=test_access_token_secret&oauth_callback_confirmed=true',
27+
'/oauth1/api/request' => '{"data":"such data! much wow!"}',
28+
];
29+
30+
protected function setUp(){
31+
parent::setUp();
32+
33+
$this->setProperty($this->provider, 'requestTokenURL', 'https://localhost/oauth1/request_token');
34+
$this->setProperty($this->provider, 'accessTokenURL', 'https://localhost/oauth1/access_token');
35+
$this->setProperty($this->provider, 'apiURL', 'https://localhost/oauth1/api/request');
36+
37+
}
38+
39+
public function testOAuth1Instance(){
40+
$this->assertInstanceOf(OAuth1Interface::class, $this->provider);
41+
}
42+
43+
public function testGetAuthURL(){
44+
parse_str(parse_url($this->provider->getAuthURL(), PHP_URL_QUERY), $query);
45+
46+
$this->assertSame('test_request_token', $query['oauth_token']);
47+
}
48+
49+
public function testGetSignature(){
50+
$signature = $this
51+
->getMethod('getSignature')
52+
->invokeArgs($this->provider, ['http://localhost/api/whatever', ['foo' => 'bar', 'oauth_signature' => 'should not see me!'], 'GET']);
53+
54+
$this->assertSame('ygg22quLhpyegiyr7yl4hLAP9S8=', $signature);
55+
}
56+
57+
/**
58+
* @expectedException \chillerlan\OAuth\Core\ProviderException
59+
* @expectedExceptionMessage getSignature: invalid url
60+
*/
61+
public function testGetSignatureInvalidURLException(){
62+
$this
63+
->getMethod('getSignature')
64+
->invokeArgs($this->provider, ['whatever', [], 'GET']);
65+
}
66+
67+
public function testGetAccessToken(){
68+
$token = new AccessToken(['accessTokenSecret' => 'test_request_token_secret']);
69+
$this->storage->storeAccessToken($this->provider->serviceName, $token);
70+
71+
$token = $this->provider->getAccessToken('test_request_token', 'verifier');
72+
73+
$this->assertSame('test_access_token', $token->accessToken);
74+
$this->assertSame('test_access_token_secret', $token->accessTokenSecret);
75+
}
76+
77+
/**
78+
* @expectedException \chillerlan\OAuth\Core\ProviderException
79+
* @expectedExceptionMessage unable to parse token response
80+
*/
81+
public function testParseTokenResponseNoData(){
82+
$this->getMethod('parseTokenResponse')->invokeArgs($this->provider, [new Response]);
83+
}
84+
85+
/**
86+
* @expectedException \chillerlan\OAuth\Core\ProviderException
87+
* @expectedExceptionMessage error retrieving access token
88+
*/
89+
public function testParseTokenResponseError(){
90+
$this
91+
->getMethod('parseTokenResponse')
92+
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('error=whatever'))])
93+
;
94+
}
95+
96+
/**
97+
* @expectedException \chillerlan\OAuth\Core\ProviderException
98+
* @expectedExceptionMessage invalid token
99+
*/
100+
public function testParseTokenResponseNoToken(){
101+
$this
102+
->getMethod('parseTokenResponse')
103+
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('oauth_token=whatever'))])
104+
;
105+
}
106+
107+
/**
108+
* @expectedException \chillerlan\OAuth\Core\ProviderException
109+
* @expectedExceptionMessage oauth callback unconfirmed
110+
*/
111+
public function testParseTokenResponseCallbackUnconfirmed(){
112+
$this
113+
->getMethod('parseTokenResponse')
114+
->invokeArgs($this->provider, [(new Response)->withBody(Psr17\create_stream_from_input('oauth_token=whatever&oauth_token_secret=whatever_secret')), true])
115+
;
116+
}
117+
118+
public function testGetRequestAuthorization(){
119+
120+
$authHeader = $this->provider
121+
->getRequestAuthorization(
122+
new Request('GET', 'https://foo.bar'),
123+
new AccessToken(['accessTokenSecret' => 'test_token_secret', 'accessToken' => 'test_token'])
124+
)
125+
->getHeaderLine('Authorization');
126+
127+
$this->assertContains('OAuth oauth_consumer_key="'.$this->options->key.'"', $authHeader);
128+
$this->assertContains('oauth_token="test_token"', $authHeader);
129+
}
130+
131+
public function testRequest(){
132+
$token = new AccessToken(['accessTokenSecret' => 'test_request_token_secret']);
133+
$this->storage->storeAccessToken($this->provider->serviceName, $token);
134+
135+
$this->assertSame('such data! much wow!', json_decode($this->provider->request('')->getBody()->getContents())->data);
136+
}
137+
138+
}

0 commit comments

Comments
 (0)