Skip to content

Commit

Permalink
P4PU fixed IamUserInfoDTO2UserInfoImplTest
Browse files Browse the repository at this point in the history
  • Loading branch information
oleksiybozhykntt committed Aug 8, 2024
1 parent d13a5cb commit d41c262
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ class OAuth2LoginConfigTest {
@MockBean
private PaymentNoticesService paymentNoticesServiceMock;
@Mock
private JwtAuthenticationFilter jwtAuthenticationFilter;
private JwtAuthenticationFilter jwtAuthenticationFilterMock;
@MockBean
private AuthService authService;
private AuthService authServiceMock;
@Autowired
private MockMvc mockMvc;
@Autowired
private WebApplicationContext context;
@MockBean
private TokenStoreService tokenStoreService;
private TokenStoreService tokenStoreServiceMock;
@Test
void givenURLWithoutCodeAndStateWhenWithoutAccessTokenThenRedirectToLogin() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/token/oneidentity"))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package it.gov.pagopa.arc.dto.mapper;

import it.gov.pagopa.arc.dto.mapper.pullpayment.PullPaymentNoticeDTO2PaymentNoticeDTO;
import it.gov.pagopa.arc.fakers.auth.IamUserInfoDTOFaker;
import it.gov.pagopa.arc.model.generated.UserInfo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.boot.test.mock.mockito.MockBean;

class IamUserInfoDTO2UserInfoImplTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ public class IamUserInfoDTOFaker {

public static IamUserInfoDTO mockInstance(){
Map<String, Object> attributes = Map.of(
"sub", "123456",
"fiscalNumber", "789012",
"familyName", "Polo",
"name", "Marco",
"email", "marco.polo@example.com",
"sub", "user_id",
"fiscalNumber", "FISCAL-CODE789456",
"familyName", "familyName",
"name", "name",
"email", "sample@sample.com",
"iss", "issuer"
);
return IamUserInfoDTO.map2IamUserInfoDTO(attributes);
Expand Down
47 changes: 28 additions & 19 deletions src/test/java/it/gov/pagopa/arc/service/AuthServiceImplTest.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,57 @@
package it.gov.pagopa.arc.service;

import it.gov.pagopa.arc.dto.IamUserInfoDTO;
import it.gov.pagopa.arc.dto.mapper.IamUserInfoDTO2UserInfo;
import it.gov.pagopa.arc.fakers.auth.IamUserInfoDTOFaker;
import org.junit.jupiter.api.AfterEach;
import it.gov.pagopa.arc.fakers.auth.UserInfoDTOFaker;
import it.gov.pagopa.arc.model.generated.UserInfo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mapstruct.factory.Mappers;
import org.mockito.InjectMocks;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;

@ExtendWith(MockitoExtension.class)
class AuthServiceImplTest {

@InjectMocks
IamUserInfoDTO2UserInfo mapper = Mappers.getMapper(IamUserInfoDTO2UserInfo.class);

@Mock
AuthServiceImpl authService;
IamUserInfoDTO2UserInfo mapperMock;
@Autowired
private AuthService authService;

private final IamUserInfoDTO iamUserInfoDTO = IamUserInfoDTOFaker.mockInstance();

@BeforeEach
void setup(){
authService = new AuthServiceImpl(mapper);
//given
void setUp() {
authService = new AuthServiceImpl(mapperMock);
SecurityContextHolder.clearContext();
UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
IamUserInfoDTOFaker.mockInstance(), null, null);
iamUserInfoDTO, null, null);
authentication.setDetails(new WebAuthenticationDetails(new MockHttpServletRequest()));
SecurityContextHolder.getContext().setAuthentication(authentication);

}

@AfterEach
void clearContext() {
SecurityContextHolder.clearContext();
}

@Test
void givenValidAuthenticationSessionThenRetrieveUserInfo() {
//given
UserInfo userInfo = UserInfoDTOFaker.mockInstance();
Mockito.when(mapperMock.mapIamUserToUserInfo(iamUserInfoDTO)).thenReturn(userInfo);
//when
UserInfo user = authService.getUserLoginInfo();
//then
Assertions.assertNotNull(authService.getUserLoginInfo());
Assertions.assertNotNull(user);

Assertions.assertEquals(user.getUserId(),iamUserInfoDTO.getUserId());
Assertions.assertEquals(user.getName(),iamUserInfoDTO.getName());
Assertions.assertEquals(user.getFamilyName(),iamUserInfoDTO.getFamilyName());
Assertions.assertEquals(user.getEmail(),iamUserInfoDTO.getEmail());
Assertions.assertEquals(user.getFiscalCode(),iamUserInfoDTO.getFiscalCode());
}

}

0 comments on commit d41c262

Please sign in to comment.