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