Skip to content

Commit

Permalink
[SELC-5856] Fix create user Role to allowed multirole
Browse files Browse the repository at this point in the history
  • Loading branch information
flaminiaScarciofolo committed Oct 28, 2024
1 parent d03af3c commit b2dcea1
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,15 @@ private boolean checkAlreadyOnboardedRole(AddUserRoleDto.Product product, UserIn
.anyMatch(onboardedProduct -> !onboardedProduct.getRole().name().equalsIgnoreCase(product.getRole()));
}

private boolean checkAlreadyOnboardedRole(CreateUserDto.Product product, UserInstitution userInstitution) {
return Optional.ofNullable(userInstitution.getProducts())
.orElse(Collections.emptyList())
.stream()
.filter(onboardedProduct -> onboardedProduct.getStatus().equals(ACTIVE))
.filter(onboardedProduct -> onboardedProduct.getProductId().equalsIgnoreCase(product.getProductId()))
.anyMatch(onboardedProduct -> !onboardedProduct.getRole().name().equalsIgnoreCase(product.getRole()));
}

/**
* Updates or creates a UserInstitution based on the provided data.
* Return null value if UserInstitution exists in ACTIVE with the same productRole
Expand All @@ -518,14 +527,10 @@ private Uni<UserInstitution> updateOrCreateUserInstitution(CreateUserDto userDto
}

log.info(USER_INSTITUTION_FOUNDED, userId, userDto.getInstitutionId());
//Verify if productRole already exists
if(Optional.ofNullable(userInstitution.getProducts())
.orElse(Collections.emptyList())
.stream()
.filter(onboardedProduct -> onboardedProduct.getStatus().equals(ACTIVE))
.filter(onboardedProduct -> userDto.getProduct().getProductId().equals(onboardedProduct.getProductId()))
.anyMatch(onboardedProduct -> userDto.getProduct().getProductRoles().contains(onboardedProduct.getProductRole()))){
return Uni.createFrom().nullItem();
log.info(USER_INSTITUTION_FOUNDED, userId, userDto.getInstitutionId());

if(checkAlreadyOnboardedRole(userDto.getProduct(), userInstitution)){
throw new InvalidRequestException(String.format("User already has different role on Product %s", userDto.getProduct().getProductId()));
}

List<String> productRoleToAdd = checkAlreadyOnboardedProductRole(userDto.getProduct().getProductId(), userDto.getProduct().getProductRoles(), userInstitution);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role() {
CreateUserDto.User user = new CreateUserDto.User();
user.setFiscalCode("fiscalCode");
CreateUserDto.Product createUserProduct = new CreateUserDto.Product();
createUserProduct.setProductId("prod-io");
createUserProduct.setProductId("test");
createUserProduct.setProductRoles(List.of("admin2","admin3"));
createUserDto.setUser(user);
createUserDto.setProduct(createUserProduct);
Expand Down Expand Up @@ -838,7 +838,7 @@ void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role() {
UniAssertSubscriber<CreateOrUpdateUserByFiscalCodeResponse> subscriber = userService.createOrUpdateUserByFiscalCode(createUserDto, loggedUser)
.subscribe().withSubscriber(UniAssertSubscriber.create());

Assertions.assertEquals(4, userInstitution.getProducts().size());
Assertions.assertEquals(2, userInstitution.getProducts().size());
// Verify the result

CreateOrUpdateUserByFiscalCodeResponse response = subscriber.awaitItem().getItem();
Expand All @@ -850,6 +850,96 @@ void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role() {
verify(userNotificationService).sendCreateUserNotification(any(), any(), any(), any(), any(),any());
}

@Test
void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role_oneAlreadyOnboarded() {
UserInstitution userInstitution = createUserInstitution();
// Prepare test data
CreateUserDto createUserDto = new CreateUserDto();
CreateUserDto.User user = new CreateUserDto.User();
user.setFiscalCode("fiscalCode");
CreateUserDto.Product createUserProduct = new CreateUserDto.Product();
createUserProduct.setProductId("test");
createUserProduct.setRole("MANAGER");
createUserProduct.setProductRoles(List.of("admin","admin3"));
createUserDto.setUser(user);
createUserDto.setProduct(createUserProduct);
LoggedUser loggedUser = LoggedUser.builder().build();

Product product = new Product();
product.setDescription("description");

UserToNotify userToNotify = new UserToNotify();
userToNotify.setUserId(userId.toString());

UserNotificationToSend userNotificationToSend = new UserNotificationToSend();
userNotificationToSend.setUser(userToNotify);

// Mock external dependencies
when(userRegistryApi.searchUsingPOST(any(), any())).thenReturn(Uni.createFrom().item(userResource));
when(userInstitutionService.findByUserIdAndInstitutionId(any(), any())).thenReturn(Uni.createFrom().item(userInstitution));
when(userRegistryApi.updateUsingPATCH(any(), any())).thenReturn(Uni.createFrom().item(Response.ok().build()));
when(userInstitutionService.persistOrUpdate(any())).thenReturn(Uni.createFrom().item(userInstitution));
when(productService.getProduct(any())).thenReturn(product);
when(userNotificationService.sendCreateUserNotification(any(), any(), any(), any(), any(),any())).thenReturn(Uni.createFrom().voidItem());
when(userUtils.buildUsersNotificationResponse(any(), any())).thenReturn(List.of(userNotificationToSend));

// Call the method
UniAssertSubscriber<CreateOrUpdateUserByFiscalCodeResponse> subscriber = userService.createOrUpdateUserByFiscalCode(createUserDto, loggedUser)
.subscribe().withSubscriber(UniAssertSubscriber.create());

Assertions.assertEquals(3, userInstitution.getProducts().size());
// Verify the result

CreateOrUpdateUserByFiscalCodeResponse response = subscriber.awaitItem().getItem();

assertEquals(userId.toString(),response.getUserId());
verify(userRegistryApi).updateUsingPATCH(any(), any());
verify(userInstitutionService).persistOrUpdate(any());
verify(userInstitutionService).findByUserIdAndInstitutionId(any(), any());
verify(userNotificationService).sendCreateUserNotification(any(), any(), any(), any(), any(),any());
}

@Test
void testCreateOrUpdateUser_UpdateUser_SuccessByFiscalCode_with2role_oneAlreadyOnboarded_withDifferentSelcRole() {
UserInstitution userInstitution = createUserInstitution();
// Prepare test data
CreateUserDto createUserDto = new CreateUserDto();
CreateUserDto.User user = new CreateUserDto.User();
user.setFiscalCode("fiscalCode");
CreateUserDto.Product createUserProduct = new CreateUserDto.Product();
createUserProduct.setProductId("test");
createUserProduct.setRole("OPERATOR");
createUserProduct.setProductRoles(List.of("admin","admin3"));
createUserDto.setUser(user);
createUserDto.setProduct(createUserProduct);
LoggedUser loggedUser = LoggedUser.builder().build();

Product product = new Product();
product.setDescription("description");

UserToNotify userToNotify = new UserToNotify();
userToNotify.setUserId(userId.toString());

UserNotificationToSend userNotificationToSend = new UserNotificationToSend();
userNotificationToSend.setUser(userToNotify);

// Mock external dependencies
when(userRegistryApi.searchUsingPOST(any(), any())).thenReturn(Uni.createFrom().item(userResource));
when(userInstitutionService.findByUserIdAndInstitutionId(any(), any())).thenReturn(Uni.createFrom().item(userInstitution));
when(userRegistryApi.updateUsingPATCH(any(), any())).thenReturn(Uni.createFrom().item(Response.ok().build()));
when(userInstitutionService.persistOrUpdate(any())).thenReturn(Uni.createFrom().item(userInstitution));
when(productService.getProduct(any())).thenReturn(product);
when(userNotificationService.sendCreateUserNotification(any(), any(), any(), any(), any(),any())).thenReturn(Uni.createFrom().voidItem());
when(userUtils.buildUsersNotificationResponse(any(), any())).thenReturn(List.of(userNotificationToSend));

userService.createOrUpdateUserByFiscalCode(createUserDto, loggedUser)
.subscribe().withSubscriber(UniAssertSubscriber.create())
.assertFailedWith(InvalidRequestException.class, "User already has different role on Product test");

Assertions.assertEquals(2, userInstitution.getProducts().size());
}


@Test
void testCreateOrUpdateUser_CreateUser_SuccessByFiscalCode() {
// Prepare test data
Expand Down

0 comments on commit b2dcea1

Please sign in to comment.