From 244f2fc10f8da46da1fc14c315370890c725cc9a Mon Sep 17 00:00:00 2001 From: Marco Villeneuve Date: Thu, 30 Nov 2023 16:28:34 -0800 Subject: [PATCH] Fixes for activation code issue --- .../api/edx/service/v1/EdxUsersService.java | 11 +++++++++++ .../edx/controller/EdxUsersControllerTest.java | 18 ++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/api/src/main/java/ca/bc/gov/educ/api/edx/service/v1/EdxUsersService.java b/api/src/main/java/ca/bc/gov/educ/api/edx/service/v1/EdxUsersService.java index 51239760..912f4d0c 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/edx/service/v1/EdxUsersService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/edx/service/v1/EdxUsersService.java @@ -388,6 +388,7 @@ public EdxUserEntity activateEdxUser(EdxActivateUser edxActivateUser) { } else { activationCodes = edxActivationCodeRepository.findEdxActivationCodeByActivationCodeInAndDistrictID(acCodes, edxActivateUser.getDistrictID()); } + validateIncomingActivationCodeTypes(edxActivateUser, activationCodes); if (!CollectionUtils.isEmpty(activationCodes) && activationCodes.size() == 2) { EdxActivationCodeEntity userCodeEntity = validateExpiryAndSetEdxUserIdForPersonalActivationCode(edxActivateUser, activationCodes); @@ -398,6 +399,16 @@ public EdxUserEntity activateEdxUser(EdxActivateUser edxActivateUser) { } } + private void validateIncomingActivationCodeTypes(EdxActivateUser user, List activationCodes){ + for (val activationCode : activationCodes) { + if((activationCode.getIsPrimary() && !activationCode.getActivationCode().equalsIgnoreCase(user.getPrimaryEdxCode())) || + (!activationCode.getIsPrimary() && !activationCode.getActivationCode().equalsIgnoreCase(user.getPersonalActivationCode()))){ + ApiError error = ApiError.builder().timestamp(LocalDateTime.now()).message("Invalid code provided.").status(BAD_REQUEST).build(); + throw new InvalidPayloadException(error); + } + } + } + private EdxActivationCodeEntity validateExpiryAndSetEdxUserIdForPersonalActivationCode(EdxActivateUser edxActivateUser, List activationCodes) { EdxActivationCodeEntity userCodeEntity = null; for (val activationCode : activationCodes) { diff --git a/api/src/test/java/ca/bc/gov/educ/api/edx/controller/EdxUsersControllerTest.java b/api/src/test/java/ca/bc/gov/educ/api/edx/controller/EdxUsersControllerTest.java index a15eddbe..fd4896d5 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/edx/controller/EdxUsersControllerTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/edx/controller/EdxUsersControllerTest.java @@ -1100,7 +1100,25 @@ void testEdxActivateUsers_GivenValidInput_UserIsCreated_WithOkStatusResponse() t .andExpect(jsonPath("$.edxUserSchools.[0].edxUserSchoolRoles", hasSize(1))) .andExpect(jsonPath("$.edxUserSchools[0].edxUserSchoolRoles[0].edxUserSchoolRoleID", is(notNullValue()))) .andDo(print()).andExpect(status().isCreated()); + } + @Test + void testEdxActivateUsers_GivenValidInput_UserIsCreated_WithBadRequestResponse() throws Exception { + UUID validationCode = UUID.randomUUID(); + UUID schoolID = UUID.randomUUID(); + this.createActivationCodeTableDataForSchoolUser(this.edxActivationCodeRepository, this.edxPermissionRepository, this.edxRoleRepository, this.edxActivationRoleRepository, true,validationCode, 2, schoolID); + EdxActivateUser edxActivateUser = new EdxActivateUser(); + edxActivateUser.setSchoolID(schoolID); + edxActivateUser.setPersonalActivationCode("ABCDE"); + edxActivateUser.setPrimaryEdxCode("WXYZ"); + edxActivateUser.setDigitalId(UUID.randomUUID().toString()); + String activateUserJson = getJsonString(edxActivateUser); + val resultActions = this.mockMvc.perform(post(URL.BASE_URL_USERS + "/activation") + .contentType(MediaType.APPLICATION_JSON) + .content(activateUserJson) + .accept(MediaType.APPLICATION_JSON) + .with(jwt().jwt((jwt) -> jwt.claim("scope", "ACTIVATE_EDX_USER")))); + resultActions.andDo(print()).andExpect(status().isBadRequest()); } @Test