Codota Logo
SecurityMockMvcRequestPostProcessors.user
Code IndexAdd Codota to your IDE (free)

How to use
user
method
in
org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors

Best Java code snippets using org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user (Showing top 20 results out of 315)

  • Add the Codota plugin to your IDE and get smart completions
private void myMethod () {
Connection c =
  • Codota IconDataSource dataSource;dataSource.getConnection()
  • Codota IconString url;DriverManager.getConnection(url)
  • Codota IconIdentityDatabaseUtil.getDBConnection()
  • Smart code suggestions by Codota
}
origin: spring-projects/spring-security

@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));
}
origin: spring-projects/spring-security

@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"));
}
origin: forcelate/forcelate-temple-java

  @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();
  }
}
origin: forcelate/forcelate-temple-java

@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();
}
origin: forcelate/forcelate-temple-java

@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());
}
origin: forcelate/forcelate-temple-java

@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());
}
origin: forcelate/forcelate-temple-java

  @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());
  }
}
origin: forcelate/forcelate-temple-java

@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());
}
origin: vtsukur/spring-rest-black-market

@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())));
}
origin: forcelate/forcelate-temple-java

@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();
}
origin: forcelate/forcelate-temple-java

@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());
}
origin: forcelate/forcelate-temple-java

@Test
public void saveUserAuthority() throws Exception {
  // Act + Assert
  mvc.perform(post("/addUser")
    .with(csrf())
    .with(user(randomString()).password(randomString()).authorities(USER)))
      .andExpect(status().isForbidden());
}
origin: vtsukur/spring-rest-black-market

.put("/ads/" + ad.getId() + "/publishing")
.accept(MediaTypes.HAL_JSON)
.with(user(userDetailsService.loadUserByUsername(Admin.HONTAREVA))));
origin: forcelate/forcelate-temple-java

  @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));
  }
}
origin: vtsukur/spring-rest-black-market

.content(requestBody)
.contentType(MediaType.APPLICATION_JSON)
.with(user(userDetailsService.loadUserByUsername(Admin.HONTAREVA))));
origin: webanno/webanno

@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"));
}
origin: webanno/webanno

  .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"))
origin: webanno/webanno

@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"));
}
origin: webanno/webanno

@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"));
}

origin: webanno/webanno

  .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"))
org.springframework.security.test.web.servlet.requestSecurityMockMvcRequestPostProcessorsuser

Javadoc

Establish a SecurityContext that has a UsernamePasswordAuthenticationToken for the Authentication#getPrincipal() and a User for the UsernamePasswordAuthenticationToken#getPrincipal(). All details are declarative and do not require that the user actually exists.

The support works by associating the user to the HttpServletRequest. To associate the request to the SecurityContextHolder you need to ensure that the SecurityContextPersistenceFilter is associated with the MockMvc instance. A few ways to do this are:

  • Invoking apply SecurityMockMvcConfigurers#springSecurity()
  • Adding Spring Security's FilterChainProxy to MockMvc
  • Manually adding SecurityContextPersistenceFilter to the MockMvc instance may make sense when using MockMvcBuilders standaloneSetup

Popular methods of SecurityMockMvcRequestPostProcessors

  • httpBasic
    Convenience mechanism for setting the Authorization header to use HTTP Basic with the given username
  • csrf
    Creates a RequestPostProcessor that will automatically populate a valid CsrfToken in the request.
  • testSecurityContext
    Creates a RequestPostProcessor that can be used to ensure that the resulting request is ran with the
  • x509
    Populates the provided X509Certificate instances on the request.
  • digest
    Creates a DigestRequestPostProcessor that enables easily adding digest based authentication to a req
  • authentication
    Establish a SecurityContext that uses the specified Authenticationfor the Authentication#getPrincipa
  • securityContext
    Establish the specified SecurityContext to be used. This works by associating the user to the HttpSe

Popular in Java

  • Parsing JSON documents to java classes using gson
  • putExtra (Intent)
  • notifyDataSetChanged (ArrayAdapter)
  • scheduleAtFixedRate (ScheduledExecutorService)
    Creates and executes a periodic action that becomes enabled first after the given initial delay, and
  • BufferedInputStream (java.io)
    Wraps an existing InputStream and buffers the input. Expensive interaction with the underlying input
  • Runnable (java.lang)
    Represents a command that can be executed. Often used to run code in a different Thread.
  • Arrays (java.util)
    This class contains various methods for manipulating arrays (such as sorting and searching). This cl
  • SortedMap (java.util)
    A map that has its keys ordered. The sorting is according to either the natural ordering of its keys
  • ServletException (javax.servlet)
    Defines a general exception a servlet can throw when it encounters difficulty.
  • BasicDataSource (org.apache.commons.dbcp)
    Basic implementation of javax.sql.DataSource that is configured via JavaBeans properties. This is no
Codota Logo
  • Products

    Search for Java codeSearch for JavaScript codeEnterprise
  • IDE Plugins

    IntelliJ IDEAWebStormAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogCodota Academy Plugin user guide Terms of usePrivacy policyJava Code IndexJavascript Code Index
Get Codota for your IDE now