From 9af3eb0db308c2ac0ebf02d0813a1a06ce52fb0b Mon Sep 17 00:00:00 2001 From: Craig Disselkoen Date: Wed, 2 Oct 2024 07:41:49 -0700 Subject: [PATCH] improve a few error messages in tests (#228) Signed-off-by: Craig Disselkoen --- CedarJavaFFI/src/tests.rs | 71 +++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 33 deletions(-) diff --git a/CedarJavaFFI/src/tests.rs b/CedarJavaFFI/src/tests.rs index c3950e7..8b9e2e6 100644 --- a/CedarJavaFFI/src/tests.rs +++ b/CedarJavaFFI/src/tests.rs @@ -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 { @@ -86,7 +84,7 @@ mod authorization_tests { } "#, ); - assert_authorization_success(result); + assert_authorization_success(&result); } #[test] @@ -110,7 +108,7 @@ mod authorization_tests { } "#, ); - assert_authorization_failure(result); + assert_authorization_failure(&result); } #[test] @@ -134,7 +132,7 @@ mod authorization_tests { } "#, ); - assert_authorization_failure(result); + assert_authorization_failure(&result); } #[test] @@ -178,7 +176,7 @@ mod authorization_tests { } "#, ); - assert_authorization_success(result); + assert_authorization_success(&result); } } @@ -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); } } @@ -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] @@ -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] @@ -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] @@ -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}`", + ); } } @@ -625,7 +630,7 @@ mod partial_authorization_tests { } "#, ); - assert_partial_authorization_success(result); + assert_partial_authorization_success(&result); } #[test] @@ -648,7 +653,7 @@ mod partial_authorization_tests { } "#, ); - assert_partial_authorization_success(result); + assert_partial_authorization_success(&result); } }