Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/standalone tests #249

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
mod config;
mod db;
pub mod db;
mod error;
pub mod events;
pub mod models;
Expand Down
2 changes: 1 addition & 1 deletion src/models/post/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ mod tests {
const REPLY_ID: &str = "2ZECXVXHZBE00";
const POST_ID: &str = "2ZECRNM66G900";

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_post_details_get_from_graph() {
// Open connections against ddbb
let config = Config::from_env();
Expand Down
2 changes: 1 addition & 1 deletion src/models/user/details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ mod tests {
"not_existing_user_id_either", // Does not exist
];

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_get_by_ids_from_redis() {
let config = Config::from_env();
setup(&config).await;
Expand Down
1 change: 1 addition & 0 deletions tests/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod service;
pub mod utils;
pub mod watcher;
118 changes: 54 additions & 64 deletions tests/service/all.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
use crate::service::utils::{get_request, invalid_get_request, post_request};
use anyhow::Result;
use pubky_nexus::models::tag::TagDetails;
use reqwest::StatusCode;
use serde_json::json;
use std::{
fs::{self, create_dir_all, remove_file, File},
io::Write,
};

use anyhow::Result;
use pubky_nexus::models::tag::TagDetails;
use serde_json::json;
use crate::utils::TestServiceServer;

const HOST_URL: &str = "http://localhost:8080";

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_static_serving() -> Result<()> {
TestServiceServer::get_test_server().await;
let client = httpc_test::new_client(HOST_URL)?;
let test_file_path = "static";
let test_file_name = "foobar";
Expand Down Expand Up @@ -48,35 +52,30 @@ async fn test_static_serving() -> Result<()> {
Ok(())
}

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_file_details() -> Result<()> {
let client = httpc_test::new_client(HOST_URL)?;
let test_file_id = "2ZK2H8P2T5NG0";
let test_file_user = "y4euc58gnmxun9wo87gwmanu6kztt9pgw1zz1yp1azp7trrsjamy";

let test_file_uri = format!("pubky://{test_file_user}/pub/pubky.app/files/{test_file_id}");

let res = client
.do_get(
format!(
"/v0/files/file/{}",
url::form_urlencoded::byte_serialize(test_file_uri.as_bytes()).collect::<String>()
)
.as_str(),
let res = get_request(
format!(
"/v0/files/file/{}",
url::form_urlencoded::byte_serialize(test_file_uri.as_bytes()).collect::<String>()
)
.await?;
.as_str(),
)
.await?;

let json_body = res.json_body()?;
assert_eq!(res.status(), 200);
assert_eq!(json_body["id"], test_file_id);
assert_eq!(json_body["owner_id"], test_file_user);
assert_eq!(res["id"], test_file_id);
assert_eq!(res["owner_id"], test_file_user);

Ok(())
}

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_files_by_ids() -> Result<()> {
let client = httpc_test::new_client(HOST_URL)?;
let test_file_id = "2ZK2H8P2T5NG0";
let test_file_user = "y4euc58gnmxun9wo87gwmanu6kztt9pgw1zz1yp1azp7trrsjamy";
let test_file_uri = format!("pubky://{test_file_user}/pub/pubky.app/files/{test_file_id}");
Expand All @@ -85,72 +84,63 @@ async fn test_files_by_ids() -> Result<()> {
let test_file_user2 = "sfgetccnq7s3h57a7imf6n7k5fqxus33yg85f1ndhnrnofjdmhjy";
let test_file_uri2 = format!("pubky://{test_file_user2}/pub/pubky.app/files/{test_file_id2}");

let res = client
.do_post(
"/v0/files/by-ids",
json!({"uris": [test_file_uri, test_file_uri2]}),
)
.await?;

let json_body = res.json_body()?;
let res = post_request(
"/v0/files/by-ids",
json!({"uris": [test_file_uri, test_file_uri2]}),
)
.await?;

assert_eq!(res.status(), 200);
assert_eq!(json_body.as_array().unwrap().len(), 2);
assert_eq!(res.as_array().unwrap().len(), 2);

assert_eq!(json_body[0]["id"], test_file_id);
assert_eq!(json_body[0]["owner_id"], test_file_user);
assert_eq!(json_body[1]["id"], test_file_id2);
assert_eq!(json_body[1]["owner_id"], test_file_user2);
assert_eq!(res[0]["id"], test_file_id);
assert_eq!(res[0]["owner_id"], test_file_user);
assert_eq!(res[1]["id"], test_file_id2);
assert_eq!(res[1]["owner_id"], test_file_user2);

Ok(())
}

#[tokio::test]
#[tokio_shared_rt::test(shared)]
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);
let res = get_request(&format!(
"/v0/post/{}/{}?viewer_id={}",
author_id, post_id, author_id
))
.await?;

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

// Panic if tags vector is bigger that 1
let post_tag_object = body["tags"][0].clone();
let post_tag_object = res["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);
invalid_get_request(
&format!("/v0/post/{}/{}", author_id, "no_post"),
StatusCode::NOT_FOUND,
)
.await?;

Ok(())
}
11 changes: 8 additions & 3 deletions tests/service/endpoints.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use anyhow::Result;

use crate::utils::TestServiceServer;

const HOST_URL: &str = "http://localhost:8080";

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_swagger_ui() -> Result<()> {
TestServiceServer::get_test_server().await;
let client = httpc_test::new_client(HOST_URL)?;

let res = client.do_get("/swagger-ui").await?;
Expand All @@ -14,8 +17,9 @@ async fn test_swagger_ui() -> Result<()> {
Ok(())
}

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_openapi_schema() -> Result<()> {
TestServiceServer::get_test_server().await;
let client = httpc_test::new_client(HOST_URL)?;

let res = client.do_get("/api-docs/openapi.json").await?;
Expand All @@ -30,8 +34,9 @@ async fn test_openapi_schema() -> Result<()> {
Ok(())
}

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_info_endpoint() -> Result<()> {
TestServiceServer::get_test_server().await;
let client = httpc_test::new_client(HOST_URL)?;

let res = client.do_get("/v0/info").await?;
Expand Down
6 changes: 3 additions & 3 deletions tests/service/stream/post/author.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use super::{ROOT_PATH, USER_ID};
use crate::service::utils::make_request;
use crate::service::utils::get_request;
use anyhow::Result;

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_stream_user_posts() -> Result<()> {
let path = format!(
"{ROOT_PATH}?author_id={}&source=author&sorting=timeline",
USER_ID
);
let body = make_request(&path).await?;
let body = get_request(&path).await?;

assert!(body.is_array());

Expand Down
6 changes: 3 additions & 3 deletions tests/service/stream/post/author_replies.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use super::ROOT_PATH;
use crate::service::utils::make_request;
use crate::service::utils::get_request;
use anyhow::Result;

#[tokio::test]
#[tokio_shared_rt::test(shared)]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the benefits for service tests of using the shared tokio runtime?

async fn test_stream_user_replies() -> Result<()> {
let author_id = "pxnu33x7jtpx9ar1ytsi4yxbp6a5o36gwhffs8zoxmbuptici1jy";
let path = format!(
"{ROOT_PATH}?author_id={}&source=author_replies&sorting=timeline",
author_id
);
let body = make_request(&path).await?;
let body = get_request(&path).await?;

assert!(body.is_array());

Expand Down
18 changes: 9 additions & 9 deletions tests/service/stream/post/bookmarks.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use super::utils::verify_post_list;
use super::ROOT_PATH;
use crate::service::utils::make_request;
use crate::service::utils::get_request;
use anyhow::Result;

// User with most bookmarks
const BOOKMARKER_ID: &str = "o1gg96ewuojmopcjbz8895478wdtxtzzuxnfjjz8o8e77csa1ngo";

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_stream_bookmarked_posts() -> Result<()> {
let observer_id = BOOKMARKER_ID;
let path = format!("{ROOT_PATH}?observer_id={}&source=bookmarks", observer_id);
let body = make_request(&path).await?;
let body = get_request(&path).await?;

assert!(body.is_array());

Expand Down Expand Up @@ -40,12 +40,12 @@ pub const POST_TK: &str = "2ZAX1DBDD5YG0";
pub const START_TIMELINE: &str = "1724134149050";
pub const END_TIMELINE: &str = "1724134141150";

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_stream_user_bookmarks_by_timeline_with_start() -> Result<()> {
let path =
format!("{ROOT_PATH}?observer_id={BOOKMARKER_ID}&source=bookmarks&start={START_TIMELINE}");

let body = make_request(&path).await?;
let body = get_request(&path).await?;
let post_list = vec![
POST_TC, POST_TD, POST_TE, POST_TF, POST_TG, POST_TH, POST_TI, POST_TJ, POST_TK,
];
Expand All @@ -54,13 +54,13 @@ async fn test_stream_user_bookmarks_by_timeline_with_start() -> Result<()> {
Ok(())
}

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_stream_user_bookmarks_by_timeline_with_start_and_end() -> Result<()> {
let path = format!(
"{ROOT_PATH}?observer_id={BOOKMARKER_ID}&source=bookmarks&start={START_TIMELINE}&end={END_TIMELINE}"
);

let body = make_request(&path).await?;
let body = get_request(&path).await?;
let post_list = vec![
POST_TC, POST_TD, POST_TE, POST_TF, POST_TG, POST_TH, POST_TI,
];
Expand All @@ -69,13 +69,13 @@ async fn test_stream_user_bookmarks_by_timeline_with_start_and_end() -> Result<(
Ok(())
}

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_stream_user_bookmarks_by_timeline_with_skip_end() -> Result<()> {
let path = format!(
"{ROOT_PATH}?observer_id={BOOKMARKER_ID}&source=bookmarks&limit=5&end={END_TIMELINE}"
);

let body = make_request(&path).await?;
let body = get_request(&path).await?;
let post_list = vec![POST_TA, POST_TB, POST_TC, POST_TD, POST_TE];
verify_post_list(post_list, body);

Expand Down
14 changes: 7 additions & 7 deletions tests/service/stream/post/kind/all.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::service::stream::post::utils::verify_post_list;
use crate::service::stream::post::ROOT_PATH;
use crate::service::utils::make_request;
use crate::service::utils::get_request;
use anyhow::Result;

const POST_A1: &str = "SIJW1TGL5BKG6";
Expand All @@ -25,11 +25,11 @@ pub const END_TIMELINE: &str = "1980477299303";
pub const PUBKY_TAG: &str = "pubky";
pub const FK_TAG: &str = "4k";

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_stream_post_kind() -> Result<()> {
let path = format!("{ROOT_PATH}?author_id={BOGOTA}&source=author");

let body = make_request(&path).await?;
let body = get_request(&path).await?;
let post_list = vec![
POST_A1, POST_A2, POST_A3, POST_A4, POST_A5, POST_A6, POST_A7, POST_A8, POST_A9, POST_A10,
];
Expand All @@ -38,24 +38,24 @@ async fn test_stream_post_kind() -> Result<()> {
Ok(())
}

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_stream_post_kind_with_start_and_end() -> Result<()> {
let path = format!(
"{ROOT_PATH}?author_id={BOGOTA}&source=author&start={START_TIMELINE}&end={END_TIMELINE}"
);

let body = make_request(&path).await?;
let body = get_request(&path).await?;
let post_list = vec![POST_A3, POST_A4, POST_A5, POST_A6, POST_A7];
verify_post_list(post_list, body);

Ok(())
}

#[tokio::test]
#[tokio_shared_rt::test(shared)]
async fn test_stream_post_kind_with_tag() -> Result<()> {
let path = format!("{ROOT_PATH}?author_id={BOGOTA}&source=author&tags={PUBKY_TAG}");

let body = make_request(&path).await?;
let body = get_request(&path).await?;
let post_list = vec![POST_A4, POST_A9, POST_W_PUBKY_TAG_1, POST_W_PUBKY_TAG_2];
verify_post_list(post_list, body);

Expand Down
Loading
Loading