Search in sources :

Example 56 with ResultActions

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

the class UserControllerIntegrationTest method testUpdatePassword_2.

@Test
public void testUpdatePassword_2() throws Exception {
    String validUsername = "valid_ok.2";
    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\": \"" + newValidPassword + "\"}";
        ResultActions resultInvalid1 = this.executeUpdatePassword(invalidBody1, "no_same_username", accessToken, status().isConflict());
        resultInvalid1.andExpect(jsonPath("$.payload", Matchers.hasSize(0)));
        resultInvalid1.andExpect(jsonPath("$.errors", Matchers.hasSize(1)));
        resultInvalid1.andExpect(jsonPath("$.errors[0].code", is("2")));
        resultInvalid1.andExpect(jsonPath("$.metaData.size()", is(0)));
        String noExistingUser = "test12345";
        String invalidBody2 = "{\"username\": \"" + noExistingUser + "\",\"oldPassword\": \"" + validPassword + "\",\"newPassword\": \"" + newValidPassword + "\"}";
        ResultActions resultInvalid2 = this.executeUpdatePassword(invalidBody2, noExistingUser, accessToken, status().isNotFound());
        resultInvalid2.andExpect(jsonPath("$.payload", Matchers.hasSize(0)));
        resultInvalid2.andExpect(jsonPath("$.errors", Matchers.hasSize(1)));
        resultInvalid2.andExpect(jsonPath("$.errors[0].code", is("1")));
        resultInvalid2.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 57 with ResultActions

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

the class UserControllerIntegrationTest method testUserPagination.

@Test
public void testUserPagination() throws Exception {
    String userPrefix = "test_pager_";
    for (int i = 0; i < 20; i++) {
        UserDetails user = this.createUser(userPrefix + i);
        this.userManager.addUser(user);
    }
    try {
        UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
        String accessToken = mockOAuthInterceptor(user);
        ResultActions result = mockMvc.perform(get("/users").contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        result.andExpect(jsonPath("$.payload", Matchers.hasSize(28)));
        result.andExpect(jsonPath("$.errors", Matchers.hasSize(0)));
        result.andExpect(jsonPath("$.metaData.size()", is(8)));
        result.andExpect(jsonPath("$.metaData.page", is(1)));
        result.andExpect(jsonPath("$.metaData.pageSize", is(100)));
        result.andExpect(jsonPath("$.metaData.totalItems", is(28)));
        result = mockMvc.perform(get("/users").param("pageSize", "10").param("page", "2").contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        result.andExpect(jsonPath("$.payload", Matchers.hasSize(10)));
        result.andExpect(jsonPath("$.payload[0].username", is("test_pager_10")));
        result.andExpect(jsonPath("$.errors", Matchers.hasSize(0)));
        result.andExpect(jsonPath("$.metaData.size()", is(8)));
        result.andExpect(jsonPath("$.metaData.page", is(2)));
        result.andExpect(jsonPath("$.metaData.pageSize", is(10)));
        result.andExpect(jsonPath("$.metaData.totalItems", is(28)));
        result = mockMvc.perform(get("/users").param("pageSize", "10").param("page", "2").contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        result.andExpect(jsonPath("$.payload", Matchers.hasSize(10)));
        result.andExpect(jsonPath("$.payload[0].username", is("test_pager_10")));
        result.andExpect(jsonPath("$.errors", Matchers.hasSize(0)));
        result.andExpect(jsonPath("$.metaData.size()", is(8)));
        result.andExpect(jsonPath("$.metaData.page", is(2)));
        result.andExpect(jsonPath("$.metaData.pageSize", is(10)));
        result.andExpect(jsonPath("$.metaData.lastPage", is(3)));
        result.andExpect(jsonPath("$.metaData.totalItems", is(28)));
        result = mockMvc.perform(get("/users").param("pageSize", "5").param("page", "4").contentType(MediaType.APPLICATION_JSON_VALUE).header("Authorization", "Bearer " + accessToken));
        result.andExpect(status().isOk());
        result.andExpect(jsonPath("$.payload", Matchers.hasSize(5)));
        result.andExpect(jsonPath("$.payload[0].username", is("test_pager_15")));
        result.andExpect(jsonPath("$.errors", Matchers.hasSize(0)));
        result.andExpect(jsonPath("$.metaData.size()", is(8)));
        result.andExpect(jsonPath("$.metaData.page", is(4)));
        result.andExpect(jsonPath("$.metaData.pageSize", is(5)));
        result.andExpect(jsonPath("$.metaData.lastPage", is(6)));
        result.andExpect(jsonPath("$.metaData.totalItems", is(28)));
    } catch (Throwable e) {
        throw e;
    } finally {
        for (int i = 0; i < 20; i++) {
            this.userManager.removeUser(userPrefix + i);
        }
    }
}
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 58 with ResultActions

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

the class UserControllerUnitTest method shouldGetUsersList.

@Test
public void shouldGetUsersList() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    when(this.userService.getUsers(any(RestListRequest.class), any(String.class))).thenReturn(mockUsers());
    ResultActions result = mockMvc.perform(get("/users").param("withProfile", "0").param("sort", "username").param("filter[0].attribute", "username").param("filter[0].operator", "like").param("filter[0].value", "user").sessionAttr("user", user).header("Authorization", "Bearer " + accessToken));
    System.out.println("result: " + result.andReturn().getResponse().getContentAsString());
    result.andExpect(status().isOk());
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) RestListRequest(org.entando.entando.web.common.model.RestListRequest) ResultActions(org.springframework.test.web.servlet.ResultActions) AbstractControllerTest(org.entando.entando.web.AbstractControllerTest) Test(org.junit.Test)

Example 59 with ResultActions

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

the class UserControllerUnitTest method shouldExecuteUserPut.

@Test
public void shouldExecuteUserPut() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    String mockJson = "{\n" + "    \"username\": \"username_test\",\n" + "    \"status\": \"inactive\",\n" + "    \"password\": \"different_password\"\n" + " }";
    when(this.userManager.getUser("username_test")).thenReturn(this.mockUserDetails("username_test"));
    ResultActions result = mockMvc.perform(put("/users/{username}", "username_test").content(mockJson).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
    String response = result.andReturn().getResponse().getContentAsString();
    System.out.println("users: " + response);
    result.andExpect(status().isOk());
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) AbstractControllerTest(org.entando.entando.web.AbstractControllerTest) Test(org.junit.Test)

Example 60 with ResultActions

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

the class UserControllerUnitTest method shouldGetUsersWithProfileDetails.

@Test
public void shouldGetUsersWithProfileDetails() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    when(this.userService.getUsers(any(RestListRequest.class), any(String.class))).thenReturn(mockUsersWithProfile());
    ResultActions result = mockMvc.perform(get("/users").param("withProfile", "1").header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());
    String response = result.andReturn().getResponse().getContentAsString();
    System.out.println(response);
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) RestListRequest(org.entando.entando.web.common.model.RestListRequest) ResultActions(org.springframework.test.web.servlet.ResultActions) AbstractControllerTest(org.entando.entando.web.AbstractControllerTest) Test(org.junit.Test)

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