|
34 | 34 |
|
35 | 35 | import java.io.ByteArrayOutputStream;
|
36 | 36 | import java.io.IOException;
|
| 37 | +import java.nio.charset.StandardCharsets; |
37 | 38 | import java.util.Arrays;
|
38 | 39 | import java.util.List;
|
| 40 | +import java.util.Locale; |
39 | 41 | import java.util.Objects;
|
40 | 42 |
|
41 | 43 | import static org.hamcrest.Matchers.containsString;
|
@@ -259,28 +261,64 @@ public void testPutRoleRequestContainsNonIndexPrivileges() {
|
259 | 261 | }
|
260 | 262 |
|
261 | 263 | public void testParseInvalidPrivilege() throws Exception {
|
262 |
| - final XContent xContent = XContentType.JSON.xContent(); |
| 264 | + final String unknownPrivilege = randomValueOtherThanMany( |
| 265 | + i -> IndexPrivilege.values().containsKey(i), |
| 266 | + () -> randomAlphaOfLength(10).toLowerCase(Locale.ROOT) |
| 267 | + ); |
263 | 268 |
|
264 |
| - final String invalidJsonString = """ |
265 |
| - { |
266 |
| - "manage": { |
267 |
| - "indices": [ |
268 |
| - { |
269 |
| - "names": ["test-*"], |
270 |
| - "privileges": ["foobar"] |
271 |
| - } |
272 |
| - ] |
273 |
| - } |
| 269 | + final String invalidJsonString = String.format(Locale.ROOT, """ |
| 270 | + { |
| 271 | + "manage": { |
| 272 | + "indices": [ |
| 273 | + { |
| 274 | + "names": ["test-*"], |
| 275 | + "privileges": ["%s"] |
| 276 | + } |
| 277 | + ] |
274 | 278 | }
|
275 |
| - """; |
| 279 | + }""", unknownPrivilege); |
| 280 | + assertInvalidPrivilegeParsing(invalidJsonString, unknownPrivilege); |
| 281 | + } |
| 282 | + |
| 283 | + public void testParseMixedValidAndInvalidPrivileges() throws Exception { |
| 284 | + final String unknownPrivilege = randomValueOtherThanMany( |
| 285 | + i -> IndexPrivilege.values().containsKey(i), |
| 286 | + () -> randomAlphaOfLength(10).toLowerCase(Locale.ROOT) |
| 287 | + ); |
| 288 | + |
| 289 | + final String validPrivilege = "read"; |
| 290 | + final String mixedPrivilegesJson = String.format(Locale.ROOT, """ |
| 291 | + { |
| 292 | + "manage": { |
| 293 | + "indices": [ |
| 294 | + { |
| 295 | + "names": ["test-*"], |
| 296 | + "privileges": ["%s", "%s"] |
| 297 | + } |
| 298 | + ] |
| 299 | + } |
| 300 | + }""",validPrivilege, unknownPrivilege); |
| 301 | + |
| 302 | + assertInvalidPrivilegeParsing(mixedPrivilegesJson, unknownPrivilege); |
| 303 | + } |
| 304 | + |
| 305 | + /** |
| 306 | + * Helper method to assert that parsing the given JSON payload results in an |
| 307 | + * IllegalArgumentException due to an unknown privilege. |
| 308 | + * |
| 309 | + * @param jsonPayload The JSON string containing the privilege data. |
| 310 | + * @param expectedErrorDetail The specific unknown privilege name expected in the error message. |
| 311 | + */ |
| 312 | + private static void assertInvalidPrivilegeParsing(final String jsonPayload, final String expectedErrorDetail) throws Exception { |
| 313 | + final XContent xContent = XContentType.JSON.xContent(); |
276 | 314 |
|
277 |
| - try (XContentParser parser = xContent.createParser(XContentParserConfiguration.EMPTY, invalidJsonString.getBytes(StandardCharsets.UTF_8))) { |
| 315 | + try (XContentParser parser = xContent.createParser(XContentParserConfiguration.EMPTY, jsonPayload.getBytes(StandardCharsets.UTF_8))) { |
278 | 316 | assertThat(parser.nextToken(), equalTo(XContentParser.Token.START_OBJECT));
|
279 | 317 | assertThat(parser.nextToken(), equalTo(XContentParser.Token.FIELD_NAME));
|
280 | 318 |
|
281 | 319 | IllegalArgumentException exception = expectThrows(IllegalArgumentException.class, () -> ManageRolesPrivilege.parse(parser));
|
282 | 320 |
|
283 |
| - assertThat(exception.getMessage(), containsString("unknown index privilege [foobar]")); |
| 321 | + assertThat(exception.getMessage(), containsString("unknown index privilege [" + expectedErrorDetail + "]")); |
284 | 322 | }
|
285 | 323 | }
|
286 | 324 |
|
|
0 commit comments