diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/AuthorizationEnabledIntegrationTest.java b/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/AuthorizationEnabledIntegrationTest.java index de8d6de39..3aa3883f5 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/AuthorizationEnabledIntegrationTest.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/AuthorizationEnabledIntegrationTest.java @@ -1,5 +1,6 @@ package uk.gov.hmcts.reform.cwrdapi.util; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.tomakehurst.wiremock.common.FileSource; import com.github.tomakehurst.wiremock.extension.Parameters; @@ -34,6 +35,7 @@ import uk.gov.hmcts.reform.lib.util.serenity5.SerenityTest; import java.util.LinkedList; +import java.util.List; import java.util.UUID; import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; @@ -57,7 +59,7 @@ @SerenityTest @WithTags({@WithTag("testType:Integration")}) @TestPropertySource(properties = {"S2S_URL=http://127.0.0.1:8990", "IDAM_URL:http://127.0.0.1:5000", - "USER_PROFILE_URL:http://127.0.0.1:8091", "spring.config.location=classpath:application-test.yml"}) + "USER_PROFILE_URL:http://127.0.0.1:8091", "spring.config.location=classpath:application-test.yml"}) @ContextConfiguration(classes = {TestConfig.class, RestTemplateConfiguration.class}) public abstract class AuthorizationEnabledIntegrationTest extends SpringBootIntegrationTest { @@ -122,72 +124,71 @@ public void setUpClient() { public void setupIdamStubs() throws Exception { s2sService.stubFor(get(urlEqualTo("/details")) - .willReturn(aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withHeader("Connection", "close") - .withBody("rd_caseworker_ref_api"))); + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", "application/json") + .withHeader("Connection", "close") + .withBody("rd_caseworker_ref_api"))); + + + UserIdentifier userDetails = UserIdentifier.builder() + .id("%s") + .uid("%s") + .forename("Super") + .surname("User") + .email("super.user@hmcts.net") + .accountStatus("active") + .roles(List.of("%s")) + .build(); sidamService.stubFor(get(urlPathMatching("/o/userinfo")) - .willReturn(aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withHeader("Connection", "close") - .withBody("{" - + " \"id\": \"%s\"," - + " \"uid\": \"%s\"," - + " \"forename\": \"Super\"," - + " \"surname\": \"User\"," - + " \"email\": \"super.user@hmcts.net\"," - + " \"accountStatus\": \"active\"," - + " \"roles\": [" - + " \"%s\"" - + " ]" - + "}") - .withTransformers("user-token-response"))); + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", "application/json") + .withHeader("Connection", "close") + .withBody(WireMockUtil.getObjectMapper().writeValueAsString(userDetails)) + .withTransformers("user-token-response"))); mockHttpServerForOidc.stubFor(get(urlPathMatching("/jwks")) - .willReturn(aResponse() - .withStatus(200) - .withHeader("Content-Type", "application/json") - .withHeader("Connection", "close") - .withBody(getDynamicJwksResponse()))); + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", "application/json") + .withHeader("Connection", "close") + .withBody(getDynamicJwksResponse()))); } - public void userProfileGetUserWireMock(String idamStatus, String roles) { + public void userProfileGetUserWireMock(String idamStatus, String roles) throws JsonProcessingException { userProfileService.stubFor(get(urlPathMatching("/v1/userprofile.*")) - .willReturn(aResponse() - .withHeader("Content-Type", "application/json") - .withHeader("Connection", "close") - .withStatus(200) - .withBody("{" - + " \"userIdentifier\":\"" + UUID.randomUUID().toString() + "\"," - + " \"firstName\": \"prashanth\"," - + " \"lastName\": \"rao\"," - + " \"email\": \"super.user@hmcts.net\"," - + " \"idamStatus\": \"" + idamStatus + "\"," - + " \"roles\": " + roles - + "}"))); + .willReturn(aResponse() + .withHeader("Content-Type", "application/json") + .withHeader("Connection", "close") + .withStatus(200) + .withBody(WireMockUtil.getObjectMapper().writeValueAsString( + getUserIdentifierData(idamStatus, roles)))) + ); } - public void userProfileGetUserByIdWireMock(String idamId, Integer status) { + public void userProfileGetUserByIdWireMock(String idamId, Integer status) throws JsonProcessingException { userProfileService.stubFor(get(urlPathMatching("/v1/userprofile.*")) .willReturn(aResponse() .withHeader("Content-Type", "application/json") .withHeader("Connection", "close") .withStatus(status) - .withBody("{" - + " \"userIdentifier\":\"" + UUID.randomUUID().toString() + "\"," - + " \"firstName\": \"prashanth\"," - + " \"lastName\": \"rao\"," - + " \"email\": \"super.user@hmcts.net\"," - + " \"idamStatus\": \"pending\"," - + " \"roles\": [" - + " \"%s\"" - + " ]" - + "}"))); + .withBody(WireMockUtil.getObjectMapper().writeValueAsString( + getUserIdentifierData("pending", "%s")))) + ); } + private UserIdentifier getUserIdentifierData(String idamStatus, String roles) { + return UserIdentifier.builder() + .userIdentifier(UUID.randomUUID().toString()) + .firstName("prashanth") + .lastName("rao") + .email("super.user@hmcts.net") + .idamStatus(idamStatus) + .roles(List.of(roles)) + .build(); + } public void userProfileDeleteUserWireMock() { userProfileService.stubFor(delete(urlPathMatching("/v1/userprofile/users.*")) @@ -209,11 +210,11 @@ public void modifyUserRoles() throws Exception { userProfileRolesResponse.setRoleAdditionResponse(roleAdditionRes); userProfileService.stubFor(put(urlPathMatching("/v1/userprofile.*")) - .willReturn(aResponse() - .withHeader("Content-Type", "application/json") - .withHeader("Connection", "close") - .withStatus(201) - .withBody(objectMapper.writeValueAsString(userProfileRolesResponse)))); + .willReturn(aResponse() + .withHeader("Content-Type", "application/json") + .withHeader("Connection", "close") + .withStatus(201) + .withBody(objectMapper.writeValueAsString(userProfileRolesResponse)))); } public void modifyUserStatus(int idamStatus) throws Exception { @@ -225,11 +226,11 @@ public void modifyUserStatus(int idamStatus) throws Exception { userProfileRolesResponse.setAttributeResponse(attributeResponse); userProfileService.stubFor(put(urlPathMatching("/v1/userprofile.*")) - .willReturn(aResponse() - .withHeader("Content-Type", "application/json") - .withHeader("Connection", "close") - .withStatus(201) - .withBody(objectMapper.writeValueAsString(userProfileRolesResponse)))); + .willReturn(aResponse() + .withHeader("Content-Type", "application/json") + .withHeader("Connection", "close") + .withStatus(201) + .withBody(objectMapper.writeValueAsString(userProfileRolesResponse)))); } //removed UUID mock here and put in Test config,hence use this only for insert integration testing @@ -237,14 +238,14 @@ public void modifyUserStatus(int idamStatus) throws Exception { @BeforeEach public void userProfilePostUserWireMock() { userProfileService.stubFor(post(urlPathMatching("/v1/userprofile")) - .inScenario("") - .willReturn(aResponse() - .withHeader("Content-Type", "application/json") - .withHeader("Connection", "close") - .withStatus(201) - .withBody("{" - + " \"idamRegistrationResponse\":\"201\"" - + "}"))); + .inScenario("") + .willReturn(aResponse() + .withHeader("Content-Type", "application/json") + .withHeader("Connection", "close") + .withStatus(201) + .withBody("{" + + " \"idamRegistrationResponse\":\"201\"" + + "}"))); } @AfterEach @@ -258,13 +259,13 @@ public void cleanupTestData() { public void userProfileCreateUserWireMock(HttpStatus status) { userProfileService.stubFor(post(urlPathMatching("/v1/userprofile")) - .willReturn(aResponse() - .withHeader("Content-Type", "application/json") - .withHeader("Connection", "close") - .withStatus(status.value()) - .withBody("{" - + " \"idamRegistrationResponse\":\"" + status.value() + "\"" - + "}"))); + .willReturn(aResponse() + .withHeader("Content-Type", "application/json") + .withHeader("Connection", "close") + .withStatus(status.value()) + .withBody("{" + + " \"idamRegistrationResponse\":\"" + status.value() + "\"" + + "}"))); } public void userProfilePostUserWireMockForStaffProfile(HttpStatus status) { @@ -303,8 +304,8 @@ public Response transform(Request request, Response response, FileSource files, formatResponse = format(formatResponse, tokenInfo.get(1), tokenInfo.get(1), tokenInfo.get(0)); return Response.Builder.like(response) - .but().body(formatResponse) - .build(); + .but().body(formatResponse) + .build(); } @Override diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/IntegrationTestOidcSecurityConfig.java b/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/IntegrationTestOidcSecurityConfig.java index 27338a284..6921c14f6 100644 --- a/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/IntegrationTestOidcSecurityConfig.java +++ b/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/IntegrationTestOidcSecurityConfig.java @@ -12,6 +12,8 @@ import org.springframework.web.context.ContextCleanupListener; import uk.gov.hmcts.reform.cwrdapi.config.WireMockExtension; +import java.util.HashMap; + import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; @@ -46,16 +48,18 @@ private ClientRegistration clientRegistration() { public void setUpMockServiceForOidc() throws JsonProcessingException, JOSEException { + HashMap data = new HashMap<>(); + data.put("issuer","http://0.0.0.0:6000/o"); + data.put("jwks_uri","http://0.0.0.0:7000/jwks"); + mockHttpServerForOidc.stubFor(get(urlPathMatching("/o/.well-known/openid-configuration")) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "application/json") .withHeader("Connection", "close") .withBody( - " {" - + " \"issuer\": \"http://0.0.0.0:6000/o\"," - + " \"jwks_uri\": \"http://0.0.0.0:7000/jwks\" " - + "}"))); + WireMockUtil.getObjectMapper().writeValueAsString(data) + ))); if (!mockHttpServerForOidc.isRunning()) { mockHttpServerForOidc.start(); diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/UserIdentifier.java b/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/UserIdentifier.java new file mode 100644 index 000000000..2837ee222 --- /dev/null +++ b/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/UserIdentifier.java @@ -0,0 +1,31 @@ +package uk.gov.hmcts.reform.cwrdapi.util; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.EqualsAndHashCode; +import lombok.Getter; +import lombok.ToString; + +import java.util.List; + +@Getter +@Builder +@AllArgsConstructor +@EqualsAndHashCode +@ToString +@JsonIgnoreProperties(ignoreUnknown = true) +public class UserIdentifier { + private String id; + private String uid; + private String forename; + private String surname; + private String email; + private String accountStatus; + private List roles; + private String userIdentifier; + private String firstName; + private String lastName; + private String idamStatus; + +} \ No newline at end of file diff --git a/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/WireMockUtil.java b/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/WireMockUtil.java new file mode 100644 index 000000000..c89032e8a --- /dev/null +++ b/src/integrationTest/java/uk/gov/hmcts/reform/cwrdapi/util/WireMockUtil.java @@ -0,0 +1,18 @@ +package uk.gov.hmcts.reform.cwrdapi.util; + +import com.fasterxml.jackson.databind.DeserializationFeature; +import com.fasterxml.jackson.databind.ObjectMapper; + +public class WireMockUtil { + + private WireMockUtil() { + + } + + public static ObjectMapper getObjectMapper() { + ObjectMapper mapper = new ObjectMapper() + .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); + return mapper; + + } +} \ No newline at end of file