Skip to content

Commit

Permalink
Add tombstone tests, better test errors (#2046)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic authored Jan 20, 2022
1 parent 272dc3e commit f23fed7
Show file tree
Hide file tree
Showing 14 changed files with 104 additions and 46 deletions.
4 changes: 4 additions & 0 deletions crates/apub/assets/lemmy/objects/tombstone.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"id": "https://lemmy.ml/comment/110273",
"type": "Tombstone"
}
7 changes: 7 additions & 0 deletions crates/apub/assets/lotide/activities/delete_note.json
Original file line number Diff line number Diff line change
@@ -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"
}
6 changes: 6 additions & 0 deletions crates/apub/assets/lotide/objects/tombstone.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"former_type": "Note",
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://narwhal.city/posts/12",
"type": "Tombstone"
}
14 changes: 13 additions & 1 deletion crates/apub/assets/mastodon/objects/note.json
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -19,7 +31,7 @@
"conversation": "tag:mamot.fr,2021-11-05:objectId=64635960:objectType=Conversation",
"content": "<p><span class=\"h-card\"><a href=\"https://mamot.fr/@retiolus\" class=\"u-url mention\">@<span>retiolus</span></a></span> 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.</p>",
"contentMap": {
"en": "<p><span class=\"h-card\"><a href=\"https://mamot.fr/@retiolus\" class=\"u-url mention\">@<span>retiolus</span></a></span> 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.</p>"
"en": "<p><span class=\"h-card\"><a href=\"https://mamot.fr/@retiolus\" class=\"u-url mention\">@<span>retiolus</span></a></span> 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.</p>"
},
"attachment": [],
"tag": [
Expand Down
8 changes: 3 additions & 5 deletions crates/apub/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: DeserializeOwned>(
path: &str,
) -> serde_json::error::Result<T> {
let file = File::open(path).unwrap();
pub(crate) fn file_to_json_object<T: DeserializeOwned>(path: &str) -> Result<T, LemmyError> {
let file = File::open(path)?;
let reader = BufReader::new(file);
serde_json::from_reader(reader)
Ok(serde_json::from_reader(reader)?)
}
}
19 changes: 12 additions & 7 deletions crates/apub/src/protocol/activities/community/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,27 @@ mod tests {
async fn test_parse_lemmy_community() {
test_parse_lemmy_item::<AnnounceActivity>(
"assets/lemmy/activities/community/announce_create_page.json",
);
)
.unwrap();

test_parse_lemmy_item::<AddMod>("assets/lemmy/activities/community/add_mod.json");
test_parse_lemmy_item::<RemoveMod>("assets/lemmy/activities/community/remove_mod.json");
test_parse_lemmy_item::<AddMod>("assets/lemmy/activities/community/add_mod.json").unwrap();
test_parse_lemmy_item::<RemoveMod>("assets/lemmy/activities/community/remove_mod.json")
.unwrap();

test_parse_lemmy_item::<BlockUserFromCommunity>(
"assets/lemmy/activities/community/block_user.json",
);
)
.unwrap();
test_parse_lemmy_item::<UndoBlockUserFromCommunity>(
"assets/lemmy/activities/community/undo_block_user.json",
);
)
.unwrap();

test_parse_lemmy_item::<UpdateCommunity>(
"assets/lemmy/activities/community/update_community.json",
);
)
.unwrap();

test_parse_lemmy_item::<Report>("assets/lemmy/activities/community/report_page.json");
test_parse_lemmy_item::<Report>("assets/lemmy/activities/community/report_page.json").unwrap();
}
}
9 changes: 6 additions & 3 deletions crates/apub/src/protocol/activities/create_or_update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@ mod tests {
async fn test_parse_create_or_update() {
test_parse_lemmy_item::<CreateOrUpdatePost>(
"assets/lemmy/activities/create_or_update/create_page.json",
);
)
.unwrap();
test_parse_lemmy_item::<CreateOrUpdatePost>(
"assets/lemmy/activities/create_or_update/update_page.json",
);
)
.unwrap();
test_parse_lemmy_item::<CreateOrUpdateComment>(
"assets/lemmy/activities/create_or_update/create_note.json",
);
)
.unwrap();

file_to_json_object::<WithContext<CreateOrUpdateComment>>(
"assets/pleroma/activities/create_note.json",
Expand Down
10 changes: 6 additions & 4 deletions crates/apub/src/protocol/activities/deletion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ mod tests {

#[actix_rt::test]
async fn test_parse_lemmy_deletion() {
test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/remove_note.json");
test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/delete_page.json");
test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/remove_note.json").unwrap();
test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/delete_page.json").unwrap();

test_parse_lemmy_item::<UndoDelete>("assets/lemmy/activities/deletion/undo_remove_note.json");
test_parse_lemmy_item::<UndoDelete>("assets/lemmy/activities/deletion/undo_delete_page.json");
test_parse_lemmy_item::<UndoDelete>("assets/lemmy/activities/deletion/undo_remove_note.json")
.unwrap();
test_parse_lemmy_item::<UndoDelete>("assets/lemmy/activities/deletion/undo_delete_page.json")
.unwrap();
}
}
9 changes: 6 additions & 3 deletions crates/apub/src/protocol/activities/following/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,14 @@ mod tests {

#[actix_rt::test]
async fn test_parse_lemmy_accept_follow() {
test_parse_lemmy_item::<FollowCommunity>("assets/lemmy/activities/following/follow.json");
test_parse_lemmy_item::<AcceptFollowCommunity>("assets/lemmy/activities/following/accept.json");
test_parse_lemmy_item::<FollowCommunity>("assets/lemmy/activities/following/follow.json")
.unwrap();
test_parse_lemmy_item::<AcceptFollowCommunity>("assets/lemmy/activities/following/accept.json")
.unwrap();
test_parse_lemmy_item::<UndoFollowCommunity>(
"assets/lemmy/activities/following/undo_follow.json",
);
)
.unwrap();

file_to_json_object::<WithContext<FollowCommunity>>("assets/pleroma/activities/follow.json")
.unwrap();
Expand Down
9 changes: 6 additions & 3 deletions crates/apub/src/protocol/activities/private_message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@ mod tests {
async fn test_parse_lemmy_private_message() {
test_parse_lemmy_item::<CreateOrUpdatePrivateMessage>(
"assets/lemmy/activities/private_message/create.json",
);
)
.unwrap();
test_parse_lemmy_item::<DeletePrivateMessage>(
"assets/lemmy/activities/private_message/delete.json",
);
)
.unwrap();
test_parse_lemmy_item::<UndoDeletePrivateMessage>(
"assets/lemmy/activities/private_message/undo_delete.json",
);
)
.unwrap();
}
}
10 changes: 6 additions & 4 deletions crates/apub/src/protocol/activities/voting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ mod tests {

#[actix_rt::test]
async fn test_parse_lemmy_voting() {
test_parse_lemmy_item::<Vote>("assets/lemmy/activities/voting/like_note.json");
test_parse_lemmy_item::<Vote>("assets/lemmy/activities/voting/dislike_page.json");
test_parse_lemmy_item::<Vote>("assets/lemmy/activities/voting/like_note.json").unwrap();
test_parse_lemmy_item::<Vote>("assets/lemmy/activities/voting/dislike_page.json").unwrap();

test_parse_lemmy_item::<UndoVote>("assets/lemmy/activities/voting/undo_like_note.json");
test_parse_lemmy_item::<UndoVote>("assets/lemmy/activities/voting/undo_dislike_page.json");
test_parse_lemmy_item::<UndoVote>("assets/lemmy/activities/voting/undo_like_note.json")
.unwrap();
test_parse_lemmy_item::<UndoVote>("assets/lemmy/activities/voting/undo_dislike_page.json")
.unwrap();
}
}
11 changes: 7 additions & 4 deletions crates/apub/src/protocol/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ mod tests {

#[actix_rt::test]
async fn test_parse_lemmy_collections() {
test_parse_lemmy_item::<GroupFollowers>("assets/lemmy/collections/group_followers.json");
let outbox = test_parse_lemmy_item::<GroupOutbox>("assets/lemmy/collections/group_outbox.json");
test_parse_lemmy_item::<GroupFollowers>("assets/lemmy/collections/group_followers.json")
.unwrap();
let outbox =
test_parse_lemmy_item::<GroupOutbox>("assets/lemmy/collections/group_outbox.json").unwrap();
assert_eq!(outbox.ordered_items.len() as i32, outbox.total_items);
test_parse_lemmy_item::<GroupModerators>("assets/lemmy/collections/group_moderators.json");
test_parse_lemmy_item::<PersonOutbox>("assets/lemmy/collections/person_outbox.json");
test_parse_lemmy_item::<GroupModerators>("assets/lemmy/collections/group_moderators.json")
.unwrap();
test_parse_lemmy_item::<PersonOutbox>("assets/lemmy/collections/person_outbox.json").unwrap();
}
}
9 changes: 5 additions & 4 deletions crates/apub/src/protocol/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,22 @@ pub struct Unparsed(HashMap<String, serde_json::Value>);
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;

/// Check that json deserialize -> serialize -> deserialize gives identical file as initial one.
/// Ensures that there are no breaking changes in sent data.
pub(crate) fn test_parse_lemmy_item<T: Serialize + DeserializeOwned + std::fmt::Debug>(
path: &str,
) -> T {
) -> Result<T, LemmyError> {
// parse file as T
let parsed = file_to_json_object::<T>(path).unwrap();
let parsed = file_to_json_object::<T>(path)?;

// parse file into hashmap, which ensures that every field is included
let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path).unwrap();
let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(path)?;
// assert that all fields are identical, otherwise print diff
assert_json_include!(actual: &parsed, expected: raw);
parsed
Ok(parsed)
}
}
25 changes: 17 additions & 8 deletions crates/apub/src/protocol/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Person>("assets/lemmy/objects/person.json");
test_parse_lemmy_item::<Group>("assets/lemmy/objects/group.json");
test_parse_lemmy_item::<Page>("assets/lemmy/objects/page.json");
test_parse_lemmy_item::<Note>("assets/lemmy/objects/note.json");
test_parse_lemmy_item::<ChatMessage>("assets/lemmy/objects/chat_message.json");
test_parse_lemmy_item::<Person>("assets/lemmy/objects/person.json").unwrap();
test_parse_lemmy_item::<Group>("assets/lemmy/objects/group.json").unwrap();
test_parse_lemmy_item::<Page>("assets/lemmy/objects/page.json").unwrap();
test_parse_lemmy_item::<Note>("assets/lemmy/objects/note.json").unwrap();
test_parse_lemmy_item::<ChatMessage>("assets/lemmy/objects/chat_message.json").unwrap();
test_parse_lemmy_item::<Tombstone>("assets/lemmy/objects/tombstone.json").unwrap();
}

#[actix_rt::test]
Expand All @@ -50,8 +58,8 @@ mod tests {

#[actix_rt::test]
async fn test_parse_object_mastodon() {
file_to_json_object::<Person>("assets/mastodon/objects/person.json").unwrap();
file_to_json_object::<Note>("assets/mastodon/objects/note.json").unwrap();
file_to_json_object::<WithContext<Person>>("assets/mastodon/objects/person.json").unwrap();
file_to_json_object::<WithContext<Note>>("assets/mastodon/objects/note.json").unwrap();
}

#[actix_rt::test]
Expand All @@ -60,5 +68,6 @@ mod tests {
file_to_json_object::<WithContext<Person>>("assets/lotide/objects/person.json").unwrap();
file_to_json_object::<WithContext<Note>>("assets/lotide/objects/note.json").unwrap();
file_to_json_object::<WithContext<Page>>("assets/lotide/objects/page.json").unwrap();
file_to_json_object::<WithContext<Tombstone>>("assets/lotide/objects/tombstone.json").unwrap();
}
}

0 comments on commit f23fed7

Please sign in to comment.