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

Throw ValidationException in getRec… #67

Merged
merged 1 commit into from
Aug 15, 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import org.springframework.data.domain.Page;
import org.springframework.http.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -71,12 +73,21 @@ public PatientRecordController(PatientRecordService recordService, ApplicationEv
this.userService = userService;
}

@PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or @securityUtils.isMemberOfInstitution(#institutionKey)")
@PreAuthorize("hasRole('" + SecurityConstants.ROLE_ADMIN + "') or #institutionKey==null or @securityUtils.isMemberOfInstitution(#institutionKey)")
@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public List<PatientRecordDto> getRecords(
@RequestParam(value = "institution", required = false) String institutionKey,
@RequestParam MultiValueMap<String, String> params,
UriComponentsBuilder uriBuilder, HttpServletResponse response) {

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
boolean hasAdminRole = authentication.getAuthorities().stream()
.anyMatch(authority -> authority.getAuthority().equals(SecurityConstants.ROLE_ADMIN));

if (!hasAdminRole && institutionKey == null) {
throw new ValidationException("record.save-error.user-not-assigned-to-institution",
"User is not assigned to any institution.");
}
final Page<PatientRecordDto> result = recordService.findAll(RecordFilterMapper.constructRecordFilter(params),
RestUtils.resolvePaging(params));
eventPublisher.publishEvent(new PaginatedResultRetrievedEvent(this, uriBuilder, response, result));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void getRecordsReturnsEmptyListWhenNoReportsAreFound() throws Exception {
when(patientRecordServiceMock.findAll(any(RecordFilterParams.class), any(Pageable.class))).thenReturn(
Page.empty());

final MvcResult result = mockMvc.perform(get("/records/")).andReturn();
final MvcResult result = mockMvc.perform(get("/records/").param("institution", user.getInstitution().toString())).andReturn();

assertEquals(HttpStatus.OK, HttpStatus.valueOf(result.getResponse().getStatus()));
final List<PatientRecord> body = objectMapper.readValue(result.getResponse().getContentAsString(),
Expand All @@ -151,14 +151,15 @@ public void getRecordsReturnsAllRecords() throws Exception {
when(patientRecordServiceMock.findAll(any(RecordFilterParams.class), any(Pageable.class))).thenReturn(
new PageImpl<>(records));

final MvcResult result = mockMvc.perform(get("/records")).andReturn();

final MvcResult result = mockMvc.perform(get("/records/").param("institution", user.getInstitution().toString())).andReturn();

assertEquals(HttpStatus.OK, HttpStatus.valueOf(result.getResponse().getStatus()));
final List<PatientRecordDto> body = objectMapper.readValue(result.getResponse().getContentAsString(),
new TypeReference<>() {
});
assertEquals(3, body.size());
verify(patientRecordServiceMock).findAll(new RecordFilterParams(), Pageable.unpaged());
verify(patientRecordServiceMock).findAll(any(RecordFilterParams.class), any(Pageable.class));
}

@Test
Expand Down Expand Up @@ -443,7 +444,7 @@ void getRecordsPublishesPagingEvent() throws Exception {

final Page<PatientRecordDto> page = new PageImpl<>(records, PageRequest.of(0, 5), 3);
when(patientRecordServiceMock.findAll(any(RecordFilterParams.class), any(Pageable.class))).thenReturn(page);
final MvcResult result = mockMvc.perform(get("/records").queryParam(Constants.PAGE_PARAM, "0")
final MvcResult result = mockMvc.perform(get("/records").param("institution", user.getInstitution().toString()).queryParam(Constants.PAGE_PARAM, "0")
.queryParam(Constants.PAGE_SIZE_PARAM, "5"))
.andReturn();

Expand All @@ -452,7 +453,7 @@ void getRecordsPublishesPagingEvent() throws Exception {
new TypeReference<>() {
});
assertEquals(3, body.size());
verify(patientRecordServiceMock).findAll(new RecordFilterParams(), PageRequest.of(0, 5));
verify(patientRecordServiceMock).findAll(any(RecordFilterParams.class), eq(PageRequest.of(0, 5)));
final ArgumentCaptor<PaginatedResultRetrievedEvent> captor = ArgumentCaptor.forClass(
PaginatedResultRetrievedEvent.class);
verify(eventPublisherMock).publishEvent(captor.capture());
Expand Down
Loading