2023-11-24 14:48:43 +00:00
|
|
|
use fediwiki::api::{
|
2023-11-28 12:04:33 +00:00
|
|
|
ApiConflict, CreateArticleData, EditArticleData, FollowInstance, GetArticleData, ResolveObject,
|
2023-11-24 14:48:43 +00:00
|
|
|
};
|
2023-12-01 13:04:51 +00:00
|
|
|
use fediwiki::database::article::ArticleView;
|
2023-11-16 14:27:35 +00:00
|
|
|
use fediwiki::error::MyResult;
|
2023-11-17 13:22:31 +00:00
|
|
|
use fediwiki::federation::objects::instance::DbInstance;
|
2023-11-20 15:48:29 +00:00
|
|
|
use fediwiki::start;
|
2023-11-16 14:27:35 +00:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
use reqwest::Client;
|
|
|
|
use serde::de::Deserialize;
|
|
|
|
use serde::ser::Serialize;
|
2023-12-01 11:11:19 +00:00
|
|
|
use std::env::current_dir;
|
|
|
|
use std::process::{Command, Stdio};
|
2023-11-16 14:27:35 +00:00
|
|
|
use std::sync::Once;
|
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
|
|
|
|
2023-11-17 11:23:14 +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-01 11:11:19 +00:00
|
|
|
pub alpha: Instance,
|
|
|
|
pub beta: Instance,
|
|
|
|
pub gamma: Instance,
|
2023-11-17 13:36:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl TestData {
|
|
|
|
pub fn start() -> Self {
|
|
|
|
static INIT: Once = Once::new();
|
|
|
|
INIT.call_once(|| {
|
|
|
|
env_logger::builder()
|
|
|
|
.filter_level(LevelFilter::Warn)
|
|
|
|
.filter_module("activitypub_federation", LevelFilter::Info)
|
|
|
|
.filter_module("fediwiki", LevelFilter::Info)
|
|
|
|
.init();
|
|
|
|
});
|
|
|
|
|
2023-12-01 14:16:07 +00:00
|
|
|
// initialize postgres databases in parallel because its slow
|
|
|
|
let (alpha_db_path, alpha_db_thread) = start_temporary_database("alpha");
|
|
|
|
let (beta_db_path, beta_db_thread) = start_temporary_database("beta");
|
|
|
|
let (gamma_db_path, gamma_db_thread) = start_temporary_database("gamma");
|
|
|
|
|
|
|
|
alpha_db_thread.join().unwrap();
|
|
|
|
beta_db_thread.join().unwrap();
|
|
|
|
gamma_db_thread.join().unwrap();
|
|
|
|
|
2023-11-17 13:36:56 +00:00
|
|
|
Self {
|
2023-12-01 14:16:07 +00:00
|
|
|
alpha: Instance::start(alpha_db_path, 8131),
|
|
|
|
beta: Instance::start(beta_db_path, 8132),
|
|
|
|
gamma: Instance::start(gamma_db_path, 8133),
|
2023-11-17 13:36:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-20 15:48:29 +00:00
|
|
|
pub fn stop(self) -> MyResult<()> {
|
2023-12-01 11:11:19 +00:00
|
|
|
self.alpha.stop();
|
|
|
|
self.beta.stop();
|
|
|
|
self.gamma.stop();
|
2023-11-17 13:36:56 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2023-11-16 14:27:35 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 14:16:07 +00:00
|
|
|
fn start_temporary_database(name: &'static str) -> (String, std::thread::JoinHandle<()>) {
|
|
|
|
let db_path = format!("{}/target/test_db/{name}", current_dir().unwrap().display());
|
|
|
|
let db_path_ = db_path.clone();
|
|
|
|
let db_thread = std::thread::spawn(move || {
|
|
|
|
Command::new("./tests/scripts/start_dev_db.sh")
|
|
|
|
.arg(&db_path_)
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
});
|
|
|
|
(db_path, db_thread)
|
|
|
|
}
|
|
|
|
|
2023-12-01 11:11:19 +00:00
|
|
|
pub struct Instance {
|
|
|
|
db_path: String,
|
|
|
|
pub hostname: String,
|
|
|
|
handle: JoinHandle<()>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Instance {
|
2023-12-01 14:16:07 +00:00
|
|
|
fn start(db_path: String, port: i32) -> 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();
|
|
|
|
});
|
|
|
|
Self {
|
|
|
|
db_path,
|
|
|
|
hostname,
|
|
|
|
handle,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn stop(self) {
|
|
|
|
self.handle.abort();
|
|
|
|
Command::new("./tests/scripts/stop_dev_db.sh")
|
|
|
|
.arg(&self.db_path)
|
|
|
|
.stdout(Stdio::null())
|
|
|
|
.stderr(Stdio::null())
|
|
|
|
.output()
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-28 12:13:36 +00:00
|
|
|
pub const TEST_ARTICLE_DEFAULT_TEXT: &str = "some\nexample\ntext\n";
|
2023-11-27 10:25:29 +00:00
|
|
|
|
2023-11-30 14:55:05 +00:00
|
|
|
pub async fn create_article(hostname: &str, title: String) -> MyResult<ArticleView> {
|
2023-11-27 10:25:29 +00:00
|
|
|
let create_form = CreateArticleData {
|
|
|
|
title: title.clone(),
|
|
|
|
};
|
2023-12-01 13:04:51 +00:00
|
|
|
let article: ArticleView = post(hostname, "article", &create_form).await?;
|
2023-11-27 10:25:29 +00:00
|
|
|
// 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,
|
2023-11-27 10:25:29 +00:00
|
|
|
new_text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
|
2023-11-27 15:34:45 +00:00
|
|
|
previous_version: article.latest_version,
|
|
|
|
resolve_conflict_id: None,
|
2023-11-27 10:25:29 +00:00
|
|
|
};
|
2023-11-28 13:29:51 +00:00
|
|
|
edit_article(hostname, &edit_form).await
|
2023-11-24 14:48:43 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 14:55:05 +00:00
|
|
|
pub async fn get_article(hostname: &str, article_id: i32) -> MyResult<ArticleView> {
|
2023-11-30 14:14:30 +00:00
|
|
|
let get_article = GetArticleData { article_id };
|
2023-11-30 14:55:05 +00:00
|
|
|
get_query::<ArticleView, _>(hostname, "article", Some(get_article.clone())).await
|
2023-11-24 14:48:43 +00:00
|
|
|
}
|
|
|
|
|
2023-11-27 15:34:45 +00:00
|
|
|
pub async fn edit_article_with_conflict(
|
|
|
|
hostname: &str,
|
|
|
|
edit_form: &EditArticleData,
|
2023-11-28 12:04:33 +00:00
|
|
|
) -> MyResult<Option<ApiConflict>> {
|
2023-11-27 15:34:45 +00:00
|
|
|
Ok(CLIENT
|
|
|
|
.patch(format!("http://{}/api/v1/article", hostname))
|
|
|
|
.form(edit_form)
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.json()
|
|
|
|
.await?)
|
|
|
|
}
|
|
|
|
|
2023-11-30 14:55:05 +00:00
|
|
|
pub async fn edit_article(hostname: &str, edit_form: &EditArticleData) -> MyResult<ArticleView> {
|
2023-11-28 12:04:33 +00:00
|
|
|
let edit_res: Option<ApiConflict> = CLIENT
|
2023-11-24 14:31:31 +00:00
|
|
|
.patch(format!("http://{}/api/v1/article", hostname))
|
2023-11-28 13:29:51 +00:00
|
|
|
.form(&edit_form)
|
2023-11-24 14:31:31 +00:00
|
|
|
.send()
|
2023-11-27 15:34:45 +00:00
|
|
|
.await?
|
|
|
|
.json()
|
2023-11-24 14:31:31 +00:00
|
|
|
.await?;
|
2023-11-27 15:34:45 +00:00
|
|
|
assert!(edit_res.is_none());
|
2023-11-30 14:55:05 +00:00
|
|
|
get_article(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
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn get_query<T, R>(hostname: &str, endpoint: &str, query: Option<R>) -> MyResult<T>
|
|
|
|
where
|
|
|
|
T: for<'de> Deserialize<'de>,
|
|
|
|
R: Serialize,
|
|
|
|
{
|
|
|
|
let mut res = CLIENT.get(format!("http://{}/api/v1/{}", hostname, endpoint));
|
|
|
|
if let Some(query) = query {
|
|
|
|
res = res.query(&query);
|
|
|
|
}
|
|
|
|
let alpha_instance: T = res.send().await?.json().await?;
|
|
|
|
Ok(alpha_instance)
|
|
|
|
}
|
|
|
|
|
2023-11-28 14:11:05 +00:00
|
|
|
pub async fn post<T: Serialize, R>(hostname: &str, endpoint: &str, form: &T) -> MyResult<R>
|
2023-11-16 14:27:35 +00:00
|
|
|
where
|
|
|
|
R: for<'de> Deserialize<'de>,
|
|
|
|
{
|
|
|
|
Ok(CLIENT
|
|
|
|
.post(format!("http://{}/api/v1/{}", hostname, endpoint))
|
|
|
|
.form(form)
|
|
|
|
.send()
|
|
|
|
.await?
|
|
|
|
.json()
|
|
|
|
.await?)
|
|
|
|
}
|
2023-11-17 13:22:31 +00:00
|
|
|
|
|
|
|
pub async fn follow_instance(follow_instance: &str, followed_instance: &str) -> MyResult<()> {
|
|
|
|
// fetch beta instance on alpha
|
|
|
|
let resolve_form = ResolveObject {
|
|
|
|
id: Url::parse(&format!("http://{}", followed_instance))?,
|
|
|
|
};
|
2023-11-22 15:41:34 +00:00
|
|
|
let instance_resolved: DbInstance =
|
|
|
|
get_query(followed_instance, "resolve_instance", Some(resolve_form)).await?;
|
2023-11-17 13:22:31 +00:00
|
|
|
|
|
|
|
// send follow
|
|
|
|
let follow_form = FollowInstance {
|
2023-11-22 15:41:34 +00:00
|
|
|
instance_id: instance_resolved.ap_id,
|
2023-11-17 13:22:31 +00:00
|
|
|
};
|
|
|
|
// cant use post helper because follow doesnt return json
|
|
|
|
CLIENT
|
|
|
|
.post(format!("http://{}/api/v1/instance/follow", follow_instance))
|
|
|
|
.form(&follow_form)
|
|
|
|
.send()
|
|
|
|
.await?;
|
|
|
|
Ok(())
|
|
|
|
}
|