Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Change brokerId to non required #124

Merged
merged 18 commits into from
Jan 10, 2025
Merged
2 changes: 0 additions & 2 deletions openapi/p4pa-auth.openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -551,8 +551,6 @@ components:
- email
- issuer
- organizations
- brokerId
- brokerFiscalCode
- canManageUsers
properties:
userId:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import it.gov.pagopa.payhub.auth.connector.client.OrganizationSearchClient;
import it.gov.pagopa.payhub.auth.dto.IamUserInfoDTO;
import it.gov.pagopa.payhub.auth.dto.IamUserOrganizationRolesDTO;
import it.gov.pagopa.payhub.auth.exception.custom.InvalidAccessTokenException;
import it.gov.pagopa.payhub.auth.exception.custom.UserNotFoundException;
import it.gov.pagopa.payhub.auth.model.Operator;
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.auth.repository.OperatorsRepository;
import it.gov.pagopa.payhub.auth.repository.UsersRepository;
import it.gov.pagopa.payhub.auth.service.TokenStoreService;
import it.gov.pagopa.payhub.auth.utils.Constants;
import it.gov.pagopa.payhub.dto.generated.UserInfo;
import it.gov.pagopa.payhub.dto.generated.UserOrganizationRoles;
Expand All @@ -29,27 +31,30 @@ public class IamUserInfoDTO2UserInfoMapper {
private final OperatorsRepository operatorsRepository;
private final OrganizationSearchClient organizationSearchClient;
private final boolean organizationAccessMode;
private final TokenStoreService tokenStoreService;

public IamUserInfoDTO2UserInfoMapper(@Value("${app.enable-access-organization-mode}") boolean organizationAccessMode,
UsersRepository usersRepository,
OperatorsRepository operatorsRepository,
OrganizationSearchClient organizationSearchClient) {
OrganizationSearchClient organizationSearchClient,
TokenStoreService tokenStoreService) {
this.usersRepository = usersRepository;
this.operatorsRepository = operatorsRepository;
this.organizationSearchClient = organizationSearchClient;
this.organizationAccessMode = organizationAccessMode;
this.tokenStoreService = tokenStoreService;
}

public UserInfo apply(IamUserInfoDTO iamUserInfoDTO, String accessToken) {
if (iamUserInfoDTO.isSystemUser()) {
return systemUserMapper(iamUserInfoDTO);
return systemUserMapper(iamUserInfoDTO, accessToken);
}
return userInfoMapper(iamUserInfoDTO, accessToken);
}

private UserInfo systemUserMapper(IamUserInfoDTO iamUserInfoDTO) {
private UserInfo systemUserMapper(IamUserInfoDTO iamUserInfoDTO, String accessToken) {
String organizationIpaCode = iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode();
return UserInfo.builder()
UserInfo userInfo = UserInfo.builder()
.userId(iamUserInfoDTO.getUserId())
.mappedExternalUserId(buildSystemMappedExternalUserId(organizationIpaCode))
.fiscalCode(iamUserInfoDTO.getFiscalCode())
Expand All @@ -61,6 +66,8 @@ private UserInfo systemUserMapper(IamUserInfoDTO iamUserInfoDTO) {
.roles(Collections.singletonList(Constants.ROLE_ADMIN))
.build()))
.build();
setBrokerInfo(userInfo, accessToken);
return userInfo;
}

public static String buildSystemMappedExternalUserId(String organizationIpaCode) {
Expand All @@ -71,8 +78,6 @@ private UserInfo userInfoMapper(IamUserInfoDTO iamUserInfoDTO, String accessToke
User user = usersRepository.findById(iamUserInfoDTO.getInnerUserId()).orElseThrow(() -> new UserNotFoundException("Cannot found user having inner id:" + iamUserInfoDTO.getInnerUserId()));
List<Operator> userRoles = operatorsRepository.findAllByUserId(iamUserInfoDTO.getInnerUserId());

Broker brokerInfo = getSessionBroker(iamUserInfoDTO, userRoles, accessToken);

UserInfo userInfo = UserInfo.builder()
.userId(user.getUserId())
.mappedExternalUserId(user.getMappedExternalUserId())
Expand All @@ -93,10 +98,7 @@ private UserInfo userInfoMapper(IamUserInfoDTO iamUserInfoDTO, String accessToke
if (iamUserInfoDTO.getOrganizationAccess() != null) {
userInfo.setOrganizationAccess(iamUserInfoDTO.getOrganizationAccess().getOrganizationIpaCode());
}
if (brokerInfo != null) {
userInfo.setBrokerId(brokerInfo.getBrokerId());
userInfo.setBrokerFiscalCode(brokerInfo.getBrokerFiscalCode());
}
setBrokerInfo(userInfo, accessToken);
userInfo.setCanManageUsers(!organizationAccessMode);
return userInfo;
}
Expand All @@ -115,4 +117,21 @@ private Broker getSessionBroker(IamUserInfoDTO iamUserInfoDTO, List<Operator> us
return null;
}

private void setBrokerInfo(UserInfo userInfo, String accessToken) {
IamUserInfoDTO iamUserInfo = tokenStoreService.load(accessToken);
if (iamUserInfo == null) {
throw new InvalidAccessTokenException("AccessToken not found");
}
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved

List<Operator> userRoles = operatorsRepository.findAllByUserId(iamUserInfo.getInnerUserId());
Broker brokerInfo = getSessionBroker(iamUserInfo, userRoles, accessToken);
antonioT90 marked this conversation as resolved.
Show resolved Hide resolved

if (brokerInfo != null) {
userInfo.setBrokerId(brokerInfo.getBrokerId());
userInfo.setBrokerFiscalCode(brokerInfo.getBrokerFiscalCode());
} else {
throw new IllegalStateException("Broker information not found for the user.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import it.gov.pagopa.payhub.auth.model.User;
import it.gov.pagopa.payhub.auth.repository.OperatorsRepository;
import it.gov.pagopa.payhub.auth.repository.UsersRepository;
import it.gov.pagopa.payhub.auth.service.TokenStoreService;
import it.gov.pagopa.payhub.auth.utils.Constants;
import it.gov.pagopa.payhub.dto.generated.UserInfo;
import it.gov.pagopa.payhub.dto.generated.UserOrganizationRoles;
import it.gov.pagopa.pu.p4pa_organization.dto.generated.Broker;
import it.gov.pagopa.pu.p4pa_organization.dto.generated.Organization;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
Expand All @@ -37,13 +39,16 @@ class IamUserInfoDTO2UserInfoMapperTest {
@Mock
private OrganizationSearchClient organizationSearchClientMock;

@Mock
private TokenStoreService tokenStoreService;

private IamUserInfoDTO2UserInfoMapper mapper;

private final boolean organizationAccessMode = false;

@BeforeEach
void init() {
mapper = new IamUserInfoDTO2UserInfoMapper(organizationAccessMode, usersRepositoryMock, operatorsRepositoryMock, organizationSearchClientMock);
mapper = new IamUserInfoDTO2UserInfoMapper(organizationAccessMode, usersRepositoryMock, operatorsRepositoryMock, organizationSearchClientMock, tokenStoreService);
}

@AfterEach
Expand All @@ -54,22 +59,21 @@ void verifyNotMoreInteractions() {
@Test
void givenNotUserWhenApplyThenUserNotFoundException() {
String accessToken = "sampleAccessToken";
// Given

IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder()
.userId("EXTERNALUSERID")
.innerUserId("INNERUSERID")
.build();

Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.empty());

// When, Then
Assertions.assertThrows(UserNotFoundException.class, () -> mapper.apply(iamUserInfo, accessToken));
}

@Test
void givenCompleteDataWhenApplyThenOk() {
String accessToken = "sampleAccessToken";
// Given

IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder()
.userId("EXTERNALUSERID")
.innerUserId("INNERUSERID")
Expand Down Expand Up @@ -109,25 +113,34 @@ void givenCompleteDataWhenApplyThenOk() {
.roles(List.of("ROLE"))
.email("EMAIL")
.build()))
.brokerId(1L)
.canManageUsers(!organizationAccessMode)
.build();

Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user));
Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles);

Organization mockOrganization = new Organization();
mockOrganization.setBrokerId(1L);
Mockito.when(organizationSearchClientMock.getOrganizationByIpaCode(Mockito.eq("ORG"), Mockito.anyString()))
.thenReturn(new Organization());
.thenReturn(mockOrganization);

Broker mockBroker = new Broker();
mockBroker.setBrokerId(1L);
Mockito.when(organizationSearchClientMock.getBrokerById(Mockito.anyLong(), Mockito.anyString()))
.thenReturn(mockBroker);

Mockito.when(tokenStoreService.load("sampleAccessToken")).thenReturn(iamUserInfo);

// When
UserInfo result = mapper.apply(iamUserInfo, accessToken);

// Then
Assertions.assertEquals(expected, result);
}

@Test
void givenNotOperatorsWhenApplyThenOk() {
String accessToken = "sampleAccessToken";
// Given

IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder()
.userId("EXTERNALUSERID")
.innerUserId("INNERUSERID")
Expand Down Expand Up @@ -155,23 +168,34 @@ void givenNotOperatorsWhenApplyThenOk() {
.issuer("ISSUER")
.organizationAccess("ORG")
.organizations(Collections.emptyList())
.brokerId(1L)
.canManageUsers(!organizationAccessMode)
.build();

Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user));
Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(Collections.emptyList());

// When
Organization mockOrganization = new Organization();
mockOrganization.setBrokerId(1L);
Mockito.when(organizationSearchClientMock.getOrganizationByIpaCode(Mockito.eq("ORG"), Mockito.anyString()))
.thenReturn(mockOrganization);

Broker mockBroker = new Broker();
mockBroker.setBrokerId(1L);
Mockito.when(organizationSearchClientMock.getBrokerById(Mockito.anyLong(), Mockito.anyString()))
.thenReturn(mockBroker);

Mockito.when(tokenStoreService.load("sampleAccessToken")).thenReturn(iamUserInfo);

UserInfo result = mapper.apply(iamUserInfo, accessToken);

// Then
Assertions.assertEquals(expected, result);
}

@Test
void givenNoOrganizationAccessWhenApplyThenOk() {
String accessToken = "sampleAccessToken";
// Given

IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder()
.userId("EXTERNALUSERID")
.innerUserId("INNERUSERID")
Expand Down Expand Up @@ -206,23 +230,34 @@ void givenNoOrganizationAccessWhenApplyThenOk() {
.roles(List.of("ROLE"))
.email("EMAIL")
.build()))
.brokerId(1L)
.canManageUsers(!organizationAccessMode)
.build();

Mockito.when(usersRepositoryMock.findById(iamUserInfo.getInnerUserId())).thenReturn(Optional.of(user));
Mockito.when(operatorsRepositoryMock.findAllByUserId(user.getUserId())).thenReturn(organizationRoles);

// When
Organization mockOrganization = new Organization();
mockOrganization.setBrokerId(1L);
Mockito.when(organizationSearchClientMock.getOrganizationByIpaCode(Mockito.eq("ORG"), Mockito.anyString()))
.thenReturn(mockOrganization);

Broker mockBroker = new Broker();
mockBroker.setBrokerId(1L);
Mockito.when(organizationSearchClientMock.getBrokerById(Mockito.anyLong(), Mockito.anyString()))
.thenReturn(mockBroker);

Mockito.when(tokenStoreService.load("sampleAccessToken")).thenReturn(iamUserInfo);

UserInfo result = mapper.apply(iamUserInfo, accessToken);

// Then
Assertions.assertEquals(expected, result);
}

@Test
void givenSystemUserWhenApplyThenOk() {
String accessToken = "sampleAccessToken";
// Given

IamUserInfoDTO iamUserInfo = IamUserInfoDTO.builder()
.systemUser(Boolean.TRUE)
.userId("EXTERNALUSERID")
Expand All @@ -248,13 +283,26 @@ void givenSystemUserWhenApplyThenOk() {
.organizationIpaCode("IPA_CODE")
.roles(List.of(Constants.ROLE_ADMIN))
.build()))
.brokerId(1L)
.build();

// When
Organization mockOrganization = new Organization();
mockOrganization.setBrokerId(1L);
Mockito.when(organizationSearchClientMock.getOrganizationByIpaCode(Mockito.eq("IPA_CODE"), Mockito.anyString()))
.thenReturn(mockOrganization);

Broker mockBroker = new Broker();
mockBroker.setBrokerId(1L);
Mockito.when(organizationSearchClientMock.getBrokerById(Mockito.anyLong(), Mockito.anyString()))
.thenReturn(mockBroker);

Mockito.when(tokenStoreService.load("sampleAccessToken")).thenReturn(iamUserInfo);
Mockito.when(operatorsRepositoryMock.findAllByUserId(Mockito.anyString())).thenReturn(Collections.emptyList());

UserInfo result = mapper.apply(iamUserInfo, accessToken);

// Then
Assertions.assertEquals(expected, result);
}


}
Loading