Skip to content

Commit

Permalink
RDCC-5489 - Staff : configure domain as list (#579)
Browse files Browse the repository at this point in the history
* RDCC-5489 - Staff : configure domain as list

* RDCC-5489 - Staff : configure domain as list - functional test cases

* RDCC-5489 - Staff : configure domain as list - suppress cve fix

* RDCC-5489 - Staff : configure domain as list - build fixes

* RDCC-5489 - Staff : configure domain as list - sonar build fixes

* RDCC-5489 - Staff : configure domain as list - sonar build fixes

* RDCC-5489 - Staff : configure domain as list - sonar build fixes

* RDCC-5489 - Staff : configure domain as list - sonar build fixes

* RDCC-5489 - Staff : configure domain as list - sonar build fixes

* RDCC-5489 - Staff : configure domain as list - flux changes

* RDCC-5489 - Staff : configure domain as list - functional test

* RDCC-5489 - Staff : configure domain as list - removed unwanted functional test and logs

* RDCC-5489 - Staff : configure domain as list - removed unwanted functional test

* RDCC-5489 - Staff : configure domain as list - build issues and test cases

* RDCC-5489 - Staff : configure domain as list -parameterized test cases solved
  • Loading branch information
vilasshelke-hmcts authored Oct 5, 2022
1 parent 4e5aed6 commit d5f828f
Show file tree
Hide file tree
Showing 14 changed files with 244 additions and 57 deletions.
5 changes: 2 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -384,9 +384,8 @@ dependencies {

implementation group: 'com.google.guava', name: 'guava', version: '31.1-jre'
implementation group: 'javax.el', name: 'javax.el-api', version: '3.0.0'
implementation group: 'com.launchdarkly', name: 'launchdarkly-java-server-sdk', version: versions.launchDarklySdk
implementation group:"org.yaml", name: "snakeyaml", version:"1.31"

implementation group: 'org.yaml', name: 'snakeyaml', version: '1.33'
implementation group: 'com.launchdarkly', name: 'launchdarkly-java-server-sdk', version: '5.10.2'

implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: versions.log4j
implementation group: 'org.apache.logging.log4j', name: 'log4j', version: versions.log4j
Expand Down
1 change: 1 addition & 0 deletions charts/rd-caseworker-ref-api/values.preview.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ java:
CWRD_DATA_PER_MESSAGE: 50
LAUNCH_DARKLY_ENV: "preview"
ENVIRONMENT_NAME: "preview"
EMAIL_DOMAIN_LIST: "justice.gov.uk,DWP.GOV.UK,hmrc.gov.uk"
postgresql:
enabled: true
auth:
Expand Down
8 changes: 0 additions & 8 deletions config/owasp/suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,4 @@
]]></notes>
<cve>CVE-2022-34305</cve>
</suppress>
<suppress>
<notes>Suppressed due to unavailability of updated version of launchdarkly-java-server-sdk</notes>
<packageUrl regex="true">^pkg:maven/org\.yaml/snakeyaml@.*$</packageUrl>
<cve>CVE-2022-25857</cve>
<cve>CVE-2022-38749</cve>
<cve>CVE-2022-38750</cve>
<cve>CVE-2022-38751</cve>
</suppress>
</suppressions>
Original file line number Diff line number Diff line change
Expand Up @@ -92,34 +92,34 @@ public void createCwProfileWhenUserNotExistsInCrdAndSidamAndUp_Ac1() {
roleRequests.add(new CaseWorkerRoleRequest("National Business Centre Team Leader",true));
roleRequests.add(new CaseWorkerRoleRequest("Regional Centre Team Leader",false));
List<CaseWorkersProfileCreationRequest> caseWorkersProfileCreationRequests = caseWorkerApiClient
.createCaseWorkerProfiles();
.createCaseWorkerProfiles();
caseWorkersProfileCreationRequests.get(0).setRoles(roleRequests);
Response response = caseWorkerApiClient.createUserProfiles(caseWorkersProfileCreationRequests);

CaseWorkerProfileCreationResponse caseWorkerProfileCreationResponse =
response.getBody().as(CaseWorkerProfileCreationResponse.class);
List<String> caseWorkerIds = caseWorkerProfileCreationResponse.getCaseWorkerIds();
Response fetchResponse = caseWorkerApiClient.getMultipleAuthHeadersInternal(ROLE_CWD_SYSTEM_USER)
.body(UserRequest.builder().userIds(caseWorkerIds).build())
.post("/refdata/case-worker/users/fetchUsersById/")
.andReturn();
.body(UserRequest.builder().userIds(caseWorkerIds).build())
.post("/refdata/case-worker/users/fetchUsersById/")
.andReturn();
fetchResponse.then()
.assertThat()
.statusCode(200);
.assertThat()
.statusCode(200);

List<uk.gov.hmcts.reform.cwrdapi.client.domain.CaseWorkerProfile> fetchedList =
Arrays.asList(fetchResponse.getBody().as(
uk.gov.hmcts.reform.cwrdapi.client.domain.CaseWorkerProfile[].class));
Arrays.asList(fetchResponse.getBody().as(
uk.gov.hmcts.reform.cwrdapi.client.domain.CaseWorkerProfile[].class));
assertEquals(1, fetchedList.size());
assertEquals("Regional Centre Team Leader", fetchedList.get(0).getRoles().get(1).getRoleName());
List<String> workAreas = fetchedList.stream().flatMap(fw -> fw.getWorkAreas().stream().map(
WorkArea::getAreaOfWork)).collect(
Collectors.toList());
WorkArea::getAreaOfWork)).collect(
Collectors.toList());
assertTrue(workAreas.contains(caseWorkersProfileCreationRequests.get(0)
.getWorkerWorkAreaRequests().get(0).getAreaOfWork()));
.getWorkerWorkAreaRequests().get(0).getAreaOfWork()));
caseWorkersProfileCreationRequests.get(0)
.getWorkerWorkAreaRequests().forEach(workerWorkAreaRequest ->
assertTrue(workAreas.contains(workerWorkAreaRequest.getAreaOfWork())));
.getWorkerWorkAreaRequests().forEach(workerWorkAreaRequest ->
assertTrue(workAreas.contains(workerWorkAreaRequest.getAreaOfWork())));
assertEquals(fetchedList.get(0).getFirstName(), caseWorkersProfileCreationRequests.get(0).getFirstName());
assertEquals(caseWorkersProfileCreationRequests.size(), caseWorkerIds.size());
}
Expand Down Expand Up @@ -169,7 +169,7 @@ public void updateCwWhenNamesMismatchExistsInCwrAndUpAndSidam_Ac3() {
var profileCreateRequests = createNewActiveCaseWorkerProfile();
var upResponse = getUserProfileFromUp(profileCreateRequests.get(0).getEmailId());
var caseWorkersProfileCreationRequests = caseWorkerApiClient
.createCaseWorkerProfiles(upResponse.getEmail());
.createCaseWorkerProfiles(upResponse.getEmail());
caseWorkersProfileCreationRequests.get(0).setFirstName("cwr-test-one");
caseWorkerApiClient.createUserProfiles(caseWorkersProfileCreationRequests);
var idamResponse = getIdamResponse(upResponse.getIdamId());
Expand Down Expand Up @@ -298,7 +298,7 @@ public void shouldUploadXlsxFileSuccessfully() throws IOException {
CaseWorkerFileCreationResponse caseWorkerFileCreationResponse = uploadCaseWorkerFileResponse
.as(CaseWorkerFileCreationResponse.class);
assertTrue(caseWorkerFileCreationResponse.getMessage().contains(REQUEST_COMPLETED_SUCCESSFULLY));
assertTrue(caseWorkerFileCreationResponse.getDetailedMessage().contains(format(RECORDS_UPLOADED, 4)));
assertTrue(caseWorkerFileCreationResponse.getDetailedMessage().contains(format(RECORDS_UPLOADED, 6)));
}

@Test
Expand Down Expand Up @@ -542,26 +542,26 @@ public void shouldFetchStaffProfileByCcdServiceNamesInDesc() {
List<CaseWorkersProfileCreationRequest> caseWorkersProfileCreationRequests = new ArrayList<>();

caseWorkersProfileCreationRequests.addAll(caseWorkerApiClient
.createCaseWorkerProfiles());
.createCaseWorkerProfiles());
caseWorkersProfileCreationRequests.addAll(caseWorkerApiClient
.createCaseWorkerProfiles());
.createCaseWorkerProfiles());
caseWorkerApiClient.createUserProfiles(caseWorkersProfileCreationRequests);
}
String ccdServiceNames = "all";
Response fetchResponse = caseWorkerApiClient.getMultipleAuthHeadersWithoutContentType(ROLE_CWD_SYSTEM_USER)
.get(STAFF_BY_SERVICE_NAME_URL
+ "?ccd_service_names=" + ccdServiceNames
+ "&page_number=0&page_size=40&sort_column=caseWorkerId&sort_direction=DESC")
.andReturn();
.get(STAFF_BY_SERVICE_NAME_URL
+ "?ccd_service_names=" + ccdServiceNames
+ "&page_number=0&page_size=40&sort_column=caseWorkerId&sort_direction=DESC")
.andReturn();
fetchResponse.then()
.assertThat()
.statusCode(200);
.assertThat()
.statusCode(200);
List<StaffProfileWithServiceName> paginatedStaffProfile =
Arrays.asList(fetchResponse.getBody().as(StaffProfileWithServiceName[].class));
Arrays.asList(fetchResponse.getBody().as(StaffProfileWithServiceName[].class));

List<String> caseWorkerIds = paginatedStaffProfile.stream()
.map(ps -> ps.getStaffProfile().getId())
.distinct().collect(Collectors.toList());
.map(ps -> ps.getStaffProfile().getId())
.distinct().collect(Collectors.toList());
assertTrue(Ordering.natural().reverse().isOrdered(caseWorkerIds));

}
Expand All @@ -575,33 +575,33 @@ public void shouldThrowRecordNotFoundErrorOnInvalidCcdServiceName() {
List<CaseWorkersProfileCreationRequest> caseWorkersProfileCreationRequests = new ArrayList<>();

CaseWorkerWorkAreaRequest workerWorkAreaRequest1 = CaseWorkerWorkAreaRequest
.caseWorkerWorkAreaRequest()
.serviceCode("ABA4").areaOfWork("Adoption").build();
.caseWorkerWorkAreaRequest()
.serviceCode("ABA4").areaOfWork("Adoption").build();

CaseWorkerWorkAreaRequest workerWorkAreaRequest2 = CaseWorkerWorkAreaRequest
.caseWorkerWorkAreaRequest()
.serviceCode("BHA3").areaOfWork("Specified Money Claims").build();
.caseWorkerWorkAreaRequest()
.serviceCode("BHA3").areaOfWork("Specified Money Claims").build();

List<CaseWorkerWorkAreaRequest> areaRequests =
ImmutableList.of(workerWorkAreaRequest1, workerWorkAreaRequest2);
ImmutableList.of(workerWorkAreaRequest1, workerWorkAreaRequest2);

caseWorkersProfileCreationRequests.addAll(caseWorkerApiClient
.createCaseWorkerProfiles());
.createCaseWorkerProfiles());
caseWorkersProfileCreationRequests.get(0).setWorkerWorkAreaRequests(areaRequests);
caseWorkersProfileCreationRequests.addAll(caseWorkerApiClient
.createCaseWorkerProfiles());
.createCaseWorkerProfiles());
caseWorkersProfileCreationRequests.get(1).setWorkerWorkAreaRequests(areaRequests);
caseWorkerApiClient.createUserProfiles(caseWorkersProfileCreationRequests);
}
String ccdServiceNames = "Invalid";
Response fetchResponse = caseWorkerApiClient.getMultipleAuthHeadersWithoutContentType(ROLE_CWD_SYSTEM_USER)
.get(STAFF_BY_SERVICE_NAME_URL
+ "?ccd_service_names=" + ccdServiceNames
+ "&page_number=1&page_size=100&sort_column=caseWorkerId&sort_direction=DESC")
.andReturn();
.get(STAFF_BY_SERVICE_NAME_URL
+ "?ccd_service_names=" + ccdServiceNames
+ "&page_number=1&page_size=100&sort_column=caseWorkerId&sort_direction=DESC")
.andReturn();
fetchResponse.then()
.assertThat()
.statusCode(404);
.assertThat()
.statusCode(404);


}
Expand Down
Binary file modified src/functionalTest/resources/Staff Data Upload.xlsx
Binary file not shown.
6 changes: 5 additions & 1 deletion src/integrationTest/resources/application-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,8 @@ excel:

refresh:
pageSize: 20
sortColumn: caseWorkerId
sortColumn: caseWorkerId

### Email domain list
email:
domainList: justice.gov.uk,DWP.GOV.UK,hmrc.gov.uk
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import uk.gov.hmcts.reform.cwrdapi.util.CaseWorkerConstants;
import uk.gov.hmcts.reform.cwrdapi.util.MappingField;
import uk.gov.hmcts.reform.cwrdapi.util.ValidateCaseWorkerChildren;
import uk.gov.hmcts.reform.cwrdapi.util.ValidateEmail;

import java.io.Serializable;
import java.time.LocalDateTime;
Expand Down Expand Up @@ -49,10 +50,7 @@ public class CaseWorkerProfile extends CaseWorkerDomain implements Serializable
private String lastName;

@MappingField(columnName = "Email", position = 1)
@Pattern(regexp = CaseWorkerConstants.USER_NAME_PATTERN + "@"
+ CaseWorkerConstants.DOMAIN_JUSTICE_GOV_UK,
message = CaseWorkerConstants.INVALID_EMAIL,
flags = Pattern.Flag.CASE_INSENSITIVE)
@ValidateEmail(message = CaseWorkerConstants.INVALID_EMAIL)
@NotEmpty(message = CaseWorkerConstants.INVALID_EMAIL)
@JsonProperty("email_id")
private String officialEmail;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package uk.gov.hmcts.reform.cwrdapi.config;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Locale;
import javax.annotation.PostConstruct;

@Slf4j
@NoArgsConstructor
@Setter
@Getter
@Configuration
public class EmailDomainPropertyInitiator {
public static String emailDomains;
@Value("${email.domainList}")
private String tempEmailDomains;

private static void setStaticEmailList(String emailDomains) {
EmailDomainPropertyInitiator.emailDomains = emailDomains.toLowerCase(Locale.ENGLISH);
}

@Bean
@PostConstruct
public void getPropertySupportBean() {
if (ObjectUtils.isNotEmpty(this.tempEmailDomains)) {
setStaticEmailList(this.tempEmailDomains.toLowerCase());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ private CaseWorkerConstants() {
public static final String DOMAIN_JUSTICE_GOV_UK = "justice.gov.uk";
public static final String USER_NAME_PATTERN = "^[A-Za-z0-9]+[\\w!#$%&'’.*+/=?`{|}~^-]+"
+ "(?:\\.[\\w!#$%&’*+/=?`{|}~^-]+)*";
public static final String INVALID_EMAIL = "You must add a valid justice.gov.uk email"
+ " address and upload the file again";
public static final String INVALID_EMAIL = "You must add a valid email address";

public static final String FILE_NO_DATA_ERROR_MESSAGE = "There is no data in the file uploaded."
+ " Upload a valid file in xlsx or xls format";
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/uk/gov/hmcts/reform/cwrdapi/util/EmailValidator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package uk.gov.hmcts.reform.cwrdapi.util;

import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import uk.gov.hmcts.reform.cwrdapi.config.EmailDomainPropertyInitiator;

import java.util.List;
import java.util.regex.Pattern;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

@Slf4j
@NoArgsConstructor
/*
EmailValidator IS EMAIL VALIDATION. THIS ALSO ALLOWED ONLY DOMAINS MENTIONED IN APPLICATION.YAML FILE
@copyrights : hmcts
*/
public class EmailValidator implements ConstraintValidator<ValidateEmail, String> {

private String emailDomainList;

@Override
public void initialize(ValidateEmail validateEmail) {
this.emailDomainList = EmailDomainPropertyInitiator.emailDomains;
}

@Override
public boolean isValid(String emailAddress, ConstraintValidatorContext constraintValidatorContext) {
if (ObjectUtils.isEmpty(emailAddress) || ObjectUtils.isEmpty(this.emailDomainList)) {
return false;
}
return this.emailPatternMatches(emailAddress.toLowerCase());
}

private boolean emailPatternMatches(String emailAddress) {
String emailDomainName = this.getDomainName(emailAddress);
if (ObjectUtils.isNotEmpty(emailDomainName) && this.isDomainValid(emailDomainName)) {
String regexPattern = CaseWorkerConstants.USER_NAME_PATTERN + "@"
+ emailDomainName;
return Pattern.compile(regexPattern, Pattern.CASE_INSENSITIVE)
.matcher(emailAddress)
.matches();
}
return false;
}

private boolean isDomainValid(String emailDomainName) {
List<String> domainList = List.of(this.emailDomainList.split(","));
return domainList.contains(emailDomainName);
}

private String getDomainName(String emailAddress) {
String[] split = emailAddress.split("@");
final Integer two = 2;
if (ObjectUtils.isNotEmpty(split) && split.length == two) {
return split[1];
}
return null;
}
}
26 changes: 26 additions & 0 deletions src/main/java/uk/gov/hmcts/reform/cwrdapi/util/ValidateEmail.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package uk.gov.hmcts.reform.cwrdapi.util;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import javax.validation.Constraint;
import javax.validation.Payload;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE_USE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

@Target({FIELD, PARAMETER, ANNOTATION_TYPE, TYPE_USE, METHOD})
@Retention(RUNTIME)
@Documented
@Constraint(validatedBy = EmailValidator.class)
public @interface ValidateEmail {
String message() default "";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};
}
4 changes: 4 additions & 0 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,7 @@ launchdarkly:
refresh:
pageSize: ${REFRESH_PAGE_SIZE:20}
sortColumn: ${REFRESH_SORT_COLUMN:caseWorkerId}

### Email domain list
email:
domainList: ${EMAIL_DOMAIN_LIST:justice.gov.uk,DWP.GOV.UK,hmrc.gov.uk}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.mockito.Spy;
import uk.gov.hmcts.reform.cwrdapi.client.domain.CaseWorkerDomain;
import uk.gov.hmcts.reform.cwrdapi.client.domain.CaseWorkerProfile;
import uk.gov.hmcts.reform.cwrdapi.config.EmailDomainPropertyInitiator;
import uk.gov.hmcts.reform.cwrdapi.service.impl.JsrValidatorInitializer;

import java.util.ArrayList;
Expand All @@ -27,6 +28,7 @@ class JsrValidatorInitializerTest {

@BeforeEach
void init() {
EmailDomainPropertyInitiator.emailDomains = "justice.gov.uk,DWP.GOV.UK,hmrc.gov.uk";
openMocks(this);
jsrValidatorInitializer.initializeFactory();
}
Expand Down
Loading

0 comments on commit d5f828f

Please sign in to comment.