tuteng commented on a change in pull request #11931: URL: https://github.com/apache/pulsar/pull/11931#discussion_r704312859
########## File path: pulsar-client/src/test/java/org/apache/pulsar/client/impl/auth/oauth2/protocol/TokenClientTest.java ########## @@ -0,0 +1,125 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.client.impl.auth.oauth2.protocol; + +import com.google.gson.Gson; +import org.apache.commons.lang3.StringUtils; +import org.asynchttpclient.BoundRequestBuilder; +import org.asynchttpclient.DefaultAsyncHttpClient; +import org.asynchttpclient.ListenableFuture; +import org.asynchttpclient.Response; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.net.URL; +import java.util.Map; +import java.util.TreeMap; +import java.util.concurrent.ExecutionException; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.testng.Assert.fail; + +/** + * Token client exchange token mock test. + */ +public class TokenClientTest { + + @Test + @SuppressWarnings("unchecked") + public void exchangeClientCredentialsSuccessByScopeTest() throws + IOException, TokenExchangeException, ExecutionException, InterruptedException { + DefaultAsyncHttpClient defaultAsyncHttpClient = mock(DefaultAsyncHttpClient.class); + URL url = new URL("http://localhost"); + TokenClient tokenClient = new TokenClient(url, defaultAsyncHttpClient); + Map<String, String> bodyMap = new TreeMap<>(); + ClientCredentialsExchangeRequest request = ClientCredentialsExchangeRequest.builder() + .audience("test-audience") + .clientId("test-client-id") + .clientSecret("test-client-secret") + .scope("test-scope") + .build(); + bodyMap.put("grant_type", "client_credentials"); + bodyMap.put("client_id", request.getClientId()); + bodyMap.put("client_secret", request.getClientSecret()); + bodyMap.put("audience", request.getAudience()); + if (!StringUtils.isBlank(request.getScope())) { + bodyMap.put("scope", request.getScope()); + } + String body = tokenClient.buildClientCredentialsBody(bodyMap); + BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class); + Response response = mock(Response.class); + ListenableFuture<Response> listenableFuture = mock(ListenableFuture.class); + when(defaultAsyncHttpClient.preparePost(url.toString())).thenReturn(boundRequestBuilder); + when(boundRequestBuilder.setHeader("Accept", "application/json")).thenReturn(boundRequestBuilder); + when(boundRequestBuilder.setHeader("Content-Type", "application/x-www-form-urlencoded")).thenReturn(boundRequestBuilder); + when(boundRequestBuilder.setBody(body)).thenReturn(boundRequestBuilder); + when(boundRequestBuilder.execute()).thenReturn(listenableFuture); + when(listenableFuture.get()).thenReturn(response); + when(response.getStatusCode()).thenReturn(200); + TokenResult tokenResult = new TokenResult(); + tokenResult.setAccessToken("test-access-token"); + tokenResult.setIdToken("test-id"); + when(response.getResponseBodyAsBytes()).thenReturn(new Gson().toJson(tokenResult).getBytes()); + TokenResult tr = tokenClient.exchangeClientCredentials(request); + Assert.assertNotNull(tr); + } + + @Test + @SuppressWarnings("unchecked") + public void exchangeClientCredentialsFailedByScopeTest() throws + IOException, TokenExchangeException, ExecutionException, InterruptedException { + DefaultAsyncHttpClient defaultAsyncHttpClient = mock(DefaultAsyncHttpClient.class); + URL url = new URL("http://localhost"); + TokenClient tokenClient = new TokenClient(url, defaultAsyncHttpClient); + Map<String, String> bodyMap = new TreeMap<>(); + ClientCredentialsExchangeRequest request = ClientCredentialsExchangeRequest.builder() + .audience("test-audience") + .clientId("test-client-id") + .clientSecret("test-client-secret") + .scope("test-scope") + .build(); + bodyMap.put("grant_type", "client_credentials"); + bodyMap.put("client_id", request.getClientId()); + bodyMap.put("client_secret", request.getClientSecret()); + bodyMap.put("audience", request.getAudience()); + String body = tokenClient.buildClientCredentialsBody(bodyMap); + BoundRequestBuilder boundRequestBuilder = mock(BoundRequestBuilder.class); + Response response = mock(Response.class); + ListenableFuture<Response> listenableFuture = mock(ListenableFuture.class); + when(defaultAsyncHttpClient.preparePost(url.toString())).thenReturn(boundRequestBuilder); + when(boundRequestBuilder.setHeader("Accept", "application/json")).thenReturn(boundRequestBuilder); + when(boundRequestBuilder.setHeader("Content-Type", "application/x-www-form-urlencoded")).thenReturn(boundRequestBuilder); + when(boundRequestBuilder.setBody(body)).thenReturn(boundRequestBuilder); Review comment: In the unit test, only this part of the logic in this function `exchangeClientCredentials` can be tested ``` Map<String, String> bodyMap = new TreeMap<>(); bodyMap.put("grant_type", "client_credentials"); bodyMap.put("client_id", req.getClientId()); bodyMap.put("client_secret", req.getClientSecret()); bodyMap.put("audience", req.getAudience()); if (!StringUtils.isBlank(req.getScope())) { bodyMap.put("scope", req.getScope()); } String body = buildClientCredentialsBody(bodyMap); ``` because it has no way to actually send an http request to a server, all http-related operations are mocked, So there is the following mock call: ``` when(boundRequestBuilder.setBody(body)).thenReturn(boundRequestBuilder); ``` Note that there is a body that is mocked here, and the test will pass when it has the same value as the body in the following function, otherwise an exception will be thrown, the body in the following function is generated in the exchangeClientCredentials function: ``` Response res = httpClient.preparePost(tokenUrl.toString()) .setHeader("Accept", "application/json") .setHeader("Content-Type", "application/x-www-form-urlencoded") .setBody(body) .execute() .get(); ``` This is the current way to verify that the scope is encoded correctly @codelipenghui -- 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]
