Search in sources :

Example 46 with ResultActions

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

the class RoleControllerUnitTest method testAddWithInvalidCodeAndName.

@Test
public void testAddWithInvalidCodeAndName() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ObjectMapper mapper = new ObjectMapper();
    RoleRequest request = new RoleRequest();
    request.setCode(StringUtils.repeat("a", 100));
    request.setName(StringUtils.repeat("b", 100));
    // request.setCode(code);
    String payload = mapper.writeValueAsString(request);
    ResultActions result = mockMvc.perform(post("/roles").contentType(MediaType.APPLICATION_JSON_VALUE).content(payload).header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isBadRequest());
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RoleRequest(org.entando.entando.web.role.model.RoleRequest) AbstractControllerTest(org.entando.entando.web.AbstractControllerTest) Test(org.junit.Test)

Example 47 with ResultActions

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

the class UserControllerDeleteAuthoritiesIntegrationTest method testDeleteAuthorities.

@Test
public void testDeleteAuthorities() throws Exception {
    String accessToken = "ok";
    when(apiOAuth2TokenManager.readAccessToken(Mockito.anyString())).thenReturn(OAuth2TestUtils.getOAuth2Token("admin", "ok"));
    String username = "valid.username_ok";
    String password = "valid.123_ok";
    String groupName = "coach";
    String roleName = "pageManager";
    try {
        this.addUserWithAuthorization(username, password, groupName, roleName);
        UserDetails targetUser = this.authenticationProviderManager.getUser(username);
        boolean hasAuthorities = this.authorizationManager.isAuthOnGroupAndRole(targetUser, groupName, roleName, false);
        assertThat(hasAuthorities, is(true));
        ResultActions result = this.executeDeleteUserAuthorities(username, accessToken);
        result.andExpect(status().isOk());
        targetUser = this.authenticationProviderManager.getUser(username);
        hasAuthorities = this.authorizationManager.isAuthOnGroupAndRole(targetUser, groupName, roleName, false);
        assertThat(hasAuthorities, is(false));
    } catch (Throwable e) {
        throw e;
    } finally {
        this.authorizationManager.deleteUserAuthorizations(username);
        this.userManager.removeUser(username);
    }
}
Also used : UserDetails(com.agiletec.aps.system.services.user.UserDetails) ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test)

Example 48 with ResultActions

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

the class UserControllerDeleteAuthoritiesIntegrationTest method testDeleteAuthoritiesSameUser.

@Test
public void testDeleteAuthoritiesSameUser() throws Exception {
    String currentUserName = "admin";
    String accessToken = "ok";
    when(apiOAuth2TokenManager.readAccessToken(Mockito.anyString())).thenReturn(OAuth2TestUtils.getOAuth2Token(currentUserName, "ok"));
    try {
        ResultActions result = this.executeDeleteUserAuthorities(currentUserName, accessToken);
        result.andExpect(status().isForbidden());
        String response = result.andReturn().getResponse().getContentAsString();
        System.out.println("resp:" + response);
        result.andExpect(jsonPath("$.errors[0].code", is(UserValidator.ERRCODE_SELF_UPDATE)));
        assertThat(this.authorizationManager.getUserAuthorizations(currentUserName).size(), is(Matchers.greaterThanOrEqualTo(1)));
    } catch (Throwable e) {
        throw e;
    }
}
Also used : ResultActions(org.springframework.test.web.servlet.ResultActions) Test(org.junit.Test)

Example 49 with ResultActions

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

the class RoleControllerIntegrationTest method testGetRoleNotFound.

@Test
public void testGetRoleNotFound() throws Exception {
    String code = "this_role_has_no_name";
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc.perform(get("/roles/{rolecode}", code).header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isNotFound());
// result.andExpect(jsonPath("$.payload.permissions.length()", greaterThan(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 50 with ResultActions

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

the class RoleControllerIntegrationTest method testGetRolesFilterByCode.

@Test
public void testGetRolesFilterByCode() throws Exception {
    UserDetails user = new OAuth2TestUtils.UserBuilder("jack_bauer", "0x24").grantedToRoleAdmin().build();
    String accessToken = mockOAuthInterceptor(user);
    ResultActions result = mockMvc.perform(get("/roles").param("filter[0].attribute", "code").param("filter[0].value", "admin").header("Authorization", "Bearer " + accessToken));
    result.andExpect(status().isOk());
    result.andExpect(jsonPath("$.payload.length()", 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)

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