Skip to content

Commit

Permalink
DT-1894: add resource tests to confirm that default value of ALL is u…
Browse files Browse the repository at this point in the history
…sed for getUsers and getStaffUsersForLocalAdministrator (ministryofjustice#914)
  • Loading branch information
simon-mitchell authored May 7, 2021
1 parent ea67c58 commit 84552dc
Showing 1 changed file with 38 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@
import org.springframework.http.HttpStatus;
import uk.gov.justice.hmpps.prison.api.model.AccessRole;
import uk.gov.justice.hmpps.prison.api.resource.impl.ResourceTest;
import uk.gov.justice.hmpps.prison.api.support.Order;
import uk.gov.justice.hmpps.prison.api.support.Page;
import uk.gov.justice.hmpps.prison.api.support.PageRequest;
import uk.gov.justice.hmpps.prison.api.support.Status;
import uk.gov.justice.hmpps.prison.repository.UserRepository;
import uk.gov.justice.hmpps.prison.service.filters.NameFilter;

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

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
Expand All @@ -22,6 +30,7 @@ public class UserResourceIntTest extends ResourceTest {
@MockBean
private UserRepository userRepository;


@Test
public void addAccessRole() {
final var requestEntity = createHttpEntityWithBearerAuthorisation("BOB", List.of("ROLE_MAINTAIN_ACCESS_ROLES"), null);
Expand Down Expand Up @@ -82,4 +91,33 @@ public void addAccessRoles_noprivileges() {

verifyNoInteractions(userRepository);
}

@Test
public void getUser_statusUsesDefaultValueAllWhenNonSupplied() {
final var requestEntity = createHttpEntityWithBearerAuthorisation(
"BOB",
List.of("ROLE_MAINTAIN_ACCESS_ROLES_ADMIN"),
Map.of());
final var pageRequest = new PageRequest(null, Order.ASC, 0L, 20L);
final var userDetails = new Page(List.of(), 0, pageRequest);
when(userRepository.findUsers(any(), any(), any(), any())).thenReturn(userDetails);
final var responseEntity = testRestTemplate.exchange("/api/users", HttpMethod.GET, requestEntity, String.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
verify(userRepository).findUsers(eq(null), any(NameFilter.class), eq(Status.ALL), any(PageRequest.class));
}

@Test
public void getStaffUsersForLocalAdministrator_statusUsesDefaultValueAllWhenNonSupplied() {
final var requestEntity = createHttpEntityWithBearerAuthorisation(
"BOB",
List.of("ROLE_MAINTAIN_ACCESS_ROLES_ADMIN"),
Map.of());
final var pageRequest = new PageRequest(null, Order.ASC, 0L, 20L);
final var userDetails = new Page(List.of(), 0, pageRequest);
when(userRepository.getUsersAsLocalAdministrator(any(),any(), any(), any(), any())).thenReturn(userDetails);
final var responseEntity = testRestTemplate.exchange("/api/users/local-administrator/available", HttpMethod.GET, requestEntity, String.class);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.OK);
verify(userRepository).getUsersAsLocalAdministrator(eq("BOB"),eq(null), any(NameFilter.class), eq(Status.ALL), any(PageRequest.class));
}

}

0 comments on commit 84552dc

Please sign in to comment.