Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
albertzaharovits committed Jun 11, 2024
1 parent e587778 commit dbf21bb
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public final class FieldNameTranslators {
)
);

final List<FieldNameTranslator> fieldNameTranslators;
private final List<FieldNameTranslator> fieldNameTranslators;

private FieldNameTranslators(List<FieldNameTranslator> fieldNameTranslators) {
this.fieldNameTranslators = fieldNameTranslators;
Expand Down Expand Up @@ -315,6 +315,10 @@ public Set<String> translatePattern(String fieldNameOrPattern) {
return indexFieldNames;
}

public boolean isFieldAllowed(String fieldName) {
return fieldNameTranslators.stream().anyMatch(t -> t.supports(fieldName));
}

private abstract static class FieldNameTranslator {

private final Function<String, String> translationFunc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ public void testAllowListOfFieldNames() {
final Authentication authentication = randomBoolean() ? AuthenticationTests.randomAuthentication(null, null) : null;

final String randomFieldName = randomValueOtherThanMany(
s -> API_KEY_FIELD_NAME_TRANSLATORS.fieldNameTranslators.stream().anyMatch(t -> t.supports(s)),
API_KEY_FIELD_NAME_TRANSLATORS::isFieldAllowed,
() -> randomAlphaOfLengthBetween(3, 20)
);
final String fieldName = randomFrom(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ public void testAllowListOfFieldNames() {
public void testTermsLookupIsNotAllowed() {
final TermsQueryBuilder q1 = QueryBuilders.termsLookupQuery("roles", new TermsLookup("lookup", "1", "id"));
final IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class, () -> UserBoolQueryBuilder.build(q1));
assertThat(e1.getMessage(), containsString("Terms query with terms lookup is not supported for User query"));
assertThat(e1.getMessage(), containsString("terms query with terms lookup is not currently supported in this context"));
}

public void testDisallowedQueryTypes() {
Expand Down Expand Up @@ -155,7 +155,7 @@ public void testDisallowedQueryTypes() {
);

final IllegalArgumentException e1 = expectThrows(IllegalArgumentException.class, () -> UserBoolQueryBuilder.build(q1));
assertThat(e1.getMessage(), containsString("Query type [" + q1.getName() + "] is not supported for User query"));
assertThat(e1.getMessage(), containsString("Query type [" + q1.getName() + "] is not currently supported in this context"));
}

public void testWillSetAllowedFields() {
Expand Down

0 comments on commit dbf21bb

Please sign in to comment.