-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Signed-off-by: Andrew Wells <anmwells@amazon.com> Co-authored-by: Kesha Hietala <khieta@amazon.com>
- Loading branch information
1 parent
8d0605a
commit 30eb00f
Showing
6 changed files
with
169 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,3 +22,4 @@ mod utils; | |
pub use utils::*; | ||
mod validate; | ||
pub use validate::*; | ||
mod tests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
#[cfg(test)] | ||
mod ffi_tests { | ||
#[cfg(feature = "partial-eval")] | ||
use crate::ffi::is_authorized_partial_json; | ||
use crate::ffi::{is_authorized_json, validate_json}; | ||
use cool_asserts::assert_matches; | ||
|
||
#[test] | ||
fn test_fail_unknown_field_policy_slice() { | ||
let json = serde_json::json!({ | ||
"principal": { | ||
"type": "User", | ||
"id": "alice" | ||
}, | ||
"action": { | ||
"type": "Photo", | ||
"id": "view" | ||
}, | ||
"resource": { | ||
"type": "Photo", | ||
"id": "door" | ||
}, | ||
"context": {}, | ||
"slice": { | ||
"policies": {}, | ||
"templatePolicies": {}, | ||
"entities": [] | ||
} | ||
}); | ||
|
||
assert_matches!(is_authorized_json(json), Err(e) => { | ||
assert_eq!(e.to_string(), "unknown field `slice`, expected one of `principal`, `action`, `resource`, `context`, `schema`, `validateRequest`, `policies`, `entities`"); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_fail_unknown_field_enable_request_validation() { | ||
let json = serde_json::json!({ | ||
"principal": { | ||
"type": "User", | ||
"id": "alice" | ||
}, | ||
"action": { | ||
"type": "Photo", | ||
"id": "view" | ||
}, | ||
"resource": { | ||
"type": "Photo", | ||
"id": "door" | ||
}, | ||
"context": {}, | ||
"policies": {}, | ||
"entities": [], | ||
"enableRequestValidation": true, | ||
}); | ||
|
||
assert_matches!(is_authorized_json(json), Err(e) => { | ||
assert_eq!(e.to_string(), "unknown field `enableRequestValidation`, expected one of `principal`, `action`, `resource`, `context`, `schema`, `validateRequest`, `policies`, `entities`"); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_fail_unknown_field_policies() { | ||
let json = serde_json::json!({ | ||
"principal": { | ||
"type": "User", | ||
"id": "alice" | ||
}, | ||
"action": { | ||
"type": "Photo", | ||
"id": "view" | ||
}, | ||
"resource": { | ||
"type": "Photo", | ||
"id": "door" | ||
}, | ||
"context": {}, | ||
"policies": { | ||
"policies": {} | ||
}, | ||
"entities": [] | ||
}); | ||
|
||
assert_matches!(is_authorized_json(json), Err(e) => { | ||
assert_eq!(e.to_string(), "unknown field `policies`, expected one of `staticPolicies`, `templates`, `templateLinks`"); | ||
}); | ||
} | ||
|
||
#[cfg(feature = "partial-eval")] | ||
#[test] | ||
fn test_fail_unknown_field_partial_evaluation() { | ||
let json = serde_json::json!({ | ||
"principal": { | ||
"type": "User", | ||
"id": "alice" | ||
}, | ||
"action": { | ||
"type": "Photo", | ||
"id": "view" | ||
}, | ||
"context": {}, | ||
"policies": { | ||
"staticPolicies": { | ||
"ID1": "permit(principal == User::\"alice\", action, resource);" | ||
} | ||
}, | ||
"entities": [], | ||
"partial_evaluation": true | ||
}); | ||
|
||
assert_matches!(is_authorized_partial_json(json), Err(e) => { | ||
assert_eq!(e.to_string(), "unknown field `partial_evaluation`, expected one of `principal`, `action`, `resource`, `context`, `schema`, `validateRequest`, `policies`, `entities`"); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_fail_unknown_field_validation() { | ||
let json = serde_json::json!({ | ||
"schema": { "json": { "": { | ||
"entityTypes": { | ||
"User": { | ||
"memberOfTypes": [ ] | ||
}, | ||
"Photo": { | ||
"memberOfTypes": [ ] | ||
} | ||
}, | ||
"actions": { | ||
"viewPhoto": { | ||
"appliesTo": { | ||
"resourceTypes": [ "Photo" ], | ||
"principalTypes": [ "User" ] | ||
} | ||
} | ||
} | ||
}}}, | ||
"Policies": "forbid(principal, action, resource);permit(principal == Photo::\"photo.jpg\", action == Action::\"viewPhoto\", resource == User::\"alice\");" | ||
}); | ||
|
||
assert_matches!(validate_json(json), Err(e) => { | ||
assert_eq!(e.to_string(), "unknown field `Policies`, expected one of `validationSettings`, `schema`, `policies`"); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters