Java Code Examples for org.springframework.security.test.context.support.WithUserDetails

The following examples show how to use org.springframework.security.test.context.support.WithUserDetails. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: java-starthere   Source File: UserServiceImplUnitTest.java    License: MIT License 6 votes vote down vote up
@Transactional
@WithUserDetails("cinnamon")
@Test
public void G_update()
{
    ArrayList<UserRoles> datas = new ArrayList<>();
    User u2 = new User("cinnamon", "password", "[email protected]", datas);
    u2.getUseremails()
      .add(new Useremail(u2, "[email protected]"));
    u2.getUseremails()
      .add(new Useremail(u2, "[email protected]"));
    u2.getUseremails()
      .add(new Useremail(u2, "[email protected]"));

    User updatedu2 = userService.update(u2, 7, false);

    System.out.println("*** DATA ***");
    System.out.println(updatedu2);
    System.out.println("*** DATA ***");

    int checking = updatedu2.getUseremails()
                            .size() - 1;
    assertEquals("[email protected]", updatedu2.getUseremails()
                                               .get(checking)
                                               .getUseremail());
}
 
Example 2
Source Project: java-starthere   Source File: UserServiceImplUnitTest.java    License: MIT License 5 votes vote down vote up
@Transactional
@WithUserDetails("cinnamon")
@Test (expected = ResourceFoundException.class)
public void GA_updateWithUserRole()
{
    Role r2 = new Role("user");

    ArrayList<UserRoles> datas = new ArrayList<>();
    User u2 = new User("cinnamon", "password", "[email protected]", datas);
    datas.add(new UserRoles(u2, r2));
    u2.getUseremails()
      .add(new Useremail(u2, "[email protected]"));
    u2.getUseremails()
      .add(new Useremail(u2, "[email protected]"));
    u2.getUseremails()
      .add(new Useremail(u2, "[email protected]"));

    User updatedu2 = userService.update(u2, 7, false);

    System.out.println("*** DATA ***");
    System.out.println(updatedu2);
    System.out.println("*** DATA ***");

    int checking = updatedu2.getUseremails()
                            .size() - 1;
    assertEquals("[email protected]", updatedu2.getUseremails()
                                               .get(checking)
                                               .getUseremail());
}
 
Example 3
Source Project: java-starthere   Source File: UserServiceImplUnitTest.java    License: MIT License 5 votes vote down vote up
@Transactional
@WithUserDetails("cinnamon")
@Test (expected = ResourceNotFoundException.class)
public void GB_updateNotCurrentUserNorAdmin()
{
    Role r2 = new Role("user");

    ArrayList<UserRoles> datas = new ArrayList<>();
    User u2 = new User("cinnamon", "password", "[email protected]", datas);
    u2.getUseremails()
      .add(new Useremail(u2, "[email protected]"));
    u2.getUseremails()
      .add(new Useremail(u2, "[email protected]"));
    u2.getUseremails()
      .add(new Useremail(u2, "[email protected]"));

    User updatedu2 = userService.update(u2, 8, false);

    System.out.println("*** DATA ***");
    System.out.println(updatedu2);
    System.out.println("*** DATA ***");

    int checking = updatedu2.getUseremails()
                            .size() - 1;
    assertEquals("[email protected]", updatedu2.getUseremails()
                                               .get(checking)
                                               .getUseremail());
}
 
Example 4
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void whenMeasuredResponseTime() throws Exception
{
    long time = System.currentTimeMillis();
    this.mockMvc.perform(get("/users/users")).andDo(print());
    long responseTime = (System.currentTimeMillis() - time);

    assertTrue("timestamp", (responseTime < 5000L));
}
 
Example 5
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void getAllUsers() throws Exception
{
    this.mockMvc.perform(get("/users/users"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("testbarn")));

}
 
Example 6
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void getReallyAllUsers() throws Exception
{
    this.mockMvc.perform(get("/users/users/all"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("testbarn")));
}
 
Example 7
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void getUserName() throws Exception
{
    this.mockMvc.perform(get("/users/getusername"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("testbarn")));
}
 
Example 8
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void getUserInfo() throws Exception
{
    this.mockMvc.perform(get("/users/getuserinfo"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("testbarn")));
}
 
Example 9
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void getUserLikeName() throws Exception
{
    this.mockMvc.perform(get("/users/user/name/like/{userName}", "test"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("testbarn")));
}
 
Example 10
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void getUserById() throws Exception
{
    this.mockMvc.perform(get("/users/user/{userid}",
                             14))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("testdog")));
}
 
Example 11
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void getUserByIdNotFound() throws Exception
{
    this.mockMvc.perform(get("/users/user/{userid}",
                             100))
                .andDo(print())
                .andExpect(status().is4xxClientError())
                .andExpect(content().string(containsString("ResourceNotFoundException")));
}
 
Example 12
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void getUserByName() throws Exception
{
    this.mockMvc.perform(get("/users/user/name/{userName}",
                             "testcat"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("testcat")));
}
 
Example 13
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void getUserByNameNotFound() throws Exception
{
    this.mockMvc.perform(get("/users/user/name/{userName}",
                             "rabbit"))
                .andDo(print())
                .andExpect(status().is4xxClientError())
                .andExpect(content().string(containsString("ResourceNotFoundException")));
}
 
Example 14
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testdog")
@Test
public void getCurrentUserName() throws Exception
{
    this.mockMvc.perform(get("/users/getusername"))
                .andDo(print())
                .andExpect(status().isOk())
                .andExpect(content().string(containsString("testdog")));
}
 
Example 15
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("testbarn")
@Test
public void givenPostAUser() throws Exception
{
    mockMvc.perform(MockMvcRequestBuilders.post("/users/user")
                                          .content("{\"username\": \"Ginger\", \"password\": \"EATEATEAT\", \"primaryemail\" : \"[email protected]\"}")
                                          .contentType(MediaType.APPLICATION_JSON)
                                          .accept(MediaType.APPLICATION_JSON))
           .andDo(print())
           .andExpect(status().isCreated())
           .andExpect(MockMvcResultMatchers.header()
                                           .exists("location"));
}
 
Example 16
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("admin")
@Test
public void deleteUserById() throws Exception
{
    mockMvc.perform(MockMvcRequestBuilders.delete("/users/user/{id}",
                                                  13))
           .andDo(print())
           .andExpect(status().is2xxSuccessful());
}
 
Example 17
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("admin")
@Test
public void deleteUserByIdNotFound() throws Exception
{
    mockMvc.perform(MockMvcRequestBuilders.delete("/users/user/{id}",
                                                  100))
           .andDo(print())
           .andExpect(status().is4xxClientError());
}
 
Example 18
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("admin")
@Test
public void UpdateUser() throws Exception
{
    mockMvc.perform(MockMvcRequestBuilders.put("/users/user/{userid}",
                                               7)
                                          .content("{\"password\": \"EATEATEAT\"}")
                                          .contentType(MediaType.APPLICATION_JSON)
                                          .accept(MediaType.APPLICATION_JSON))
           .andDo(print())
           .andExpect(status().isOk());
}
 
Example 19
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("admin")
@Test
public void deleteUserRoleByIds() throws Exception
{
    mockMvc.perform(MockMvcRequestBuilders.delete("/users/user/{userid}/role/{roleid}",
                                                  7,
                                                  2))
           .andDo(print())
           .andExpect(status().is2xxSuccessful());
}
 
Example 20
Source Project: java-starthere   Source File: UserControllerIntegrationTest.java    License: MIT License 5 votes vote down vote up
@WithUserDetails("admin")
@Test
public void postUserRoleByIds() throws Exception
{
    mockMvc.perform(MockMvcRequestBuilders.post("/users/user/{userid}/role/{roleid}",
                                                7,
                                                1))
           .andDo(print())
           .andExpect(status().is2xxSuccessful());
}
 
Example 21
Source Project: oauth2-blog   Source File: HelloMvcTest.java    License: MIT License 5 votes vote down vote up
@Test
@WithUserDetails("[email protected]")
public void shouldAllowUserWithUserRole() throws Exception {
    mockMvc.perform(MockMvcRequestBuilders.get("/api/hello?name=Seb")
            .accept(MediaType.ALL))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.greetings", is("Welcome Seb ([email protected])!")));
}
 
Example 22
Source Project: oauth2-blog   Source File: HelloMvcTest.java    License: MIT License 5 votes vote down vote up
@Test
@WithUserDetails("[email protected]")
public void shouldRejectUserWithNoAuthorities() throws Exception {
    mockMvc.perform(MockMvcRequestBuilders.get("/api/hello?name=Seb")
            .accept(MediaType.ALL))
            .andExpect(status().isForbidden());
}
 
Example 23
@Test
@WithUserDetails()
public void testSaveWithMock() throws Exception {

    this.mockMvc
        .perform(
            post("/v1/vehicles")
                .content(this.objectMapper.writeValueAsBytes(VehicleForm.builder().name("test").build()))
                .contentType(MediaType.APPLICATION_JSON)
        )
        .andExpect(status().isCreated());
}
 
Example 24
@Test
@WithUserDetails("[email protected]")
public void test_user1_defaultAfterLogin() throws Exception {
    mvc
            .perform(get("/default"))
            .andExpect(status().is3xxRedirection())
            .andExpect(redirectedUrl("/"))
            .andDo(print())
    ;
}
 
Example 25
@Test
@WithUserDetails("[email protected]")
public void test_admin1_defaultAfterLogin() throws Exception {
    mvc
            .perform(get("/default"))
            .andExpect(status().is3xxRedirection())
            .andExpect(redirectedUrl("/events/"))
            .andDo(print())
    ;
}
 
Example 26
@Test
    @WithUserDetails("[email protected]")
    public void test_admin1_showCreateLink() throws Exception {
//        mvc
//                .perform(get("/showCreateLink"))
//                .andExpect(status().is3xxRedirection())
//                .andExpect(redirectedUrl("/events/"))
//                .andDo(print())
//        ;
    }
 
Example 27
Source Project: Spring-Security-Third-Edition   Source File: EventDaoTests.java    License: MIT License 5 votes vote down vote up
@Test
   @WithUserDetails("[email protected]")
public void getEvent_USER_byId_USER1() {
       Event event = dao.getEvent(100);
       assertThat(event.getId()).isEqualTo(100);
       assertThat(event.getOwner()).isEqualTo(CalendarStubs.user1());
       assertThat(event.getAttendee()).isEqualTo(CalendarStubs.admin1());
}
 
Example 28
Source Project: Spring-Security-Third-Edition   Source File: EventDaoTests.java    License: MIT License 5 votes vote down vote up
@Test
   @WithUserDetails("[email protected]")
public void getEvent_USER_byId_ADMIN1() {
       Event event = dao.getEvent(102);
       assertThat(event.getId()).isEqualTo(102);
       assertThat(event.getOwner()).isEqualTo(CalendarStubs.admin1());
       assertThat(event.getAttendee()).isEqualTo(CalendarStubs.user2());
}
 
Example 29
Source Project: Spring-Security-Third-Edition   Source File: EventDaoTests.java    License: MIT License 5 votes vote down vote up
@Test
   @WithUserDetails("[email protected]")
public void getEvent_USER_byId_USER2() {
       Event event = dao.getEvent(101);
       assertThat(event.getId()).isEqualTo(101);
       assertThat(event.getOwner()).isEqualTo(CalendarStubs.user2());
       assertThat(event.getAttendee()).isEqualTo(CalendarStubs.user1());
}
 
Example 30
Source Project: Spring-Security-Third-Edition   Source File: EventDaoTests.java    License: MIT License 5 votes vote down vote up
@Test
   @WithUserDetails("[email protected]")
public void getEvent_USER_byId_USER1() {
       Event event = dao.getEvent(100);
       assertThat(event.getId()).isEqualTo(100);
       assertThat(event.getOwner()).isEqualTo(CalendarUserStub.user1());
       assertThat(event.getAttendee()).isEqualTo(CalendarUserStub.admin1());
}