From edc64bbbb2ea5c4576a25ebfd293b6d44502ff43 Mon Sep 17 00:00:00 2001 From: silverpill Date: Fri, 29 Jul 2022 08:19:07 +0000 Subject: [PATCH] Add tests for Delete(Note) activity builder --- src/activitypub/builders/create_note.rs | 47 +++++++++++++++++++++++++ src/activitypub/builders/delete_note.rs | 42 ++++++++++++++++++++++ 2 files changed, 89 insertions(+) diff --git a/src/activitypub/builders/create_note.rs b/src/activitypub/builders/create_note.rs index 8d0c106..554df09 100644 --- a/src/activitypub/builders/create_note.rs +++ b/src/activitypub/builders/create_note.rs @@ -285,6 +285,53 @@ mod tests { assert_eq!(note.cc.is_empty(), true); } + #[test] + fn test_build_note_subscribers_only() { + let post = Post { + visibility: Visibility::Subscribers, + ..Default::default() + }; + let subscriber_id = "https://test.com/users/3"; + let subscriber = DbActorProfile { + username: "subscriber".to_string(), + actor_json: Some(Actor { + id: subscriber_id.to_string(), + ..Default::default() + }), + actor_id: Some(subscriber_id.to_string()), + ..Default::default() + }; + let note = build_note(INSTANCE_HOST, INSTANCE_URL, &post, vec![subscriber]); + + assert_eq!(note.to, vec![ + local_actor_subscribers(INSTANCE_URL, &post.author.username), + ]); + assert_eq!(note.cc, vec![subscriber_id]); + } + + #[test] + fn test_build_note_direct() { + let mentioned_id = "https://test.com/users/3"; + let mentioned = DbActorProfile { + username: "mention".to_string(), + actor_json: Some(Actor { + id: mentioned_id.to_string(), + ..Default::default() + }), + actor_id: Some(mentioned_id.to_string()), + ..Default::default() + }; + let post = Post { + visibility: Visibility::Direct, + mentions: vec![mentioned], + ..Default::default() + }; + let note = build_note(INSTANCE_HOST, INSTANCE_URL, &post, vec![]); + + assert_eq!(note.to, vec![mentioned_id]); + assert_eq!(note.cc.is_empty(), true); + } + #[test] fn test_build_note_with_local_parent() { let parent = Post::default(); diff --git a/src/activitypub/builders/delete_note.rs b/src/activitypub/builders/delete_note.rs index 834fd16..78578ef 100644 --- a/src/activitypub/builders/delete_note.rs +++ b/src/activitypub/builders/delete_note.rs @@ -76,3 +76,45 @@ pub async fn prepare_delete_note( recipients, }) } + +#[cfg(test)] +mod tests { + use serde_json::json; + use crate::activitypub::{ + constants::AP_PUBLIC, + identifiers::local_actor_followers, + }; + use super::*; + + const INSTANCE_HOST: &str = "example.com"; + const INSTANCE_URL: &str = "https://example.com"; + + #[test] + fn test_build_delete_note() { + let author = DbActorProfile { + username: "author".to_string(), + ..Default::default() + }; + let post = Post { author, ..Default::default() }; + let activity = build_delete_note( + INSTANCE_HOST, + INSTANCE_URL, + &post, + vec![], + ); + + assert_eq!( + activity.id, + format!("{}/objects/{}/delete", INSTANCE_URL, post.id), + ); + assert_eq!( + activity.object["id"], + format!("{}/objects/{}", INSTANCE_URL, post.id), + ); + assert_eq!(activity.to.unwrap(), json!([AP_PUBLIC])); + assert_eq!( + activity.cc.unwrap(), + json!([local_actor_followers(INSTANCE_URL, "author")]), + ); + } +}