Skip to content

Commit

Permalink
improve a few error messages in tests (#228)
Browse files Browse the repository at this point in the history
Signed-off-by: Craig Disselkoen <cdiss@amazon.com>
  • Loading branch information
cdisselkoen authored Oct 2, 2024
1 parent 1a577d4 commit 9af3eb0
Showing 1 changed file with 38 additions and 33 deletions.
71 changes: 38 additions & 33 deletions CedarJavaFFI/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,48 +24,46 @@ use cedar_policy::ffi::{AuthorizationAnswer, ValidationAnswer};
use cool_asserts::assert_matches;

#[track_caller]
fn assert_failure(result: String) {
let result: Answer = serde_json::from_str(result.as_str()).unwrap();
fn assert_failure(result: &str) {
let result: Answer = serde_json::from_str(result).unwrap();
assert_matches!(result, Answer::Failure { .. });
}

fn assert_success(result: String) {
let result: Answer = serde_json::from_str(result.as_str()).unwrap();
match result {
Answer::Success { .. } => {}
Answer::Failure { .. } => panic!("expected a success, not {:?}", result),
};
#[track_caller]
fn assert_success(result: &str) {
let result: Answer = serde_json::from_str(result).unwrap();
assert_matches!(result, Answer::Success { .. });
}

#[track_caller]
fn assert_authorization_success(result: String) {
let result: AuthorizationAnswer = serde_json::from_str(result.as_str()).unwrap();
fn assert_authorization_success(result: &str) {
let result: AuthorizationAnswer = serde_json::from_str(result).unwrap();
assert_matches!(result, AuthorizationAnswer::Success { .. });
}

#[track_caller]
fn assert_authorization_failure(result: String) {
let result: AuthorizationAnswer = serde_json::from_str(result.as_str()).unwrap();
fn assert_authorization_failure(result: &str) {
let result: AuthorizationAnswer = serde_json::from_str(result).unwrap();
assert_matches!(result, AuthorizationAnswer::Failure { .. });
}

#[cfg(feature = "partial-eval")]
#[track_caller]
fn assert_partial_authorization_success(result: String) {
let result: PartialAuthorizationAnswer = serde_json::from_str(result.as_str()).unwrap();
fn assert_partial_authorization_success(result: &str) {
let result: PartialAuthorizationAnswer = serde_json::from_str(result).unwrap();
assert_matches!(result, PartialAuthorizationAnswer::Residuals { .. });
}

#[track_caller]
fn assert_validation_success(result: String) {
let result: ValidationAnswer = serde_json::from_str(result.as_str()).unwrap();
fn assert_validation_success(result: &str) {
let result: ValidationAnswer = serde_json::from_str(result).unwrap();
assert_matches!(result, ValidationAnswer::Success { .. });
}

#[test]
fn unrecognized_call_fails() {
let result = call_cedar("BadOperation", "");
assert_failure(result);
assert_failure(&result);
}

mod authorization_tests {
Expand All @@ -86,7 +84,7 @@ mod authorization_tests {
}
"#,
);
assert_authorization_success(result);
assert_authorization_success(&result);
}

#[test]
Expand All @@ -110,7 +108,7 @@ mod authorization_tests {
}
"#,
);
assert_authorization_failure(result);
assert_authorization_failure(&result);
}

#[test]
Expand All @@ -134,7 +132,7 @@ mod authorization_tests {
}
"#,
);
assert_authorization_failure(result);
assert_authorization_failure(&result);
}

#[test]
Expand Down Expand Up @@ -178,7 +176,7 @@ mod authorization_tests {
}
"#,
);
assert_authorization_success(result);
assert_authorization_success(&result);
}
}

Expand All @@ -188,13 +186,13 @@ mod validation_tests {
#[test]
fn empty_validation_call_json_schema_succeeds() {
let result = call_cedar("ValidateOperation", r#"{ "schema": {}, "policies": {} }"#);
assert_validation_success(result);
assert_validation_success(&result);
}

#[test]
fn empty_validation_call_succeeds() {
let result = call_cedar("ValidateOperation", r#"{ "schema": "", "policies": {} }"#);
assert_validation_success(result);
assert_validation_success(&result);
}
}

Expand Down Expand Up @@ -353,7 +351,7 @@ mod entity_validation_tests {
}
});
let result = call_cedar("ValidateEntities", json_data.to_string().as_str());
assert_success(result);
assert_success(&result);
}

#[test]
Expand Down Expand Up @@ -503,13 +501,13 @@ mod entity_validation_tests {
}
});
let result = call_cedar("ValidateEntities", json_data.to_string().as_str());
assert_failure(result);
assert_failure(&result);
}

#[test]
#[should_panic]
fn validate_entities_invalid_json_fails() {
let result = call_cedar("ValidateEntities", "{]");
call_cedar("ValidateEntities", "{]");
}

#[test]
Expand Down Expand Up @@ -538,10 +536,14 @@ mod entity_validation_tests {
}
});
let result = call_cedar("ValidateEntities", json_data.to_string().as_str());
assert_failure(result.clone());
assert_failure(&result);

assert!(result
.contains("unknown field `shape44`, expected one of `memberOfTypes`, `shape`, `tags`"));
assert!(
result.contains(
"unknown field `shape44`, expected one of `memberOfTypes`, `shape`, `tags`"
),
"result was `{result}`",
);
}

#[test]
Expand Down Expand Up @@ -595,9 +597,12 @@ mod entity_validation_tests {
}
});
let result = call_cedar("ValidateEntities", json_data.to_string().as_str());
assert_failure(result.clone());
assert_failure(&result);

assert!(result.contains("input graph has a cycle containing vertex `PhotoApp::UserGroup"));
assert!(
result.contains("input graph has a cycle containing vertex `PhotoApp::UserGroup"),
"result was `{result}`",
);
}
}

Expand Down Expand Up @@ -625,7 +630,7 @@ mod partial_authorization_tests {
}
"#,
);
assert_partial_authorization_success(result);
assert_partial_authorization_success(&result);
}

#[test]
Expand All @@ -648,7 +653,7 @@ mod partial_authorization_tests {
}
"#,
);
assert_partial_authorization_success(result);
assert_partial_authorization_success(&result);
}
}

Expand Down

0 comments on commit 9af3eb0

Please sign in to comment.