1+ /*******************************************************************************
2+ * Copyright (c) 2017 Intuit
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ *******************************************************************************/
16+ package com .intuit .oauth2 .client ;
17+
18+
19+ import static org .testng .Assert .assertEquals ;
20+ import static org .testng .Assert .assertFalse ;
21+ import static org .testng .Assert .assertNotNull ;
22+ import static org .testng .Assert .assertNull ;
23+ import static org .testng .Assert .assertTrue ;
24+
25+ import com .fasterxml .jackson .core .JsonProcessingException ;
26+ import com .fasterxml .jackson .databind .ObjectMapper ;
27+ import com .fasterxml .jackson .databind .ObjectWriter ;
28+ import com .intuit .oauth2 .config .OAuth2Config ;
29+ import com .intuit .oauth2 .config .ProxyConfig ;
30+ import com .intuit .oauth2 .data .BearerTokenResponse ;
31+ import com .intuit .oauth2 .data .PlatformResponse ;
32+ import com .intuit .oauth2 .data .UserInfoResponse ;
33+ import com .intuit .oauth2 .exception .InvalidRequestException ;
34+ import com .intuit .oauth2 .exception .OAuthException ;
35+ import com .intuit .oauth2 .exception .OpenIdException ;
36+ import com .intuit .oauth2 .http .HttpRequestClient ;
37+ import com .intuit .oauth2 .http .Request ;
38+ import com .intuit .oauth2 .http .Response ;
39+ import com .intuit .oauth2 .utils .MapperImpl ;
40+ import java .nio .charset .StandardCharsets ;
41+ import mockit .Mock ;
42+ import mockit .MockUp ;
43+ import org .apache .commons .io .IOUtils ;
44+ import org .apache .http .message .BasicNameValuePair ;
45+ import org .testng .annotations .BeforeClass ;
46+ import org .testng .annotations .Test ;
47+
48+ public class OAuth2PlatformClientTest {
49+
50+ public static final ObjectMapper mapper = MapperImpl .getInstance ();
51+ public static final String AUTH_CODE = "authCode" ;
52+ public static final String REDIRECT_URI = "https://4f4390eb.ngrok.io/oauth2redirect" ;
53+
54+ private MockedHttpRequestClient mockedHttpRequestClient ;
55+ private OAuth2PlatformClient oAuth2PlatformClient ;
56+ private OAuth2Config oauth2Config ;
57+ private ProxyConfig proxyConfig ;
58+
59+ public OAuth2PlatformClientTest () {
60+
61+ oauth2Config = new OAuth2Config .OAuth2ConfigBuilder ("test-client" , "test-secret" )
62+ .proxyConfig (proxyConfig ).buildConfig ();
63+ oAuth2PlatformClient = new OAuth2PlatformClient (oauth2Config );
64+
65+ }
66+
67+ @ BeforeClass
68+ public void setup () {
69+ proxyConfig = new ProxyConfig .ProxyConfigBuilder ("test-host" , "8080" )
70+ .username ("username" ).password ("password" ).domain ("test-domain" ).buildConfig ();
71+ mockedHttpRequestClient = new MockedHttpRequestClient ();
72+ }
73+
74+
75+ @ Test
76+ public void canRetrieveBearerTokensTest () throws Exception {
77+ ObjectWriter writer = mapper .writerFor (BearerTokenResponse .class );
78+
79+ BearerTokenResponse mockBTResponse = new BearerTokenResponse ();
80+ mockBTResponse .setAccessToken ("access-token" );
81+ mockBTResponse .setRefreshToken ("refresh-token" );
82+ mockBTResponse .setExpiresIn (200L );
83+ mockBTResponse .setTokenType ("bearer-token" );
84+ Response mockResponse = new Response (
85+ IOUtils .toInputStream (writer .writeValueAsString (mockBTResponse ), StandardCharsets .UTF_8 ), 200 );
86+ mockedHttpRequestClient .setMockResponse (mockResponse );
87+
88+ BearerTokenResponse bearerTokenResponse = oAuth2PlatformClient .retrieveBearerTokens (AUTH_CODE , REDIRECT_URI );
89+
90+ assertNotNull (mockedHttpRequestClient .getServiceRequestReceived ().getAuthString ());
91+ assertTrue (mockedHttpRequestClient .getServiceRequestReceived ().getPostParams ()
92+ .contains (new BasicNameValuePair ("grant_type" , "authorization_code" )));
93+ assertEquals (mockBTResponse .getAccessToken (), bearerTokenResponse .getAccessToken ());
94+ assertEquals (mockBTResponse .getRefreshToken (), bearerTokenResponse .getRefreshToken ());
95+ assertEquals (mockBTResponse .getExpiresIn (), bearerTokenResponse .getExpiresIn ());
96+ assertEquals (mockBTResponse .getTokenType (), bearerTokenResponse .getTokenType ());
97+ }
98+
99+ @ Test (expectedExceptions = OAuthException .class )
100+ public void retrieveBearerTokensThrowsOAuthExceptionOnErrorStatus () throws JsonProcessingException , OAuthException {
101+ ObjectWriter writer = mapper .writerFor (BearerTokenResponse .class );
102+ Response mockResponse = new Response (
103+ IOUtils .toInputStream (writer .writeValueAsString (new BearerTokenResponse ()), StandardCharsets .UTF_8 ), 500 );
104+ mockedHttpRequestClient .setMockResponse (mockResponse );
105+
106+ oAuth2PlatformClient .retrieveBearerTokens (AUTH_CODE , REDIRECT_URI );
107+ }
108+
109+ @ Test
110+ public void canRefreshTokenTest () throws Exception {
111+ ObjectWriter writer = mapper .writerFor (BearerTokenResponse .class );
112+
113+ BearerTokenResponse mockBTResponse = new BearerTokenResponse ();
114+ mockBTResponse .setAccessToken ("access-token" );
115+ mockBTResponse .setRefreshToken ("refresh-token" );
116+ mockBTResponse .setExpiresIn (200L );
117+ mockBTResponse .setTokenType ("bearer-token" );
118+ Response mockResponse = new Response (
119+ IOUtils .toInputStream (writer .writeValueAsString (mockBTResponse ), StandardCharsets .UTF_8 ), 200 );
120+ mockedHttpRequestClient .setMockResponse (mockResponse );
121+
122+ BearerTokenResponse bearerTokenResponse = oAuth2PlatformClient .refreshToken ("test-token" );
123+
124+ assertNotNull (mockedHttpRequestClient .getServiceRequestReceived ().getAuthString ());
125+ assertTrue (mockedHttpRequestClient .getServiceRequestReceived ().getPostParams ()
126+ .contains (new BasicNameValuePair ("refresh_token" , "test-token" )));
127+ assertEquals (mockBTResponse .getAccessToken (), bearerTokenResponse .getAccessToken ());
128+ assertEquals (mockBTResponse .getRefreshToken (), bearerTokenResponse .getRefreshToken ());
129+ assertEquals (mockBTResponse .getExpiresIn (), bearerTokenResponse .getExpiresIn ());
130+ assertEquals (mockBTResponse .getTokenType (), bearerTokenResponse .getTokenType ());
131+ }
132+
133+ @ Test (expectedExceptions = OAuthException .class )
134+ public void refreshTokenThrowsOAuthExceptionOnErrorStatus () throws JsonProcessingException , OAuthException {
135+ ObjectWriter writer = mapper .writerFor (BearerTokenResponse .class );
136+ Response mockResponse = new Response (
137+ IOUtils .toInputStream (writer .writeValueAsString (new BearerTokenResponse ()), StandardCharsets .UTF_8 ), 500 );
138+ mockedHttpRequestClient .setMockResponse (mockResponse );
139+
140+ oAuth2PlatformClient .retrieveBearerTokens (AUTH_CODE , REDIRECT_URI );
141+ }
142+
143+ @ Test
144+ public void canRevokeTokenTest () throws Exception {
145+
146+ Response mockResponse = new Response (null , 200 );
147+ mockedHttpRequestClient .setMockResponse (mockResponse );
148+
149+ PlatformResponse platformResponse = oAuth2PlatformClient .revokeToken ("revoke-token" );
150+
151+ assertNotNull (mockedHttpRequestClient .getServiceRequestReceived ().getAuthString ());
152+ assertTrue (mockedHttpRequestClient .getServiceRequestReceived ().getPostParams ()
153+ .contains (new BasicNameValuePair ("token" , "revoke-token" )));
154+ assertEquals (platformResponse .getStatus (), "SUCCESS" );
155+ }
156+
157+ @ Test
158+ public void returnsErrorStatusOnRevokeTokenFailureTest () throws Exception {
159+
160+ Response mockResponse = new Response (null , 500 );
161+ mockedHttpRequestClient .setMockResponse (mockResponse );
162+
163+ PlatformResponse platformResponse = oAuth2PlatformClient .revokeToken ("revoke-token" );
164+
165+ assertNotNull (mockedHttpRequestClient .getServiceRequestReceived ().getAuthString ());
166+ assertTrue (mockedHttpRequestClient .getServiceRequestReceived ().getPostParams ()
167+ .contains (new BasicNameValuePair ("token" , "revoke-token" )));
168+ assertEquals (platformResponse .getStatus (), "ERROR" );
169+ assertEquals (platformResponse .getErrorMessage (), "Failed to revoke token" );
170+ }
171+
172+ @ Test
173+ public void canGetUserInfoTest () throws Exception {
174+
175+ ObjectWriter writer = mapper .writerFor (UserInfoResponse .class );
176+
177+ UserInfoResponse mockUserInfoResponse = new UserInfoResponse ();
178+ mockUserInfoResponse .setEmail ("abc@xyz.com" );
179+
180+ Response mockResponse = new Response (
181+ IOUtils .toInputStream (writer .writeValueAsString (mockUserInfoResponse ), StandardCharsets .UTF_8 ), 200 );
182+ mockedHttpRequestClient .setMockResponse (mockResponse );
183+
184+ UserInfoResponse userInfoResponse = oAuth2PlatformClient .getUserInfo ("test-token" );
185+
186+ assertNotNull (mockedHttpRequestClient .getServiceRequestReceived ().getAuthString ());
187+ assertNull (mockedHttpRequestClient .getServiceRequestReceived ().getPostParams ());
188+ assertEquals (userInfoResponse .getEmail (), mockUserInfoResponse .getEmail ());
189+ }
190+
191+ @ Test (expectedExceptions = OpenIdException .class )
192+ public void getUserInfoThrowsOpenIdExceptionOnErrorStatus () throws JsonProcessingException , OpenIdException {
193+ ObjectWriter writer = mapper .writerFor (UserInfoResponse .class );
194+ Response mockResponse = new Response (
195+ IOUtils .toInputStream (writer .writeValueAsString (new UserInfoResponse ()), StandardCharsets .UTF_8 ), 500 );
196+ mockedHttpRequestClient .setMockResponse (mockResponse );
197+
198+ oAuth2PlatformClient .getUserInfo ("test-token" );
199+ }
200+
201+ @ Test
202+ public void validateIDTokenReturnsFalseOnInvalidTokenTest () throws OpenIdException {
203+ String idToken = "eyJraWQiOiIxZTlnZGs3IiwiYWxnIjoiUlMyNTYifQ.ewogImlz\n "
204+ + "cyI6ICJodHRwOi8vc2VydmVyLmV4YW1wbGUuY29tIiwKICJzdWIiOiAiMjQ4\n "
205+ + "Mjg5NzYxMDAxIiwKICJhdWQiOiAiczZCaGRSa3F0MyIsCiAibm9uY2UiOiAi\n "
206+ + "bi0wUzZfV3pBMk1qIiwKICJleHAiOiAxMzExMjgxOTcwLAogImlhdCI6IDEz\n "
207+ + "MTEyODA5NzAsCiAibmFtZSI6ICJKYW5lIERvZSIsCiAiZ2l2ZW5fbmFtZSI6\n "
208+ + "ICJKYW5lIiwKICJmYW1pbHlfbmFtZSI6ICJEb2UiLAogImdlbmRlciI6ICJm\n "
209+ + "ZW1hbGUiLAogImJpcnRoZGF0ZSI6ICIwMDAwLTEwLTMxIiwKICJlbWFpbCI6\n "
210+ + "ICJqYW5lZG9lQGV4YW1wbGUuY29tIiwKICJwaWN0dXJlIjogImh0dHA6Ly9l\n "
211+ + "eGFtcGxlLmNvbS9qYW5lZG9lL21lLmpwZyIKfQ.rHQjEmBqn9Jre0OLykYNn\n "
212+ + "spA10Qql2rvx4FsD00jwlB0Sym4NzpgvPKsDjn_wMkHxcp6CilPcoKrWHcip\n "
213+ + "R2iAjzLvDNAReF97zoJqq880ZD1bwY82JDauCXELVR9O6_B0w3K-E7yM2mac\n "
214+ + "AAgNCUwtik6SjoSUZRcf-O5lygIyLENx882p6MtmwaL1hd6qn5RZOQ0TLrOY\n "
215+ + "u0532g9Exxcm-ChymrB4xLykpDj3lUivJt63eEGGN6DH5K6o33TcxkIjNrCD\n "
216+ + "4XB1CKKumZvCedgHHF3IAK4dVEDSUoGlH9z4pP_eWYNXvqQOjGs-rDaQzUHl\n "
217+ + "6cQQWNiDpWOl_lxXjQEvQ" ;
218+ assertFalse (oAuth2PlatformClient .validateIDToken (idToken ));
219+ }
220+
221+ private static final class MockedHttpRequestClient extends MockUp <HttpRequestClient > {
222+
223+ private Response mockResponse ;
224+ private Request serviceRequestReceived ; // Used for asserting the request that was received
225+
226+ void setMockResponse (Response mockResponse ) {
227+ this .mockResponse = mockResponse ;
228+ }
229+
230+ Request getServiceRequestReceived () {
231+ return serviceRequestReceived ;
232+ }
233+
234+ @ Mock
235+ public Response makeRequest (Request request ) throws InvalidRequestException {
236+ serviceRequestReceived = request ;
237+ return mockResponse ;
238+ }
239+ }
240+
241+ }
0 commit comments