From d4d3e6c3652949f8171c2cf356e560c37dffa3b6 Mon Sep 17 00:00:00 2001 From: "Chak Shing, Lo" Date: Thu, 5 Sep 2024 12:06:17 +0100 Subject: [PATCH 1/9] add cafcass system user --- Jenkinsfile_CNP | 8 +++-- .../CafcassSystemUpdateUserConfiguration.java | 18 ++++++++++ .../interceptors/CafcassApiInterceptor.java | 5 ++- .../cafcass/api/CafcassSystemUserService.java | 35 +++++++++++++++++++ .../src/main/resources/application-local.yaml | 3 ++ 5 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java create mode 100644 service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java diff --git a/Jenkinsfile_CNP b/Jenkinsfile_CNP index 94712bba04a..6026e57dc98 100644 --- a/Jenkinsfile_CNP +++ b/Jenkinsfile_CNP @@ -16,7 +16,9 @@ def serviceSecrets = [ secret('smoke-test-la-username', 'SMOKE_TEST_LA_USER_USERNAME'), secret('smoke-test-la-password', 'SMOKE_TEST_LA_USER_PASSWORD'), secret('system-update-user-username', 'SYSTEM_UPDATE_USER_USERNAME'), - secret('system-update-user-password', 'SYSTEM_UPDATE_USER_PASSWORD') + secret('system-update-user-password', 'SYSTEM_UPDATE_USER_PASSWORD'), + secret('cafcass-system-update-user-username', 'CAFCASS_SYSTEM_USER_USERNAME'), + secret('cafcass-system-update-user-password', 'CAFCASS_SYSTEM_USER_PASSWORD'), ] ] @@ -44,11 +46,11 @@ def otherSecrets = [ secret('idam-data-store-client-secret', 'IDAM_OAUTH2_DATA_STORE_CLIENT_SECRET'), secret('idam-data-store-system-user-username', 'IDAM_DATA_STORE_SYSTEM_USER_USERNAME'), secret('idam-data-store-system-user-password', 'IDAM_DATA_STORE_SYSTEM_USER_PASSWORD'), - secret('ccd-case-document-am-api-s2s-secret', 'CCD_CASE_DOCUMENT_AM_API_SECRET') + secret('ccd-case-document-am-api-s2s-secret', 'CCD_CASE_DOCUMENT_AM_API_SECRET'), ], 'fpl-${env}': [ secret('definition-importer-username', 'CCD_CONFIGURER_IMPORTER_USERNAME'), - secret('definition-importer-password', 'CCD_CONFIGURER_IMPORTER_PASSWORD') + secret('definition-importer-password', 'CCD_CONFIGURER_IMPORTER_PASSWORD'), ] ] diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java new file mode 100644 index 00000000000..2374945c84c --- /dev/null +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java @@ -0,0 +1,18 @@ +package uk.gov.hmcts.reform.fpl.config; + +import lombok.Data; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Configuration; + +@Data +@Configuration +public class CafcassSystemUpdateUserConfiguration { + private final String userName; + private final String password; + + public CafcassSystemUpdateUserConfiguration(@Value("${fpl.cafcass_system_update.username}") String userName, + @Value("${fpl.cafcass_system_update.password}") String password) { + this.userName = userName; + this.password = password; + } +} diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptor.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptor.java index f563f4e634b..3a2f3280a1c 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptor.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptor.java @@ -6,7 +6,9 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.servlet.HandlerInterceptor; +import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; import uk.gov.hmcts.reform.fpl.exceptions.api.AuthorizationException; +import uk.gov.hmcts.reform.fpl.service.cafcass.api.CafcassSystemUserService; import uk.gov.hmcts.reform.idam.client.IdamClient; import uk.gov.hmcts.reform.idam.client.models.UserInfo; @@ -22,13 +24,14 @@ @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class CafcassApiInterceptor implements HandlerInterceptor { private final ObjectProvider idamClient; + private final CafcassSystemUpdateUserConfiguration userConfig; public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String authToken = request.getHeader("Authorization"); if (isNotEmpty(authToken)) { UserInfo userInfo = Objects.requireNonNull(idamClient.getIfAvailable()).getUserInfo(authToken); - if (userInfo != null && userInfo.getRoles().contains(CAFCASS_SYSTEM_UPDATE.getRoleName())) { + if (userInfo != null && userInfo.getSub().equalsIgnoreCase(userConfig.getUserName())) { return true; } } diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java new file mode 100644 index 00000000000..c93c8ac6f4b --- /dev/null +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java @@ -0,0 +1,35 @@ +package uk.gov.hmcts.reform.fpl.service.cafcass.api; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.stereotype.Service; +import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; +import uk.gov.hmcts.reform.idam.client.IdamClient; + +import static uk.gov.hmcts.reform.fpl.config.CacheConfiguration.LOCAL_CACHE_MANAGER; +import static uk.gov.hmcts.reform.fpl.config.CacheConfiguration.SYS_USER_CACHE; + +@Slf4j +@Service +@EnableCaching +@RequiredArgsConstructor(onConstructor_ = {@Autowired}) +public class CafcassSystemUserService { + + private final CafcassSystemUpdateUserConfiguration userConfig; + private final IdamClient idamClient; + + @Cacheable(cacheManager = LOCAL_CACHE_MANAGER, cacheNames = SYS_USER_CACHE, unless = "#result == null", + key = "#root.target.SYS_USER_TOKEN_CACHE_KEY") + public String getSysUserToken() { + log.info("Requesting cafcass system-user token from IDAM"); + return idamClient.getAccessToken(userConfig.getUserName(), userConfig.getPassword()); + } + + public String getUserSub(String userToken) { + return idamClient.getUserInfo(userToken).getSub(); + } + +} diff --git a/service/src/main/resources/application-local.yaml b/service/src/main/resources/application-local.yaml index 2fa1153589c..7c6fd0b2c84 100644 --- a/service/src/main/resources/application-local.yaml +++ b/service/src/main/resources/application-local.yaml @@ -126,6 +126,9 @@ fpl: system_update: username: ${IDAM_DATA_STORE_SYSTEM_USER_USERNAME} password: ${IDAM_DATA_STORE_SYSTEM_USER_PASSWORD} + cafcass_system_update: + username: ${CAFCASS_SYSTEM_USER_USERNAME} + password: ${CAFCASS_SYSTEM_USER_PASSWORD} ctsc_inbox: 'FamilyPublicLaw+ctsc@gmail.com' ctsc_team_lead_inbox: 'FamilyPublicLaw+ctsc-team-lead@gmail.com' rcj_family_high_court_inbox: 'FamilyPublicLaw+rcjfamilyhighcourt@gmail.com' From e251ac8e0820ff95ec69eb8c4f9bf49013cbf876 Mon Sep 17 00:00:00 2001 From: "Chak Shing, Lo" Date: Thu, 5 Sep 2024 14:20:41 +0100 Subject: [PATCH 2/9] Update CafcassApiInterceptorTest.java --- .../fpl/interceptors/CafcassApiInterceptorTest.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/service/src/test/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptorTest.java b/service/src/test/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptorTest.java index 2b831c7e583..2fc0004d077 100644 --- a/service/src/test/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptorTest.java +++ b/service/src/test/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptorTest.java @@ -6,6 +6,7 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.beans.factory.ObjectProvider; +import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; import uk.gov.hmcts.reform.fpl.exceptions.api.AuthorizationException; import uk.gov.hmcts.reform.idam.client.IdamClient; import uk.gov.hmcts.reform.idam.client.models.UserInfo; @@ -24,13 +25,17 @@ public class CafcassApiInterceptorTest { private static final String AUTH_TOKEN_TEST = "bearerToken"; private static final UserInfo CAFCASS_SYSTEM_UPDATE_USER = - UserInfo.builder().roles(List.of(CAFCASS_SYSTEM_UPDATE.getRoleName())).build(); + UserInfo.builder().sub("cafcass-system-update-user@example.com") + .roles(List.of(CAFCASS_SYSTEM_UPDATE.getRoleName())).build(); private static final UserInfo LOCAL_AUTHORITY_UPDATE_USER = - UserInfo.builder().roles(List.of(LOCAL_AUTHORITY.getRoleName())).build(); + UserInfo.builder().sub("la-user@example.com") + .roles(List.of(LOCAL_AUTHORITY.getRoleName())).build(); @Mock private IdamClient idamClient; @Mock + private CafcassSystemUpdateUserConfiguration userConfig; + @Mock private ObjectProvider idamClientObjectProvider; @InjectMocks private CafcassApiInterceptor underTest; @@ -39,6 +44,7 @@ public class CafcassApiInterceptorTest { public void shouldReturnTrueIfCafcassSystemUpdateUser() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); when(request.getHeader("Authorization")).thenReturn(AUTH_TOKEN_TEST); + when(userConfig.getUserName()).thenReturn("cafcass-system-update-user@example.com"); when(idamClientObjectProvider.getIfAvailable()).thenReturn(idamClient); when(idamClient.getUserInfo(AUTH_TOKEN_TEST)).thenReturn(CAFCASS_SYSTEM_UPDATE_USER); @@ -49,6 +55,7 @@ public void shouldReturnTrueIfCafcassSystemUpdateUser() throws Exception { public void shouldThrowAuthExceptionIfNotCafcassSystemUpdateUser() throws Exception { HttpServletRequest request = mock(HttpServletRequest.class); when(request.getHeader("Authorization")).thenReturn(AUTH_TOKEN_TEST); + when(userConfig.getUserName()).thenReturn("cafcass-system-update-user@example.com"); when(idamClientObjectProvider.getIfAvailable()).thenReturn(idamClient); when(idamClient.getUserInfo(AUTH_TOKEN_TEST)).thenReturn(LOCAL_AUTHORITY_UPDATE_USER); From 3bf642d7a1ca06f268969256e9a27142193efcc6 Mon Sep 17 00:00:00 2001 From: "Chak Shing, Lo" Date: Thu, 5 Sep 2024 15:12:21 +0100 Subject: [PATCH 3/9] remove cafcass sys user pwd --- .../CafcassSystemUpdateUserConfiguration.java | 5 +-- .../cafcass/api/CafcassSystemUserService.java | 35 ------------------- .../src/main/resources/application-local.yaml | 1 - 3 files changed, 1 insertion(+), 40 deletions(-) delete mode 100644 service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java index 2374945c84c..c8a433ee336 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java @@ -8,11 +8,8 @@ @Configuration public class CafcassSystemUpdateUserConfiguration { private final String userName; - private final String password; - public CafcassSystemUpdateUserConfiguration(@Value("${fpl.cafcass_system_update.username}") String userName, - @Value("${fpl.cafcass_system_update.password}") String password) { + public CafcassSystemUpdateUserConfiguration(@Value("${fpl.cafcass_system_update.username}") String userName) { this.userName = userName; - this.password = password; } } diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java deleted file mode 100644 index c93c8ac6f4b..00000000000 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java +++ /dev/null @@ -1,35 +0,0 @@ -package uk.gov.hmcts.reform.fpl.service.cafcass.api; - -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.Cacheable; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.stereotype.Service; -import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; -import uk.gov.hmcts.reform.idam.client.IdamClient; - -import static uk.gov.hmcts.reform.fpl.config.CacheConfiguration.LOCAL_CACHE_MANAGER; -import static uk.gov.hmcts.reform.fpl.config.CacheConfiguration.SYS_USER_CACHE; - -@Slf4j -@Service -@EnableCaching -@RequiredArgsConstructor(onConstructor_ = {@Autowired}) -public class CafcassSystemUserService { - - private final CafcassSystemUpdateUserConfiguration userConfig; - private final IdamClient idamClient; - - @Cacheable(cacheManager = LOCAL_CACHE_MANAGER, cacheNames = SYS_USER_CACHE, unless = "#result == null", - key = "#root.target.SYS_USER_TOKEN_CACHE_KEY") - public String getSysUserToken() { - log.info("Requesting cafcass system-user token from IDAM"); - return idamClient.getAccessToken(userConfig.getUserName(), userConfig.getPassword()); - } - - public String getUserSub(String userToken) { - return idamClient.getUserInfo(userToken).getSub(); - } - -} diff --git a/service/src/main/resources/application-local.yaml b/service/src/main/resources/application-local.yaml index 7c6fd0b2c84..0b1f6c2f8fa 100644 --- a/service/src/main/resources/application-local.yaml +++ b/service/src/main/resources/application-local.yaml @@ -128,7 +128,6 @@ fpl: password: ${IDAM_DATA_STORE_SYSTEM_USER_PASSWORD} cafcass_system_update: username: ${CAFCASS_SYSTEM_USER_USERNAME} - password: ${CAFCASS_SYSTEM_USER_PASSWORD} ctsc_inbox: 'FamilyPublicLaw+ctsc@gmail.com' ctsc_team_lead_inbox: 'FamilyPublicLaw+ctsc-team-lead@gmail.com' rcj_family_high_court_inbox: 'FamilyPublicLaw+rcjfamilyhighcourt@gmail.com' From 30fd3c769841e88093e41620e949dfb4018f9eb7 Mon Sep 17 00:00:00 2001 From: "Chak Shing, Lo" Date: Thu, 5 Sep 2024 16:31:48 +0100 Subject: [PATCH 4/9] Update CafcassSystemUpdateUserConfiguration.java --- .../fpl/config/CafcassSystemUpdateUserConfiguration.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java index c8a433ee336..8b6c5c53e19 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java @@ -1,10 +1,11 @@ package uk.gov.hmcts.reform.fpl.config; import lombok.Data; +import lombok.Getter; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; -@Data +@Getter @Configuration public class CafcassSystemUpdateUserConfiguration { private final String userName; From bd072b1db9f7ad72f4b751c476a7e899aa2f841b Mon Sep 17 00:00:00 2001 From: "Chak Shing, Lo" Date: Thu, 5 Sep 2024 16:34:17 +0100 Subject: [PATCH 5/9] fix import --- .../reform/fpl/config/CafcassSystemUpdateUserConfiguration.java | 1 - .../hmcts/reform/fpl/interceptors/CafcassApiInterceptor.java | 2 -- 2 files changed, 3 deletions(-) diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java index 8b6c5c53e19..ba054762a08 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java @@ -1,6 +1,5 @@ package uk.gov.hmcts.reform.fpl.config; -import lombok.Data; import lombok.Getter; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptor.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptor.java index 3a2f3280a1c..8f1a1eed507 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptor.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/interceptors/CafcassApiInterceptor.java @@ -8,7 +8,6 @@ import org.springframework.web.servlet.HandlerInterceptor; import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; import uk.gov.hmcts.reform.fpl.exceptions.api.AuthorizationException; -import uk.gov.hmcts.reform.fpl.service.cafcass.api.CafcassSystemUserService; import uk.gov.hmcts.reform.idam.client.IdamClient; import uk.gov.hmcts.reform.idam.client.models.UserInfo; @@ -17,7 +16,6 @@ import javax.servlet.http.HttpServletResponse; import static org.apache.commons.lang3.ObjectUtils.isNotEmpty; -import static uk.gov.hmcts.reform.fpl.enums.UserRole.CAFCASS_SYSTEM_UPDATE; @Slf4j @Service From ba62ab12bad4eb525bbe617b87b970eacf3a8b50 Mon Sep 17 00:00:00 2001 From: "Chak Shing, Lo" Date: Thu, 5 Sep 2024 18:15:09 +0100 Subject: [PATCH 6/9] update config --- Jenkinsfile_CNP | 8 +++++--- .../resources/application-integration-test.yaml | 2 ++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Jenkinsfile_CNP b/Jenkinsfile_CNP index 6026e57dc98..8a0c268837c 100644 --- a/Jenkinsfile_CNP +++ b/Jenkinsfile_CNP @@ -17,8 +17,7 @@ def serviceSecrets = [ secret('smoke-test-la-password', 'SMOKE_TEST_LA_USER_PASSWORD'), secret('system-update-user-username', 'SYSTEM_UPDATE_USER_USERNAME'), secret('system-update-user-password', 'SYSTEM_UPDATE_USER_PASSWORD'), - secret('cafcass-system-update-user-username', 'CAFCASS_SYSTEM_USER_USERNAME'), - secret('cafcass-system-update-user-password', 'CAFCASS_SYSTEM_USER_PASSWORD'), + secret('cafcass-system-update-user-username', 'CAFCASS_SYSTEM_USER_USERNAME') ] ] @@ -29,7 +28,8 @@ def integrationTestSecrets = [ secret('e2e-test-password', 'E2E_TEST_PASSWORD'), secret('system-update-user-username', 'SYSTEM_UPDATE_USER_USERNAME'), secret('system-update-user-password', 'SYSTEM_UPDATE_USER_PASSWORD'), - secret('e2e-test-judge-password', 'E2E_TEST_JUDGE_PASSWORD') + secret('e2e-test-judge-password', 'E2E_TEST_JUDGE_PASSWORD'), + secret('cafcass-system-update-user-username', 'CAFCASS_SYSTEM_USER_USERNAME') ] ] @@ -124,6 +124,7 @@ def setupSecretsForIntegrationTests(pipelineConf) { env.INTEGRATION_TEST_DOCMOSIS_TORNADO_OUTPUT_FOLDER = "${WORKSPACE}/build/docmosis-generated" env.SYSTEM_UPDATE_USER_PASSWORD = "${SYSTEM_UPDATE_USER_PASSWORD}" env.SYSTEM_UPDATE_USER_USERNAME = "${SYSTEM_UPDATE_USER_USERNAME}" + env.CAFCASS_SYSTEM_USER_USERNAME = "${CAFCASS_SYSTEM_USER_USERNAME}" } } } @@ -139,6 +140,7 @@ def teardownSecretsForIntegrationTests() { env.INTEGRATION_TEST_DOCMOSIS_TORNADO_OUTPUT_FOLDER = '' env.SYSTEM_UPDATE_USER_PASSWORD = '' env.SYSTEM_UPDATE_USER_USERNAME = '' + env.CAFCASS_SYSTEM_USER_USERNAME = '' } def setupShutteringSecret() { diff --git a/service/src/integrationTest/resources/application-integration-test.yaml b/service/src/integrationTest/resources/application-integration-test.yaml index 1e354753d37..b81942e23b9 100644 --- a/service/src/integrationTest/resources/application-integration-test.yaml +++ b/service/src/integrationTest/resources/application-integration-test.yaml @@ -117,6 +117,8 @@ fpl: system_update: username: ${SYSTEM_UPDATE_USER_USERNAME:} password: ${SYSTEM_UPDATE_USER_PASSWORD:} + cafcass_system_update: + username: ${CAFCASS_SYSTEM_USER_USERNAME} ctsc_inbox: 'FamilyPublicLaw+ctsc@gmail.com' court_to_court_admin: mapping: "344=>FamilyPublicLaw+ctsc@gmail.com;332=>FamilyPublicLaw+PublicLawEmail@gmail.com" From 46d53853b77d2d867286f445b6046ae2d4601e36 Mon Sep 17 00:00:00 2001 From: "Chak Shing, Lo" Date: Fri, 6 Sep 2024 10:32:13 +0100 Subject: [PATCH 7/9] udpate cafcass sys user auth --- Jenkinsfile_CNP | 8 ++++++-- .../resources/application-integration-test.yaml | 3 ++- .../config/CafcassSystemUpdateUserConfiguration.java | 5 ++++- .../reform/fpl/service/SecureDocStoreService.java | 12 +++++++++++- .../cafcass/api/CafcassApiDocumentService.java | 5 ++++- service/src/main/resources/application-local.yaml | 1 + .../fpl/service/SecureDocStoreServiceTest.java | 3 +++ .../cafcass/api/CafcassApiDocumentServiceTest.java | 8 ++++++-- 8 files changed, 37 insertions(+), 8 deletions(-) diff --git a/Jenkinsfile_CNP b/Jenkinsfile_CNP index 8a0c268837c..ba876e2560c 100644 --- a/Jenkinsfile_CNP +++ b/Jenkinsfile_CNP @@ -17,7 +17,8 @@ def serviceSecrets = [ secret('smoke-test-la-password', 'SMOKE_TEST_LA_USER_PASSWORD'), secret('system-update-user-username', 'SYSTEM_UPDATE_USER_USERNAME'), secret('system-update-user-password', 'SYSTEM_UPDATE_USER_PASSWORD'), - secret('cafcass-system-update-user-username', 'CAFCASS_SYSTEM_USER_USERNAME') + secret('cafcass-system-update-user-username', 'CAFCASS_SYSTEM_USER_USERNAME'), + secret('cafcass-system-update-user-password', 'CAFCASS_SYSTEM_USER_PASSWORD') ] ] @@ -29,7 +30,8 @@ def integrationTestSecrets = [ secret('system-update-user-username', 'SYSTEM_UPDATE_USER_USERNAME'), secret('system-update-user-password', 'SYSTEM_UPDATE_USER_PASSWORD'), secret('e2e-test-judge-password', 'E2E_TEST_JUDGE_PASSWORD'), - secret('cafcass-system-update-user-username', 'CAFCASS_SYSTEM_USER_USERNAME') + secret('cafcass-system-update-user-username', 'CAFCASS_SYSTEM_USER_USERNAME'), + secret('cafcass-system-update-user-password', 'CAFCASS_SYSTEM_USER_PASSWORD') ] ] @@ -125,6 +127,7 @@ def setupSecretsForIntegrationTests(pipelineConf) { env.SYSTEM_UPDATE_USER_PASSWORD = "${SYSTEM_UPDATE_USER_PASSWORD}" env.SYSTEM_UPDATE_USER_USERNAME = "${SYSTEM_UPDATE_USER_USERNAME}" env.CAFCASS_SYSTEM_USER_USERNAME = "${CAFCASS_SYSTEM_USER_USERNAME}" + env.CAFCASS_SYSTEM_USER_PASSWORD = "${CAFCASS_SYSTEM_USER_PASSWORD}" } } } @@ -141,6 +144,7 @@ def teardownSecretsForIntegrationTests() { env.SYSTEM_UPDATE_USER_PASSWORD = '' env.SYSTEM_UPDATE_USER_USERNAME = '' env.CAFCASS_SYSTEM_USER_USERNAME = '' + env.CAFCASS_SYSTEM_USER_PASSWORD = '' } def setupShutteringSecret() { diff --git a/service/src/integrationTest/resources/application-integration-test.yaml b/service/src/integrationTest/resources/application-integration-test.yaml index b81942e23b9..e21f0ad8efa 100644 --- a/service/src/integrationTest/resources/application-integration-test.yaml +++ b/service/src/integrationTest/resources/application-integration-test.yaml @@ -118,7 +118,8 @@ fpl: username: ${SYSTEM_UPDATE_USER_USERNAME:} password: ${SYSTEM_UPDATE_USER_PASSWORD:} cafcass_system_update: - username: ${CAFCASS_SYSTEM_USER_USERNAME} + username: ${CAFCASS_SYSTEM_USER_USERNAME:} + password: ${CAFCASS_SYSTEM_USER_PASSWORD:} ctsc_inbox: 'FamilyPublicLaw+ctsc@gmail.com' court_to_court_admin: mapping: "344=>FamilyPublicLaw+ctsc@gmail.com;332=>FamilyPublicLaw+PublicLawEmail@gmail.com" diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java index ba054762a08..2c9e169c76d 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CafcassSystemUpdateUserConfiguration.java @@ -8,8 +8,11 @@ @Configuration public class CafcassSystemUpdateUserConfiguration { private final String userName; + private final String password; - public CafcassSystemUpdateUserConfiguration(@Value("${fpl.cafcass_system_update.username}") String userName) { + public CafcassSystemUpdateUserConfiguration(@Value("${fpl.cafcass_system_update.username}") String userName, + @Value("${fpl.cafcass_system_update.password}") String password) { this.userName = userName; + this.password = password; } } diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreService.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreService.java index 2803a629953..bf5a4a5edca 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreService.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreService.java @@ -19,6 +19,7 @@ import uk.gov.hmcts.reform.document.domain.Classification; import uk.gov.hmcts.reform.fpl.exceptions.EmptyFileException; import uk.gov.hmcts.reform.fpl.request.RequestData; +import uk.gov.hmcts.reform.idam.client.IdamClient; import java.util.Optional; import java.util.UUID; @@ -35,6 +36,7 @@ public class SecureDocStoreService { private final AuthTokenGenerator authTokenGenerator; private final CaseDocumentClientApi caseDocumentClientApi; private final RequestData requestData; + private final IdamClient idamClient; public Document uploadDocument(byte[] pdf, String fileName, String contentType) { @@ -56,10 +58,18 @@ public Document uploadDocument(byte[] pdf, String fileName, String contentType) return document; } + public byte[] downloadDocument(final String documentUrlString, final String userName, final String password) { + return downloadDocument(documentUrlString, idamClient.getAccessToken(userName, password)); + } + public byte[] downloadDocument(final String documentUrlString) { + return downloadDocument(documentUrlString, requestData.authorisation()); + } + + public byte[] downloadDocument(final String documentUrlString, String authorisation) { UUID documentId = getDocumentIdFromUrl(documentUrlString); ResponseEntity documentDownloadResponse = caseDocumentClientApi.getDocumentBinary( - requestData.authorisation(), authTokenGenerator.generate(), documentId); + authorisation, authTokenGenerator.generate(), documentId); if (isNotEmpty(documentDownloadResponse) && HttpStatus.OK == documentDownloadResponse.getStatusCode()) { return Optional.of(documentDownloadResponse) diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentService.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentService.java index a97fc6d378f..52ad06c9a73 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentService.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentService.java @@ -3,6 +3,7 @@ import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; import uk.gov.hmcts.reform.fpl.exceptions.EmptyFileException; import uk.gov.hmcts.reform.fpl.service.SecureDocStoreService; @@ -10,8 +11,10 @@ @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class CafcassApiDocumentService { private final SecureDocStoreService secureDocStoreService; + private final CafcassSystemUpdateUserConfiguration cafcassSysUser; public byte[] downloadDocumentByDocumentId(String documentId) throws IllegalArgumentException, EmptyFileException { - return secureDocStoreService.downloadDocument(documentId); + return secureDocStoreService.downloadDocument(documentId, cafcassSysUser.getUserName(), + cafcassSysUser.getPassword()); } } diff --git a/service/src/main/resources/application-local.yaml b/service/src/main/resources/application-local.yaml index 0b1f6c2f8fa..7c6fd0b2c84 100644 --- a/service/src/main/resources/application-local.yaml +++ b/service/src/main/resources/application-local.yaml @@ -128,6 +128,7 @@ fpl: password: ${IDAM_DATA_STORE_SYSTEM_USER_PASSWORD} cafcass_system_update: username: ${CAFCASS_SYSTEM_USER_USERNAME} + password: ${CAFCASS_SYSTEM_USER_PASSWORD} ctsc_inbox: 'FamilyPublicLaw+ctsc@gmail.com' ctsc_team_lead_inbox: 'FamilyPublicLaw+ctsc-team-lead@gmail.com' rcj_family_high_court_inbox: 'FamilyPublicLaw+rcjfamilyhighcourt@gmail.com' diff --git a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreServiceTest.java b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreServiceTest.java index 4ecb9db9b08..44a3c9f36ac 100644 --- a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreServiceTest.java +++ b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreServiceTest.java @@ -15,6 +15,7 @@ import uk.gov.hmcts.reform.ccd.document.am.model.Document; import uk.gov.hmcts.reform.ccd.document.am.model.UploadResponse; import uk.gov.hmcts.reform.fpl.request.RequestData; +import uk.gov.hmcts.reform.idam.client.IdamClient; import java.util.UUID; @@ -40,6 +41,8 @@ class SecureDocStoreServiceTest { private CaseDocumentClientApi caseDocumentClientApi; @Mock private RequestData requestData; + @Mock + private IdamClient idamClient; @Mock private ResponseEntity resourceResponseEntity; diff --git a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java index e8468586ea1..f171213ed14 100644 --- a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java +++ b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java @@ -2,6 +2,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; import uk.gov.hmcts.reform.fpl.service.SecureDocStoreService; import java.util.UUID; @@ -12,19 +13,22 @@ public class CafcassApiDocumentServiceTest { private SecureDocStoreService secureDocStoreService = mock(SecureDocStoreService.class); + private CafcassSystemUpdateUserConfiguration cafcassSysUser = + new CafcassSystemUpdateUserConfiguration("cafcass@test.com", "test"); private CafcassApiDocumentService underTest; @BeforeEach void setUpWithMockConverters() { - underTest = new CafcassApiDocumentService(secureDocStoreService); + underTest = new CafcassApiDocumentService(secureDocStoreService, cafcassSysUser); } @Test void shouldReturnDocumentBinary() { UUID docId = UUID.randomUUID(); byte[] docBinary = "This is a document".getBytes(); - when(secureDocStoreService.downloadDocument(docId.toString())).thenReturn(docBinary); + when(secureDocStoreService.downloadDocument(docId.toString(), cafcassSysUser.getUserName(), + cafcassSysUser.getPassword())).thenReturn(docBinary); assertArrayEquals(docBinary, underTest.downloadDocumentByDocumentId(docId.toString())); } From a402ace9b102514beb158ee434a5ee21995172ce Mon Sep 17 00:00:00 2001 From: "Chak Shing, Lo" Date: Fri, 6 Sep 2024 11:20:57 +0100 Subject: [PATCH 8/9] update with cache --- .../reform/fpl/config/CacheConfiguration.java | 4 +- .../fpl/service/SecureDocStoreService.java | 6 - .../api/CafcassApiDocumentService.java | 6 +- .../cafcass/api/CafcassSystemUserService.java | 37 +++++ .../service/SecureDocStoreServiceTest.java | 3 - .../api/CafcassApiDocumentServiceTest.java | 8 +- .../api/CafcassSystemUserServiceTest.java | 140 ++++++++++++++++++ 7 files changed, 184 insertions(+), 20 deletions(-) create mode 100644 service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java create mode 100644 service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserServiceTest.java diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CacheConfiguration.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CacheConfiguration.java index 1635803f276..5ea08cbcf1f 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CacheConfiguration.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/config/CacheConfiguration.java @@ -23,6 +23,7 @@ public class CacheConfiguration { public static final String ORGANISATION_CACHE = "organisationCache"; public static final String SYS_USER_CACHE = "systemUserCache"; + public static final String CAFCASS_SYS_USER_CACHE = "cafcassSystemUserCache"; public static final int SYSTEM_USER_CACHE_EXPIRY = 120; @@ -40,12 +41,11 @@ public CacheManager requestScopeCacheManager() { @Bean @Scope(value = WebApplicationContext.SCOPE_APPLICATION, proxyMode = ScopedProxyMode.TARGET_CLASS) public CacheManager localCacheManager() { - CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(SYS_USER_CACHE); + CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager(SYS_USER_CACHE, CAFCASS_SYS_USER_CACHE); caffeineCacheManager.setCaffeine(Caffeine.newBuilder() .initialCapacity(10) .maximumSize(100) .expireAfterWrite(Duration.ofMinutes(SYSTEM_USER_CACHE_EXPIRY))); return caffeineCacheManager; } - } diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreService.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreService.java index bf5a4a5edca..74a535a0f08 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreService.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreService.java @@ -19,7 +19,6 @@ import uk.gov.hmcts.reform.document.domain.Classification; import uk.gov.hmcts.reform.fpl.exceptions.EmptyFileException; import uk.gov.hmcts.reform.fpl.request.RequestData; -import uk.gov.hmcts.reform.idam.client.IdamClient; import java.util.Optional; import java.util.UUID; @@ -36,7 +35,6 @@ public class SecureDocStoreService { private final AuthTokenGenerator authTokenGenerator; private final CaseDocumentClientApi caseDocumentClientApi; private final RequestData requestData; - private final IdamClient idamClient; public Document uploadDocument(byte[] pdf, String fileName, String contentType) { @@ -58,10 +56,6 @@ public Document uploadDocument(byte[] pdf, String fileName, String contentType) return document; } - public byte[] downloadDocument(final String documentUrlString, final String userName, final String password) { - return downloadDocument(documentUrlString, idamClient.getAccessToken(userName, password)); - } - public byte[] downloadDocument(final String documentUrlString) { return downloadDocument(documentUrlString, requestData.authorisation()); } diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentService.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentService.java index 52ad06c9a73..7842c228d3f 100644 --- a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentService.java +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentService.java @@ -3,7 +3,6 @@ import lombok.RequiredArgsConstructor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; -import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; import uk.gov.hmcts.reform.fpl.exceptions.EmptyFileException; import uk.gov.hmcts.reform.fpl.service.SecureDocStoreService; @@ -11,10 +10,9 @@ @RequiredArgsConstructor(onConstructor = @__(@Autowired)) public class CafcassApiDocumentService { private final SecureDocStoreService secureDocStoreService; - private final CafcassSystemUpdateUserConfiguration cafcassSysUser; + private final CafcassSystemUserService cafcassSystemUserService; public byte[] downloadDocumentByDocumentId(String documentId) throws IllegalArgumentException, EmptyFileException { - return secureDocStoreService.downloadDocument(documentId, cafcassSysUser.getUserName(), - cafcassSysUser.getPassword()); + return secureDocStoreService.downloadDocument(documentId, cafcassSystemUserService.getUserToken()); } } diff --git a/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java new file mode 100644 index 00000000000..5b914f67c27 --- /dev/null +++ b/service/src/main/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserService.java @@ -0,0 +1,37 @@ +package uk.gov.hmcts.reform.fpl.service.cafcass.api; + +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.stereotype.Service; +import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; +import uk.gov.hmcts.reform.idam.client.IdamClient; + +import static uk.gov.hmcts.reform.fpl.config.CacheConfiguration.CAFCASS_SYS_USER_CACHE; +import static uk.gov.hmcts.reform.fpl.config.CacheConfiguration.LOCAL_CACHE_MANAGER; + +@Slf4j +@Service +@EnableCaching +@RequiredArgsConstructor(onConstructor_ = {@Autowired}) +public class CafcassSystemUserService { + + public static final String CAFCASS_SYS_USER_TOKEN_CACHE_KEY = "cafcassSysUserToken"; + + private final CafcassSystemUpdateUserConfiguration userConfig; + private final IdamClient idamClient; + + @Cacheable(cacheManager = LOCAL_CACHE_MANAGER, cacheNames = CAFCASS_SYS_USER_CACHE, + unless = "#result == null", key = "#root.target.CAFCASS_SYS_USER_TOKEN_CACHE_KEY") + public String getUserToken() { + log.info("Requesting cafcass system-user token from IDAM"); + return idamClient.getAccessToken(userConfig.getUserName(), userConfig.getPassword()); + } + + public String getUserId(String userToken) { + return idamClient.getUserInfo(userToken).getUid(); + } + +} diff --git a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreServiceTest.java b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreServiceTest.java index 44a3c9f36ac..4ecb9db9b08 100644 --- a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreServiceTest.java +++ b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/SecureDocStoreServiceTest.java @@ -15,7 +15,6 @@ import uk.gov.hmcts.reform.ccd.document.am.model.Document; import uk.gov.hmcts.reform.ccd.document.am.model.UploadResponse; import uk.gov.hmcts.reform.fpl.request.RequestData; -import uk.gov.hmcts.reform.idam.client.IdamClient; import java.util.UUID; @@ -41,8 +40,6 @@ class SecureDocStoreServiceTest { private CaseDocumentClientApi caseDocumentClientApi; @Mock private RequestData requestData; - @Mock - private IdamClient idamClient; @Mock private ResponseEntity resourceResponseEntity; diff --git a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java index f171213ed14..3d7622db24c 100644 --- a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java +++ b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java @@ -2,7 +2,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; import uk.gov.hmcts.reform.fpl.service.SecureDocStoreService; import java.util.UUID; @@ -13,8 +12,7 @@ public class CafcassApiDocumentServiceTest { private SecureDocStoreService secureDocStoreService = mock(SecureDocStoreService.class); - private CafcassSystemUpdateUserConfiguration cafcassSysUser = - new CafcassSystemUpdateUserConfiguration("cafcass@test.com", "test"); + private CafcassSystemUserService cafcassSysUser = mock(CafcassSystemUserService.class); private CafcassApiDocumentService underTest; @@ -27,8 +25,8 @@ void setUpWithMockConverters() { void shouldReturnDocumentBinary() { UUID docId = UUID.randomUUID(); byte[] docBinary = "This is a document".getBytes(); - when(secureDocStoreService.downloadDocument(docId.toString(), cafcassSysUser.getUserName(), - cafcassSysUser.getPassword())).thenReturn(docBinary); + when(cafcassSysUser.getUserToken()).thenReturn("test token"); + when(secureDocStoreService.downloadDocument(docId.toString(), cafcassSysUser.getUserToken())).thenReturn(docBinary); assertArrayEquals(docBinary, underTest.downloadDocumentByDocumentId(docId.toString())); } diff --git a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserServiceTest.java b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserServiceTest.java new file mode 100644 index 00000000000..ed83aad1fac --- /dev/null +++ b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserServiceTest.java @@ -0,0 +1,140 @@ +package uk.gov.hmcts.reform.fpl.service.cafcass.api; + +import org.apache.commons.lang3.RandomStringUtils; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.cache.CacheManager; +import org.springframework.test.annotation.DirtiesContext; +import org.springframework.test.context.junit.jupiter.SpringExtension; +import uk.gov.hmcts.reform.fpl.config.CacheConfiguration; +import uk.gov.hmcts.reform.fpl.config.CafcassSystemUpdateUserConfiguration; +import uk.gov.hmcts.reform.fpl.config.SystemUpdateUserConfiguration; +import uk.gov.hmcts.reform.fpl.service.SystemUserService; +import uk.gov.hmcts.reform.idam.client.IdamClient; +import uk.gov.hmcts.reform.idam.client.models.UserInfo; + +import java.util.UUID; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static uk.gov.hmcts.reform.fpl.config.CacheConfiguration.CAFCASS_SYS_USER_CACHE; +import static uk.gov.hmcts.reform.fpl.config.CacheConfiguration.SYS_USER_CACHE; +import static uk.gov.hmcts.reform.fpl.service.SystemUserService.SYS_USER_TOKEN_CACHE_KEY; +import static uk.gov.hmcts.reform.fpl.service.cafcass.api.CafcassSystemUserService.CAFCASS_SYS_USER_TOKEN_CACHE_KEY; + +@ExtendWith(SpringExtension.class) +@SpringBootTest(classes = {CafcassSystemUserService.class, CacheConfiguration.class, SystemUserService.class}) +@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD) +class CafcassSystemUserServiceTest { + + private static final String CAFCASS_SYS_USER_NAME = "cafcass_sys_name"; + private static final String CAFCASSS_SYS_USER_PASS = "cafcass_sys_pass"; + + @MockBean + private IdamClient idamClient; + + @MockBean + private CafcassSystemUpdateUserConfiguration cafcassSysUserConfig; + + @MockBean + private SystemUpdateUserConfiguration sysUserConfig; + + @Autowired + private CacheManager localCacheManager; + + @Autowired + private SystemUserService systemUserService; + + @Autowired + private CafcassSystemUserService underTest; + + + @BeforeEach + void init() { + given(cafcassSysUserConfig.getUserName()).willReturn(CAFCASS_SYS_USER_NAME); + given(cafcassSysUserConfig.getPassword()).willReturn(CAFCASSS_SYS_USER_PASS); + } + + @Test + void shouldReturnSystemUserId() { + String token = RandomStringUtils.randomAlphanumeric(10); + + UserInfo userInfo = UserInfo.builder() + .uid(UUID.randomUUID().toString()) + .build(); + + given(idamClient.getUserInfo(token)).willReturn(userInfo); + + String actualId = underTest.getUserId(token); + + assertThat(actualId).isEqualTo(userInfo.getUid()); + } + + @Test + void shouldGetCachedTokenIfValid() { + String token = RandomStringUtils.randomAlphanumeric(10); + + given(idamClient.getAccessToken(CAFCASS_SYS_USER_NAME, CAFCASSS_SYS_USER_PASS)).willReturn(token); + String retrieved = underTest.getUserToken(); + String retrievedSecond = underTest.getUserToken(); + + // should get first time, then get cached version + verify(idamClient, times(1)).getAccessToken(CAFCASS_SYS_USER_NAME, CAFCASSS_SYS_USER_PASS); + + // ensure the token is the same all the time + assertThat(retrieved).isEqualTo(token); + assertThat(retrievedSecond).isEqualTo(token); + assertThat(getCachedToken()).isEqualTo(token); + } + + @Test + void shouldNotInterfereAnyOtherCache() { + String cafcass_token = RandomStringUtils.randomAlphanumeric(10); + String sys_token = RandomStringUtils.randomAlphanumeric(10); + + given(sysUserConfig.getUserName()).willReturn("SYS_USER"); + given(sysUserConfig.getPassword()).willReturn("SYS_USER_PWD"); + given(idamClient.getAccessToken(CAFCASS_SYS_USER_NAME, CAFCASSS_SYS_USER_PASS)).willReturn(cafcass_token); + given(idamClient.getAccessToken("SYS_USER", "SYS_USER_PWD")).willReturn(sys_token); + + underTest.getUserToken(); + systemUserService.getSysUserToken(); + + for (int i = 0; i < 10; i++) { + underTest.getUserToken(); + systemUserService.getSysUserToken(); + assertThat(localCacheManager.getCache(SYS_USER_CACHE).get(SYS_USER_TOKEN_CACHE_KEY, String.class)) + .isEqualTo(sys_token); + assertThat(getCachedToken()).isEqualTo(cafcass_token); + + systemUserService.getSysUserToken(); + underTest.getUserToken(); + assertThat(localCacheManager.getCache(SYS_USER_CACHE).get(SYS_USER_TOKEN_CACHE_KEY, String.class)) + .isEqualTo(sys_token); + assertThat(getCachedToken()).isEqualTo(cafcass_token); + + underTest.getUserToken(); + systemUserService.getSysUserToken(); + assertThat(getCachedToken()).isEqualTo(cafcass_token); + assertThat(localCacheManager.getCache(SYS_USER_CACHE).get(SYS_USER_TOKEN_CACHE_KEY, String.class)) + .isEqualTo(sys_token); + + systemUserService.getSysUserToken(); + underTest.getUserToken(); + assertThat(getCachedToken()).isEqualTo(cafcass_token); + assertThat(localCacheManager.getCache(SYS_USER_CACHE).get(SYS_USER_TOKEN_CACHE_KEY, String.class)) + .isEqualTo(sys_token); + } + } + + private String getCachedToken() { + return localCacheManager.getCache(CAFCASS_SYS_USER_CACHE).get(CAFCASS_SYS_USER_TOKEN_CACHE_KEY, String.class); + } + +} From cbdc19dae6138d199ddca72752db96874dcf6209 Mon Sep 17 00:00:00 2001 From: "Chak Shing, Lo" Date: Fri, 6 Sep 2024 11:58:51 +0100 Subject: [PATCH 9/9] fix check style --- Jenkinsfile_CNP | 4 ++-- .../api/CafcassApiDocumentServiceTest.java | 3 ++- .../api/CafcassSystemUserServiceTest.java | 24 +++++++++---------- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/Jenkinsfile_CNP b/Jenkinsfile_CNP index ba876e2560c..f07fee0ab5f 100644 --- a/Jenkinsfile_CNP +++ b/Jenkinsfile_CNP @@ -48,11 +48,11 @@ def otherSecrets = [ secret('idam-data-store-client-secret', 'IDAM_OAUTH2_DATA_STORE_CLIENT_SECRET'), secret('idam-data-store-system-user-username', 'IDAM_DATA_STORE_SYSTEM_USER_USERNAME'), secret('idam-data-store-system-user-password', 'IDAM_DATA_STORE_SYSTEM_USER_PASSWORD'), - secret('ccd-case-document-am-api-s2s-secret', 'CCD_CASE_DOCUMENT_AM_API_SECRET'), + secret('ccd-case-document-am-api-s2s-secret', 'CCD_CASE_DOCUMENT_AM_API_SECRET') ], 'fpl-${env}': [ secret('definition-importer-username', 'CCD_CONFIGURER_IMPORTER_USERNAME'), - secret('definition-importer-password', 'CCD_CONFIGURER_IMPORTER_PASSWORD'), + secret('definition-importer-password', 'CCD_CONFIGURER_IMPORTER_PASSWORD') ] ] diff --git a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java index 3d7622db24c..9733f2dc0e6 100644 --- a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java +++ b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassApiDocumentServiceTest.java @@ -26,7 +26,8 @@ void shouldReturnDocumentBinary() { UUID docId = UUID.randomUUID(); byte[] docBinary = "This is a document".getBytes(); when(cafcassSysUser.getUserToken()).thenReturn("test token"); - when(secureDocStoreService.downloadDocument(docId.toString(), cafcassSysUser.getUserToken())).thenReturn(docBinary); + when(secureDocStoreService.downloadDocument(docId.toString(), cafcassSysUser.getUserToken())) + .thenReturn(docBinary); assertArrayEquals(docBinary, underTest.downloadDocumentByDocumentId(docId.toString())); } diff --git a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserServiceTest.java b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserServiceTest.java index ed83aad1fac..c3f48d47342 100644 --- a/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserServiceTest.java +++ b/service/src/test/java/uk/gov/hmcts/reform/fpl/service/cafcass/api/CafcassSystemUserServiceTest.java @@ -95,13 +95,13 @@ void shouldGetCachedTokenIfValid() { @Test void shouldNotInterfereAnyOtherCache() { - String cafcass_token = RandomStringUtils.randomAlphanumeric(10); - String sys_token = RandomStringUtils.randomAlphanumeric(10); + String cafcassToken = RandomStringUtils.randomAlphanumeric(10); + String sysToken = RandomStringUtils.randomAlphanumeric(10); given(sysUserConfig.getUserName()).willReturn("SYS_USER"); given(sysUserConfig.getPassword()).willReturn("SYS_USER_PWD"); - given(idamClient.getAccessToken(CAFCASS_SYS_USER_NAME, CAFCASSS_SYS_USER_PASS)).willReturn(cafcass_token); - given(idamClient.getAccessToken("SYS_USER", "SYS_USER_PWD")).willReturn(sys_token); + given(idamClient.getAccessToken(CAFCASS_SYS_USER_NAME, CAFCASSS_SYS_USER_PASS)).willReturn(cafcassToken); + given(idamClient.getAccessToken("SYS_USER", "SYS_USER_PWD")).willReturn(sysToken); underTest.getUserToken(); systemUserService.getSysUserToken(); @@ -110,26 +110,26 @@ void shouldNotInterfereAnyOtherCache() { underTest.getUserToken(); systemUserService.getSysUserToken(); assertThat(localCacheManager.getCache(SYS_USER_CACHE).get(SYS_USER_TOKEN_CACHE_KEY, String.class)) - .isEqualTo(sys_token); - assertThat(getCachedToken()).isEqualTo(cafcass_token); + .isEqualTo(sysToken); + assertThat(getCachedToken()).isEqualTo(cafcassToken); systemUserService.getSysUserToken(); underTest.getUserToken(); assertThat(localCacheManager.getCache(SYS_USER_CACHE).get(SYS_USER_TOKEN_CACHE_KEY, String.class)) - .isEqualTo(sys_token); - assertThat(getCachedToken()).isEqualTo(cafcass_token); + .isEqualTo(sysToken); + assertThat(getCachedToken()).isEqualTo(cafcassToken); underTest.getUserToken(); systemUserService.getSysUserToken(); - assertThat(getCachedToken()).isEqualTo(cafcass_token); + assertThat(getCachedToken()).isEqualTo(cafcassToken); assertThat(localCacheManager.getCache(SYS_USER_CACHE).get(SYS_USER_TOKEN_CACHE_KEY, String.class)) - .isEqualTo(sys_token); + .isEqualTo(sysToken); systemUserService.getSysUserToken(); underTest.getUserToken(); - assertThat(getCachedToken()).isEqualTo(cafcass_token); + assertThat(getCachedToken()).isEqualTo(cafcassToken); assertThat(localCacheManager.getCache(SYS_USER_CACHE).get(SYS_USER_TOKEN_CACHE_KEY, String.class)) - .isEqualTo(sys_token); + .isEqualTo(sysToken); } }