Add tests for create_note function

This commit is contained in:
silverpill 2021-10-09 12:53:53 +00:00
parent 8610719bd0
commit 9f114f4255
7 changed files with 138 additions and 23 deletions

View file

@ -3,7 +3,6 @@ use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use uuid::Uuid;
use crate::config::Config;
use crate::models::posts::types::Post;
use crate::models::profiles::types::DbActorProfile;
use crate::utils::files::get_file_url;
@ -100,20 +99,20 @@ fn create_activity(
}
pub fn create_note(
config: &Config,
instance_url: &str,
post: &Post,
in_reply_to: Option<&Post>,
) -> Object {
let object_id = get_object_url(
&config.instance_url(),
instance_url,
&post.id,
);
let actor_id = get_actor_url(
&config.instance_url(),
instance_url,
&post.author.username,
);
let attachments: Vec<Attachment> = post.attachments.iter().map(|db_item| {
let url = get_file_url(&config.instance_url(), &db_item.file_name);
let url = get_file_url(instance_url, &db_item.file_name);
let media_type = db_item.media_type.clone().unwrap_or("".to_string());
Attachment {
name: "".to_string(),
@ -128,7 +127,7 @@ pub fn create_note(
assert_eq!(post.id, in_reply_to_id);
match post.author.is_local() {
false => post.object_id.clone(),
true => Some(get_object_url(&config.instance_url(), &post.id)),
true => Some(get_object_url(instance_url, &post.id)),
}
},
None => None,
@ -141,7 +140,7 @@ pub fn create_note(
attachment: Some(attachments),
object: None,
published: Some(post.created_at),
attributed_to: Some(actor_id.clone()),
attributed_to: Some(actor_id),
in_reply_to: in_reply_to_object_id,
content: Some(post.content.clone()),
to: Some(json!(AP_PUBLIC)),
@ -149,13 +148,13 @@ pub fn create_note(
}
pub fn create_activity_note(
config: &Config,
instance_url: &str,
post: &Post,
in_reply_to: Option<&Post>,
) -> Activity {
let object = create_note(config, post, in_reply_to);
let object = create_note(instance_url, post, in_reply_to);
let activity = create_activity(
&config.instance_url(),
instance_url,
&post.author.username,
CREATE,
None,
@ -165,7 +164,7 @@ pub fn create_activity_note(
}
pub fn create_activity_follow(
config: &Config,
instance_url: &str,
actor_profile: &DbActorProfile,
follow_request_id: &Uuid,
target_id: &str,
@ -184,7 +183,7 @@ pub fn create_activity_follow(
to: None,
};
let activity = create_activity(
&config.instance_url(),
instance_url,
&actor_profile.username,
FOLLOW,
Some(*follow_request_id),
@ -194,7 +193,7 @@ pub fn create_activity_follow(
}
pub fn create_activity_accept_follow(
config: &Config,
instance_url: &str,
actor_profile: &DbActorProfile,
follow_activity_id: &str,
) -> Activity {
@ -213,7 +212,7 @@ pub fn create_activity_accept_follow(
to: None,
};
let activity = create_activity(
&config.instance_url(),
instance_url,
&actor_profile.username,
ACCEPT,
None,
@ -223,18 +222,18 @@ pub fn create_activity_accept_follow(
}
pub fn create_activity_undo_follow(
config: &Config,
instance_url: &str,
actor_profile: &DbActorProfile,
follow_request_id: &Uuid,
target_id: &str,
) -> Activity {
// TODO: retrieve 'Follow' activity from database
let follow_activity_id = get_object_url(
&config.instance_url(),
instance_url,
follow_request_id,
);
let follow_actor_id = get_actor_url(
&config.instance_url(),
instance_url,
&actor_profile.username,
);
let object = Object {
@ -251,7 +250,7 @@ pub fn create_activity_undo_follow(
to: None,
};
let activity = create_activity(
&config.instance_url(),
instance_url,
&actor_profile.username,
UNDO,
None,
@ -281,3 +280,70 @@ impl OrderedCollection {
}
}
}
#[cfg(test)]
mod tests {
use super::*;
const INSTANCE_URL: &str = "https://example.com";
#[test]
fn test_create_note() {
let author = DbActorProfile {
username: "author".to_string(),
..Default::default()
};
let post = Post { author, ..Default::default() };
let note = create_note(INSTANCE_URL, &post, None);
assert_eq!(
note.id,
format!("{}/objects/{}", INSTANCE_URL, post.id),
);
assert_eq!(note.attachment.unwrap().len(), 0);
assert_eq!(
note.attributed_to.unwrap(),
format!("{}/users/{}", INSTANCE_URL, post.author.username),
);
assert_eq!(note.in_reply_to.is_none(), true);
assert_eq!(note.content.unwrap(), post.content);
}
#[test]
fn test_create_note_with_local_parent() {
let parent = Post::default();
let post = Post {
in_reply_to_id: Some(parent.id),
..Default::default()
};
let note = create_note(INSTANCE_URL, &post, Some(&parent));
assert_eq!(
note.in_reply_to.unwrap(),
format!("{}/objects/{}", INSTANCE_URL, parent.id),
);
}
#[test]
fn test_create_note_with_remote_parent() {
let parent_author = DbActorProfile {
actor_json: Some(json!("test")),
..Default::default()
};
let parent = Post {
author: parent_author,
object_id: Some("https://test.net/obj/123".to_string()),
..Default::default()
};
let post = Post {
in_reply_to_id: Some(parent.id),
..Default::default()
};
let note = create_note(INSTANCE_URL, &post, Some(&parent));
assert_eq!(
note.in_reply_to.unwrap(),
parent.object_id.unwrap(),
);
}
}

View file

@ -141,7 +141,11 @@ pub async fn receive_activity(
let target_profile = get_profile_by_acct(db_client, &target_username).await?;
// Create and send 'Accept' activity
let target_user = get_user_by_id(db_client, &target_profile.id).await?;
let new_activity = create_activity_accept_follow(&config, &target_profile, &activity.id);
let new_activity = create_activity_accept_follow(
&config.instance_url(),
&target_profile,
&activity.id,
);
// Save relationship
follow(db_client, &source_profile.id, &target_profile.id).await?;

View file

@ -139,7 +139,7 @@ pub async fn get_object(
},
None => None,
};
let object = create_note(&config, post, in_reply_to);
let object = create_note(&config.instance_url(), post, in_reply_to);
let response = HttpResponse::Ok()
.content_type(ACTIVITY_CONTENT_TYPE)
.json(object);

View file

@ -124,7 +124,7 @@ async fn follow(
let actor: Actor = serde_json::from_value(actor_value)
.map_err(|_| HttpError::InternalError)?;
let activity = create_activity_follow(
&config,
&config.instance_url(),
&current_user.profile,
&request.id,
&actor.id,
@ -171,7 +171,7 @@ async fn unfollow(
let actor: Actor = serde_json::from_value(actor_value)
.map_err(|_| HttpError::InternalError)?;
let activity = create_activity_undo_follow(
&config,
&config.instance_url(),
&current_user.profile,
&follow_request.id,
&actor.id,

View file

@ -44,7 +44,11 @@ async fn create_status(
},
None => None,
};
let activity = create_activity_note(&config, &post, in_reply_to.as_ref());
let activity = create_activity_note(
&config.instance_url(),
&post,
in_reply_to.as_ref(),
);
let followers = get_followers(db_client, &current_user.id).await?;
let mut recipients: Vec<Actor> = Vec::new();
for follower in followers {

View file

@ -40,6 +40,25 @@ pub struct Post {
pub created_at: DateTime<Utc>,
}
#[cfg(test)]
impl Default for Post {
fn default() -> Self {
Self {
id: Uuid::new_v4(),
author: Default::default(),
content: "".to_string(),
in_reply_to_id: None,
reply_count: 0,
attachments: vec![],
object_id: None,
ipfs_cid: None,
token_id: None,
token_tx_id: None,
created_at: Utc::now(),
}
}
}
impl TryFrom<&Row> for Post {
type Error = tokio_postgres::Error;

View file

@ -74,6 +74,28 @@ impl DbActorProfile {
}
}
#[cfg(test)]
impl Default for DbActorProfile {
fn default() -> Self {
Self {
id: Uuid::new_v4(),
username: "".to_string(),
acct: "".to_string(),
display_name: None,
bio: None,
bio_source: None,
avatar_file_name: None,
banner_file_name: None,
extra_fields: ExtraFields(vec![]),
follower_count: 0,
following_count: 0,
post_count: 0,
created_at: Utc::now(),
actor_json: None,
}
}
}
pub struct ProfileCreateData {
pub username: String,
pub display_name: Option<String>,