From 119d3e4f6a4defbe21de8fb446ef72f171d1b8fa Mon Sep 17 00:00:00 2001 From: Kitaiti Makoto Date: Fri, 11 Feb 2022 02:29:29 +0900 Subject: [PATCH] [plume-common]Add tests for new ActivityPub functions --- plume-common/src/activity_pub/inbox.rs | 175 +++++++++++++++++++++++++ 1 file changed, 175 insertions(+) diff --git a/plume-common/src/activity_pub/inbox.rs b/plume-common/src/activity_pub/inbox.rs index 86ea85e5..3a7eb5d8 100644 --- a/plume-common/src/activity_pub/inbox.rs +++ b/plume-common/src/activity_pub/inbox.rs @@ -834,6 +834,15 @@ mod tests { gen_keypair, Error as SignError, Result as SignResult, Signer, }; use activitypub::{activity::*, actor::Person, object::Note}; + use activitystreams::{ + activity::{ + Announce as Announce07, Create as Create07, Delete as Delete07, Like as Like07, + }, + actor::Person as Person07, + base::Base, + object::Note as Note07, + prelude::*, + }; use once_cell::sync::Lazy; use openssl::{hash::MessageDigest, pkey::PKey, rsa::Rsa}; @@ -893,6 +902,22 @@ mod tests { &*MY_SIGNER } } + impl FromId07<()> for MyActor { + type Error = (); + type Object = Person07; + + fn from_db(_: &(), _id: &str) -> Result { + Ok(Self) + } + + fn from_activity(_: &(), _obj: Person07) -> Result { + Ok(Self) + } + + fn get_sender() -> &'static dyn Signer { + &*MY_SIGNER + } + } impl AsActor<&()> for MyActor { fn get_inbox_url(&self) -> String { @@ -961,6 +986,63 @@ mod tests { } } + struct MyObject07; + impl FromId07<()> for MyObject07 { + type Error = (); + type Object = Note07; + + fn from_db(_: &(), _id: &str) -> Result { + Ok(Self) + } + + fn from_activity(_: &(), _obj: Note07) -> Result { + Ok(Self) + } + + fn get_sender() -> &'static dyn Signer { + &*MY_SIGNER + } + } + impl AsObject07 for MyObject07 { + type Error = (); + type Output = (); + + fn activity(self, _: &(), _actor: MyActor, _id: &str) -> Result { + println!("MyActor is creating a Note"); + Ok(()) + } + } + + impl AsObject07 for MyObject07 { + type Error = (); + type Output = (); + + fn activity(self, _: &(), _actor: MyActor, _id: &str) -> Result { + println!("MyActor is liking a Note"); + Ok(()) + } + } + + impl AsObject07 for MyObject07 { + type Error = (); + type Output = (); + + fn activity(self, _: &(), _actor: MyActor, _id: &str) -> Result { + println!("MyActor is deleting a Note"); + Ok(()) + } + } + + impl AsObject07 for MyObject07 { + type Error = (); + type Output = (); + + fn activity(self, _: &(), _actor: MyActor, _id: &str) -> Result { + println!("MyActor is announcing a Note"); + Ok(()) + } + } + fn build_create() -> Create { let mut act = Create::default(); act.object_props @@ -980,6 +1062,19 @@ mod tests { act } + fn build_create07() -> Create07 { + let mut person = Person07::new(); + person.set_id("https://test.ap/actor".parse().unwrap()); + let mut note = Note07::new(); + note.set_id("https://test.ap/note".parse().unwrap()); + let mut act = Create07::new( + Base::retract(person).unwrap().into_generic().unwrap(), + Base::retract(note).unwrap().into_generic().unwrap(), + ); + act.set_id("https://test.ap/activity".parse().unwrap()); + act + } + #[test] fn test_inbox_basic() { let act = serde_json::to_value(build_create()).unwrap(); @@ -989,6 +1084,15 @@ mod tests { assert!(res.is_ok()); } + #[test] + fn test_inbox_basic07() { + let act = serde_json::to_value(build_create07()).unwrap(); + let res: Result<(), ()> = Inbox::handle(&(), act) + .with07::(None) + .done(); + assert!(res.is_ok()); + } + #[test] fn test_inbox_multi_handlers() { let act = serde_json::to_value(build_create()).unwrap(); @@ -1001,6 +1105,18 @@ mod tests { assert!(res.is_ok()); } + #[test] + fn test_inbox_multi_handlers07() { + let act = serde_json::to_value(build_create()).unwrap(); + let res: Result<(), ()> = Inbox::handle(&(), act) + .with07::(None) + .with07::(None) + .with07::(None) + .with07::(None) + .done(); + assert!(res.is_ok()); + } + #[test] fn test_inbox_failure() { let act = serde_json::to_value(build_create()).unwrap(); @@ -1012,6 +1128,17 @@ mod tests { assert!(res.is_err()); } + #[test] + fn test_inbox_failure07() { + let act = serde_json::to_value(build_create07()).unwrap(); + // Create is not handled by this inbox + let res: Result<(), ()> = Inbox::handle(&(), act) + .with07::(None) + .with07::(None) + .done(); + assert!(res.is_err()); + } + struct FailingActor; impl FromId<()> for FailingActor { type Error = (); @@ -1054,6 +1181,38 @@ mod tests { } } + impl FromId07<()> for FailingActor { + type Error = (); + type Object = Person07; + + fn from_db(_: &(), _id: &str) -> Result { + Err(()) + } + + fn from_activity(_: &(), _obj: Self::Object) -> Result { + Err(()) + } + + fn get_sender() -> &'static dyn Signer { + &*MY_SIGNER + } + } + + impl AsObject07 for MyObject07 { + type Error = (); + type Output = (); + + fn activity( + self, + _: &(), + _actor: FailingActor, + _id: &str, + ) -> Result { + println!("FailingActor is creating a Note"); + Ok(()) + } + } + #[test] fn test_inbox_actor_failure() { let act = serde_json::to_value(build_create()).unwrap(); @@ -1069,4 +1228,20 @@ mod tests { .done(); assert!(res.is_ok()); } + + #[test] + fn test_inbox_actor_failure07() { + let act = serde_json::to_value(build_create07()).unwrap(); + + let res: Result<(), ()> = Inbox::handle(&(), act.clone()) + .with07::(None) + .done(); + assert!(res.is_err()); + + let res: Result<(), ()> = Inbox::handle(&(), act.clone()) + .with07::(None) + .with07::(None) + .done(); + assert!(res.is_ok()); + } }