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

The following examples show how to use org.springframework.security.test.context.support.WithUserDetails. 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 File: UserServiceImplUnitTest.java    From java-starthere with 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 File: HelloMvcTest.java    From oauth2-blog with 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 #3
Source File: UserServiceImplUnitTest.java    From java-starthere with 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 #4
Source File: HelloMvcTest.java    From oauth2-blog with 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 #5
Source File: ApplicationTests.java    From spring-webmvc-jwt-sample with GNU General Public License v3.0 5 votes vote down vote up
@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 #6
Source File: DefaultControllerTests.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Test
@WithUserDetails("[email protected]")
public void test_user1_defaultAfterLogin() throws Exception {
    mvc
            .perform(get("/default"))
            .andExpect(status().is3xxRedirection())
            .andExpect(redirectedUrl("/"))
            .andDo(print())
    ;
}
 
Example #7
Source File: DefaultControllerTests.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Test
@WithUserDetails("[email protected]")
public void test_admin1_defaultAfterLogin() throws Exception {
    mvc
            .perform(get("/default"))
            .andExpect(status().is3xxRedirection())
            .andExpect(redirectedUrl("/events/"))
            .andDo(print())
    ;
}
 
Example #8
Source File: WelcomeControllerTests.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Test
    @WithUserDetails("[email protected]")
    public void test_admin1_showCreateLink() throws Exception {
//        mvc
//                .perform(get("/showCreateLink"))
//                .andExpect(status().is3xxRedirection())
//                .andExpect(redirectedUrl("/events/"))
//                .andDo(print())
//        ;
    }
 
Example #9
Source File: EventDaoTests.java    From Spring-Security-Third-Edition with 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 #10
Source File: EventDaoTests.java    From Spring-Security-Third-Edition with 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 #11
Source File: EventDaoTests.java    From Spring-Security-Third-Edition with 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 #12
Source File: EventDaoTests.java    From Spring-Security-Third-Edition with 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());
}
 
Example #13
Source File: EventDaoTests.java    From Spring-Security-Third-Edition with 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(CalendarUserStub.admin1());
       assertThat(event.getAttendee()).isEqualTo(CalendarUserStub.user2());
}
 
Example #14
Source File: EventDaoTests.java    From Spring-Security-Third-Edition with 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(CalendarUserStub.user2());
       assertThat(event.getAttendee()).isEqualTo(CalendarUserStub.user1());
}
 
Example #15
Source File: CalendarApplicationTests.java    From Spring-Security-Third-Edition with MIT License 5 votes vote down vote up
@Test
@WithUserDetails("[email protected]")
public void test_events_user() throws Exception {
    mvc.perform(get("/events/"))
            .andExpect(status().is4xxClientError())
    ;
}
 
Example #16
Source File: CorsTest.java    From hawkbit with Eclipse Public License 1.0 5 votes vote down vote up
@WithUserDetails("admin")
@Test
@Description("Ensures that Cors is working.")
public void validateCorsRequest() throws Exception {
    performOptionsRequestToRestWithOrigin(ALLOWED_ORIGIN_FIRST).andExpect(status().isOk());
    performOptionsRequestToRestWithOrigin(ALLOWED_ORIGIN_SECOND).andExpect(status().isOk());

    final String invalidOriginResponseBody = performOptionsRequestToRestWithOrigin(INVALID_ORIGIN)
            .andExpect(status().isForbidden()).andReturn().getResponse().getContentAsString();
    assertThat(invalidOriginResponseBody).isEqualTo(INVALID_CORS_REQUEST);

    final String invalidCorsUrlResponseBody = performOptionsRequestToUrlWithOrigin(MgmtRestConstants.BASE_SYSTEM_MAPPING, ALLOWED_ORIGIN_FIRST)
            .andExpect(status().isForbidden()).andReturn().getResponse().getContentAsString();
    assertThat(invalidCorsUrlResponseBody).isEqualTo(INVALID_CORS_REQUEST);
}
 
Example #17
Source File: UserDetailsIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
@WithUserDetails(value = "jane", userDetailsServiceBeanName = "userDetailService")
public void givenJane_callSecuredLoadUserDetailWithJane_thenOK() {
    CustomUser user = userService.securedLoadUserDetail("jane");
    assertEquals("jane", user.getNickName());
    assertEquals("jane", user.getUsername());
}
 
Example #18
Source File: CustomUserDetailsServiceIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
@WithUserDetails("john")
public void givenUserWithReadPermissions_whenRequestUserInfo_thenRetrieveUserData() throws Exception {
    this.mvc.perform(get("/user").with(csrf()))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.user.privileges[0].name").value("FOO_READ_PRIVILEGE"))
        .andExpect(jsonPath("$.user.organization.name").value("FirstOrg"))
        .andExpect(jsonPath("$.user.username").value("john"));
}
 
Example #19
Source File: CustomUserDetailsServiceIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
@WithUserDetails("tom")
public void givenUserWithWritePermissions_whenRequestUserInfo_thenRetrieveUserData() throws Exception {
    this.mvc.perform(get("/user").with(csrf()))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.user.privileges").isArray())
        .andExpect(jsonPath("$.user.organization.name").value("SecondOrg"))
        .andExpect(jsonPath("$.user.username").value("tom"));
}
 
Example #20
Source File: CustomUserDetailsServiceIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
@WithUserDetails("john")
public void givenUserWithReadPermissions_whenRequestFoo_thenRetrieveSampleFoo() throws Exception {
    this.mvc.perform(get("/foos/1").with(csrf()))
        .andExpect(status().isOk())
        .andExpect(jsonPath("$.name").value("Sample"));
}
 
Example #21
Source File: CustomUserDetailsServiceIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
@WithUserDetails("john")
public void givenUserWithReadPermissions_whenCreateNewFoo_thenForbiddenStatusRetrieved() throws Exception {
    this.mvc.perform(post("/foos").with(csrf())
        .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
        .content(asJsonString(new Foo())))
        .andExpect(status().isForbidden());
}
 
Example #22
Source File: CustomUserDetailsServiceIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
@WithUserDetails("tom")
public void givenUserWithWritePermissions_whenCreateNewFoo_thenOkStatusRetrieved() throws Exception {
    this.mvc.perform(post("/foos").with(csrf())
        .header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
        .content(asJsonString(new Foo())))
        .andExpect(status().isCreated());
}
 
Example #23
Source File: UserControllerIntegrationTest.java    From java-starthere with 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 #24
Source File: UserServiceImplUnitTest.java    From java-starthere with 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 #25
Source File: UserControllerIntegrationTest.java    From java-starthere with 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 #26
Source File: UserControllerIntegrationTest.java    From java-starthere with 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 #27
Source File: UserControllerIntegrationTest.java    From java-starthere with 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 #28
Source File: UserControllerIntegrationTest.java    From java-starthere with 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 #29
Source File: UserControllerIntegrationTest.java    From java-starthere with 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 #30
Source File: UserControllerIntegrationTest.java    From java-starthere with 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")));
}