@Test public void configureWhenRequestCacheProvidedAndClientAuthorizationRequiredExceptionThrownThenRequestCacheUsed() throws Exception { this.spring.register(OAuth2ClientConfig.class).autowire(); MvcResult mvcResult = this.mockMvc.perform(get("/resource1").with(user("user1"))) .andExpect(status().is3xxRedirection()) .andReturn(); assertThat(mvcResult.getResponse().getRedirectedUrl()).matches("https://provider.com/oauth2/authorize\\?" + "response_type=code&client_id=client-1&" + "scope=user&state=.{15,}&" + "redirect_uri=http://localhost/client-1"); verify(requestCache).saveRequest(any(HttpServletRequest.class), any(HttpServletResponse.class)); }
@Test // http@access-denied-page public void configureWhenAccessDeniedPageSetAndRequestForbiddenThenForwardedToAccessDeniedPage() throws Exception { this.spring.register(AccessDeniedPageConfig.class).autowire(); this.mockMvc.perform(get("/admin").with(user(PasswordEncodedUser.user()))) .andExpect(status().isForbidden()) .andExpect(forwardedUrl("/AccessDeniedPage")); }
@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 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 findAllAdminRole() throws Exception { // Act + Assert mvc.perform(get("/api/event") .with(user(ADMIN_USERNAME).password(ADMIN_PASSWORD).roles(ADMIN_ROLE))) .andExpect(status().isOk()); }
@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 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 createAd() throws Exception { Ad ad = ad(); String requestBody = saveRequestJsonString(ad); ResultActions resultActions = mockMvc.perform(MockMvcRequestBuilders .post("/ads") .accept(MediaTypes.HAL_JSON) .content(requestBody) .contentType(MediaType.APPLICATION_JSON) .with(user(userDetailsService.loadUserByUsername(Admin.HONTAREVA)))); final Ad createdBooking = findCreatedBooking(); resultActions.andExpect(status().isCreated()) .andExpect(header().string(HttpHeaders.LOCATION, "http://localhost/ads/" + createdBooking.getId())) .andExpect(jsonPath("$.type", is(ad.getType().name()))) .andExpect(jsonPath("$.amount", is(ad.getAmount().intValue()))) .andExpect(jsonPath("$.currency", is(ad.getCurrency().name()))) .andExpect(jsonPath("$.rate", is(ad.getRate().doubleValue()))) .andExpect(jsonPath("$.location.city", is(ad.getLocation().getCity()))) .andExpect(jsonPath("$.location.area", is(ad.getLocation().getArea()))) .andExpect(jsonPath("$.comment", is(ad.getComment()))); }
@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 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 saveUserAuthority() throws Exception { // Act + Assert mvc.perform(post("/addUser") .with(csrf()) .with(user(randomString()).password(randomString()).authorities(USER))) .andExpect(status().isForbidden()); }
.put("/ads/" + ad.getId() + "/publishing") .accept(MediaTypes.HAL_JSON) .with(user(userDetailsService.loadUserByUsername(Admin.HONTAREVA))));
@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)); } }
.content(requestBody) .contentType(MediaType.APPLICATION_JSON) .with(user(userDetailsService.loadUserByUsername(Admin.HONTAREVA))));
@Test public void t005_testCurationDelete() throws Exception { mvc.perform(delete(API_BASE + "/projects/1/documents/1/curation") .with(csrf().asHeader()) .with(user("admin").roles("ADMIN")) .param("projectId", "1") .param("documentId", "1")) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")); mvc.perform(get(API_BASE + "/projects/1/documents") .with(csrf().asHeader()) .with(user("admin").roles("ADMIN"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.body[0].id").value("1")) .andExpect(jsonPath("$.body[0].name").value("test.txt")) .andExpect(jsonPath("$.body[0].state").value("ANNOTATION-IN-PROGRESS")); }
.with(user("admin").roles("ADMIN"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .file("content", "This is a test.".getBytes("UTF-8")) .with(csrf().asHeader()) .with(user("admin").roles("ADMIN")) .param("name", "test.txt") .param("format", "text")) .with(user("admin").roles("ADMIN"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8"))
@Test public void t002_testDocumentCreate() throws Exception { mvc.perform(get(API_BASE + "/projects/1/documents") .with(csrf().asHeader()) .with(user("admin").roles("ADMIN"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.messages").isEmpty()); mvc.perform(multipart(API_BASE + "/projects/1/documents") .file("content", "This is a test.".getBytes("UTF-8")) .with(csrf().asHeader()) .with(user("admin").roles("ADMIN")) .param("name", "test.txt") .param("format", "text")) .andExpect(status().isCreated()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.body.id").value("1")) .andExpect(jsonPath("$.body.name").value("test.txt")); mvc.perform(get(API_BASE + "/projects/1/documents") .with(csrf().asHeader()) .with(user("admin").roles("ADMIN"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.body[0].id").value("1")) .andExpect(jsonPath("$.body[0].name").value("test.txt")) .andExpect(jsonPath("$.body[0].state").value("NEW")); }
@Test public void t001_testProjectCreate() throws Exception { mvc.perform(get(API_BASE + "/projects") .with(csrf().asHeader()) .with(user("admin").roles("ADMIN"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.messages").isEmpty()); mvc.perform(post(API_BASE + "/projects") .with(csrf().asHeader()) .with(user("admin").roles("ADMIN")) .contentType(MediaType.MULTIPART_FORM_DATA) .param("name", "project1")) .andExpect(status().isCreated()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.body.id").value("1")) .andExpect(jsonPath("$.body.name").value("project1")); mvc.perform(get(API_BASE + "/projects") .with(csrf().asHeader()) .with(user("admin").roles("ADMIN"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .andExpect(jsonPath("$.body[0].id").value("1")) .andExpect(jsonPath("$.body[0].name").value("project1")); }
.with(user("admin").roles("ADMIN"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8")) .file("content", "This is a test.".getBytes("UTF-8")) .with(csrf().asHeader()) .with(user("admin").roles("ADMIN")) .param("name", "test.txt") .param("format", "text") .with(user("admin").roles("ADMIN"))) .andExpect(status().isOk()) .andExpect(content().contentType("application/json;charset=UTF-8"))