Skip to content

Commit

Permalink
Reorganize apub protocol tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nutomic committed Feb 14, 2022
1 parent d86e350 commit 1561521
Show file tree
Hide file tree
Showing 23 changed files with 189 additions and 142 deletions.
7 changes: 7 additions & 0 deletions crates/apub/assets/mastodon/activities/create_note.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
{
"ostatus": "http://ostatus.org#",
"atomUri": "ostatus:atomUri"
}
],
"id": "https://mastodon.madrid/users/felix/statuses/107224289116410645/activity",
"type": "Create",
"actor": "https://mastodon.madrid/users/felix",
Expand Down
7 changes: 7 additions & 0 deletions crates/apub/assets/mastodon/activities/follow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://masto.asonix.dog/1ea87517-63c5-4118-8831-460ee641b2cf",
"type": "Follow",
"actor": "https://masto.asonix.dog/users/asonix",
"object": "https://ds9.lemmy.ml/c/testcom"
}
13 changes: 13 additions & 0 deletions crates/apub/assets/mastodon/activities/undo_follow.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://masto.asonix.dog/users/asonix#follows/449/undo",
"type": "Undo",
"actor": "https://masto.asonix.dog/users/asonix",
"object": {
"id": "https://masto.asonix.dog/1ea87517-63c5-4118-8831-460ee641b2cf",
"type": "Follow",
"actor": "https://masto.asonix.dog/users/asonix",
"object": "https://ds9.lemmy.ml/c/testcom"
}
}

8 changes: 7 additions & 1 deletion crates/apub/assets/smithereen/objects/note.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,11 @@
}
},
"sensitive": false,
"likes": "https://friends.grishka.me/posts/66561/likes"
"likes": "https://friends.grishka.me/posts/66561/likes",
"@context": [
"https://www.w3.org/ns/activitystreams",
{
"sensitive": "as:sensitive"
}
]
}
11 changes: 7 additions & 4 deletions crates/apub/src/collections/community_moderators.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,13 @@ impl ApubObject for ApubCommunityModerators {
#[cfg(test)]
mod tests {
use super::*;
use crate::objects::{
community::tests::parse_lemmy_community,
person::tests::parse_lemmy_person,
tests::{file_to_json_object, init_context},
use crate::{
objects::{
community::tests::parse_lemmy_community,
person::tests::parse_lemmy_person,
tests::init_context,
},
protocol::tests::file_to_json_object,
};
use lemmy_apub_lib::activity_queue::create_activity_queue;
use lemmy_db_schema::{
Expand Down
15 changes: 9 additions & 6 deletions crates/apub/src/objects/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,12 +208,15 @@ impl ApubObject for ApubComment {
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::objects::{
community::{tests::parse_lemmy_community, ApubCommunity},
instance::ApubSite,
person::{tests::parse_lemmy_person, ApubPerson},
post::ApubPost,
tests::{file_to_json_object, init_context},
use crate::{
objects::{
community::{tests::parse_lemmy_community, ApubCommunity},
instance::ApubSite,
person::{tests::parse_lemmy_person, ApubPerson},
post::ApubPost,
tests::init_context,
},
protocol::tests::file_to_json_object,
};
use assert_json_diff::assert_json_include;
use lemmy_apub_lib::activity_queue::create_activity_queue;
Expand Down
6 changes: 3 additions & 3 deletions crates/apub/src/objects/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,9 @@ impl ApubCommunity {
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::objects::{
instance::tests::parse_lemmy_instance,
tests::{file_to_json_object, init_context},
use crate::{
objects::{instance::tests::parse_lemmy_instance, tests::init_context},
protocol::tests::file_to_json_object,
};
use lemmy_apub_lib::activity_queue::create_activity_queue;
use lemmy_db_schema::{source::site::Site, traits::Crud};
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/src/objects/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ pub(in crate::objects) async fn fetch_instance_actor_for_object(
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use crate::objects::tests::{file_to_json_object, init_context};
use crate::{objects::tests::init_context, protocol::tests::file_to_json_object};
use lemmy_apub_lib::activity_queue::create_activity_queue;
use lemmy_db_schema::traits::Crud;
use serial_test::serial;
Expand Down
9 changes: 1 addition & 8 deletions crates/apub/src/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ pub(crate) mod tests {
use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
use reqwest::Client;
use reqwest_middleware::ClientBuilder;
use serde::de::DeserializeOwned;
use std::{fs::File, io::BufReader, sync::Arc};
use std::sync::Arc;
use tokio::sync::Mutex;

// TODO: would be nice if we didnt have to use a full context for tests.
Expand Down Expand Up @@ -90,10 +89,4 @@ pub(crate) mod tests {
.start();
LemmyContext::create(pool, chat_server, client, activity_queue, settings, secret)
}

pub(crate) fn file_to_json_object<T: DeserializeOwned>(path: &str) -> Result<T, LemmyError> {
let file = File::open(path)?;
let reader = BufReader::new(file);
Ok(serde_json::from_reader(reader)?)
}
}
4 changes: 2 additions & 2 deletions crates/apub/src/objects/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,9 @@ pub(crate) mod tests {
use crate::{
objects::{
instance::{tests::parse_lemmy_instance, ApubSite},
tests::{file_to_json_object, init_context},
tests::init_context,
},
protocol::objects::instance::Instance,
protocol::{objects::instance::Instance, tests::file_to_json_object},
};
use lemmy_apub_lib::activity_queue::create_activity_queue;
use lemmy_db_schema::{source::site::Site, traits::Crud};
Expand Down
13 changes: 8 additions & 5 deletions crates/apub/src/objects/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,14 @@ impl ApubObject for ApubPost {
#[cfg(test)]
mod tests {
use super::*;
use crate::objects::{
community::tests::parse_lemmy_community,
person::tests::parse_lemmy_person,
post::ApubPost,
tests::{file_to_json_object, init_context},
use crate::{
objects::{
community::tests::parse_lemmy_community,
person::tests::parse_lemmy_person,
post::ApubPost,
tests::init_context,
},
protocol::tests::file_to_json_object,
};
use lemmy_apub_lib::activity_queue::create_activity_queue;
use lemmy_db_schema::source::site::Site;
Expand Down
6 changes: 3 additions & 3 deletions crates/apub/src/objects/private_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,9 @@ impl ApubObject for ApubPrivateMessage {
#[cfg(test)]
mod tests {
use super::*;
use crate::objects::{
person::ApubPerson,
tests::{file_to_json_object, init_context},
use crate::{
objects::{person::ApubPerson, tests::init_context},
protocol::tests::file_to_json_object,
};
use assert_json_diff::assert_json_include;
use lemmy_apub_lib::activity_queue::create_activity_queue;
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/protocol/activities/block/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ mod tests {
tests::test_parse_lemmy_item,
};

#[actix_rt::test]
async fn test_parse_lemmy_block() {
#[test]
fn test_parse_lemmy_block() {
test_parse_lemmy_item::<BlockUser>("assets/lemmy/activities/block/block_user.json").unwrap();
test_parse_lemmy_item::<UndoBlockUser>("assets/lemmy/activities/block/undo_block_user.json")
.unwrap();
Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/protocol/activities/community/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ mod tests {
tests::test_parse_lemmy_item,
};

#[actix_rt::test]
async fn test_parse_lemmy_community() {
#[test]
fn test_parse_lemmy_community_activities() {
test_parse_lemmy_item::<AnnounceActivity>(
"assets/lemmy/activities/community/announce_create_page.json",
)
Expand Down
38 changes: 8 additions & 30 deletions crates/apub/src/protocol/activities/create_or_update/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ pub mod private_message;

#[cfg(test)]
mod tests {
use crate::{
context::WithContext,
objects::tests::file_to_json_object,
protocol::{
activities::create_or_update::{
comment::CreateOrUpdateComment,
post::CreateOrUpdatePost,
private_message::CreateOrUpdatePrivateMessage,
},
tests::test_parse_lemmy_item,
use crate::protocol::{
activities::create_or_update::{
comment::CreateOrUpdateComment,
post::CreateOrUpdatePost,
private_message::CreateOrUpdatePrivateMessage,
},
tests::test_parse_lemmy_item,
};

#[actix_rt::test]
async fn test_parse_create_or_update() {
#[test]
fn test_parse_lemmy_create_or_update() {
test_parse_lemmy_item::<CreateOrUpdatePost>(
"assets/lemmy/activities/create_or_update/create_page.json",
)
Expand All @@ -35,23 +31,5 @@ mod tests {
"assets/lemmy/activities/create_or_update/create_private_message.json",
)
.unwrap();

file_to_json_object::<WithContext<CreateOrUpdateComment>>(
"assets/pleroma/activities/create_note.json",
)
.unwrap();
file_to_json_object::<WithContext<CreateOrUpdateComment>>(
"assets/smithereen/activities/create_note.json",
)
.unwrap();
file_to_json_object::<CreateOrUpdateComment>("assets/mastodon/activities/create_note.json")
.unwrap();

file_to_json_object::<CreateOrUpdatePost>("assets/lotide/activities/create_page.json").unwrap();
file_to_json_object::<CreateOrUpdateComment>("assets/lotide/activities/create_note_reply.json")
.unwrap();

file_to_json_object::<CreateOrUpdateComment>("assets/friendica/activities/create_note.json")
.unwrap();
}
}
17 changes: 5 additions & 12 deletions crates/apub/src/protocol/activities/deletion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@ pub mod undo_delete;

#[cfg(test)]
mod tests {
use crate::{
context::WithContext,
objects::tests::file_to_json_object,
protocol::{
activities::deletion::{delete::Delete, undo_delete::UndoDelete},
tests::test_parse_lemmy_item,
},
use crate::protocol::{
activities::deletion::{delete::Delete, undo_delete::UndoDelete},
tests::test_parse_lemmy_item,
};

#[actix_rt::test]
async fn test_parse_deletion() {
#[test]
fn test_parse_lemmy_deletion() {
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();

Expand All @@ -27,8 +23,5 @@ mod tests {
"assets/lemmy/activities/deletion/undo_delete_private_message.json",
)
.unwrap();

file_to_json_object::<WithContext<Delete>>("assets/pleroma/activities/delete.json").unwrap();
file_to_json_object::<WithContext<Delete>>("assets/mastodon/activities/delete.json").unwrap();
}
}
23 changes: 8 additions & 15 deletions crates/apub/src/protocol/activities/following/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,17 @@ pub mod undo_follow;

#[cfg(test)]
mod tests {
use crate::{
context::WithContext,
objects::tests::file_to_json_object,
protocol::{
activities::following::{
accept::AcceptFollowCommunity,
follow::FollowCommunity,
undo_follow::UndoFollowCommunity,
},
tests::test_parse_lemmy_item,
use crate::protocol::{
activities::following::{
accept::AcceptFollowCommunity,
follow::FollowCommunity,
undo_follow::UndoFollowCommunity,
},
tests::test_parse_lemmy_item,
};

#[actix_rt::test]
async fn test_parse_lemmy_accept_follow() {
#[test]
fn test_parse_lemmy_accept_follow() {
test_parse_lemmy_item::<FollowCommunity>("assets/lemmy/activities/following/follow.json")
.unwrap();
test_parse_lemmy_item::<AcceptFollowCommunity>("assets/lemmy/activities/following/accept.json")
Expand All @@ -27,8 +23,5 @@ mod tests {
"assets/lemmy/activities/following/undo_follow.json",
)
.unwrap();

file_to_json_object::<WithContext<FollowCommunity>>("assets/pleroma/activities/follow.json")
.unwrap();
}
}
43 changes: 43 additions & 0 deletions crates/apub/src/protocol/activities/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,46 @@ pub enum CreateOrUpdateType {
Create,
Update,
}

#[cfg(test)]
mod tests {
use crate::protocol::{
activities::{
create_or_update::{comment::CreateOrUpdateComment, post::CreateOrUpdatePost},
deletion::delete::Delete,
following::{follow::FollowCommunity, undo_follow::UndoFollowCommunity},
},
tests::test_json,
};

#[test]
fn test_parse_smithereen_activities() {
test_json::<CreateOrUpdateComment>("assets/smithereen/activities/create_note.json").unwrap();
}

#[test]
fn test_parse_pleroma_activities() {
test_json::<CreateOrUpdateComment>("assets/pleroma/activities/create_note.json").unwrap();
test_json::<Delete>("assets/pleroma/activities/delete.json").unwrap();
test_json::<FollowCommunity>("assets/pleroma/activities/follow.json").unwrap();
}

#[test]
fn test_parse_mastodon_activities() {
test_json::<CreateOrUpdateComment>("assets/mastodon/activities/create_note.json").unwrap();
test_json::<Delete>("assets/mastodon/activities/delete.json").unwrap();
test_json::<FollowCommunity>("assets/mastodon/activities/follow.json").unwrap();
test_json::<UndoFollowCommunity>("assets/mastodon/activities/undo_follow.json").unwrap();
}

#[test]
fn test_parse_lotide_activities() {
test_json::<CreateOrUpdatePost>("assets/lotide/activities/create_page.json").unwrap();
test_json::<CreateOrUpdateComment>("assets/lotide/activities/create_note_reply.json").unwrap();
}

#[test]
fn test_parse_friendica_activities() {
test_json::<CreateOrUpdateComment>("assets/friendica/activities/create_note.json").unwrap();
}
}
4 changes: 2 additions & 2 deletions crates/apub/src/protocol/activities/voting/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ mod tests {
tests::test_parse_lemmy_item,
};

#[actix_rt::test]
async fn test_parse_lemmy_voting() {
#[test]
fn test_parse_lemmy_voting() {
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();

Expand Down
4 changes: 2 additions & 2 deletions crates/apub/src/protocol/collections/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ mod tests {
tests::test_parse_lemmy_item,
};

#[actix_rt::test]
async fn test_parse_lemmy_collections() {
#[test]
fn test_parse_lemmy_collections() {
test_parse_lemmy_item::<GroupFollowers>("assets/lemmy/collections/group_followers.json")
.unwrap();
let outbox =
Expand Down
Loading

0 comments on commit 1561521

Please sign in to comment.