11
11
12
12
namespace chillerlan \OAuthTest \Providers \Unit ;
13
13
14
- use chillerlan \OAuth \Core \{AccessToken , OAuth1Interface , OAuthInterface };
14
+ use chillerlan \OAuth \Core \{AccessToken , OAuth1Interface };
15
+ use chillerlan \HTTP \Utils \MessageUtil ;
15
16
use chillerlan \OAuth \Providers \ProviderException ;
17
+ use function str_starts_with ;
16
18
17
19
/**
18
20
* @property \chillerlan\OAuth\Core\OAuth1Interface $provider
19
21
*/
20
22
abstract class OAuth1ProviderUnitTestAbstract extends OAuthProviderUnitTestAbstract{
21
23
22
- protected OAuthInterface |OAuth1Interface $ provider ;
23
-
24
24
public function testOAuth1Instance ():void {
25
25
$ this ::assertInstanceOf (OAuth1Interface::class, $ this ->provider );
26
26
}
27
27
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
+
28
153
/*
29
154
* request authorization
30
155
*/
@@ -42,6 +167,7 @@ public function testGetRequestAuthorization():void{
42
167
$ this ::assertStringContainsString ('oauth_token="test_token" ' , $ authHeader );
43
168
}
44
169
170
+
45
171
/*
46
172
* signature
47
173
*/
0 commit comments