Search in sources :

Example 66 with ResultActions

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

the class UserControllerIntegrationTest method executeUpdatePassword.

private ResultActions executeUpdatePassword(String body, String username, String accessToken, ResultMatcher expected) throws Exception {
    ResultActions result = mockMvc.perform(post("/users/{username}/password", 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 67 with ResultActions

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

the class UserControllerIntegrationTest method testGetUsersDefaultSorting.

@Test
public void testGetUsersDefaultSorting() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc.perform(get("/users").header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());
    result.andExpect(jsonPath("$.metaData.pageSize", is(100)));
    result.andExpect(jsonPath("$.metaData.sort", is("username")));
    result.andExpect(jsonPath("$.metaData.page", is(1)));
}
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 68 with ResultActions

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

the class UserControllerIntegrationTest method testGetUsersWithoutProfile.

@Test
public void testGetUsersWithoutProfile() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc.perform(get("/users").param("withProfile", "0").header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());
    System.out.println("with no profile: " + result.andReturn().getResponse().getContentAsString());
    result.andExpect(jsonPath("$.payload", Matchers.hasSize(0)));
    result.andExpect(jsonPath("$.metaData.additionalParams.withProfile", is("0")));
}
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 69 with ResultActions

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

the class UserControllerIntegrationTest method testAddUserAuthorities_2.

@Test
public void testAddUserAuthorities_2() throws Exception {
    Group group = createGroup(1);
    Role role = createRole(1);
    String username = "mockuser_1";
    UserDetails mockuser = this.createUser(username);
    try {
        this.groupManager.addGroup(group);
        this.roleManager.addRole(role);
        this.userManager.addUser(mockuser);
        UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
        String accessToken = mockOAuthInterceptor(user);
        String mockJson1 = "[{\"group\":\"group1\", \"role\":\"role1\"}]";
        ResultActions result1 = mockMvc.perform(post("/users/{target}/authorities", username).content(mockJson1).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
        result1.andExpect(status().isOk());
        result1.andExpect(jsonPath("$.payload[0].group", is("group1")));
        List<Authorization> auths = this.authorizationManager.getUserAuthorizations(username);
        Assert.assertEquals(1, auths.size());
        String mockJson2 = "[{\"group\":\"customers\", \"role\":\"supervisor\"}]";
        ResultActions result2 = mockMvc.perform(post("/users/{target}/authorities", username).content(mockJson2).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
        result2.andExpect(status().isOk());
        result2.andExpect(jsonPath("$.payload[0].group", is("customers")));
        auths = this.authorizationManager.getUserAuthorizations(username);
        Assert.assertEquals(2, auths.size());
        ResultActions result3 = mockMvc.perform(get("/users/{target}/authorities", username).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
        result3.andExpect(status().isOk());
        result3.andExpect(jsonPath("$.payload", Matchers.hasSize(2)));
        String mockJson4 = "[{\"group\":\"helpdesk\", \"role\":\"pageManager\"}]";
        ResultActions result4 = mockMvc.perform(put("/users/{target}/authorities", username).content(mockJson4).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
        result4.andExpect(status().isOk());
        result4.andExpect(jsonPath("$.payload[0].group", is("helpdesk")));
        auths = this.authorizationManager.getUserAuthorizations(username);
        Assert.assertEquals(1, auths.size());
        Assert.assertEquals("helpdesk", auths.get(0).getGroup().getName());
        String mockJson5 = "[{\"group\":\"wrong_group\", \"role\":\"pageManager\"}]";
        ResultActions result5 = mockMvc.perform(put("/users/{target}/authorities", username).content(mockJson5).contentType(MediaType.APPLICATION_JSON).header("Authorization", "Bearer " + accessToken));
        result5.andExpect(status().isBadRequest());
        result5.andExpect(jsonPath("$.errors.size()", is(1)));
        result5.andExpect(jsonPath("$.errors[0].code", is("2")));
        auths = this.authorizationManager.getUserAuthorizations(username);
        Assert.assertEquals(1, auths.size());
        Assert.assertEquals("helpdesk", auths.get(0).getGroup().getName());
    } finally {
        this.authorizationManager.deleteUserAuthorizations(username);
        this.groupManager.removeGroup(group);
        this.roleManager.removeRole(role);
        this.userManager.removeUser(username);
    }
}
Also used : Role(com.agiletec.aps.system.services.role.Role) Authorization(com.agiletec.aps.system.services.authorization.Authorization) Group(com.agiletec.aps.system.services.group.Group) 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 70 with ResultActions

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

the class UserControllerIntegrationTest method testGetUsersWithProfile.

@Test
public void testGetUsersWithProfile() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc.perform(get("/users").param("withProfile", "1").header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());
    result.andExpect(jsonPath("$.payload", Matchers.hasSize(Matchers.greaterThan(0))));
    result.andExpect(jsonPath("$.metaData.additionalParams.withProfile", is("1")));
    System.out.println("with profile: " + result.andReturn().getResponse().getContentAsString());
}
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)

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