Skip to content

Commit

Permalink
Reorganize federation tests (#2092)
Browse files Browse the repository at this point in the history
* Reorganize apub protocol tests

* Reorder apub protocol struct members to clarify mandatory/optional fields
  • Loading branch information
Nutomic authored Feb 17, 2022
1 parent e094989 commit 762b85b
Show file tree
Hide file tree
Showing 47 changed files with 244 additions and 211 deletions.
19 changes: 0 additions & 19 deletions crates/apub/assets/lemmy/objects/instance.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,4 @@
{
"@context": [
"https://www.w3.org/ns/activitystreams",
{
"stickied": "as:stickied",
"pt": "https://join-lemmy.org#",
"sc": "http://schema.org#",
"matrixUserId": {
"type": "sc:Text",
"id": "as:alsoKnownAs"
},
"sensitive": "as:sensitive",
"comments_enabled": {
"type": "sc:Boolean",
"id": "pt:commentsEnabled"
},
"moderators": "as:moderators"
},
"https://w3id.org/security/v1"
],
"type": "Service",
"id": "https://enterprise.lemmy.ml/",
"name": "Enterprise",
Expand Down
2 changes: 1 addition & 1 deletion crates/apub/assets/lemmy/objects/person.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"type": "Image",
"url": "https://enterprise.lemmy.ml/pictrs/image/XenaYI5hTn.png"
},
"matrix_user_id": "@picard:matrix.org",
"matrixUserId": "@picard:matrix.org",
"inbox": "https://enterprise.lemmy.ml/u/picard/inbox",
"outbox": "https://enterprise.lemmy.ml/u/picard/outbox",
"endpoints": {
Expand Down
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
16 changes: 9 additions & 7 deletions crates/apub/src/objects/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ impl ApubObject for ApubComment {
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
tag: maa.tags,
unparsed: Default::default(),
};

Ok(note)
Expand Down Expand Up @@ -208,12 +207,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
7 changes: 3 additions & 4 deletions crates/apub/src/objects/community.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ impl ApubObject for ApubCommunity {
public_key: self.get_public_key()?,
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
unparsed: Default::default(),
};
Ok(group)
}
Expand Down Expand Up @@ -214,9 +213,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
5 changes: 2 additions & 3 deletions crates/apub/src/objects/instance.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
check_is_apub_id_valid,
objects::get_summary_from_string_or_source,
protocol::{objects::instance::Instance, ImageObject, Source, Unparsed},
protocol::{objects::instance::Instance, ImageObject, Source},
};
use activitystreams_kinds::actor::ServiceType;
use chrono::NaiveDateTime;
Expand Down Expand Up @@ -86,7 +86,6 @@ impl ApubObject for ApubSite {
public_key: self.get_public_key()?,
published: convert_datetime(self.published),
updated: self.updated.map(convert_datetime),
unparsed: Unparsed::default(),
};
Ok(instance)
}
Expand Down Expand Up @@ -186,7 +185,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)?)
}
}
5 changes: 2 additions & 3 deletions crates/apub/src/objects/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ impl ApubObject for ApubPerson {
}),
public_key: self.get_public_key()?,
updated: self.updated.map(convert_datetime),
unparsed: Default::default(),
inbox: self.inbox_url.clone().into(),
};
Ok(person)
Expand Down Expand Up @@ -204,9 +203,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
14 changes: 8 additions & 6 deletions crates/apub/src/objects/post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ impl ApubObject for ApubPost {
stickied: Some(self.stickied),
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
unparsed: Default::default(),
};
Ok(page)
}
Expand Down Expand Up @@ -207,11 +206,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
7 changes: 3 additions & 4 deletions crates/apub/src/objects/private_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ impl ApubObject for ApubPrivateMessage {
source: Some(Source::new(self.content.clone())),
published: Some(convert_datetime(self.published)),
updated: self.updated.map(convert_datetime),
unparsed: Default::default(),
};
Ok(note)
}
Expand Down Expand Up @@ -159,9 +158,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
5 changes: 3 additions & 2 deletions crates/apub/src/protocol/activities/block/block_user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@ pub struct BlockUser {
pub(crate) target: ObjectId<SiteOrCommunity>,
#[serde(rename = "type")]
pub(crate) kind: BlockType,
pub(crate) id: Url,

/// Quick and dirty solution.
/// TODO: send a separate Delete activity instead
pub(crate) remove_data: Option<bool>,
/// block reason, written to mod log
pub(crate) summary: Option<String>,
pub(crate) id: Url,
pub(crate) expires: Option<DateTime<FixedOffset>>,
#[serde(flatten)]
pub(crate) unparsed: Unparsed,
pub(crate) expires: Option<DateTime<FixedOffset>>,
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct UndoBlockUser {
#[serde(rename = "type")]
pub(crate) kind: UndoType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/community/add_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct AddMod {
#[serde(rename = "type")]
pub(crate) kind: AddType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/community/announce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub struct AnnounceActivity {
#[serde(rename = "type")]
pub(crate) kind: AnnounceType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ pub struct RemoveMod {
pub(crate) kind: RemoveType,
pub(crate) target: Url,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/community/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub struct Report {
#[serde(rename = "type")]
pub(crate) kind: FlagType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
1 change: 1 addition & 0 deletions crates/apub/src/protocol/activities/community/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct UpdateCommunity {
#[serde(rename = "type")]
pub(crate) kind: UpdateType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ pub struct CreateOrUpdateComment {
#[serde(rename = "type")]
pub(crate) kind: CreateOrUpdateType,
pub(crate) id: Url,

#[serde(flatten)]
pub(crate) unparsed: Unparsed,
}
Loading

0 comments on commit 762b85b

Please sign in to comment.