Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions CedarJava/src/test/java/com/cedarpolicy/ValidationTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public void givenExampleSchemaAndCorrectPolicyReturnsValid() {

/** Test. */
@Test
public void givenExampleSchemaAndIncorrectPolicyReturnsValid() {
public void givenExampleSchemaAndImpossiblePolicyReturnsWarning() {
givenSchema(PHOTOFLASH_SCHEMA);
givenPolicy(
"policy0",
Expand All @@ -90,9 +90,9 @@ public void givenExampleSchemaAndIncorrectPolicyReturnsValid() {
+ " resource == User::\"bob\""
+ ");");
ValidationResponse response = whenValidated();
thenIsNotValid(response);
thenHasWarnings(response);
ValidationResponse levelResponse = whenLevelValidated(1);
thenIsNotValid(levelResponse);
thenHasWarnings(levelResponse);
}

/** Test. */
Expand Down Expand Up @@ -169,16 +169,16 @@ public void validateTemplateLinkedPolicyFailsWhenExpected() {
thenValidationFailed(levelResponse3);


// validation returns an error if we provide a link with the wrong type
// validation warns if we provide a link with the wrong type
LinkValue badLink1 = new LinkValue("?resource", EntityUID.parse("Library::User::\"Victor\"").get());
this.policies = new PolicySet(new HashSet<>(), templates,
List.of(new TemplateLink("template1", "policy", List.of(badLink1))));
ValidationResponse response4 = whenValidated();
thenIsNotValid(response4);
thenHasWarnings(response4);
ValidationResponse levelResponse4 = whenLevelValidated(1);
thenIsNotValid(levelResponse4);
thenHasWarnings(levelResponse4);

// validation returns an error if we provide a link with an invalid type
// validation returns an error if we provide a link with an unrecognized type
LinkValue badLink2 = new LinkValue("?resource", EntityUID.parse("Library::BOOK::\"The black Swan\"").get());
this.policies = new PolicySet(new HashSet<>(), templates,
List.of(new TemplateLink("template1", "policy", List.of(badLink2))));
Expand Down Expand Up @@ -284,6 +284,22 @@ private void thenIsNotValid(ValidationResponse response) {
});
}

private void thenHasWarnings(ValidationResponse response) {
assertEquals(response.type, SuccessOrFailure.Success);
final ValidationSuccessResponse success = assertDoesNotThrow(() -> response.success.get());
assertTrue(
success.validationErrors.isEmpty(),
() -> {
String errors = response.success.get().validationErrors.stream()
.map(note -> String.format("in policy %s: %s", note.getPolicyId(), note.getError()))
.collect(Collectors.joining("\n"));
return "Expected no validation errors but got:\n" + errors;
});
assertFalse(
success.validationWarnings.isEmpty(),
() -> "Expected validation warnings but did not find any");
}

private void thenValidationFailed(ValidationResponse response) {
assertEquals(response.type, SuccessOrFailure.Failure);
final List<DetailedError> errors = assertDoesNotThrow(() -> response.errors.get());
Expand Down
2 changes: 1 addition & 1 deletion CedarJavaFFI/src/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1201,7 +1201,7 @@ pub fn get_cedar_schema_internal<'a>(
let cedar_format = schema_to_text(schema);

match cedar_format {
SchemaToTextAnswer::Success { text, warnings } => {
SchemaToTextAnswer::Success { text, .. } => {
let jstr = env.new_string(&text)?;
Ok(JValueGen::Object(JObject::from(jstr)).into())
}
Expand Down
Loading