1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-11-26 08:21:09 +00:00
ibis/tests/common.rs

272 lines
8.6 KiB
Rust
Raw Normal View History

2023-12-05 00:17:02 +00:00
use anyhow::anyhow;
2024-01-03 16:06:52 +00:00
use ibis::backend::api::article::{CreateArticleData, EditArticleData, ForkArticleData};
use ibis::backend::api::instance::FollowInstance;
use ibis::backend::api::user::RegisterUserData;
use ibis::backend::api::user::{LoginResponse, LoginUserData};
use ibis::backend::api::ResolveObject;
use ibis::backend::database::conflict::ApiConflict;
use ibis::backend::database::instance::DbInstance;
use ibis::backend::error::MyResult;
use ibis::backend::start;
use ibis::common::ArticleView;
use ibis::frontend::api;
use ibis::frontend::api::get_query;
2023-11-16 14:27:35 +00:00
use once_cell::sync::Lazy;
2024-01-03 16:06:52 +00:00
use reqwest::{Client, StatusCode};
2023-11-16 14:27:35 +00:00
use serde::de::Deserialize;
2023-12-01 11:11:19 +00:00
use std::env::current_dir;
2023-12-12 15:32:57 +00:00
use std::fs::create_dir_all;
2023-12-01 11:11:19 +00:00
use std::process::{Command, Stdio};
2023-12-04 01:42:53 +00:00
use std::sync::atomic::{AtomicI32, Ordering};
2023-11-16 14:27:35 +00:00
use std::sync::Once;
2023-12-04 01:42:53 +00:00
use std::thread::{sleep, spawn};
use std::time::Duration;
2023-11-17 13:36:56 +00:00
use tokio::task::JoinHandle;
2023-11-16 14:27:35 +00:00
use tracing::log::LevelFilter;
2023-11-17 13:22:31 +00:00
use url::Url;
2023-11-16 14:27:35 +00:00
pub static CLIENT: Lazy<Client> = Lazy::new(Client::new);
2023-11-16 14:27:35 +00:00
2023-11-17 13:36:56 +00:00
pub struct TestData {
2023-12-20 16:08:19 +00:00
pub alpha: IbisInstance,
pub beta: IbisInstance,
pub gamma: IbisInstance,
2023-11-17 13:36:56 +00:00
}
impl TestData {
pub async fn start() -> Self {
2023-11-17 13:36:56 +00:00
static INIT: Once = Once::new();
INIT.call_once(|| {
env_logger::builder()
.filter_level(LevelFilter::Warn)
.filter_module("activitypub_federation", LevelFilter::Info)
2023-12-20 16:08:19 +00:00
.filter_module("ibis", LevelFilter::Info)
2023-11-17 13:36:56 +00:00
.init();
});
2023-12-04 01:42:53 +00:00
// Run things on different ports and db paths to allow parallel tests
static COUNTER: AtomicI32 = AtomicI32::new(0);
let current_run = COUNTER.fetch_add(1, Ordering::Relaxed);
// Give each test a moment to start its postgres databases
sleep(Duration::from_millis(current_run as u64 * 500));
let first_port = 8000 + (current_run * 3);
let port_alpha = first_port;
let port_beta = first_port + 1;
let port_gamma = first_port + 2;
let alpha_db_path = generate_db_path("alpha", port_alpha);
let beta_db_path = generate_db_path("beta", port_beta);
let gamma_db_path = generate_db_path("gamma", port_gamma);
2023-12-01 23:59:24 +00:00
// initialize postgres databases in parallel because its slow
for j in [
2023-12-20 16:08:19 +00:00
IbisInstance::prepare_db(alpha_db_path.clone()),
IbisInstance::prepare_db(beta_db_path.clone()),
IbisInstance::prepare_db(gamma_db_path.clone()),
2023-12-01 23:59:24 +00:00
] {
j.join().unwrap();
}
2023-11-17 13:36:56 +00:00
Self {
2023-12-20 16:08:19 +00:00
alpha: IbisInstance::start(alpha_db_path, port_alpha, "alpha").await,
beta: IbisInstance::start(beta_db_path, port_beta, "beta").await,
gamma: IbisInstance::start(gamma_db_path, port_gamma, "gamma").await,
2023-11-17 13:36:56 +00:00
}
}
2023-11-20 15:48:29 +00:00
pub fn stop(self) -> MyResult<()> {
2023-12-01 23:59:24 +00:00
for j in [self.alpha.stop(), self.beta.stop(), self.gamma.stop()] {
j.join().unwrap();
}
2023-11-17 13:36:56 +00:00
Ok(())
}
2023-11-16 14:27:35 +00:00
}
2023-12-04 01:42:53 +00:00
/// Generate a unique db path for each postgres so that tests can run in parallel.
fn generate_db_path(name: &'static str, port: i32) -> String {
2023-12-12 15:32:57 +00:00
let path = format!(
2023-12-04 01:42:53 +00:00
"{}/target/test_db/{name}-{port}",
current_dir().unwrap().display()
2023-12-12 15:32:57 +00:00
);
create_dir_all(&path).unwrap();
path
}
2023-12-20 16:08:19 +00:00
pub struct IbisInstance {
2023-12-01 11:11:19 +00:00
pub hostname: String,
pub jwt: String,
2023-12-01 23:59:24 +00:00
db_path: String,
db_handle: JoinHandle<()>,
2023-12-01 11:11:19 +00:00
}
2023-12-20 16:08:19 +00:00
impl IbisInstance {
2023-12-01 23:59:24 +00:00
fn prepare_db(db_path: String) -> std::thread::JoinHandle<()> {
spawn(move || {
Command::new("./tests/scripts/start_dev_db.sh")
.arg(&db_path)
.stdout(Stdio::null())
.stderr(Stdio::null())
.output()
.unwrap();
})
}
async fn start(db_path: String, port: i32, username: &str) -> Self {
2023-12-01 11:11:19 +00:00
let db_url = format!("postgresql://lemmy:password@/lemmy?host={db_path}");
let hostname = format!("localhost:{port}");
let hostname_ = hostname.clone();
let handle = tokio::task::spawn(async move {
start(&hostname_, &db_url).await.unwrap();
});
2024-01-03 12:29:25 +00:00
// wait a moment for the backend to start
2023-12-13 15:58:29 +00:00
tokio::time::sleep(Duration::from_millis(100)).await;
let register_res = register(&hostname, username, "hunter2").await.unwrap();
assert!(!register_res.jwt.is_empty());
2023-12-01 11:11:19 +00:00
Self {
2023-12-13 15:58:29 +00:00
jwt: register_res.jwt,
2023-12-01 11:11:19 +00:00
hostname,
db_path,
2023-12-01 23:59:24 +00:00
db_handle: handle,
2023-12-01 11:11:19 +00:00
}
}
2023-12-01 23:59:24 +00:00
fn stop(self) -> std::thread::JoinHandle<()> {
self.db_handle.abort();
spawn(move || {
Command::new("./tests/scripts/stop_dev_db.sh")
.arg(&self.db_path)
.stdout(Stdio::null())
.stderr(Stdio::null())
.output()
.unwrap();
})
2023-12-01 11:11:19 +00:00
}
}
pub const TEST_ARTICLE_DEFAULT_TEXT: &str = "some\nexample\ntext\n";
2023-12-20 16:08:19 +00:00
pub async fn create_article(instance: &IbisInstance, title: String) -> MyResult<ArticleView> {
let create_form = CreateArticleData {
title: title.clone(),
};
let req = CLIENT
.post(format!("http://{}/api/v1/article", &instance.hostname))
.form(&create_form)
.bearer_auth(&instance.jwt);
2024-01-03 16:06:52 +00:00
let article: ArticleView = api::handle_json_res(req).await?;
// create initial edit to ensure that conflicts are generated (there are no conflicts on empty file)
let edit_form = EditArticleData {
2023-12-01 13:04:51 +00:00
article_id: article.article.id,
new_text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: article.latest_version,
2023-11-27 15:34:45 +00:00
resolve_conflict_id: None,
};
2023-12-18 15:40:27 +00:00
edit_article(instance, &edit_form).await
2023-11-24 14:48:43 +00:00
}
2023-11-27 15:34:45 +00:00
pub async fn edit_article_with_conflict(
2023-12-20 16:08:19 +00:00
instance: &IbisInstance,
2023-11-27 15:34:45 +00:00
edit_form: &EditArticleData,
2023-11-28 12:04:33 +00:00
) -> MyResult<Option<ApiConflict>> {
2023-12-05 00:17:02 +00:00
let req = CLIENT
.patch(format!("http://{}/api/v1/article", instance.hostname))
.form(edit_form)
.bearer_auth(&instance.jwt);
2024-01-03 16:06:52 +00:00
api::handle_json_res(req).await
2023-11-27 15:34:45 +00:00
}
2023-12-20 16:08:19 +00:00
pub async fn get_conflicts(instance: &IbisInstance) -> MyResult<Vec<ApiConflict>> {
2023-12-19 14:32:14 +00:00
let req = CLIENT
.get(format!(
"http://{}/api/v1/edit_conflicts",
&instance.hostname
))
.bearer_auth(&instance.jwt);
2024-01-03 16:06:52 +00:00
api::handle_json_res(req).await
2023-12-19 14:32:14 +00:00
}
pub async fn edit_article(
2023-12-20 16:08:19 +00:00
instance: &IbisInstance,
edit_form: &EditArticleData,
) -> MyResult<ArticleView> {
let edit_res = edit_article_with_conflict(instance, edit_form).await?;
2023-11-27 15:34:45 +00:00
assert!(edit_res.is_none());
2024-01-03 16:06:52 +00:00
api::get_article(&instance.hostname, edit_form.article_id).await
2023-11-24 14:31:31 +00:00
}
2023-11-16 14:27:35 +00:00
pub async fn get<T>(hostname: &str, endpoint: &str) -> MyResult<T>
where
T: for<'de> Deserialize<'de>,
{
get_query(hostname, endpoint, None::<i32>).await
}
2023-12-13 15:58:29 +00:00
pub async fn fork_article(
2023-12-20 16:08:19 +00:00
instance: &IbisInstance,
2023-12-13 15:58:29 +00:00
form: &ForkArticleData,
) -> MyResult<ArticleView> {
2023-12-05 00:17:02 +00:00
let req = CLIENT
2023-12-13 15:58:29 +00:00
.post(format!("http://{}/api/v1/article/fork", instance.hostname))
.form(form)
.bearer_auth(&instance.jwt);
2024-01-03 16:06:52 +00:00
api::handle_json_res(req).await
2023-11-16 14:27:35 +00:00
}
2023-11-17 13:22:31 +00:00
2023-12-20 16:08:19 +00:00
pub async fn follow_instance(instance: &IbisInstance, follow_instance: &str) -> MyResult<()> {
2023-11-17 13:22:31 +00:00
// fetch beta instance on alpha
let resolve_form = ResolveObject {
2023-12-04 14:10:07 +00:00
id: Url::parse(&format!("http://{}", follow_instance))?,
2023-11-17 13:22:31 +00:00
};
2023-11-22 15:41:34 +00:00
let instance_resolved: DbInstance =
2024-01-03 16:06:52 +00:00
api::get_query(&instance.hostname, "resolve_instance", Some(resolve_form)).await?;
2023-11-17 13:22:31 +00:00
// send follow
let follow_form = FollowInstance {
id: instance_resolved.id,
2023-11-17 13:22:31 +00:00
};
// cant use post helper because follow doesnt return json
2023-12-13 12:13:10 +00:00
let res = CLIENT
2023-12-14 16:06:44 +00:00
.post(format!(
"http://{}/api/v1/instance/follow",
instance.hostname
))
2023-11-17 13:22:31 +00:00
.form(&follow_form)
2023-12-14 16:06:44 +00:00
.bearer_auth(&instance.jwt)
2023-11-17 13:22:31 +00:00
.send()
.await?;
2023-12-13 12:13:10 +00:00
if res.status() == StatusCode::OK {
Ok(())
} else {
Err(anyhow!("API error: {}", res.text().await?).into())
}
2023-11-17 13:22:31 +00:00
}
2023-12-13 15:58:29 +00:00
pub async fn register(hostname: &str, username: &str, password: &str) -> MyResult<LoginResponse> {
let register_form = RegisterUserData {
username: username.to_string(),
password: password.to_string(),
};
let req = CLIENT
.post(format!("http://{}/api/v1/user/register", hostname))
.form(&register_form);
2024-01-03 16:06:52 +00:00
api::handle_json_res(req).await
2023-12-13 15:58:29 +00:00
}
pub async fn login(
2023-12-20 16:08:19 +00:00
instance: &IbisInstance,
2023-12-13 15:58:29 +00:00
username: &str,
password: &str,
) -> MyResult<LoginResponse> {
let login_form = LoginUserData {
username: username.to_string(),
password: password.to_string(),
};
let req = CLIENT
.post(format!("http://{}/api/v1/user/login", instance.hostname))
.form(&login_form);
2024-01-03 16:06:52 +00:00
api::handle_json_res(req).await
2023-12-13 15:58:29 +00:00
}