Search in sources :

Example 1 with SecuredController

use of io.restassured.module.mockmvc.http.SecuredController in project rest-assured by rest-assured.

the class SecuredControllerTest method statically_defined_auth_has_precedence_over_statically_defined_request_spec.

@Test
public void statically_defined_auth_has_precedence_over_statically_defined_request_spec() {
    RestAssuredMockMvc.authentication = RestAssuredMockMvc.principal(new User("authorized_user", "password", Collections.<GrantedAuthority>emptyList()));
    RestAssuredMockMvc.requestSpecification = new MockMvcRequestSpecBuilder().setAuth(RestAssuredMockMvc.authentication(new TestingAuthenticationToken("name", "pw"))).build();
    try {
        RestAssuredMockMvc.given().standaloneSetup(new SecuredController()).param("name", "Johan").when().get("/springSecurityGreeting").then().statusCode(200).body("content", equalTo("Hello, Johan!"));
    } finally {
        RestAssuredMockMvc.reset();
    }
}
Also used : SecuredController(io.restassured.module.mockmvc.http.SecuredController) User(org.springframework.security.core.userdetails.User) MockMvcRequestSpecBuilder(io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Test(org.junit.Test)

Example 2 with SecuredController

use of io.restassured.module.mockmvc.http.SecuredController in project rest-assured by rest-assured.

the class SecuredControllerTest method spring_context_holder_is_cleared_after_failed_test.

@Test
public void spring_context_holder_is_cleared_after_failed_test() {
    exception.expect(NestedServletException.class);
    exception.expectMessage("Not authorized");
    try {
        RestAssuredMockMvc.given().standaloneSetup(new SecuredController()).auth().principal(new User("authorized_user2", "password", Collections.<GrantedAuthority>emptyList())).param("name", "Johan").when().get("/springSecurityGreeting").then().statusCode(200).body("content", equalTo("Hello, Johan!"));
    } finally {
        assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
    }
}
Also used : SecuredController(io.restassured.module.mockmvc.http.SecuredController) User(org.springframework.security.core.userdetails.User) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.Test)

Example 3 with SecuredController

use of io.restassured.module.mockmvc.http.SecuredController in project rest-assured by rest-assured.

the class SecuredControllerTest method spring_context_holder_is_cleared_after_test.

@Test
public void spring_context_holder_is_cleared_after_test() {
    RestAssuredMockMvc.given().standaloneSetup(new SecuredController()).auth().principal(new User("authorized_user", "password", Collections.<GrantedAuthority>emptyList())).param("name", "Johan").when().get("/springSecurityGreeting").then().statusCode(200).body("content", equalTo("Hello, Johan!"));
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
Also used : SecuredController(io.restassured.module.mockmvc.http.SecuredController) User(org.springframework.security.core.userdetails.User) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Test(org.junit.Test)

Example 4 with SecuredController

use of io.restassured.module.mockmvc.http.SecuredController in project rest-assured by rest-assured.

the class SecuredControllerTest method spring_context_holder_is_cleared_after_failed_test_when_auth_is_statically_defined.

@Test
public void spring_context_holder_is_cleared_after_failed_test_when_auth_is_statically_defined() {
    RestAssuredMockMvc.authentication = RestAssuredMockMvc.principal(new User("authorized_user", "password", Collections.<GrantedAuthority>emptyList()));
    try {
        RestAssuredMockMvc.given().standaloneSetup(new SecuredController()).param("name", "Johan").when().get("/springSecurityGreeting").then().statusCode(200).body("content", equalTo("Hello, Johan!"));
    } finally {
        RestAssuredMockMvc.reset();
    }
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
Also used : SecuredController(io.restassured.module.mockmvc.http.SecuredController) User(org.springframework.security.core.userdetails.User) Test(org.junit.Test)

Aggregations

SecuredController (io.restassured.module.mockmvc.http.SecuredController)4 Test (org.junit.Test)4 User (org.springframework.security.core.userdetails.User)4 GrantedAuthority (org.springframework.security.core.GrantedAuthority)2 MockMvcRequestSpecBuilder (io.restassured.module.mockmvc.specification.MockMvcRequestSpecBuilder)1 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)1