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

[SELC-6181] feat: added API to retrieve manager info given onboardingId #384

Merged
merged 1 commit into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions app/src/main/resources/swagger/api-docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3079,6 +3079,97 @@
} ]
}
},
"/v1/users/onboarding/{onboardingId}/manager" : {
"get" : {
"tags" : [ "user" ],
"summary" : "getManagerInfo",
"description" : "Returns true if current manager and previous one are the same",
"operationId" : "checkManager_1",
"parameters" : [ {
"name" : "name",
"in" : "query",
"required" : false,
"style" : "form",
"schema" : {
"type" : "string"
}
}, {
"name" : "onboardingId",
"in" : "path",
"description" : "onboardingId",
"required" : true,
"style" : "simple",
"schema" : {
"type" : "string"
}
} ],
"responses" : {
"200" : {
"description" : "OK",
"content" : {
"application/json" : {
"schema" : {
"$ref" : "#/components/schemas/ManagerInfoResponse"
}
}
}
},
"400" : {
"description" : "Bad Request",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"401" : {
"description" : "Unauthorized",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"403" : {
"description" : "Forbidden",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"404" : {
"description" : "Not Found",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
},
"500" : {
"description" : "Internal Server Error",
"content" : {
"application/problem+json" : {
"schema" : {
"$ref" : "#/components/schemas/Problem"
}
}
}
}
},
"security" : [ {
"bearerAuth" : [ "global" ]
} ]
}
},
"/v1/users/validate" : {
"post" : {
"tags" : [ "user" ],
Expand Down Expand Up @@ -4153,6 +4244,21 @@
}
}
},
"ManagerInfoResponse" : {
"title" : "ManagerInfoResponse",
"required" : [ "name", "surname" ],
"type" : "object",
"properties" : {
"name" : {
"type" : "string",
"description" : "User's name"
},
"surname" : {
"type" : "string",
"description" : "User's surname"
}
}
},
"MatchInfoResultResource" : {
"title" : "MatchInfoResultResource",
"type" : "object",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import it.pagopa.selfcare.commons.base.logging.LogUtils;
import it.pagopa.selfcare.commons.base.security.SelfCareUser;
import it.pagopa.selfcare.commons.web.model.Problem;
import it.pagopa.selfcare.commons.web.security.JwtAuthenticationToken;
import it.pagopa.selfcare.onboarding.core.UserService;
import it.pagopa.selfcare.onboarding.web.model.CheckManagerResponse;
import it.pagopa.selfcare.onboarding.web.model.ManagerInfoResponse;
import it.pagopa.selfcare.onboarding.web.model.OnboardingUserDto;
import it.pagopa.selfcare.onboarding.web.model.UserDataValidationDto;
import it.pagopa.selfcare.onboarding.web.model.mapper.OnboardingResourceMapper;
import it.pagopa.selfcare.onboarding.web.model.mapper.UserMapper;
import it.pagopa.selfcare.onboarding.web.model.mapper.UserResourceMapper;
import lombok.extern.slf4j.Slf4j;
import org.owasp.encoder.Encode;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -22,6 +26,8 @@

import javax.validation.Valid;

import java.security.Principal;

import static org.springframework.http.MediaType.APPLICATION_PROBLEM_JSON_VALUE;


Expand All @@ -33,12 +39,15 @@ public class UserController {

private final UserService userService;
private final OnboardingResourceMapper onboardingResourceMapper;
private final UserResourceMapper userResourceMapper;

@Autowired
public UserController(UserService userService,
OnboardingResourceMapper onboardingResourceMapper) {
OnboardingResourceMapper onboardingResourceMapper,
UserResourceMapper userResourceMapper) {
this.userService = userService;
this.onboardingResourceMapper = onboardingResourceMapper;
this.userResourceMapper = userResourceMapper;
}

@PostMapping("/validate")
Expand Down Expand Up @@ -106,4 +115,23 @@ public CheckManagerResponse checkManager(@RequestBody @Valid OnboardingUserDto r
return new CheckManagerResponse(checkManager);
}

@ApiResponse(responseCode = "403",
description = "Forbidden",
content = {
@Content(mediaType = APPLICATION_PROBLEM_JSON_VALUE,
schema = @Schema(implementation = Problem.class))
})
@GetMapping(value = "/onboarding/{onboardingId}/manager")
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "", notes = "${swagger.onboarding.users.api.check-manager}", nickname = "checkManager")
public ManagerInfoResponse getManagerInfo(@PathVariable("onboardingId") String onboardingId, Principal principal) {
log.trace("getManagerInfo start");
JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) principal;
SelfCareUser selfCareUser = (SelfCareUser) jwtAuthenticationToken.getPrincipal();

ManagerInfoResponse managerInfoResponse = userResourceMapper.toManagerInfoResponse(userService.getManagerInfo(onboardingId, selfCareUser.getFiscalCode()));
log.trace("getManagerInfo end");
return managerInfoResponse;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package it.pagopa.selfcare.onboarding.web.model;

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
public class ManagerInfoResponse {
@ApiModelProperty(value = "${swagger.onboarding.user.model.name}", required = true)
private String name;
@ApiModelProperty(value = "${swagger.onboarding.user.model.surname}", required = true)
private String surname;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package it.pagopa.selfcare.onboarding.web.model.mapper;

import it.pagopa.selfcare.onboarding.connector.model.onboarding.User;
import it.pagopa.selfcare.onboarding.web.model.ManagerInfoResponse;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface UserResourceMapper {
ManagerInfoResponse toManagerInfoResponse(User user);
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package it.pagopa.selfcare.onboarding.web.controller;

import it.pagopa.selfcare.commons.base.security.SelfCareUser;
import it.pagopa.selfcare.commons.web.security.JwtAuthenticationToken;
import it.pagopa.selfcare.onboarding.connector.model.onboarding.OnboardingData;
import it.pagopa.selfcare.onboarding.core.UserService;
import it.pagopa.selfcare.onboarding.core.exception.InvalidUserFieldsException;
import it.pagopa.selfcare.onboarding.web.config.WebTestConfig;
import it.pagopa.selfcare.onboarding.web.handler.OnboardingExceptionHandler;
import it.pagopa.selfcare.onboarding.web.model.OnboardingUserDto;
import it.pagopa.selfcare.onboarding.web.model.mapper.OnboardingResourceMapperImpl;
import it.pagopa.selfcare.onboarding.web.model.mapper.UserResourceMapperImpl;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -20,6 +23,8 @@
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import java.security.Principal;

import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.not;
import static org.mockito.ArgumentMatchers.any;
Expand All @@ -33,7 +38,8 @@
UserController.class,
OnboardingResourceMapperImpl.class,
WebTestConfig.class,
OnboardingExceptionHandler.class
OnboardingExceptionHandler.class,
UserResourceMapperImpl.class
})
class UserControllerTest {

Expand Down Expand Up @@ -136,4 +142,28 @@ void checkManager(@Value("classpath:stubs/onboardingUsers.json") Resource onboar
verifyNoMoreInteractions(userServiceMock);
}

/**
* Method under test: {@link UserController#getManagerInfo(String, Principal)}
*/
@Test
void getManagerInfo() throws Exception {
//given
JwtAuthenticationToken mockPrincipal = Mockito.mock(JwtAuthenticationToken.class);
SelfCareUser selfCareUser = SelfCareUser.builder("example")
.fiscalCode("fiscalCode")
.build();
Mockito.when(mockPrincipal.getPrincipal()).thenReturn(selfCareUser);

// when
mvc.perform(MockMvcRequestBuilders
.get(BASE_URL + "/onboarding/onboarding-test-id/manager")
.principal(mockPrincipal)
.contentType(APPLICATION_JSON_VALUE)
.accept(APPLICATION_JSON_VALUE))
.andExpect(status().isOk());
// then
verify(userServiceMock, times(1))
.getManagerInfo(any(), any());
verifyNoMoreInteractions(userServiceMock);
}
}
Loading