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);
}
}
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);
}
}
}
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());
}
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());
}
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);
}
Aggregations