From 83fbbefaabad9118e5d3d8b5c08d11f1ab6c2f84 Mon Sep 17 00:00:00 2001 From: silverpill Date: Tue, 26 Apr 2022 22:35:39 +0000 Subject: [PATCH] Add test for get_local_actor() --- src/activitypub/actor.rs | 30 ++++++++++++++++++++++++++++++ src/http_signatures/create.rs | 6 +++--- src/utils/crypto.rs | 7 +++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/activitypub/actor.rs b/src/activitypub/actor.rs index ffa9df1..48b3a20 100644 --- a/src/activitypub/actor.rs +++ b/src/activitypub/actor.rs @@ -251,3 +251,33 @@ pub fn get_instance_actor( }; Ok(actor) } + +#[cfg(test)] +mod tests { + use crate::models::profiles::types::DbActorProfile; + use crate::utils::crypto::{ + generate_weak_private_key, + serialize_private_key, + }; + use super::*; + + const INSTANCE_URL: &str = "https://example.com"; + + #[test] + fn test_local_actor() { + let private_key = generate_weak_private_key().unwrap(); + let private_key_pem = serialize_private_key(private_key).unwrap(); + let profile = DbActorProfile { + username: "testuser".to_string(), + ..Default::default() + }; + let user = User { + private_key: private_key_pem, + profile, + ..Default::default() + }; + let actor = get_local_actor(&user, INSTANCE_URL).unwrap(); + assert_eq!(actor.id, "https://example.com/users/testuser"); + assert_eq!(actor.preferred_username, user.profile.username); + } +} diff --git a/src/http_signatures/create.rs b/src/http_signatures/create.rs index 9675fd6..9d4b833 100644 --- a/src/http_signatures/create.rs +++ b/src/http_signatures/create.rs @@ -79,13 +79,13 @@ pub fn create_http_signature( #[cfg(test)] mod tests { - use rand::rngs::OsRng; + use crate::utils::crypto::generate_weak_private_key; use super::*; #[test] fn test_create_signature_get() { let request_url = "https://example.org/inbox"; - let actor_key = RsaPrivateKey::new(&mut OsRng, 512).unwrap(); + let actor_key = generate_weak_private_key().unwrap(); let actor_key_id = "https://myserver.org/actor#main-key"; let headers = create_http_signature( @@ -114,7 +114,7 @@ mod tests { fn test_create_signature_post() { let request_url = "https://example.org/inbox"; let request_body = "{}"; - let actor_key = RsaPrivateKey::new(&mut OsRng, 512).unwrap(); + let actor_key = generate_weak_private_key().unwrap(); let actor_key_id = "https://myserver.org/actor#main-key"; let result = create_http_signature( diff --git a/src/utils/crypto.rs b/src/utils/crypto.rs index d10b061..70e0f3b 100644 --- a/src/utils/crypto.rs +++ b/src/utils/crypto.rs @@ -26,6 +26,13 @@ pub fn generate_private_key() -> Result { RsaPrivateKey::new(&mut rng, bits) } +#[cfg(test)] +pub fn generate_weak_private_key() -> Result { + let mut rng = rand::rngs::OsRng; + let bits = 512; + RsaPrivateKey::new(&mut rng, bits) +} + pub fn serialize_private_key( private_key: RsaPrivateKey, ) -> Result {