@Test public void indexUserAuthority() throws Exception { // Arrange int size = randomSmallInteger(); List<User> usersWithoutCurrent = list(User.class, size); List<User> users = list(User.class, size); when(userService.findUsersExceptCurrentUser()).thenReturn(usersWithoutCurrent); when(userService.findAll()).thenReturn(users); // Act + Assert mvc.perform(get("/index") .with(user(randomString()).password(randomString()).authorities(USER))) .andExpect(status().isOk()) .andExpect(view().name(Pages.INDEX)) .andExpect(model().attribute("users", equalTo(users))) .andExpect(model().attribute("usersWithoutCurrent", equalTo(usersWithoutCurrent))); // Assert verify(userService).findAll(); verify(userService).findUsersExceptCurrentUser(); }
@Test public void findAllAdminAuthority() throws Exception { // Arrange int size = RandomUtils.randomSmallInteger(); List<User> users = InitializationUtils.list(User.class, size); when(userService.findAll()).thenReturn(users); // Act + Assert mvc.perform(get("/users") .with(user(randomString()).password(randomString()).authorities(ADMIN)) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) .andExpect(jsonPath("$", hasSize(size))); // Assert verify(userService).findAll(); } }
@Test public void indexAdminAuthority() throws Exception { // Arrange int size = randomSmallInteger(); List<User> usersWithoutCurrent = list(User.class, size); List<User> users = list(User.class, size); when(userService.findUsersExceptCurrentUser()).thenReturn(usersWithoutCurrent); when(userService.findAll()).thenReturn(users); // Act + Assert mvc.perform(get("/index") .with(user(randomString()).password(randomString()).authorities(ADMIN))) .andExpect(status().isOk()) .andExpect(view().name(Pages.INDEX)) .andExpect(model().attribute("users", equalTo(users))) .andExpect(model().attribute("usersWithoutCurrent", equalTo(usersWithoutCurrent))) .andExpect(model().attribute("user", hasProperty("username", isEmptyOrNullString()))) .andExpect(model().attribute("user", hasProperty("password", isEmptyOrNullString()))) .andExpect(model().attribute("user", hasProperty("role", isEmptyOrNullString()))); // Assert verify(userService).findAll(); verify(userService).findUsersExceptCurrentUser(); }
@Test public void saveAdminAuthority() throws Exception { // Arrange User user = User.builder().build(); when(userService.save(user)).thenReturn(user); mvc.perform(post("/addUser") .with(csrf()) .with(user(randomString()).password(randomString()).authorities(ADMIN)) .contentType(MediaType.APPLICATION_FORM_URLENCODED)) .andExpect(status().isFound()) .andExpect(view().name("redirect:/index")) .andExpect(model().attribute("user", equalTo(user))) .andExpect(model().attribute("user", hasProperty("username", isEmptyOrNullString()))) .andExpect(model().attribute("user", hasProperty("password", isEmptyOrNullString()))) .andExpect(model().attribute("user", hasProperty("role", isEmptyOrNullString()))); // Assert verify(userService).save(any(User.class)); } }
@Test public void saveUserAuthority() throws Exception { // Act + Assert mvc.perform(post("/addUser") .with(csrf()) .with(user(randomString()).password(randomString()).authorities(USER))) .andExpect(status().isForbidden()); }
@Test public void findByIdAdminRole() throws Exception { Integer eventId = randomInteger(); // Act + Assert mvc.perform(get("/api/event/" + eventId) .with(user(ADMIN_USERNAME).password(ADMIN_PASSWORD).roles(ADMIN_ROLE))) .andExpect(status().isOk()); }
@Test public void findAllUserAuthority() throws Exception { // Act + Assert mvc.perform(get("/users") .with(user(randomString()).password(randomString()).authorities(USER)) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isForbidden()); }
@Test public void findAllUserRole() throws Exception { // Act + Assert mvc.perform(get("/api/event") .with(user(USER_USERNAME).password(USER_PASSWORD).roles(USER_ROLE))) .andExpect(status().isOk()); }
@Test public void findByIdUserRole() throws Exception { Integer eventId = randomInteger(); // Act + Assert mvc.perform(get("/api/event/" + eventId) .with(user(USER_USERNAME).password(USER_PASSWORD).roles(USER_ROLE))) .andExpect(status().isForbidden()); } }
@Test public void findAllAdminRole() throws Exception { // Act + Assert mvc.perform(get("/api/event") .with(user(ADMIN_USERNAME).password(ADMIN_PASSWORD).roles(ADMIN_ROLE))) .andExpect(status().isOk()); }