|
14 | 14 | import org.mockito.junit.MockitoJUnitRunner; |
15 | 15 |
|
16 | 16 | import com.contentstack.cms.Contentstack; |
| 17 | +import com.contentstack.cms.core.Util; |
17 | 18 | import com.contentstack.cms.models.OAuthConfig; |
18 | 19 | import com.contentstack.cms.models.OAuthTokens; |
19 | 20 | import com.google.gson.Gson; |
@@ -195,6 +196,154 @@ public void testAuthUrlUniqueness() { |
195 | 196 | assertNotEquals("URLs should be different due to different PKCE challenges", url1, url2); |
196 | 197 | } |
197 | 198 |
|
| 199 | + @Test |
| 200 | + public void testDefaultOAuthEndpoints() { |
| 201 | + // Test with default hosts |
| 202 | + OAuthConfig config = OAuthConfig.builder() |
| 203 | + .appId(TEST_APP_ID) |
| 204 | + .clientId(TEST_CLIENT_ID) |
| 205 | + .redirectUri(TEST_REDIRECT_URI) |
| 206 | + .build(); |
| 207 | + OAuthHandler handler = new OAuthHandler(mockHttpClient, config); |
| 208 | + |
| 209 | + String authUrl = handler.authorize(); |
| 210 | + String tokenUrl = config.getTokenEndpoint(); |
| 211 | + |
| 212 | + assertTrue("Auth URL should use default app host", |
| 213 | + authUrl.contains(Util.OAUTH_APP_HOST)); |
| 214 | + assertTrue("Token URL should use default API host", |
| 215 | + tokenUrl.contains(Util.OAUTH_API_HOST)); |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + public void testHostTransformations() { |
| 220 | + // Test cases: {API Host, Expected App Host, Expected Token Host} |
| 221 | + String[][] testCases = { |
| 222 | + // Default region |
| 223 | + {"api.contentstack.io", "app.contentstack.com", "developerhub-api.contentstack.com"}, |
| 224 | + {"api-contentstack.com", "app-contentstack.com", "developerhub-api-contentstack.com"}, |
| 225 | + |
| 226 | + // AWS regions |
| 227 | + {"eu-api.contentstack.com", "eu-app.contentstack.com", "eu-developerhub-api.contentstack.com"}, |
| 228 | + {"eu-api-contentstack.com", "eu-app-contentstack.com", "eu-developerhub-api-contentstack.com"}, |
| 229 | + {"au-api.contentstack.com", "au-app.contentstack.com", "au-developerhub-api.contentstack.com"}, |
| 230 | + {"au-api-contentstack.com", "au-app-contentstack.com", "au-developerhub-api-contentstack.com"}, |
| 231 | + |
| 232 | + // Azure regions |
| 233 | + {"azure-na-api.contentstack.com", "azure-na-app.contentstack.com", "azure-na-developerhub-api.contentstack.com"}, |
| 234 | + {"azure-na-api-contentstack.com", "azure-na-app-contentstack.com", "azure-na-developerhub-api-contentstack.com"}, |
| 235 | + {"azure-eu-api.contentstack.com", "azure-eu-app.contentstack.com", "azure-eu-developerhub-api.contentstack.com"}, |
| 236 | + {"azure-eu-api-contentstack.com", "azure-eu-app-contentstack.com", "azure-eu-developerhub-api-contentstack.com"}, |
| 237 | + |
| 238 | + // GCP regions |
| 239 | + {"gcp-na-api.contentstack.com", "gcp-na-app.contentstack.com", "gcp-na-developerhub-api.contentstack.com"}, |
| 240 | + {"gcp-na-api-contentstack.com", "gcp-na-app-contentstack.com", "gcp-na-developerhub-api-contentstack.com"}, |
| 241 | + {"gcp-eu-api.contentstack.com", "gcp-eu-app.contentstack.com", "gcp-eu-developerhub-api.contentstack.com"}, |
| 242 | + {"gcp-eu-api-contentstack.com", "gcp-eu-app-contentstack.com", "gcp-eu-developerhub-api-contentstack.com"} |
| 243 | + }; |
| 244 | + |
| 245 | + for (String[] testCase : testCases) { |
| 246 | + String apiHost = testCase[0]; |
| 247 | + String expectedAppHost = testCase[1]; |
| 248 | + String expectedTokenHost = testCase[2]; |
| 249 | + |
| 250 | + OAuthConfig config = OAuthConfig.builder() |
| 251 | + .appId(TEST_APP_ID) |
| 252 | + .clientId(TEST_CLIENT_ID) |
| 253 | + .redirectUri(TEST_REDIRECT_URI) |
| 254 | + .host(apiHost) |
| 255 | + .build(); |
| 256 | + OAuthHandler handler = new OAuthHandler(mockHttpClient, config); |
| 257 | + |
| 258 | + String authUrl = handler.authorize(); |
| 259 | + String tokenUrl = config.getTokenEndpoint(); |
| 260 | + |
| 261 | + assertTrue(String.format("Auth URL for %s should contain %s", apiHost, expectedAppHost), |
| 262 | + authUrl.contains(expectedAppHost)); |
| 263 | + assertTrue(String.format("Token URL for %s should contain %s", apiHost, expectedTokenHost), |
| 264 | + tokenUrl.contains(expectedTokenHost)); |
| 265 | + } |
| 266 | + } |
| 267 | + |
| 268 | + @Test |
| 269 | + public void testHostStorage() { |
| 270 | + String testHost = "eu-api.contentstack.com"; |
| 271 | + |
| 272 | + // Test host storage in OAuthConfig |
| 273 | + OAuthConfig config = OAuthConfig.builder() |
| 274 | + .appId(TEST_APP_ID) |
| 275 | + .clientId(TEST_CLIENT_ID) |
| 276 | + .redirectUri(TEST_REDIRECT_URI) |
| 277 | + .host(testHost) |
| 278 | + .build(); |
| 279 | + |
| 280 | + assertEquals("Host should be stored in OAuthConfig", |
| 281 | + testHost, config.getHost()); |
| 282 | + |
| 283 | + // Test host storage via Contentstack.Builder |
| 284 | + Contentstack client = new Contentstack.Builder() |
| 285 | + .setOAuth(TEST_APP_ID, TEST_CLIENT_ID, TEST_CLIENT_SECRET, TEST_REDIRECT_URI, testHost) |
| 286 | + .build(); |
| 287 | + |
| 288 | + String authUrl = client.getOAuthAuthorizationUrl(); |
| 289 | + assertTrue("Auth URL should use stored host", |
| 290 | + authUrl.contains("eu-app.contentstack.com")); |
| 291 | + |
| 292 | + // Test host storage via PKCE builder |
| 293 | + client = new Contentstack.Builder() |
| 294 | + .setOAuthWithPKCE(TEST_APP_ID, TEST_CLIENT_ID, TEST_REDIRECT_URI, testHost) |
| 295 | + .build(); |
| 296 | + |
| 297 | + authUrl = client.getOAuthAuthorizationUrl(); |
| 298 | + assertTrue("Auth URL should use stored host with PKCE", |
| 299 | + authUrl.contains("eu-app.contentstack.com")); |
| 300 | + } |
| 301 | + |
| 302 | + @Test |
| 303 | + public void testDefaultHosts() { |
| 304 | + // Test with no host specified |
| 305 | + OAuthConfig config = OAuthConfig.builder() |
| 306 | + .appId(TEST_APP_ID) |
| 307 | + .clientId(TEST_CLIENT_ID) |
| 308 | + .redirectUri(TEST_REDIRECT_URI) |
| 309 | + .build(); |
| 310 | + OAuthHandler handler = new OAuthHandler(mockHttpClient, config); |
| 311 | + |
| 312 | + String authUrl = handler.authorize(); |
| 313 | + String tokenUrl = config.getTokenEndpoint(); |
| 314 | + |
| 315 | + assertTrue("Auth URL should use default app host", |
| 316 | + authUrl.contains(Util.OAUTH_APP_HOST)); |
| 317 | + assertTrue("Token URL should use default API host", |
| 318 | + tokenUrl.contains(Util.OAUTH_API_HOST)); |
| 319 | + } |
| 320 | + |
| 321 | + @Test |
| 322 | + public void testCustomEndpoints() { |
| 323 | + // Test with custom endpoints |
| 324 | + String customAuthEndpoint = "https://custom.auth.endpoint"; |
| 325 | + String customTokenEndpoint = "https://custom.token.endpoint"; |
| 326 | + |
| 327 | + OAuthConfig config = OAuthConfig.builder() |
| 328 | + .appId(TEST_APP_ID) |
| 329 | + .clientId(TEST_CLIENT_ID) |
| 330 | + .redirectUri(TEST_REDIRECT_URI) |
| 331 | + .authEndpoint(customAuthEndpoint) |
| 332 | + .tokenEndpoint(customTokenEndpoint) |
| 333 | + .build(); |
| 334 | + OAuthHandler handler = new OAuthHandler(mockHttpClient, config); |
| 335 | + |
| 336 | + String authUrl = handler.authorize(); |
| 337 | + String tokenUrl = config.getTokenEndpoint(); |
| 338 | + |
| 339 | + assertEquals("Should use custom auth endpoint", |
| 340 | + customAuthEndpoint, authUrl); |
| 341 | + assertEquals("Should use custom token endpoint", |
| 342 | + customTokenEndpoint, tokenUrl); |
| 343 | + } |
| 344 | + |
| 345 | + |
| 346 | + |
198 | 347 | // ================= |
199 | 348 | // TOKEN EXCHANGE TESTS |
200 | 349 | // ================= |
|
0 commit comments