Skip to content

Commit

Permalink
add post view integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tipogi committed Jan 16, 2025
1 parent 85a9bf6 commit 6b1abd2
Show file tree
Hide file tree
Showing 7 changed files with 143 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/routes/v0/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub const USER_FRIENDS_ROUTE: &str = concatcp!(USER_ROUTE, "/friends");
pub const USER_MUTED_ROUTE: &str = concatcp!(USER_ROUTE, "/muted");

// -- POST endpoints --
const POST_PREFIX: &str = concatcp!(VERSION_ROUTE, "/post");
pub const POST_PREFIX: &str = concatcp!(VERSION_ROUTE, "/post");
pub const POST_ROUTE: &str = concatcp!(POST_PREFIX, "/{author_id}/{post_id}");
pub const POST_RELATIONSHIPS_ROUTE: &str = concatcp!(POST_ROUTE, "/relationships");
pub const POST_BOOKMARK_ROUTE: &str = concatcp!(POST_ROUTE, "/bookmark");
Expand Down
4 changes: 2 additions & 2 deletions src/routes/v0/post/tags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use utoipa::OpenApi;
("limit_taggers" = Option<usize>, Query, description = "Upper limit on the number of taggers per tag"),
),
responses(
(status = 200, description = "Post tags", body = TagPost),
(status = 404, description = "Post not found"),
(status = 200, description = "Post tags", body = Vec<TagDetails>),
(status = 500, description = "Internal server error")
)
)]
Expand Down Expand Up @@ -65,7 +65,7 @@ pub async fn post_tags_handler(
("limit" = Option<usize>, Query, description = "Number of taggers to return for pagination")
),
responses(
(status = 200, description = "Post tags", body = TagPost),
(status = 200, description = "Post tags", body = Taggers),
(status = 404, description = "Post not found"),
(status = 500, description = "Internal server error")
)
Expand Down
4 changes: 2 additions & 2 deletions src/routes/v0/post/view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ use utoipa::OpenApi;
("author_id" = String, Path, description = "Author Pubky ID"),
("post_id" = String, Path, description = "Post Crockford32 ID"),
("viewer_id" = Option<String>, Query, description = "Viewer Pubky ID"),
("max_tags" = Option<usize>, Query, description = "Upper limit on the number of tags for the post"),
("max_taggers" = Option<usize>, Query, description = "Upper limit on the number of taggers per tag")
("limit_tags" = Option<usize>, Query, description = "Upper limit on the number of tags for the post"),
("limit_taggers" = Option<usize>, Query, description = "Upper limit on the number of taggers per tag")
),
responses(
(status = 200, description = "Post", body = PostView),
Expand Down
51 changes: 0 additions & 51 deletions tests/service/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{
};

use anyhow::Result;
use pubky_nexus::models::tag::TagDetails;
use serde_json::json;

const HOST_URL: &str = "http://localhost:8080";
Expand Down Expand Up @@ -104,53 +103,3 @@ async fn test_files_by_ids() -> Result<()> {

Ok(())
}

#[tokio::test]
async fn test_get_post() -> Result<()> {
let client = httpc_test::new_client(HOST_URL)?;

let author_id = "y4euc58gnmxun9wo87gwmanu6kztt9pgw1zz1yp1azp7trrsjamy";
let post_id = "2ZCW1TGR5BKG0";

let res = client
.do_get(&format!(
"/v0/post/{}/{}?viewer_id={}",
author_id, post_id, author_id
))
.await?;
assert_eq!(res.status(), 200);

let body = res.json_body()?;

assert_eq!(body["details"]["content"], "I am told we can reply now!");
assert_eq!(body["details"]["indexed_at"].as_u64(), Some(1718616844478));
assert_eq!(body["details"]["id"], post_id);
assert_eq!(body["details"]["author"], author_id);
assert_eq!(body["details"]["attachments"].as_array().unwrap().len(), 1);
assert_eq!(
(body["details"]["attachments"].as_array().unwrap())[0],
"pubky://y4euc58gnmxun9wo87gwmanu6kztt9pgw1zz1yp1azp7trrsjamy/pub/pubky.app/files/2ZKH7K7M9G3G0".to_string()
);
assert_eq!(
body["details"]["uri"],
"pubky://y4euc58gnmxun9wo87gwmanu6kztt9pgw1zz1yp1azp7trrsjamy/pub/pubky.app/posts/2ZCW1TGR5BKG0"
);
assert_eq!(body["counts"]["tags"].as_u64(), Some(5));
assert_eq!(body["counts"]["replies"].as_u64(), Some(2));
assert_eq!(body["counts"]["reposts"].as_u64(), Some(1));
assert_eq!(body["bookmark"]["indexed_at"].as_u64(), Some(1721764200000));
assert_eq!(body["bookmark"]["id"], "2Z9PFGC3WWWW0");

// Panic if tags vector is bigger that 1
let post_tag_object = body["tags"][0].clone();
let post_tag: TagDetails = serde_json::from_value(post_tag_object.clone())?;
assert_eq!(post_tag.label, "pubky");

// Test non-existing post
let res = client
.do_get(&format!("/v0/post/{}/{}", author_id, "no_post"))
.await?;
assert_eq!(res.status(), 404);

Ok(())
}
1 change: 1 addition & 0 deletions tests/service/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ pub mod stream;
pub mod tags;
pub mod user;
pub mod utils;
pub mod post;
7 changes: 7 additions & 0 deletions tests/service/post/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use pubky_nexus::routes::v0::endpoints;

pub mod view;

pub const ROOT_PATH: &str = endpoints::POST_PREFIX;
pub const CAIRO_USER: &str = "f5tcy5gtgzshipr6pag6cn9uski3s8tjare7wd3n7enmyokgjk1o";
pub const ENCRYPTION_TAG: &str = "encryption";
130 changes: 130 additions & 0 deletions tests/service/post/view.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use crate::service::{post::{CAIRO_USER, ENCRYPTION_TAG, ROOT_PATH}, stream::post::{POST_H, TAG_LABEL_2}, utils::{make_request, HOST_URL}};
use anyhow::Result;
use pubky_nexus::models::tag::TagDetails;

#[tokio::test]
async fn test_get_post_view() -> Result<()> {
let client = httpc_test::new_client(HOST_URL)?;

let author_id = "y4euc58gnmxun9wo87gwmanu6kztt9pgw1zz1yp1azp7trrsjamy";
let post_id = "2ZCW1TGR5BKG0";

let res = client
.do_get(&format!(
"/v0/post/{}/{}?viewer_id={}",
author_id, post_id, author_id
))
.await?;
assert_eq!(res.status(), 200);

let body = res.json_body()?;

assert_eq!(body["details"]["content"], "I am told we can reply now!");
assert_eq!(body["details"]["indexed_at"].as_u64(), Some(1718616844478));
assert_eq!(body["details"]["id"], post_id);
assert_eq!(body["details"]["author"], author_id);
assert_eq!(body["details"]["attachments"].as_array().unwrap().len(), 1);
assert_eq!(
(body["details"]["attachments"].as_array().unwrap())[0],
"pubky://y4euc58gnmxun9wo87gwmanu6kztt9pgw1zz1yp1azp7trrsjamy/pub/pubky.app/files/2ZKH7K7M9G3G0".to_string()
);
assert_eq!(
body["details"]["uri"],
"pubky://y4euc58gnmxun9wo87gwmanu6kztt9pgw1zz1yp1azp7trrsjamy/pub/pubky.app/posts/2ZCW1TGR5BKG0"
);
assert_eq!(body["counts"]["tags"].as_u64(), Some(5));
assert_eq!(body["counts"]["replies"].as_u64(), Some(2));
assert_eq!(body["counts"]["reposts"].as_u64(), Some(1));
assert_eq!(body["bookmark"]["indexed_at"].as_u64(), Some(1721764200000));
assert_eq!(body["bookmark"]["id"], "2Z9PFGC3WWWW0");

// Panic if tags vector is bigger that 1
let post_tag_object = body["tags"][0].clone();
let post_tag: TagDetails = serde_json::from_value(post_tag_object.clone())?;
assert_eq!(post_tag.label, "pubky");

// Test non-existing post
let res = client
.do_get(&format!("/v0/post/{}/{}", author_id, "no_post"))
.await?;
assert_eq!(res.status(), 404);

Ok(())
}



#[tokio::test]
async fn test_get_post_view_with_limit_tags() -> Result<()> {

let path = format!("{}/{}/{}?limit_tags=1", ROOT_PATH,CAIRO_USER, POST_H);

let body = make_request(&path).await?;
//let post_tag: PostView = serde_json::from_value(body.clone())?;
assert!(body.is_object());

// Check the tag list
let tags = body["tags"].as_array().expect("Post tags should be an array");
// Check the total posts using that tag
assert_eq!(tags.len(), 1);

// opersource tag
assert_eq!(tags[0]["label"], TAG_LABEL_2);
assert_eq!(tags[0]["taggers_count"], 4);
let taggers = tags[0]["taggers"].as_array().expect("Tag taggers should be an array");
assert_eq!(taggers.len(), 4);

Ok(())
}

#[tokio::test]
async fn test_get_post_view_with_limit_taggers() -> Result<()> {

let path = format!("{}/{}/{}?limit_taggers=2", ROOT_PATH,CAIRO_USER, POST_H);

let body = make_request(&path).await?;

assert!(body.is_object());
// Check the tag list
let tags = body["tags"].as_array().expect("Post tags should be an array");
// Check the total posts using that tag
assert_eq!(tags.len(), 2);

// opersource tag
assert_eq!(tags[0]["label"], TAG_LABEL_2);
assert_eq!(tags[0]["taggers_count"], 4);
let opensource_taggers = tags[0]["taggers"].as_array().expect("Tag taggers should be an array");
assert_eq!(opensource_taggers.len(), 2);


assert_eq!(tags[1]["label"], ENCRYPTION_TAG);
assert_eq!(tags[1]["taggers_count"], 2);
let encryption_taggers = tags[1]["taggers"].as_array().expect("Tag taggers should be an array");
assert_eq!(encryption_taggers.len(), 2);

Ok(())
}

#[tokio::test]
async fn test_get_post_view_with_limit_tags_and_taggers() -> Result<()> {

let path = format!("{}/{}/{}?limit_tags=1&limit_taggers=2", ROOT_PATH,CAIRO_USER, POST_H);

let body = make_request(&path).await?;

assert!(body.is_object());

// Check the tag list
let tags = body["tags"].as_array().expect("Post tags should be an array");
// Check the total posts using that tag
assert_eq!(tags.len(), 1);

// opersource tag
assert_eq!(tags[0]["label"], TAG_LABEL_2);
assert_eq!(tags[0]["taggers_count"], 4);
let taggers = tags[0]["taggers"].as_array().expect("Tag taggers should be an array");
assert_eq!(taggers.len(), 2);

Ok(())
}

0 comments on commit 6b1abd2

Please sign in to comment.