From f23fed70bc347913a35bcc56cb825b4a2b449ccb Mon Sep 17 00:00:00 2001 From: Nutomic Date: Thu, 20 Jan 2022 14:13:29 +0000 Subject: [PATCH] Add tombstone tests, better test errors (#2046) --- .../apub/assets/lemmy/objects/tombstone.json | 4 +++ .../assets/lotide/activities/delete_note.json | 7 ++++++ .../apub/assets/lotide/objects/tombstone.json | 6 +++++ crates/apub/assets/mastodon/objects/note.json | 14 ++++++++++- crates/apub/src/objects/mod.rs | 8 +++--- .../src/protocol/activities/community/mod.rs | 19 ++++++++------ .../activities/create_or_update/mod.rs | 9 ++++--- .../src/protocol/activities/deletion/mod.rs | 10 +++++--- .../src/protocol/activities/following/mod.rs | 9 ++++--- .../activities/private_message/mod.rs | 9 ++++--- .../src/protocol/activities/voting/mod.rs | 10 +++++--- crates/apub/src/protocol/collections/mod.rs | 11 +++++--- crates/apub/src/protocol/mod.rs | 9 ++++--- crates/apub/src/protocol/objects/mod.rs | 25 +++++++++++++------ 14 files changed, 104 insertions(+), 46 deletions(-) create mode 100644 crates/apub/assets/lemmy/objects/tombstone.json create mode 100644 crates/apub/assets/lotide/activities/delete_note.json create mode 100644 crates/apub/assets/lotide/objects/tombstone.json diff --git a/crates/apub/assets/lemmy/objects/tombstone.json b/crates/apub/assets/lemmy/objects/tombstone.json new file mode 100644 index 00000000..e2dc8cc6 --- /dev/null +++ b/crates/apub/assets/lemmy/objects/tombstone.json @@ -0,0 +1,4 @@ +{ + "id": "https://lemmy.ml/comment/110273", + "type": "Tombstone" +} diff --git a/crates/apub/assets/lotide/activities/delete_note.json b/crates/apub/assets/lotide/activities/delete_note.json new file mode 100644 index 00000000..1cd44915 --- /dev/null +++ b/crates/apub/assets/lotide/activities/delete_note.json @@ -0,0 +1,7 @@ +{ + "actor": "https://narwhal.city/users/3", + "object": "https://narwhal.city/posts/12", + "@context": "https://www.w3.org/ns/activitystreams", + "id": "https://narwhal.city/posts/12/delete", + "type": "Delete" +} diff --git a/crates/apub/assets/lotide/objects/tombstone.json b/crates/apub/assets/lotide/objects/tombstone.json new file mode 100644 index 00000000..def00e6e --- /dev/null +++ b/crates/apub/assets/lotide/objects/tombstone.json @@ -0,0 +1,6 @@ +{ + "former_type": "Note", + "@context": "https://www.w3.org/ns/activitystreams", + "id": "https://narwhal.city/posts/12", + "type": "Tombstone" +} diff --git a/crates/apub/assets/mastodon/objects/note.json b/crates/apub/assets/mastodon/objects/note.json index cf0d9fe5..176b4035 100644 --- a/crates/apub/assets/mastodon/objects/note.json +++ b/crates/apub/assets/mastodon/objects/note.json @@ -1,4 +1,16 @@ { + "@context": [ + "https://www.w3.org/ns/activitystreams", + { + "ostatus": "http://ostatus.org#", + "atomUri": "ostatus:atomUri", + "inReplyToAtomUri": "ostatus:inReplyToAtomUri", + "conversation": "ostatus:conversation", + "sensitive": "as:sensitive", + "toot": "http://joinmastodon.org/ns#", + "votersCount": "toot:votersCount" + } + ], "id": "https://mastodon.madrid/users/felix/statuses/107224289116410645", "type": "Note", "summary": null, @@ -19,7 +31,7 @@ "conversation": "tag:mamot.fr,2021-11-05:objectId=64635960:objectType=Conversation", "content": "

@retiolus i have never been disappointed by a thinkpad. if you want to save money, get a model from a few years ago, there isnt a huge difference anyway.

", "contentMap": { - "en": "

@retiolus i have neverbeendisappointed by a thinkpad. if you want to save money, get a model from a few years ago, there isnt a huge difference anyway.

" + "en": "

@retiolus i have never been disappointed by a thinkpad. if you want to save money, get a model from a few years ago, there isnt a huge difference anyway.

" }, "attachment": [], "tag": [ diff --git a/crates/apub/src/objects/mod.rs b/crates/apub/src/objects/mod.rs index 23503839..b5a4760d 100644 --- a/crates/apub/src/objects/mod.rs +++ b/crates/apub/src/objects/mod.rs @@ -90,11 +90,9 @@ pub(crate) mod tests { LemmyContext::create(pool, chat_server, client, activity_queue, settings, secret) } - pub(crate) fn file_to_json_object( - path: &str, - ) -> serde_json::error::Result { - let file = File::open(path).unwrap(); + pub(crate) fn file_to_json_object(path: &str) -> Result { + let file = File::open(path)?; let reader = BufReader::new(file); - serde_json::from_reader(reader) + Ok(serde_json::from_reader(reader)?) } } diff --git a/crates/apub/src/protocol/activities/community/mod.rs b/crates/apub/src/protocol/activities/community/mod.rs index 4f1f2be9..56332a87 100644 --- a/crates/apub/src/protocol/activities/community/mod.rs +++ b/crates/apub/src/protocol/activities/community/mod.rs @@ -25,22 +25,27 @@ mod tests { async fn test_parse_lemmy_community() { test_parse_lemmy_item::( "assets/lemmy/activities/community/announce_create_page.json", - ); + ) + .unwrap(); - test_parse_lemmy_item::("assets/lemmy/activities/community/add_mod.json"); - test_parse_lemmy_item::("assets/lemmy/activities/community/remove_mod.json"); + test_parse_lemmy_item::("assets/lemmy/activities/community/add_mod.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/activities/community/remove_mod.json") + .unwrap(); test_parse_lemmy_item::( "assets/lemmy/activities/community/block_user.json", - ); + ) + .unwrap(); test_parse_lemmy_item::( "assets/lemmy/activities/community/undo_block_user.json", - ); + ) + .unwrap(); test_parse_lemmy_item::( "assets/lemmy/activities/community/update_community.json", - ); + ) + .unwrap(); - test_parse_lemmy_item::("assets/lemmy/activities/community/report_page.json"); + test_parse_lemmy_item::("assets/lemmy/activities/community/report_page.json").unwrap(); } } diff --git a/crates/apub/src/protocol/activities/create_or_update/mod.rs b/crates/apub/src/protocol/activities/create_or_update/mod.rs index 160bbfba..43576e44 100644 --- a/crates/apub/src/protocol/activities/create_or_update/mod.rs +++ b/crates/apub/src/protocol/activities/create_or_update/mod.rs @@ -16,13 +16,16 @@ mod tests { async fn test_parse_create_or_update() { test_parse_lemmy_item::( "assets/lemmy/activities/create_or_update/create_page.json", - ); + ) + .unwrap(); test_parse_lemmy_item::( "assets/lemmy/activities/create_or_update/update_page.json", - ); + ) + .unwrap(); test_parse_lemmy_item::( "assets/lemmy/activities/create_or_update/create_note.json", - ); + ) + .unwrap(); file_to_json_object::>( "assets/pleroma/activities/create_note.json", diff --git a/crates/apub/src/protocol/activities/deletion/mod.rs b/crates/apub/src/protocol/activities/deletion/mod.rs index a4e08fab..80254834 100644 --- a/crates/apub/src/protocol/activities/deletion/mod.rs +++ b/crates/apub/src/protocol/activities/deletion/mod.rs @@ -10,10 +10,12 @@ mod tests { #[actix_rt::test] async fn test_parse_lemmy_deletion() { - test_parse_lemmy_item::("assets/lemmy/activities/deletion/remove_note.json"); - test_parse_lemmy_item::("assets/lemmy/activities/deletion/delete_page.json"); + test_parse_lemmy_item::("assets/lemmy/activities/deletion/remove_note.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/activities/deletion/delete_page.json").unwrap(); - test_parse_lemmy_item::("assets/lemmy/activities/deletion/undo_remove_note.json"); - test_parse_lemmy_item::("assets/lemmy/activities/deletion/undo_delete_page.json"); + test_parse_lemmy_item::("assets/lemmy/activities/deletion/undo_remove_note.json") + .unwrap(); + test_parse_lemmy_item::("assets/lemmy/activities/deletion/undo_delete_page.json") + .unwrap(); } } diff --git a/crates/apub/src/protocol/activities/following/mod.rs b/crates/apub/src/protocol/activities/following/mod.rs index f855cb32..2b21b483 100644 --- a/crates/apub/src/protocol/activities/following/mod.rs +++ b/crates/apub/src/protocol/activities/following/mod.rs @@ -19,11 +19,14 @@ mod tests { #[actix_rt::test] async fn test_parse_lemmy_accept_follow() { - test_parse_lemmy_item::("assets/lemmy/activities/following/follow.json"); - test_parse_lemmy_item::("assets/lemmy/activities/following/accept.json"); + test_parse_lemmy_item::("assets/lemmy/activities/following/follow.json") + .unwrap(); + test_parse_lemmy_item::("assets/lemmy/activities/following/accept.json") + .unwrap(); test_parse_lemmy_item::( "assets/lemmy/activities/following/undo_follow.json", - ); + ) + .unwrap(); file_to_json_object::>("assets/pleroma/activities/follow.json") .unwrap(); diff --git a/crates/apub/src/protocol/activities/private_message/mod.rs b/crates/apub/src/protocol/activities/private_message/mod.rs index de074ba6..04be31b1 100644 --- a/crates/apub/src/protocol/activities/private_message/mod.rs +++ b/crates/apub/src/protocol/activities/private_message/mod.rs @@ -17,12 +17,15 @@ mod tests { async fn test_parse_lemmy_private_message() { test_parse_lemmy_item::( "assets/lemmy/activities/private_message/create.json", - ); + ) + .unwrap(); test_parse_lemmy_item::( "assets/lemmy/activities/private_message/delete.json", - ); + ) + .unwrap(); test_parse_lemmy_item::( "assets/lemmy/activities/private_message/undo_delete.json", - ); + ) + .unwrap(); } } diff --git a/crates/apub/src/protocol/activities/voting/mod.rs b/crates/apub/src/protocol/activities/voting/mod.rs index b38e7fe3..ab405d0d 100644 --- a/crates/apub/src/protocol/activities/voting/mod.rs +++ b/crates/apub/src/protocol/activities/voting/mod.rs @@ -10,10 +10,12 @@ mod tests { #[actix_rt::test] async fn test_parse_lemmy_voting() { - test_parse_lemmy_item::("assets/lemmy/activities/voting/like_note.json"); - test_parse_lemmy_item::("assets/lemmy/activities/voting/dislike_page.json"); + test_parse_lemmy_item::("assets/lemmy/activities/voting/like_note.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/activities/voting/dislike_page.json").unwrap(); - test_parse_lemmy_item::("assets/lemmy/activities/voting/undo_like_note.json"); - test_parse_lemmy_item::("assets/lemmy/activities/voting/undo_dislike_page.json"); + test_parse_lemmy_item::("assets/lemmy/activities/voting/undo_like_note.json") + .unwrap(); + test_parse_lemmy_item::("assets/lemmy/activities/voting/undo_dislike_page.json") + .unwrap(); } } diff --git a/crates/apub/src/protocol/collections/mod.rs b/crates/apub/src/protocol/collections/mod.rs index 214bd6d3..183052be 100644 --- a/crates/apub/src/protocol/collections/mod.rs +++ b/crates/apub/src/protocol/collections/mod.rs @@ -17,10 +17,13 @@ mod tests { #[actix_rt::test] async fn test_parse_lemmy_collections() { - test_parse_lemmy_item::("assets/lemmy/collections/group_followers.json"); - let outbox = test_parse_lemmy_item::("assets/lemmy/collections/group_outbox.json"); + test_parse_lemmy_item::("assets/lemmy/collections/group_followers.json") + .unwrap(); + let outbox = + test_parse_lemmy_item::("assets/lemmy/collections/group_outbox.json").unwrap(); assert_eq!(outbox.ordered_items.len() as i32, outbox.total_items); - test_parse_lemmy_item::("assets/lemmy/collections/group_moderators.json"); - test_parse_lemmy_item::("assets/lemmy/collections/person_outbox.json"); + test_parse_lemmy_item::("assets/lemmy/collections/group_moderators.json") + .unwrap(); + test_parse_lemmy_item::("assets/lemmy/collections/person_outbox.json").unwrap(); } } diff --git a/crates/apub/src/protocol/mod.rs b/crates/apub/src/protocol/mod.rs index a7a23396..4b3992fd 100644 --- a/crates/apub/src/protocol/mod.rs +++ b/crates/apub/src/protocol/mod.rs @@ -42,6 +42,7 @@ pub struct Unparsed(HashMap); pub(crate) mod tests { use crate::objects::tests::file_to_json_object; use assert_json_diff::assert_json_include; + use lemmy_utils::LemmyError; use serde::{de::DeserializeOwned, Serialize}; use std::collections::HashMap; @@ -49,14 +50,14 @@ pub(crate) mod tests { /// Ensures that there are no breaking changes in sent data. pub(crate) fn test_parse_lemmy_item( path: &str, - ) -> T { + ) -> Result { // parse file as T - let parsed = file_to_json_object::(path).unwrap(); + let parsed = file_to_json_object::(path)?; // parse file into hashmap, which ensures that every field is included - let raw = file_to_json_object::>(path).unwrap(); + let raw = file_to_json_object::>(path)?; // assert that all fields are identical, otherwise print diff assert_json_include!(actual: &parsed, expected: raw); - parsed + Ok(parsed) } } diff --git a/crates/apub/src/protocol/objects/mod.rs b/crates/apub/src/protocol/objects/mod.rs index 04c65357..139fe0fc 100644 --- a/crates/apub/src/protocol/objects/mod.rs +++ b/crates/apub/src/protocol/objects/mod.rs @@ -20,18 +20,26 @@ mod tests { context::WithContext, objects::tests::file_to_json_object, protocol::{ - objects::{chat_message::ChatMessage, group::Group, note::Note, page::Page, person::Person}, + objects::{ + chat_message::ChatMessage, + group::Group, + note::Note, + page::Page, + person::Person, + tombstone::Tombstone, + }, tests::test_parse_lemmy_item, }, }; #[actix_rt::test] async fn test_parse_object_lemmy() { - test_parse_lemmy_item::("assets/lemmy/objects/person.json"); - test_parse_lemmy_item::("assets/lemmy/objects/group.json"); - test_parse_lemmy_item::("assets/lemmy/objects/page.json"); - test_parse_lemmy_item::("assets/lemmy/objects/note.json"); - test_parse_lemmy_item::("assets/lemmy/objects/chat_message.json"); + test_parse_lemmy_item::("assets/lemmy/objects/person.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/group.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/page.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/note.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/chat_message.json").unwrap(); + test_parse_lemmy_item::("assets/lemmy/objects/tombstone.json").unwrap(); } #[actix_rt::test] @@ -50,8 +58,8 @@ mod tests { #[actix_rt::test] async fn test_parse_object_mastodon() { - file_to_json_object::("assets/mastodon/objects/person.json").unwrap(); - file_to_json_object::("assets/mastodon/objects/note.json").unwrap(); + file_to_json_object::>("assets/mastodon/objects/person.json").unwrap(); + file_to_json_object::>("assets/mastodon/objects/note.json").unwrap(); } #[actix_rt::test] @@ -60,5 +68,6 @@ mod tests { file_to_json_object::>("assets/lotide/objects/person.json").unwrap(); file_to_json_object::>("assets/lotide/objects/note.json").unwrap(); file_to_json_object::>("assets/lotide/objects/page.json").unwrap(); + file_to_json_object::>("assets/lotide/objects/tombstone.json").unwrap(); } } -- 2.44.1