Accept posts using Note type (and better error messages for tests)

lotide-federation
Felix Ableitner 2022-01-13 03:48:00 +01:00
parent 8a8f9c729a
commit 4db322ab53
13 changed files with 70 additions and 37 deletions

View File

@ -0,0 +1,12 @@
{
"@context": "https://www.w3.org/ns/activitystreams",
"id": "https://narwhal.city/posts/9",
"type": "Page",
"name": "What's Dylan Grillin'? (reupload)",
"to": "https://narwhal.city/communities/4",
"attributedTo": "https://narwhal.city/users/1",
"published": "2020-12-30T07:29:19.460932+00:00",
"url": "https://www.youtube.com/watch?v=ZI4LGTXscR4",
"summary": "What's Dylan Grillin'? (reupload)",
"cc": "https://www.w3.org/ns/activitystreams#Public"
}

View File

@ -177,7 +177,7 @@ mod tests {
let new_mod = parse_lemmy_person(&context).await;
let json: GroupModerators =
file_to_json_object("assets/lemmy/collections/group_moderators.json");
file_to_json_object("assets/lemmy/collections/group_moderators.json").unwrap();
let url = Url::parse("https://enterprise.lemmy.ml/c/tenforward").unwrap();
let mut request_counter = 0;
let community_context = CommunityContext {

View File

@ -227,7 +227,7 @@ pub(crate) mod tests {
) -> (ApubPerson, ApubCommunity, ApubPost) {
let person = parse_lemmy_person(context).await;
let community = parse_lemmy_community(context).await;
let post_json = file_to_json_object("assets/lemmy/objects/page.json");
let post_json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
ApubPost::verify(&post_json, url, context, &mut 0)
.await
.unwrap();
@ -252,7 +252,7 @@ pub(crate) mod tests {
let url = Url::parse("https://enterprise.lemmy.ml/comment/38741").unwrap();
let data = prepare_comment_test(&url, &context).await;
let json: Note = file_to_json_object("assets/lemmy/objects/note.json");
let json: Note = file_to_json_object("assets/lemmy/objects/note.json").unwrap();
let mut request_counter = 0;
ApubComment::verify(&json, &url, &context, &mut request_counter)
.await
@ -286,14 +286,14 @@ pub(crate) mod tests {
let pleroma_url =
Url::parse("https://queer.hacktivis.me/objects/8d4973f4-53de-49cd-8c27-df160e16a9c2")
.unwrap();
let person_json = file_to_json_object("assets/pleroma/objects/person.json");
let person_json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
ApubPerson::verify(&person_json, &pleroma_url, &context, &mut 0)
.await
.unwrap();
ApubPerson::from_apub(person_json, &context, &mut 0)
.await
.unwrap();
let json = file_to_json_object("assets/pleroma/objects/note.json");
let json = file_to_json_object("assets/pleroma/objects/note.json").unwrap();
let mut request_counter = 0;
ApubComment::verify(&json, &pleroma_url, &context, &mut request_counter)
.await

View File

@ -225,7 +225,7 @@ pub(crate) mod tests {
use serial_test::serial;
pub(crate) async fn parse_lemmy_community(context: &LemmyContext) -> ApubCommunity {
let mut json: Group = file_to_json_object("assets/lemmy/objects/group.json");
let mut json: Group = file_to_json_object("assets/lemmy/objects/group.json").unwrap();
// change these links so they dont fetch over the network
json.moderators = None;
json.outbox =

View File

@ -90,9 +90,11 @@ 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) -> T {
pub(crate) fn file_to_json_object<T: DeserializeOwned>(
path: &str,
) -> serde_json::error::Result<T> {
let file = File::open(path).unwrap();
let reader = BufReader::new(file);
serde_json::from_reader(reader).unwrap()
serde_json::from_reader(reader)
}
}

View File

@ -209,7 +209,7 @@ pub(crate) mod tests {
use serial_test::serial;
pub(crate) async fn parse_lemmy_person(context: &LemmyContext) -> ApubPerson {
let json = file_to_json_object("assets/lemmy/objects/person.json");
let json = file_to_json_object("assets/lemmy/objects/person.json").unwrap();
let url = Url::parse("https://enterprise.lemmy.ml/u/picard").unwrap();
let mut request_counter = 0;
ApubPerson::verify(&json, &url, context, &mut request_counter)
@ -243,7 +243,7 @@ pub(crate) mod tests {
let client = reqwest::Client::new().into();
let manager = create_activity_queue(client);
let context = init_context(manager.queue_handle().clone());
let json = file_to_json_object("assets/pleroma/objects/person.json");
let json = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
let url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
let mut request_counter = 0;
ApubPerson::verify(&json, &url, &context, &mut request_counter)

View File

@ -2,12 +2,15 @@ use crate::{
activities::{verify_is_public, verify_person_in_community},
check_is_apub_id_valid,
protocol::{
objects::{page::Page, tombstone::Tombstone},
objects::{
page::{Page, PageType},
tombstone::Tombstone,
},
ImageObject,
Source,
},
};
use activitystreams_kinds::{object::PageType, public};
use activitystreams_kinds::public;
use chrono::NaiveDateTime;
use lemmy_api_common::blocking;
use lemmy_apub_lib::{
@ -222,7 +225,7 @@ mod tests {
let community = parse_lemmy_community(&context).await;
let person = parse_lemmy_person(&context).await;
let json = file_to_json_object("assets/lemmy/objects/page.json");
let json = file_to_json_object("assets/lemmy/objects/page.json").unwrap();
let url = Url::parse("https://enterprise.lemmy.ml/post/55143").unwrap();
let mut request_counter = 0;
ApubPost::verify(&json, &url, &context, &mut request_counter)

View File

@ -171,14 +171,14 @@ mod tests {
use serial_test::serial;
async fn prepare_comment_test(url: &Url, context: &LemmyContext) -> (ApubPerson, ApubPerson) {
let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json");
let lemmy_person = file_to_json_object("assets/lemmy/objects/person.json").unwrap();
ApubPerson::verify(&lemmy_person, url, context, &mut 0)
.await
.unwrap();
let person1 = ApubPerson::from_apub(lemmy_person, context, &mut 0)
.await
.unwrap();
let pleroma_person = file_to_json_object("assets/pleroma/objects/person.json");
let pleroma_person = file_to_json_object("assets/pleroma/objects/person.json").unwrap();
let pleroma_url = Url::parse("https://queer.hacktivis.me/users/lanodan").unwrap();
ApubPerson::verify(&pleroma_person, &pleroma_url, context, &mut 0)
.await
@ -202,7 +202,7 @@ mod tests {
let context = init_context(manager.queue_handle().clone());
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
let data = prepare_comment_test(&url, &context).await;
let json: ChatMessage = file_to_json_object("assets/lemmy/objects/chat_message.json");
let json: ChatMessage = file_to_json_object("assets/lemmy/objects/chat_message.json").unwrap();
let mut request_counter = 0;
ApubPrivateMessage::verify(&json, &url, &context, &mut request_counter)
.await
@ -232,7 +232,7 @@ mod tests {
let url = Url::parse("https://enterprise.lemmy.ml/private_message/1621").unwrap();
let data = prepare_comment_test(&url, &context).await;
let pleroma_url = Url::parse("https://queer.hacktivis.me/objects/2").unwrap();
let json = file_to_json_object("assets/pleroma/objects/chat_message.json");
let json = file_to_json_object("assets/pleroma/objects/chat_message.json").unwrap();
let mut request_counter = 0;
ApubPrivateMessage::verify(&json, &pleroma_url, &context, &mut request_counter)
.await

View File

@ -26,13 +26,17 @@ mod tests {
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",
);
file_to_json_object::<CreateOrUpdateComment>("assets/mastodon/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");
file_to_json_object::<CreateOrUpdateComment>("assets/lotide/activities/create_note_reply.json");
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();
}
}

View File

@ -25,6 +25,7 @@ mod tests {
"assets/lemmy/activities/following/undo_follow.json",
);
file_to_json_object::<WithContext<FollowCommunity>>("assets/pleroma/activities/follow.json");
file_to_json_object::<WithContext<FollowCommunity>>("assets/pleroma/activities/follow.json")
.unwrap();
}
}

View File

@ -45,13 +45,17 @@ pub(crate) mod tests {
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 {
let parsed = file_to_json_object::<T>(path);
// parse file as T
let parsed = file_to_json_object::<T>(path).unwrap();
// ensure that no field is ignored when parsing
let raw = file_to_json_object::<HashMap<String, serde_json::Value>>(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();
// assert that all fields are identical, otherwise print diff
assert_json_include!(actual: &parsed, expected: raw);
parsed
}

View File

@ -36,27 +36,29 @@ mod tests {
#[actix_rt::test]
async fn test_parse_object_pleroma() {
file_to_json_object::<WithContext<Person>>("assets/pleroma/objects/person.json");
file_to_json_object::<WithContext<Note>>("assets/pleroma/objects/note.json");
file_to_json_object::<WithContext<ChatMessage>>("assets/pleroma/objects/chat_message.json");
file_to_json_object::<WithContext<Person>>("assets/pleroma/objects/person.json").unwrap();
file_to_json_object::<WithContext<Note>>("assets/pleroma/objects/note.json").unwrap();
file_to_json_object::<WithContext<ChatMessage>>("assets/pleroma/objects/chat_message.json")
.unwrap();
}
#[actix_rt::test]
async fn test_parse_object_smithereen() {
file_to_json_object::<WithContext<Person>>("assets/smithereen/objects/person.json");
file_to_json_object::<Note>("assets/smithereen/objects/note.json");
file_to_json_object::<WithContext<Person>>("assets/smithereen/objects/person.json").unwrap();
file_to_json_object::<Note>("assets/smithereen/objects/note.json").unwrap();
}
#[actix_rt::test]
async fn test_parse_object_mastodon() {
file_to_json_object::<Person>("assets/mastodon/objects/person.json");
file_to_json_object::<Note>("assets/mastodon/objects/note.json");
file_to_json_object::<Person>("assets/mastodon/objects/person.json").unwrap();
file_to_json_object::<Note>("assets/mastodon/objects/note.json").unwrap();
}
#[actix_rt::test]
async fn test_parse_object_lotide() {
file_to_json_object::<WithContext<Group>>("assets/lotide/objects/group.json");
file_to_json_object::<WithContext<Person>>("assets/lotide/objects/person.json");
file_to_json_object::<WithContext<Note>>("assets/lotide/objects/note.json");
file_to_json_object::<WithContext<Group>>("assets/lotide/objects/group.json").unwrap();
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();
}
}

View File

@ -2,7 +2,6 @@ use crate::{
objects::{community::ApubCommunity, person::ApubPerson, post::ApubPost},
protocol::{ImageObject, Source, Unparsed},
};
use activitystreams_kinds::object::PageType;
use chrono::{DateTime, FixedOffset};
use lemmy_apub_lib::{
data::Data,
@ -16,6 +15,12 @@ use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use url::Url;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub enum PageType {
Page,
Note,
}
#[skip_serializing_none]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]