Add test for get_local_actor()

This commit is contained in:
silverpill 2022-04-26 22:35:39 +00:00
parent 8deea0c867
commit 83fbbefaab
3 changed files with 40 additions and 3 deletions

View file

@ -251,3 +251,33 @@ pub fn get_instance_actor(
}; };
Ok(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);
}
}

View file

@ -79,13 +79,13 @@ pub fn create_http_signature(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use rand::rngs::OsRng; use crate::utils::crypto::generate_weak_private_key;
use super::*; use super::*;
#[test] #[test]
fn test_create_signature_get() { fn test_create_signature_get() {
let request_url = "https://example.org/inbox"; 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 actor_key_id = "https://myserver.org/actor#main-key";
let headers = create_http_signature( let headers = create_http_signature(
@ -114,7 +114,7 @@ mod tests {
fn test_create_signature_post() { fn test_create_signature_post() {
let request_url = "https://example.org/inbox"; let request_url = "https://example.org/inbox";
let request_body = "{}"; 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 actor_key_id = "https://myserver.org/actor#main-key";
let result = create_http_signature( let result = create_http_signature(

View file

@ -26,6 +26,13 @@ pub fn generate_private_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
RsaPrivateKey::new(&mut rng, bits) RsaPrivateKey::new(&mut rng, bits)
} }
#[cfg(test)]
pub fn generate_weak_private_key() -> Result<RsaPrivateKey, rsa::errors::Error> {
let mut rng = rand::rngs::OsRng;
let bits = 512;
RsaPrivateKey::new(&mut rng, bits)
}
pub fn serialize_private_key( pub fn serialize_private_key(
private_key: RsaPrivateKey, private_key: RsaPrivateKey,
) -> Result<String, rsa::pkcs8::Error> { ) -> Result<String, rsa::pkcs8::Error> {