|
17 | 17 |
|
18 | 18 | import static java.lang.String.format;
|
19 | 19 | import static java.util.Arrays.asList;
|
| 20 | +import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; |
20 | 21 | import static org.hamcrest.CoreMatchers.containsString;
|
21 | 22 | import static org.hamcrest.CoreMatchers.equalTo;
|
22 | 23 | import static org.hamcrest.CoreMatchers.hasItem;
|
@@ -67,6 +68,7 @@ public class AccountAttributesTests {
|
67 | 68 | public static final ResultMatcher UNAUTHORIZED = status().isUnauthorized();
|
68 | 69 | public static final ResultMatcher FORBIDDEN = status().isForbidden();
|
69 | 70 | public static final ResultMatcher NOT_FOUND = status().isNotFound();
|
| 71 | + public static final ResultMatcher BAD_REQUEST = status().isBadRequest(); |
70 | 72 |
|
71 | 73 | public static final String TEST_USER = "test";
|
72 | 74 | public static final String TEST_100_USER = "test_100";
|
@@ -145,7 +147,7 @@ public void aUserCanListHisAttributes() throws Exception {
|
145 | 147 |
|
146 | 148 | mvc.perform(get(ACCOUNT_ATTR_URL_TEMPLATE, testAccount.getUuid())).andExpect(OK);
|
147 | 149 | }
|
148 |
| - |
| 150 | + |
149 | 151 | @Test
|
150 | 152 | @WithMockUser(username = "test", roles = "USER")
|
151 | 153 | public void managingAttributesRequiresPrivilegedUser() throws Exception {
|
@@ -390,4 +392,58 @@ public void multiAttributeSetTest() throws Exception {
|
390 | 392 | attrs.forEach(a -> assertThat(results, hasItem(a)));
|
391 | 393 | }
|
392 | 394 | }
|
| 395 | + |
| 396 | + @Test |
| 397 | + @WithMockUser(username = "admin", roles = "ADMIN") |
| 398 | + public void attributeValidationTests() throws Exception { |
| 399 | + |
| 400 | + AttributeDTO noNameAttribute = AttributeDTO.newInstance(null, ATTR_VALUE); |
| 401 | + |
| 402 | + mvc |
| 403 | + .perform(put(ACCOUNT_ATTR_URL_TEMPLATE, noNameAttribute).contentType(APPLICATION_JSON) |
| 404 | + .content(mapper.writeValueAsString(noNameAttribute))) |
| 405 | + .andExpect(BAD_REQUEST) |
| 406 | + .andExpect(jsonPath("$.error", containsString("must not be blank"))); |
| 407 | + |
| 408 | + final String SOME_INVALID_NAMES[] = |
| 409 | + {"-pippo", "/ciccio/paglia", ".starts-with-dot", "carriage\nreturn", "another\rreturn"}; |
| 410 | + |
| 411 | + for (String name : SOME_INVALID_NAMES) { |
| 412 | + AttributeDTO invalidAttribute = AttributeDTO.newInstance(name, ATTR_VALUE); |
| 413 | + mvc |
| 414 | + .perform(put(ACCOUNT_ATTR_URL_TEMPLATE, invalidAttribute).contentType(APPLICATION_JSON) |
| 415 | + .content(mapper.writeValueAsString(invalidAttribute))) |
| 416 | + .andExpect(BAD_REQUEST) |
| 417 | + .andExpect(jsonPath("$.error", containsString("invalid name (does not match with regexp"))); |
| 418 | + } |
| 419 | + |
| 420 | + final String SOME_INVALID_VALES[] = {"carriage\nreturn", "another\rreturn"}; |
| 421 | + |
| 422 | + for (String value : SOME_INVALID_VALES) { |
| 423 | + AttributeDTO invalidAttribute = AttributeDTO.newInstance(ATTR_NAME, value); |
| 424 | + mvc |
| 425 | + .perform(put(ACCOUNT_ATTR_URL_TEMPLATE, invalidAttribute).contentType(APPLICATION_JSON) |
| 426 | + .content(mapper.writeValueAsString(invalidAttribute))) |
| 427 | + .andExpect(BAD_REQUEST) |
| 428 | + .andExpect(jsonPath("$.error", |
| 429 | + containsString("The string must not contain any new line or carriage return"))); |
| 430 | + } |
| 431 | + |
| 432 | + AttributeDTO longNameAttribute = AttributeDTO.newInstance(randomAlphabetic(65), ATTR_VALUE); |
| 433 | + |
| 434 | + mvc |
| 435 | + .perform(put(ACCOUNT_ATTR_URL_TEMPLATE, longNameAttribute).contentType(APPLICATION_JSON) |
| 436 | + .content(mapper.writeValueAsString(longNameAttribute))) |
| 437 | + .andExpect(BAD_REQUEST) |
| 438 | + .andExpect(jsonPath("$.error", containsString("name cannot be longer than 64 chars"))); |
| 439 | + |
| 440 | + |
| 441 | + AttributeDTO longValueAttribute = AttributeDTO.newInstance(ATTR_NAME, randomAlphabetic(257)); |
| 442 | + |
| 443 | + mvc |
| 444 | + .perform(put(ACCOUNT_ATTR_URL_TEMPLATE, longValueAttribute).contentType(APPLICATION_JSON) |
| 445 | + .content(mapper.writeValueAsString(longValueAttribute))) |
| 446 | + .andExpect(BAD_REQUEST) |
| 447 | + .andExpect(jsonPath("$.error", containsString("value cannot be longer than 256 chars"))); |
| 448 | + } |
393 | 449 | }
|
0 commit comments