Skip to content

Commit

Permalink
[SELC-6181] feat: added implementation of new API to find manager dat…
Browse files Browse the repository at this point in the history
…a given onboardingId
  • Loading branch information
empassaro committed Dec 19, 2024
1 parent b36000f commit 162c11a
Show file tree
Hide file tree
Showing 8 changed files with 512 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@
public class ManagerVerification {
private String origin;
private String companyName;
private boolean verified;

public ManagerVerification(boolean verified) {
this.verified = verified;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ public interface UserService {
void onboardingUsers(OnboardingData onboardingData);
void onboardingUsersAggregator(OnboardingData onboardingData);
boolean checkManager(OnboardingData onboardingData);
User getManagerInfo(String onboardingId, String userTaxCode);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import it.pagopa.selfcare.commons.base.logging.LogUtils;
import it.pagopa.selfcare.onboarding.connector.api.OnboardingMsConnector;
import it.pagopa.selfcare.onboarding.connector.api.UserRegistryConnector;
import it.pagopa.selfcare.onboarding.connector.exceptions.ResourceNotFoundException;
import it.pagopa.selfcare.onboarding.connector.model.institutions.ManagerVerification;
import it.pagopa.selfcare.onboarding.connector.model.onboarding.OnboardingData;
import it.pagopa.selfcare.onboarding.connector.model.onboarding.User;
import it.pagopa.selfcare.onboarding.connector.model.user.Certification;
import it.pagopa.selfcare.onboarding.connector.model.user.CertifiedField;
import it.pagopa.selfcare.onboarding.core.exception.InvalidUserFieldsException;
import it.pagopa.selfcare.onboarding.core.exception.OnboardingNotAllowedException;
import it.pagopa.selfcare.onboarding.core.utils.PgManagerVerifier;
import lombok.extern.slf4j.Slf4j;
import org.owasp.encoder.Encode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
Expand All @@ -19,6 +24,8 @@

import static it.pagopa.selfcare.onboarding.connector.model.user.User.Fields.familyName;
import static it.pagopa.selfcare.onboarding.connector.model.user.User.Fields.name;
import static it.pagopa.selfcare.onboarding.core.utils.Utils.getManager;
import static it.pagopa.selfcare.onboarding.core.utils.Utils.isUserAdmin;

@Slf4j
@Service
Expand All @@ -28,12 +35,15 @@ public class UserServiceImpl implements UserService {
private static final String INVALID_FIELD_REASON = "the value does not match with the certified data";
private final UserRegistryConnector userRegistryConnector;
private final OnboardingMsConnector onboardingMsConnector;
private final PgManagerVerifier pgManagerVerifier;

@Autowired
public UserServiceImpl(UserRegistryConnector userRegistryConnector,
OnboardingMsConnector onboardingMsConnector) {
OnboardingMsConnector onboardingMsConnector,
PgManagerVerifier pgManagerVerifier) {
this.userRegistryConnector = userRegistryConnector;
this.onboardingMsConnector = onboardingMsConnector;
this.pgManagerVerifier = pgManagerVerifier;
}

@Override
Expand Down Expand Up @@ -91,4 +101,38 @@ private <T> boolean isValid(T field, CertifiedField<T> certifiedField) {
: certifiedField.getValue().equals(field));
}

@Override
public User getManagerInfo(String onboardingId, String userTaxCode) {
log.trace("getManagerInfo start");
log.debug("getManagerInfo onboardingId = {}", Encode.forJava(onboardingId));

OnboardingData onboardingData;
try {
onboardingData = onboardingMsConnector.getOnboardingWithUserInfo(onboardingId);
} catch (ResourceNotFoundException e) {
log.error("Onboarding not found", e);
throw new ResourceNotFoundException("Onboarding not found");
}

String institutionTaxCode = onboardingData.getInstitutionUpdate().getTaxCode();
log.debug("getManagerInfo institutionTaxCode = {}", institutionTaxCode);

User managerInfo = getManager(onboardingData.getUsers())
.orElseThrow(() -> new ResourceNotFoundException("Manager not found"));

boolean isAlreadyAdmin = isUserAdmin(userTaxCode, onboardingData.getUsers());
if (isAlreadyAdmin) {
return managerInfo;
}

ManagerVerification managerVerification = pgManagerVerifier.doVerify(userTaxCode, institutionTaxCode);
if (!managerVerification.isVerified()) {
log.error("User is not an admin of the institution");
throw new OnboardingNotAllowedException("User is not an admin of the institution");
}

log.trace("getManagerInfo end");
return managerInfo;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package it.pagopa.selfcare.onboarding.core.utils;

import it.pagopa.selfcare.commons.base.logging.LogUtils;
import it.pagopa.selfcare.commons.base.utils.Origin;
import it.pagopa.selfcare.onboarding.connector.api.PartyRegistryProxyConnector;
import it.pagopa.selfcare.onboarding.connector.exceptions.InvalidRequestException;
import it.pagopa.selfcare.onboarding.connector.model.institutions.ManagerVerification;
import it.pagopa.selfcare.onboarding.connector.model.institutions.MatchInfoResult;
import it.pagopa.selfcare.onboarding.connector.model.institutions.infocamere.BusinessInfoIC;
import it.pagopa.selfcare.onboarding.connector.model.institutions.infocamere.InstitutionInfoIC;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.owasp.encoder.Encode;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Slf4j
@Service
@AllArgsConstructor
public class PgManagerVerifier {
private final PartyRegistryProxyConnector partyRegistryProxyConnector;

public ManagerVerification doVerify(String userTaxCode, String institutionTaxCode) {
log.debug(LogUtils.CONFIDENTIAL_MARKER, "Checking if user with taxCode {} is manager of institution with taxCode {} on INFOCAMERE", Encode.forJava(userTaxCode), Encode.forJava(institutionTaxCode));
InstitutionInfoIC institutionInfoIC = partyRegistryProxyConnector.getInstitutionsByUserFiscalCode(userTaxCode);
if (Objects.nonNull(institutionInfoIC) && Objects.nonNull(institutionInfoIC.getBusinesses())){
for (BusinessInfoIC business : institutionInfoIC.getBusinesses()) {
if (institutionTaxCode.equals(business.getBusinessTaxId())) {
log.debug("User found as manager in INFOCAMERE for business with name = {}", business.getBusinessName());
return new ManagerVerification(Origin.INFOCAMERE.getValue(), business.getBusinessName(), true);
}
}
}

try {
log.debug(LogUtils.CONFIDENTIAL_MARKER, "Checking if user with taxCode {} is manager of institution with taxCode {} on ADE", Encode.forJava(userTaxCode), Encode.forJava(institutionTaxCode));
MatchInfoResult matchInfoResult = partyRegistryProxyConnector.matchInstitutionAndUser(institutionTaxCode, userTaxCode);
if (Objects.nonNull(matchInfoResult) && matchInfoResult.isVerificationResult()) {
log.debug("User found as manager in ADE, response = {}", matchInfoResult);
return new ManagerVerification(Origin.ADE.getValue(), null, true);
}
} catch (InvalidRequestException e) {
log.debug("User not found as manager in ADE");
return new ManagerVerification(false);
}

log.debug("User not found as manager in INFOCAMERE or ADE");
return new ManagerVerification(false);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package it.pagopa.selfcare.onboarding.core.utils;

import it.pagopa.selfcare.onboarding.common.PartyRole;
import it.pagopa.selfcare.onboarding.connector.model.onboarding.User;

import java.util.List;
import java.util.Optional;

public class Utils {
private Utils() {
}

public static boolean isUserAdmin(String userTaxCode, List<User> users) {
return users.stream()
.anyMatch(user -> user.getTaxCode().equals(userTaxCode) &&
(user.getRole() == PartyRole.MANAGER
|| user.getRole() == PartyRole.DELEGATE
|| user.getRole() == PartyRole.SUB_DELEGATE)
);
}

public static Optional<User> getManager(List<User> users) {
return users.stream()
.filter(user -> user.getRole() == PartyRole.MANAGER)
.findFirst();
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package it.pagopa.selfcare.onboarding.core;

import it.pagopa.selfcare.commons.utils.TestUtils;
import it.pagopa.selfcare.onboarding.common.PartyRole;
import it.pagopa.selfcare.onboarding.connector.api.OnboardingMsConnector;
import it.pagopa.selfcare.onboarding.connector.api.UserRegistryConnector;
import it.pagopa.selfcare.onboarding.connector.exceptions.ResourceNotFoundException;
import it.pagopa.selfcare.onboarding.connector.model.institutions.ManagerVerification;
import it.pagopa.selfcare.onboarding.connector.model.onboarding.OnboardingData;
import it.pagopa.selfcare.onboarding.connector.model.onboarding.User;
import it.pagopa.selfcare.onboarding.connector.model.user.Certification;
import it.pagopa.selfcare.onboarding.connector.model.user.CertifiedField;
import it.pagopa.selfcare.onboarding.core.exception.InvalidUserFieldsException;
import it.pagopa.selfcare.onboarding.core.exception.OnboardingNotAllowedException;
import it.pagopa.selfcare.onboarding.core.utils.PgManagerVerifier;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.api.function.Executable;
Expand All @@ -16,6 +21,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

import java.util.EnumSet;
import java.util.List;
import java.util.Optional;

import static it.pagopa.selfcare.onboarding.connector.model.user.User.Fields.familyName;
Expand All @@ -36,6 +42,9 @@ class UserServiceImplTest {
@Mock
private OnboardingMsConnector onboardingMsConnector;

@Mock
private PgManagerVerifier pgManagerVerifierMock;


@Test
void validate_nullUser() {
Expand Down Expand Up @@ -180,5 +189,128 @@ void checkManager() {
verifyNoMoreInteractions(onboardingMsConnector);
}

@Test
void getManagerInfo_shouldReturnManagerInfoWhenGivenUserIsManager() {
// given
String onboardingId = "onboardingId";
String userTaxCode = "userTaxCode";
OnboardingData onboardingData = TestUtils.mockInstance(new OnboardingData());
onboardingData.getInstitutionUpdate().setTaxCode("institutionTaxCode");
User manager = TestUtils.mockInstance(new User());
manager.setName("managerName");
manager.setSurname("managerSurname");
manager.setRole(PartyRole.MANAGER);
onboardingData.setUsers(List.of(manager));
when(onboardingMsConnector.getOnboardingWithUserInfo(onboardingId)).thenReturn(onboardingData);


ManagerVerification managerVerification = new ManagerVerification();
managerVerification.setVerified(true);
when(pgManagerVerifierMock.doVerify(userTaxCode, "institutionTaxCode")).thenReturn(managerVerification);

// when
User result = userService.getManagerInfo(onboardingId, userTaxCode);

// then
assertNotNull(result);
assertEquals("managerName", result.getName());
assertEquals("managerSurname", result.getSurname());
assertEquals(PartyRole.MANAGER, result.getRole());
verify(onboardingMsConnector, times(1)).getOnboardingWithUserInfo(onboardingId);
verify(pgManagerVerifierMock, times(1)).doVerify(userTaxCode, "institutionTaxCode");
}

@Test
void getManagerInfo_shouldReturnManagerInfoWhenGivenUserIsAlreadyAdmin() {
// given
String onboardingId = "onboardingId";
String userTaxCode = "userTaxCode";
OnboardingData onboardingData = TestUtils.mockInstance(new OnboardingData());
onboardingData.getInstitutionUpdate().setTaxCode("institutionTaxCode");
User manager = TestUtils.mockInstance(new User());
manager.setTaxCode(userTaxCode);
manager.setName("managerName");
manager.setSurname("managerSurname");
manager.setRole(PartyRole.MANAGER);
onboardingData.setUsers(List.of(manager));
when(onboardingMsConnector.getOnboardingWithUserInfo(onboardingId)).thenReturn(onboardingData);

// when
User result = userService.getManagerInfo(onboardingId, userTaxCode);

// then
assertNotNull(result);
assertEquals("managerName", result.getName());
assertEquals("managerSurname", result.getSurname());
assertEquals(PartyRole.MANAGER, result.getRole());
verify(onboardingMsConnector, times(1)).getOnboardingWithUserInfo(onboardingId);
verify(pgManagerVerifierMock, times(0)).doVerify(userTaxCode, "institutionTaxCode");
}

@Test
void getManagerInfo_shouldThrowResourceNotFoundExceptionWhenOnboardingNotFound() {
// given
String onboardingId = "onboardingId";
String userTaxCode = "userTaxCode";
when(onboardingMsConnector.getOnboardingWithUserInfo(onboardingId)).thenThrow(new ResourceNotFoundException("Onboarding not found"));

// when
Executable executable = () -> userService.getManagerInfo(onboardingId, userTaxCode);

// then
ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, executable);
assertEquals("Onboarding not found", exception.getMessage());
verify(onboardingMsConnector, times(1)).getOnboardingWithUserInfo(onboardingId);
verifyNoInteractions(pgManagerVerifierMock);
}

@Test
void getManagerInfo_shouldThrowOnboardingNotAllowedExceptionWhenUserIsNotManager() {
// given
String onboardingId = "onboardingId";
String userTaxCode = "userTaxCode";
OnboardingData onboardingData = TestUtils.mockInstance(new OnboardingData());
onboardingData.getInstitutionUpdate().setTaxCode("institutionTaxCode");

User user = TestUtils.mockInstance(new User());
user.setRole(PartyRole.MANAGER);
onboardingData.setUsers(List.of(user));

when(onboardingMsConnector.getOnboardingWithUserInfo(onboardingId)).thenReturn(onboardingData);

ManagerVerification managerVerification = new ManagerVerification();
managerVerification.setVerified(false);
when(pgManagerVerifierMock.doVerify(userTaxCode, "institutionTaxCode")).thenReturn(managerVerification);

// when
Executable executable = () -> userService.getManagerInfo(onboardingId, userTaxCode);

// then
OnboardingNotAllowedException exception = assertThrows(OnboardingNotAllowedException.class, executable);
assertEquals("User is not an admin of the institution", exception.getMessage());
verify(onboardingMsConnector, times(1)).getOnboardingWithUserInfo(onboardingId);
verify(pgManagerVerifierMock, times(1)).doVerify(userTaxCode, "institutionTaxCode");
}

@Test
void getManagerInfo_shouldThrowResourceNotFoundExceptionWhenManagerNotFound() {
// given
String onboardingId = "onboardingId";
String userTaxCode = "userTaxCode";
OnboardingData onboardingData = TestUtils.mockInstance(new OnboardingData());
onboardingData.getInstitutionUpdate().setTaxCode("institutionTaxCode");
User user = TestUtils.mockInstance(new User());
user.setRole(PartyRole.OPERATOR);
onboardingData.setUsers(List.of(user));

when(onboardingMsConnector.getOnboardingWithUserInfo(onboardingId)).thenReturn(onboardingData);

// when
Executable executable = () -> userService.getManagerInfo(onboardingId, userTaxCode);

// then
ResourceNotFoundException exception = assertThrows(ResourceNotFoundException.class, executable);
assertEquals("Manager not found", exception.getMessage());
verify(onboardingMsConnector, times(1)).getOnboardingWithUserInfo(onboardingId);
}
}
Loading

0 comments on commit 162c11a

Please sign in to comment.