Merge pull request #380 from mozilla-services/fix/376

fix: ignore the collection field in POSTS also
This commit is contained in:
JR Conlin 2019-12-17 12:37:00 -08:00 committed by GitHub
commit 6f68721c6f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 6 deletions

View File

@ -373,13 +373,21 @@ fn put_bso() {
}
#[test]
fn put_meta_storage() {
fn bsos_can_have_a_collection_field() {
let start = SyncTimestamp::default();
// test that "collection" is accepted, even if ignored
let body = json!({"id": "global", "collection": "meta", "payload": "SomePayload"});
let bytes = test_endpoint_with_body(http::Method::PUT, "/1.5/42/storage/meta/global", body);
let result: PutBso = serde_json::from_slice(&bytes).unwrap();
let bso1 = json!({"id": "global", "collection": "meta", "payload": "SomePayload"});
let bsos = json!(
[bso1,
{"id": "2", "collection": "foo", "payload": "SomePayload"},
]);
let bytes = test_endpoint_with_body(http::Method::POST, "/1.5/42/storage/meta", bsos);
let result: PostBsos = serde_json::from_slice(&bytes.to_vec()).unwrap();
assert_eq!(result.success.len(), 2);
assert_eq!(result.failed.len(), 0);
let bytes = test_endpoint_with_body(http::Method::PUT, "/1.5/42/storage/meta/global", bso1);
let result: PutBso = serde_json::from_slice(&bytes).unwrap();
assert!(result >= start);
}

View File

@ -75,8 +75,15 @@ impl BatchBsoBody {
/// Function to convert valid raw JSON BSO body to a BatchBsoBody
fn from_raw_bso(val: &Value) -> Result<BatchBsoBody, String> {
let map = val.as_object().ok_or("invalid json")?;
// Verify all the keys are valid. modified is allowed but ignored
let valid_keys = ["id", "sortindex", "payload", "ttl", "modified"];
// Verify all the keys are valid. modified/collection are allowed but ignored
let valid_keys = [
"id",
"sortindex",
"payload",
"ttl",
"modified",
"collection",
];
for key_name in map.keys() {
if !valid_keys.contains(&key_name.as_str()) {
return Err(format!("unknown field {}", key_name));