()
| 522 | |
| 523 | #[tokio::test(flavor = "multi_thread", worker_threads = 2)] |
| 524 | async fn test_parse_sync_items() -> Result<()> { |
| 525 | let t = TestContext::new_alice().await; |
| 526 | |
| 527 | assert!(t.parse_sync_items(r#"{bad json}"#.to_string()).is_err()); |
| 528 | |
| 529 | assert!(t.parse_sync_items(r#"{"badname":[]}"#.to_string()).is_err()); |
| 530 | |
| 531 | for bad_item_example in [ |
| 532 | r#"{"items":[{"timestamp":1631781316,"data":{"BadItem":{"invitenumber":"in","auth":"a","grpid":null}}}]}"#, |
| 533 | r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":123}}}]}"#, // `123` is invalid for `String` |
| 534 | r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":true}}}]}"#, // `true` is invalid for `String` |
| 535 | r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":[]}}}]}"#, // `[]` is invalid for `String` |
| 536 | r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","auth":{}}}}]}"#, // `{}` is invalid for `String` |
| 537 | r#"{"items":[{"timestamp":1631781316,"data":{"AddQrToken":{"invitenumber":"in","grpid":null}}}]}"#, // missing field |
| 538 | r#"{"items":[{"timestamp":1631781316,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":"Burn"}}}]}"#, // Unknown enum value |
| 539 | ] { |
| 540 | let sync_items = t.parse_sync_items(bad_item_example.to_string()).unwrap(); |
| 541 | assert_eq!(sync_items.items.len(), 1); |
| 542 | assert!(matches!(sync_items.items[0].timestamp, 1631781316)); |
| 543 | assert!(matches!( |
| 544 | sync_items.items[0].data, |
| 545 | SyncDataOrUnknown::Unknown(_) |
| 546 | )); |
| 547 | } |
| 548 | |
| 549 | // Test enums inside items and SystemTime |
| 550 | let sync_items = t.parse_sync_items( |
| 551 | r#"{"items":[{"timestamp":1631781318,"data":{"AlterChat":{"id":{"ContactAddr":"bob@example.net"},"action":{"SetMuted":{"Until":{"secs_since_epoch":42,"nanos_since_epoch":999000000}}}}}}]}"#.to_string(), |
| 552 | )?; |
| 553 | assert_eq!(sync_items.items.len(), 1); |
| 554 | let SyncDataOrUnknown::SyncData(AlterChat { id, action }) = |
| 555 | &sync_items.items.first().unwrap().data |
| 556 | else { |
| 557 | bail!("bad item"); |
| 558 | }; |
| 559 | assert_eq!( |
| 560 | *id, |
| 561 | chat::SyncId::ContactAddr("bob@example.net".to_string()) |
| 562 | ); |
| 563 | assert_eq!( |
| 564 | *action, |
| 565 | chat::SyncAction::SetMuted(chat::MuteDuration::Until( |
| 566 | SystemTime::UNIX_EPOCH + Duration::from_millis(42999) |
| 567 | )) |
| 568 | ); |
| 569 | |
| 570 | // empty item list is okay |
| 571 | assert_eq!( |
| 572 | t.parse_sync_items(r#"{"items":[]}"#.to_string())? |
| 573 | .items |
| 574 | .len(), |
| 575 | 0 |
| 576 | ); |
| 577 | |
| 578 | // to allow forward compatibility, additional fields should not break parsing |
| 579 | let sync_items = t |
| 580 | .parse_sync_items( |
| 581 | r#"{"items":[ |
nothing calls this directly
no test coverage detected