Search in sources :

Example 51 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project entando-core by entando.

the class UserControllerIntegrationTest method executeUserPost.

private ResultActions executeUserPost(String body, String accessToken, ResultMatcher expected) throws Exception {
    ResultActions result = mockMvc.perform(post("/users").content(body).contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken));
    result.andExpect(expected);
    return result;
}
Also used : ResultActions(org.springframework.test.web.servlet.ResultActions)

Example 52 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project entando-core by entando.

the class UserControllerIntegrationTest method executeUserPut.

private ResultActions executeUserPut(String body, String username, String accessToken, ResultMatcher expected) throws Exception {
    ResultActions result = mockMvc.perform(put("/users/{username}", new Object[] { username }).content(body).contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken));
    result.andExpect(expected);
    return result;
}
Also used : ResultActions(org.springframework.test.web.servlet.ResultActions)

Example 53 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project entando-core by entando.

the class UserControllerIntegrationTest method testAddUserWithLongName.

@Test
public void testAddUserWithLongName() throws Exception {
    String validUsername = "valid.username_with_very_long_name_with_a_total_of_80_characters_maximum_allowed";
    try {
        UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
        String accessToken = mockOAuthInterceptor(user);
        String mockJson = "{\"username\": \"" + validUsername + "\",\"status\": \"active\",\"password\": \"password\"}";
        ResultActions result = this.executeUserPost(mockJson, accessToken, status().isOk());
        result.andExpect(jsonPath("$.payload.username", is(validUsername)));
        ResultActions result2 = this.executeUserPost(mockJson, accessToken, status().isConflict());
        result2.andExpect(jsonPath("$.payload", Matchers.hasSize(0)));
        result2.andExpect(jsonPath("$.errors", Matchers.hasSize(1)));
        result2.andExpect(jsonPath("$.errors[0].code", is("1")));
        result2.andExpect(jsonPath("$.metaData.size()", is(0)));
        ResultActions resultDelete = mockMvc.perform(delete("/users/{username}", URLEncoder.encode(validUsername, "ISO-8859-1")).content(mockJson).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
        resultDelete.andExpect(status().isOk());
        resultDelete.andExpect(jsonPath("$.payload.code", is(validUsername)));
    } catch (Throwable e) {
        this.userManager.removeUser(validUsername);
        throw e;
    } finally {
        UserDetails user = this.userManager.getUser(validUsername);
        assertNull(user);
    }
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test) AbstractControllerIntegrationTest(org.entando.entando.web.AbstractControllerIntegrationTest)

Example 54 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project entando-core by entando.

the class UserControllerIntegrationTest method testUpdatePassword_1.

@Test
public void testUpdatePassword_1() throws Exception {
    String validUsername = "valid.username_ok";
    String validPassword = "valid.123_ok";
    String newValidPassword = "valid.1234_ok";
    try {
        UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
        String accessToken = mockOAuthInterceptor(user);
        String mockJson = "{\"username\": \"" + validUsername + "\",\"status\": \"active\",\"password\": \"" + validPassword + "\"}";
        this.executeUserPost(mockJson, accessToken, status().isOk());
        String invalidBody1 = "{\"username\": \"" + validUsername + "\",\"oldPassword\": \"" + validPassword + "\",\"newPassword\": \"$invalid%%\"}";
        ResultActions resultInvalid1 = this.executeUpdatePassword(invalidBody1, validUsername, accessToken, status().isBadRequest());
        resultInvalid1.andExpect(jsonPath("$.payload", Matchers.hasSize(0)));
        resultInvalid1.andExpect(jsonPath("$.errors", Matchers.hasSize(1)));
        resultInvalid1.andExpect(jsonPath("$.errors[0].code", is("3")));
        resultInvalid1.andExpect(jsonPath("$.metaData.size()", is(0)));
        String invalidBody2 = "{\"username\": \"" + validUsername + "\",\"oldPassword\": \"" + validPassword + "\",\"newPassword\": \"upasswordvelylong_.veryverylong\"}";
        ResultActions resultInvalid2 = this.executeUpdatePassword(invalidBody2, validUsername, accessToken, status().isBadRequest());
        resultInvalid2.andExpect(jsonPath("$.payload", Matchers.hasSize(0)));
        resultInvalid2.andExpect(jsonPath("$.errors", Matchers.hasSize(1)));
        resultInvalid2.andExpect(jsonPath("$.errors[0].code", is("3")));
        resultInvalid2.andExpect(jsonPath("$.metaData.size()", is(0)));
        String invalidBody3 = "{\"username\": \"" + validUsername + "\",\"oldPassword\": \"invalid\",\"newPassword\": \"" + newValidPassword + "\"}";
        ResultActions resultInvalid3 = this.executeUpdatePassword(invalidBody3, validUsername, accessToken, status().isBadRequest());
        resultInvalid3.andExpect(jsonPath("$.payload", Matchers.hasSize(0)));
        resultInvalid3.andExpect(jsonPath("$.errors", Matchers.hasSize(1)));
        resultInvalid3.andExpect(jsonPath("$.errors[0].code", is("4")));
        resultInvalid3.andExpect(jsonPath("$.metaData.size()", is(0)));
        String invalidBody4 = "{\"username\": \"" + validUsername + "\",\"oldPassword\": \"invalid\",\"newPassword\": \"\"}";
        ResultActions resultInvalid4 = this.executeUpdatePassword(invalidBody4, validUsername, accessToken, status().isBadRequest());
        resultInvalid4.andExpect(jsonPath("$.payload", Matchers.hasSize(0)));
        resultInvalid4.andExpect(jsonPath("$.errors", Matchers.hasSize(1)));
        resultInvalid4.andExpect(jsonPath("$.errors[0].code", is("52")));
        resultInvalid4.andExpect(jsonPath("$.metaData.size()", is(0)));
        String validBody = "{\"username\": \"" + validUsername + "\",\"oldPassword\": \"" + validPassword + "\",\"newPassword\": \"" + newValidPassword + "\"}";
        ResultActions resultValid = this.executeUpdatePassword(validBody, validUsername, accessToken, status().isOk());
        resultValid.andExpect(jsonPath("$.payload.username", is(validUsername)));
        resultValid.andExpect(jsonPath("$.errors", Matchers.hasSize(0)));
        resultValid.andExpect(jsonPath("$.metaData.size()", is(0)));
    } catch (Throwable e) {
        throw e;
    } finally {
        this.userManager.removeUser(validUsername);
    }
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test) AbstractControllerIntegrationTest(org.entando.entando.web.AbstractControllerIntegrationTest)

Example 55 with ResultActions

use of org.springframework.test.web.servlet.ResultActions in project entando-core by entando.

the class UserControllerIntegrationTest method testGetUsersWithoutPermission.

@Test
public void testGetUsersWithoutPermission() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("normal_user", "0x24").build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc.perform(get("/users").header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isForbidden());
}
Also used : OAuth2TestUtils(org.entando.entando.web.utils.OAuth2TestUtils) UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test) AbstractControllerIntegrationTest(org.entando.entando.web.AbstractControllerIntegrationTest)

Aggregations

ResultActions (org.springframework.test.web.servlet.ResultActions)624 Test (org.junit.Test)547 UserDetails (com.agiletec.aps.system.services.user.UserDetails)297 AbstractControllerIntegrationTest (org.entando.entando.web.AbstractControllerIntegrationTest)221 AbstractControllerTest (org.entando.entando.web.AbstractControllerTest)101 WebMvcTest (org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest)65 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)61 MvcResult (org.springframework.test.web.servlet.MvcResult)43 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)39 RequestBuilder (org.springframework.test.web.servlet.RequestBuilder)32 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)26 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)23 UUID (java.util.UUID)22 InputStream (java.io.InputStream)21 Map (java.util.Map)19 IEntityTypesConfigurer (com.agiletec.aps.system.common.entity.IEntityTypesConfigurer)17 HashMap (java.util.HashMap)17 Test (org.junit.jupiter.api.Test)16 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)16 WebIntegrationBaseTest (org.nextprot.api.web.dbunit.base.mvc.WebIntegrationBaseTest)14