singhpk234 commented on code in PR #16023:
URL: https://github.com/apache/iceberg/pull/16023#discussion_r3147709394


##########
core/src/test/java/org/apache/iceberg/rest/auth/TestOAuth2Util.java:
##########
@@ -135,4 +136,79 @@ public void testCredentialFlowForSessionRefresh() throws 
IOException {
               any());
     }
   }
+
+  @Test
+  void refreshExpiredTokenShouldIncludeOptionalOAuthParams() throws 
IOException {
+    // expired token triggers refreshExpiredToken() which should forward 
optionalOAuthParams
+    String audience = "https://my-catalog.example.com";;
+    AuthConfig authConfig =
+        ImmutableAuthConfig.builder()
+            .keepRefreshed(true)
+            .exchangeEnabled(false)
+            .token("expired_token")
+            .credential("testClientId:testClientSecret")
+            .oauth2ServerUri("/v1/token")
+            .expiresAtMillis(System.currentTimeMillis() - 10_000)
+            .optionalOAuthParams(ImmutableMap.of("audience", audience, 
"scope", "catalog"))
+            .build();
+
+    OAuthTokenResponse response =
+        
OAuthTokenResponse.builder().withToken("refreshed_token").withTokenType(BEARER).build();
+
+    try (RESTClient client = Mockito.mock(RESTClient.class);
+        OAuth2Util.AuthSession session = new OAuth2Util.AuthSession(Map.of(), 
authConfig)) {
+      Mockito.when(client.postForm(any(), anyMap(), any(), anyMap(), 
any())).thenReturn(response);
+
+      session.refresh(client);
+
+      Mockito.verify(client)
+          .postForm(
+              any(),
+              argThat(
+                  formData ->
+                      CLIENT_CREDENTIALS.equals(formData.get(GRANT_TYPE))
+                          && audience.equals(formData.get("audience"))
+                          && "catalog".equals(formData.get("scope"))),
+              Mockito.eq(OAuthTokenResponse.class),
+              anyMap(),
+              any());
+    }
+  }
+
+  @Test
+  void refreshCurrentTokenNonExchangeShouldIncludeOptionalOAuthParams() throws 
IOException {
+    String audience = "https://my-catalog.example.com";;
+    AuthConfig authConfig =
+        ImmutableAuthConfig.builder()
+            .keepRefreshed(true)
+            .exchangeEnabled(false)
+            .token("valid_token")
+            .credential("testClientId:testClientSecret")
+            .oauth2ServerUri("/v1/token")
+            .expiresAtMillis(System.currentTimeMillis() + 300_000)
+            .optionalOAuthParams(ImmutableMap.of("audience", audience, 
"scope", "catalog"))
+            .build();
+
+    OAuthTokenResponse response =
+        
OAuthTokenResponse.builder().withToken("refreshed_token").withTokenType(BEARER).build();
+
+    try (RESTClient client = Mockito.mock(RESTClient.class);
+        OAuth2Util.AuthSession session = new OAuth2Util.AuthSession(Map.of(), 
authConfig)) {
+      Mockito.when(client.postForm(any(), anyMap(), any(), anyMap(), 
any())).thenReturn(response);
+
+      session.refresh(client);
+
+      Mockito.verify(client)
+          .postForm(
+              any(),
+              argThat(
+                  formData ->
+                      CLIENT_CREDENTIALS.equals(formData.get(GRANT_TYPE))
+                          && audience.equals(formData.get("audience"))
+                          && "catalog".equals(formData.get("scope"))),
+              Mockito.eq(OAuthTokenResponse.class),
+              anyMap(),
+              any());
+    }

Review Comment:
   can we reuse this path across test ?



##########
core/src/test/java/org/apache/iceberg/rest/auth/TestOAuth2Util.java:
##########
@@ -135,4 +136,79 @@ public void testCredentialFlowForSessionRefresh() throws 
IOException {
               any());
     }
   }
+
+  @Test
+  void refreshExpiredTokenShouldIncludeOptionalOAuthParams() throws 
IOException {
+    // expired token triggers refreshExpiredToken() which should forward 
optionalOAuthParams
+    String audience = "https://my-catalog.example.com";;
+    AuthConfig authConfig =
+        ImmutableAuthConfig.builder()
+            .keepRefreshed(true)
+            .exchangeEnabled(false)
+            .token("expired_token")
+            .credential("testClientId:testClientSecret")
+            .oauth2ServerUri("/v1/token")
+            .expiresAtMillis(System.currentTimeMillis() - 10_000)
+            .optionalOAuthParams(ImmutableMap.of("audience", audience, 
"scope", "catalog"))
+            .build();
+
+    OAuthTokenResponse response =
+        
OAuthTokenResponse.builder().withToken("refreshed_token").withTokenType(BEARER).build();
+
+    try (RESTClient client = Mockito.mock(RESTClient.class);
+        OAuth2Util.AuthSession session = new OAuth2Util.AuthSession(Map.of(), 
authConfig)) {
+      Mockito.when(client.postForm(any(), anyMap(), any(), anyMap(), 
any())).thenReturn(response);
+
+      session.refresh(client);
+
+      Mockito.verify(client)
+          .postForm(
+              any(),
+              argThat(
+                  formData ->
+                      CLIENT_CREDENTIALS.equals(formData.get(GRANT_TYPE))
+                          && audience.equals(formData.get("audience"))
+                          && "catalog".equals(formData.get("scope"))),
+              Mockito.eq(OAuthTokenResponse.class),
+              anyMap(),
+              any());
+    }
+  }
+
+  @Test
+  void refreshCurrentTokenNonExchangeShouldIncludeOptionalOAuthParams() throws 
IOException {

Review Comment:
   can you please elaborate more about this scenario ?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to