https://linen.dev logo
#help
Title
# help
g

Guillaume GAYOT

07/17/2023, 8:50 AM
Hello ! I have several questions I couldn't find answer to in the documentation (Don't hesitate to point me to the doc if I miss something) : I need to stub an external call to a server that's protected by oauth2 client credentials using spring security. Basically, I have this configuration :
Copy code
lys.api-key=${LYS_API_KEY_FROM_DEVPORTAL}
lys.base-url =${LYS_BASE_URL:<https://ccdp-uat1.priv.nprd.api.devportal.adeo.cloud/backend-lys-loyalty-integration/v1>}

spring.security.oauth2.client.registration.lys-lmfr.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.lys-lmfr.client-id=${CLIENT_ID_LMFR_LYS_LOYALTY:0Rc89eed227070596f812f71c8005e9fd6}
spring.security.oauth2.client.registration.lys-lmfr.client-secret=${CLIENT_SECRET_LMFR_LYS_LOYALTY}
spring.security.oauth2.client.provider.lys-lmfr.token-uri=${LYS_TOKEN_URI:<https://idpb2e-rec.adeo.com/as/token.oauth2>}
spring.security.oauth2.client.provider.lys-lmfr.issuer-uri=${LYS_ISSUER_URI:<https://idpb2e-rec.adeo.com>}
and this code :
Copy code
private WebClient webClient(ServerOAuth2AuthorizedClientExchangeFilterFunction oauth, String buNumber) {
    return WebClient.builder()
            .filter(oauth)
            .baseUrl(lysProperties.getBaseUrl())
            .defaultHeader("X-Gateway-APIKey", lysProperties.getApiKey())
            .defaultHeader("x-adeo-bu-id", buNumber)
            .build();
}
@Bean
@Qualifier("lys-lmfr")
public AbstractLysProvider lmfrProvider() {
    InMemoryReactiveOAuth2AuthorizedClientService clientService = new InMemoryReactiveOAuth2AuthorizedClientService(clientRegistrations);
    AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager authorizedClientManager = new AuthorizedClientServiceReactiveOAuth2AuthorizedClientManager(clientRegistrations, clientService);
    ServerOAuth2AuthorizedClientExchangeFilterFunction oauth = new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
    oauth.setDefaultClientRegistrationId("lys-lmfr");
    return new AbstractLysProvider(webClient(oauth, "1"), mapper);
}
I stubed this call in a test like this
Copy code
stubFor(get(urlPathMatching("/loyalty-accounts?employeeUid=20010987"))
                .withHeader("x-adeo-bu-id", equalTo("1"))
        .willReturn(aResponse()
                .withStatus(200)
                .withBodyFile("lmfr-200.json")));

//act assert

List<LoyaltySubscriptionInfoDto> list = given().when()
        .get("/loyalty?ldap=20010987&buNumbers=1,14,2")
        .then()
        .statusCode(200)
        .extract()
        .body()
        .jsonPath()
        .getList(".", LoyaltySubscriptionInfoDto.class);
I can't make work for know : I keep getting errors from the oauth provider. I don't know how to unable it. Any idea ?
t

Tom

07/17/2023, 9:06 AM
Hi @Guillaume GAYOT you can’t use
urlPathMatching
with a query string as you’ve attempted here. Since you’re not using a regex in the URL I suggest changing this to
urlPathEqualTo("/loyalty-accounts")
and add
.withQueryParam("employeeUid", equalTo("20010987"))
to the chain.
🤌 1
g

Guillaume GAYOT

07/17/2023, 9:23 AM
Thanks ! But I'm still stuck on the same issue : the call return an error "invalid client credentials" from the oauth provider.
I found a way to stub the oauth provider response ! Thanks for the help 🙂
t

Tom

07/17/2023, 2:05 PM
Ah, great news! Glad you managed to figure it out.