Reorganize apub protocol tests

reorganize-federation-tests
Felix Ableitner 2022-02-10 14:38:30 +01:00
parent e094989a4c
commit 37860d81a3
22 changed files with 188 additions and 141 deletions

View File

@ -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", "id": "https://mastodon.madrid/users/felix/statuses/107224289116410645/activity",
"type": "Create", "type": "Create",
"actor": "https://mastodon.madrid/users/felix", "actor": "https://mastodon.madrid/users/felix",

View File

@ -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"
}

View File

@ -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"
}
}

View File

@ -28,5 +28,11 @@
} }
}, },
"sensitive": false, "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"
}
]
} }

View File

@ -138,10 +138,13 @@ impl ApubObject for ApubCommunityModerators {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::objects::{ use crate::{
objects::{
community::tests::parse_lemmy_community, community::tests::parse_lemmy_community,
person::tests::parse_lemmy_person, person::tests::parse_lemmy_person,
tests::{file_to_json_object, init_context}, tests::init_context,
},
protocol::tests::file_to_json_object,
}; };
use lemmy_apub_lib::activity_queue::create_activity_queue; use lemmy_apub_lib::activity_queue::create_activity_queue;
use lemmy_db_schema::{ use lemmy_db_schema::{

View File

@ -208,12 +208,15 @@ impl ApubObject for ApubComment {
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use super::*; use super::*;
use crate::objects::{ use crate::{
objects::{
community::{tests::parse_lemmy_community, ApubCommunity}, community::{tests::parse_lemmy_community, ApubCommunity},
instance::ApubSite, instance::ApubSite,
person::{tests::parse_lemmy_person, ApubPerson}, person::{tests::parse_lemmy_person, ApubPerson},
post::ApubPost, post::ApubPost,
tests::{file_to_json_object, init_context}, tests::init_context,
},
protocol::tests::file_to_json_object,
}; };
use assert_json_diff::assert_json_include; use assert_json_diff::assert_json_include;
use lemmy_apub_lib::activity_queue::create_activity_queue; use lemmy_apub_lib::activity_queue::create_activity_queue;

View File

@ -214,9 +214,9 @@ impl ApubCommunity {
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use super::*; use super::*;
use crate::objects::{ use crate::{
instance::tests::parse_lemmy_instance, objects::{instance::tests::parse_lemmy_instance, tests::init_context},
tests::{file_to_json_object, init_context}, protocol::tests::file_to_json_object,
}; };
use lemmy_apub_lib::activity_queue::create_activity_queue; use lemmy_apub_lib::activity_queue::create_activity_queue;
use lemmy_db_schema::{source::site::Site, traits::Crud}; use lemmy_db_schema::{source::site::Site, traits::Crud};

View File

@ -186,7 +186,7 @@ pub(in crate::objects) async fn fetch_instance_actor_for_object(
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use super::*; 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_apub_lib::activity_queue::create_activity_queue;
use lemmy_db_schema::traits::Crud; use lemmy_db_schema::traits::Crud;
use serial_test::serial; use serial_test::serial;

View File

@ -41,8 +41,7 @@ pub(crate) mod tests {
use lemmy_websocket::{chat_server::ChatServer, LemmyContext}; use lemmy_websocket::{chat_server::ChatServer, LemmyContext};
use reqwest::Client; use reqwest::Client;
use reqwest_middleware::ClientBuilder; use reqwest_middleware::ClientBuilder;
use serde::de::DeserializeOwned; use std::sync::Arc;
use std::{fs::File, io::BufReader, sync::Arc};
use tokio::sync::Mutex; use tokio::sync::Mutex;
// TODO: would be nice if we didnt have to use a full context for tests. // TODO: would be nice if we didnt have to use a full context for tests.
@ -90,10 +89,4 @@ pub(crate) mod tests {
.start(); .start();
LemmyContext::create(pool, chat_server, client, activity_queue, settings, secret) 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)?)
}
} }

View File

@ -204,9 +204,9 @@ pub(crate) mod tests {
use crate::{ use crate::{
objects::{ objects::{
instance::{tests::parse_lemmy_instance, ApubSite}, 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_apub_lib::activity_queue::create_activity_queue;
use lemmy_db_schema::{source::site::Site, traits::Crud}; use lemmy_db_schema::{source::site::Site, traits::Crud};

View File

@ -207,11 +207,14 @@ impl ApubObject for ApubPost {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::objects::{ use crate::{
objects::{
community::tests::parse_lemmy_community, community::tests::parse_lemmy_community,
person::tests::parse_lemmy_person, person::tests::parse_lemmy_person,
post::ApubPost, post::ApubPost,
tests::{file_to_json_object, init_context}, tests::init_context,
},
protocol::tests::file_to_json_object,
}; };
use lemmy_apub_lib::activity_queue::create_activity_queue; use lemmy_apub_lib::activity_queue::create_activity_queue;
use lemmy_db_schema::source::site::Site; use lemmy_db_schema::source::site::Site;

View File

@ -159,9 +159,9 @@ impl ApubObject for ApubPrivateMessage {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::objects::{ use crate::{
person::ApubPerson, objects::{person::ApubPerson, tests::init_context},
tests::{file_to_json_object, init_context}, protocol::tests::file_to_json_object,
}; };
use assert_json_diff::assert_json_include; use assert_json_diff::assert_json_include;
use lemmy_apub_lib::activity_queue::create_activity_queue; use lemmy_apub_lib::activity_queue::create_activity_queue;

View File

@ -8,8 +8,8 @@ mod tests {
tests::test_parse_lemmy_item, tests::test_parse_lemmy_item,
}; };
#[actix_rt::test] #[test]
async fn test_parse_lemmy_block() { fn test_parse_lemmy_block() {
test_parse_lemmy_item::<BlockUser>("assets/lemmy/activities/block/block_user.json").unwrap(); 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") test_parse_lemmy_item::<UndoBlockUser>("assets/lemmy/activities/block/undo_block_user.json")
.unwrap(); .unwrap();

View File

@ -17,8 +17,8 @@ mod tests {
tests::test_parse_lemmy_item, tests::test_parse_lemmy_item,
}; };
#[actix_rt::test] #[test]
async fn test_parse_lemmy_community() { fn test_parse_lemmy_community_activities() {
test_parse_lemmy_item::<AnnounceActivity>( test_parse_lemmy_item::<AnnounceActivity>(
"assets/lemmy/activities/community/announce_create_page.json", "assets/lemmy/activities/community/announce_create_page.json",
) )

View File

@ -4,21 +4,17 @@ pub mod private_message;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::protocol::{
context::WithContext,
objects::tests::file_to_json_object,
protocol::{
activities::create_or_update::{ activities::create_or_update::{
comment::CreateOrUpdateComment, comment::CreateOrUpdateComment,
post::CreateOrUpdatePost, post::CreateOrUpdatePost,
private_message::CreateOrUpdatePrivateMessage, private_message::CreateOrUpdatePrivateMessage,
}, },
tests::test_parse_lemmy_item, tests::test_parse_lemmy_item,
},
}; };
#[actix_rt::test] #[test]
async fn test_parse_create_or_update() { fn test_parse_lemmy_create_or_update() {
test_parse_lemmy_item::<CreateOrUpdatePost>( test_parse_lemmy_item::<CreateOrUpdatePost>(
"assets/lemmy/activities/create_or_update/create_page.json", "assets/lemmy/activities/create_or_update/create_page.json",
) )
@ -35,23 +31,5 @@ mod tests {
"assets/lemmy/activities/create_or_update/create_private_message.json", "assets/lemmy/activities/create_or_update/create_private_message.json",
) )
.unwrap(); .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();
} }
} }

View File

@ -3,17 +3,13 @@ pub mod undo_delete;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::protocol::{
context::WithContext,
objects::tests::file_to_json_object,
protocol::{
activities::deletion::{delete::Delete, undo_delete::UndoDelete}, activities::deletion::{delete::Delete, undo_delete::UndoDelete},
tests::test_parse_lemmy_item, tests::test_parse_lemmy_item,
},
}; };
#[actix_rt::test] #[test]
async fn test_parse_deletion() { 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/remove_note.json").unwrap();
test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/delete_page.json").unwrap(); test_parse_lemmy_item::<Delete>("assets/lemmy/activities/deletion/delete_page.json").unwrap();
@ -27,8 +23,5 @@ mod tests {
"assets/lemmy/activities/deletion/undo_delete_private_message.json", "assets/lemmy/activities/deletion/undo_delete_private_message.json",
) )
.unwrap(); .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();
} }
} }

View File

@ -4,21 +4,17 @@ pub mod undo_follow;
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::protocol::{
context::WithContext,
objects::tests::file_to_json_object,
protocol::{
activities::following::{ activities::following::{
accept::AcceptFollowCommunity, accept::AcceptFollowCommunity,
follow::FollowCommunity, follow::FollowCommunity,
undo_follow::UndoFollowCommunity, undo_follow::UndoFollowCommunity,
}, },
tests::test_parse_lemmy_item, tests::test_parse_lemmy_item,
},
}; };
#[actix_rt::test] #[test]
async fn test_parse_lemmy_accept_follow() { fn test_parse_lemmy_accept_follow() {
test_parse_lemmy_item::<FollowCommunity>("assets/lemmy/activities/following/follow.json") test_parse_lemmy_item::<FollowCommunity>("assets/lemmy/activities/following/follow.json")
.unwrap(); .unwrap();
test_parse_lemmy_item::<AcceptFollowCommunity>("assets/lemmy/activities/following/accept.json") test_parse_lemmy_item::<AcceptFollowCommunity>("assets/lemmy/activities/following/accept.json")
@ -27,8 +23,5 @@ mod tests {
"assets/lemmy/activities/following/undo_follow.json", "assets/lemmy/activities/following/undo_follow.json",
) )
.unwrap(); .unwrap();
file_to_json_object::<WithContext<FollowCommunity>>("assets/pleroma/activities/follow.json")
.unwrap();
} }
} }

View File

@ -13,3 +13,46 @@ pub enum CreateOrUpdateType {
Create, Create,
Update, 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();
}
}

View File

@ -8,8 +8,8 @@ mod tests {
tests::test_parse_lemmy_item, tests::test_parse_lemmy_item,
}; };
#[actix_rt::test] #[test]
async fn test_parse_lemmy_voting() { 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/like_note.json").unwrap();
test_parse_lemmy_item::<Vote>("assets/lemmy/activities/voting/dislike_page.json").unwrap(); test_parse_lemmy_item::<Vote>("assets/lemmy/activities/voting/dislike_page.json").unwrap();

View File

@ -15,8 +15,8 @@ mod tests {
tests::test_parse_lemmy_item, tests::test_parse_lemmy_item,
}; };
#[actix_rt::test] #[test]
async fn test_parse_lemmy_collections() { fn test_parse_lemmy_collections() {
test_parse_lemmy_item::<GroupFollowers>("assets/lemmy/collections/group_followers.json") test_parse_lemmy_item::<GroupFollowers>("assets/lemmy/collections/group_followers.json")
.unwrap(); .unwrap();
let outbox = let outbox =

View File

@ -49,11 +49,21 @@ pub struct Unparsed(HashMap<String, serde_json::Value>);
#[cfg(test)] #[cfg(test)]
pub(crate) mod tests { pub(crate) mod tests {
use crate::objects::tests::file_to_json_object; use crate::context::WithContext;
use assert_json_diff::assert_json_include; use assert_json_diff::assert_json_include;
use lemmy_utils::LemmyError; use lemmy_utils::LemmyError;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Serialize};
use std::collections::HashMap; use std::{collections::HashMap, fs::File, io::BufReader};
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)?)
}
pub(crate) fn test_json<T: DeserializeOwned>(path: &str) -> Result<WithContext<T>, LemmyError> {
file_to_json_object::<WithContext<T>>(path)
}
/// Check that json deserialize -> serialize -> deserialize gives identical file as initial one. /// Check that json deserialize -> serialize -> deserialize gives identical file as initial one.
/// Ensures that there are no breaking changes in sent data. /// Ensures that there are no breaking changes in sent data.

View File

@ -17,10 +17,7 @@ pub struct Endpoints {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::protocol::{
context::WithContext,
objects::tests::file_to_json_object,
protocol::{
objects::{ objects::{
chat_message::ChatMessage, chat_message::ChatMessage,
group::Group, group::Group,
@ -30,12 +27,11 @@ mod tests {
person::Person, person::Person,
tombstone::Tombstone, tombstone::Tombstone,
}, },
tests::test_parse_lemmy_item, tests::{test_json, test_parse_lemmy_item},
},
}; };
#[actix_rt::test] #[test]
async fn test_parse_objects_lemmy() { fn test_parse_objects_lemmy() {
test_parse_lemmy_item::<Instance>("assets/lemmy/objects/instance.json").unwrap(); test_parse_lemmy_item::<Instance>("assets/lemmy/objects/instance.json").unwrap();
test_parse_lemmy_item::<Group>("assets/lemmy/objects/group.json").unwrap(); test_parse_lemmy_item::<Group>("assets/lemmy/objects/group.json").unwrap();
test_parse_lemmy_item::<Person>("assets/lemmy/objects/person.json").unwrap(); test_parse_lemmy_item::<Person>("assets/lemmy/objects/person.json").unwrap();
@ -45,38 +41,37 @@ mod tests {
test_parse_lemmy_item::<Tombstone>("assets/lemmy/objects/tombstone.json").unwrap(); test_parse_lemmy_item::<Tombstone>("assets/lemmy/objects/tombstone.json").unwrap();
} }
#[actix_rt::test] #[test]
async fn test_parse_objects_pleroma() { fn test_parse_objects_pleroma() {
file_to_json_object::<WithContext<Person>>("assets/pleroma/objects/person.json").unwrap(); test_json::<Person>("assets/pleroma/objects/person.json").unwrap();
file_to_json_object::<WithContext<Note>>("assets/pleroma/objects/note.json").unwrap(); test_json::<Note>("assets/pleroma/objects/note.json").unwrap();
file_to_json_object::<WithContext<ChatMessage>>("assets/pleroma/objects/chat_message.json") test_json::<ChatMessage>("assets/pleroma/objects/chat_message.json").unwrap();
.unwrap();
} }
#[actix_rt::test] #[test]
async fn test_parse_objects_smithereen() { fn test_parse_objects_smithereen() {
file_to_json_object::<WithContext<Person>>("assets/smithereen/objects/person.json").unwrap(); test_json::<Person>("assets/smithereen/objects/person.json").unwrap();
file_to_json_object::<Note>("assets/smithereen/objects/note.json").unwrap(); test_json::<Note>("assets/smithereen/objects/note.json").unwrap();
} }
#[actix_rt::test] #[test]
async fn test_parse_objects_mastodon() { fn test_parse_objects_mastodon() {
file_to_json_object::<WithContext<Person>>("assets/mastodon/objects/person.json").unwrap(); test_json::<Person>("assets/mastodon/objects/person.json").unwrap();
file_to_json_object::<WithContext<Note>>("assets/mastodon/objects/note.json").unwrap(); test_json::<Note>("assets/mastodon/objects/note.json").unwrap();
} }
#[actix_rt::test] #[test]
async fn test_parse_objects_lotide() { fn test_parse_objects_lotide() {
file_to_json_object::<WithContext<Group>>("assets/lotide/objects/group.json").unwrap(); test_json::<Group>("assets/lotide/objects/group.json").unwrap();
file_to_json_object::<WithContext<Person>>("assets/lotide/objects/person.json").unwrap(); test_json::<Person>("assets/lotide/objects/person.json").unwrap();
file_to_json_object::<WithContext<Note>>("assets/lotide/objects/note.json").unwrap(); test_json::<Note>("assets/lotide/objects/note.json").unwrap();
file_to_json_object::<WithContext<Page>>("assets/lotide/objects/page.json").unwrap(); test_json::<Page>("assets/lotide/objects/page.json").unwrap();
file_to_json_object::<WithContext<Tombstone>>("assets/lotide/objects/tombstone.json").unwrap(); test_json::<Tombstone>("assets/lotide/objects/tombstone.json").unwrap();
} }
#[actix_rt::test] #[test]
async fn test_parse_object_friendica() { fn test_parse_object_friendica() {
file_to_json_object::<WithContext<Person>>("assets/friendica/objects/person.json").unwrap(); test_json::<Person>("assets/friendica/objects/person.json").unwrap();
file_to_json_object::<WithContext<Note>>("assets/friendica/objects/note.json").unwrap(); test_json::<Note>("assets/friendica/objects/note.json").unwrap();
} }
} }