1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-12-25 13:41:24 +00:00
ibis/tests/test.rs

382 lines
13 KiB
Rust
Raw Normal View History

2023-11-15 12:05:07 +00:00
extern crate fediwiki;
2023-11-16 09:25:54 +00:00
2023-11-16 14:27:35 +00:00
mod common;
2023-11-24 14:48:43 +00:00
use crate::common::{
2023-11-27 15:34:45 +00:00
create_article, edit_article, edit_article_with_conflict, follow_instance, get_article,
get_query, TestData, TEST_ARTICLE_DEFAULT_TEXT,
2023-11-24 14:48:43 +00:00
};
2023-11-16 14:27:35 +00:00
use common::get;
2023-11-28 12:04:33 +00:00
use fediwiki::api::{ApiConflict, EditArticleData, ResolveObject};
2023-11-16 09:25:54 +00:00
use fediwiki::error::MyResult;
2023-11-15 12:05:07 +00:00
use fediwiki::federation::objects::article::DbArticle;
use fediwiki::federation::objects::edit::ApubEdit;
2023-11-16 11:48:57 +00:00
use fediwiki::federation::objects::instance::DbInstance;
use serial_test::serial;
2023-11-16 11:48:57 +00:00
use url::Url;
2023-11-15 12:05:07 +00:00
#[tokio::test]
2023-11-16 11:48:57 +00:00
#[serial]
async fn test_create_read_and_edit_article() -> MyResult<()> {
2023-11-17 13:36:56 +00:00
let data = TestData::start();
2023-11-15 12:05:07 +00:00
2023-11-16 12:38:41 +00:00
// error on nonexistent article
2023-11-24 14:48:43 +00:00
let title = "Manu_Chao".to_string();
let not_found = get_article(data.hostname_alpha, &title).await;
2023-11-16 12:38:41 +00:00
assert!(not_found.is_err());
// create article
2023-11-24 14:48:43 +00:00
let create_res = create_article(data.hostname_alpha, title.clone()).await?;
assert_eq!(title, create_res.title);
2023-11-16 12:38:41 +00:00
assert!(create_res.local);
// now article can be read
2023-11-24 14:48:43 +00:00
let get_res = get_article(data.hostname_alpha, &create_res.title).await?;
assert_eq!(title, get_res.title);
assert_eq!(TEST_ARTICLE_DEFAULT_TEXT, get_res.text);
2023-11-16 12:38:41 +00:00
assert!(get_res.local);
// edit article
let edit_form = EditArticleData {
ap_id: create_res.ap_id.clone(),
new_text: "Lorem Ipsum 2".to_string(),
2023-11-27 15:34:45 +00:00
previous_version: get_res.latest_version,
resolve_conflict_id: None,
};
2023-11-24 14:31:31 +00:00
let edit_res = edit_article(data.hostname_alpha, &create_res.title, &edit_form).await?;
assert_eq!(edit_form.new_text, edit_res.text);
assert_eq!(2, edit_res.edits.len());
2023-11-17 13:36:56 +00:00
data.stop()
2023-11-15 12:05:07 +00:00
}
2023-11-15 14:07:02 +00:00
#[tokio::test]
2023-11-16 11:48:57 +00:00
#[serial]
2023-11-16 09:25:54 +00:00
async fn test_follow_instance() -> MyResult<()> {
2023-11-17 13:36:56 +00:00
let data = TestData::start();
2023-11-15 14:07:02 +00:00
2023-11-16 11:48:57 +00:00
// check initial state
2023-11-17 13:36:56 +00:00
let alpha_instance: DbInstance = get(data.hostname_alpha, "instance").await?;
2023-11-16 11:48:57 +00:00
assert_eq!(0, alpha_instance.follows.len());
2023-11-17 13:36:56 +00:00
let beta_instance: DbInstance = get(data.hostname_beta, "instance").await?;
2023-11-16 11:48:57 +00:00
assert_eq!(0, beta_instance.followers.len());
follow_instance(data.hostname_alpha, data.hostname_beta).await?;
2023-11-16 11:48:57 +00:00
// check that follow was federated
2023-11-17 13:36:56 +00:00
let beta_instance: DbInstance = get(data.hostname_beta, "instance").await?;
2023-11-16 11:48:57 +00:00
assert_eq!(1, beta_instance.followers.len());
2023-11-17 13:36:56 +00:00
let alpha_instance: DbInstance = get(data.hostname_alpha, "instance").await?;
2023-11-16 11:48:57 +00:00
assert_eq!(1, alpha_instance.follows.len());
2023-11-16 09:25:54 +00:00
2023-11-17 13:36:56 +00:00
data.stop()
2023-11-16 09:25:54 +00:00
}
2023-11-16 15:40:43 +00:00
#[tokio::test]
#[serial]
async fn test_synchronize_articles() -> MyResult<()> {
2023-11-17 13:36:56 +00:00
let data = TestData::start();
2023-11-16 15:40:43 +00:00
// create article on alpha
2023-11-24 14:48:43 +00:00
let title = "Manu_Chao".to_string();
let create_res = create_article(data.hostname_alpha, title.clone()).await?;
assert_eq!(title, create_res.title);
assert_eq!(1, create_res.edits.len());
2023-11-16 15:40:43 +00:00
assert!(create_res.local);
// edit the article
let edit_form = EditArticleData {
ap_id: create_res.ap_id.clone(),
new_text: "Lorem Ipsum 2\n".to_string(),
2023-11-27 15:34:45 +00:00
previous_version: create_res.latest_version,
resolve_conflict_id: None,
};
edit_article(data.hostname_alpha, &title, &edit_form).await?;
2023-11-16 15:40:43 +00:00
// article is not yet on beta
2023-11-24 14:48:43 +00:00
let get_res = get_article(data.hostname_beta, &create_res.title).await;
2023-11-16 15:40:43 +00:00
assert!(get_res.is_err());
// fetch alpha instance on beta, articles are also fetched automatically
let resolve_object = ResolveObject {
2023-11-17 13:36:56 +00:00
id: Url::parse(&format!("http://{}", data.hostname_alpha))?,
2023-11-16 15:40:43 +00:00
};
2023-11-22 15:41:34 +00:00
get_query::<DbInstance, _>(data.hostname_beta, "resolve_instance", Some(resolve_object))
.await?;
2023-11-16 15:40:43 +00:00
// get the article and compare
2023-11-24 14:48:43 +00:00
let get_res = get_article(data.hostname_beta, &create_res.title).await?;
2023-11-16 15:40:43 +00:00
assert_eq!(create_res.ap_id, get_res.ap_id);
2023-11-24 14:48:43 +00:00
assert_eq!(title, get_res.title);
assert_eq!(2, get_res.edits.len());
assert_eq!(edit_form.new_text, get_res.text);
2023-11-16 15:40:43 +00:00
assert!(!get_res.local);
2023-11-17 13:36:56 +00:00
data.stop()
2023-11-16 15:40:43 +00:00
}
2023-11-17 13:22:31 +00:00
#[tokio::test]
#[serial]
async fn test_edit_local_article() -> MyResult<()> {
2023-11-17 13:36:56 +00:00
let data = TestData::start();
follow_instance(data.hostname_alpha, data.hostname_beta).await?;
2023-11-17 13:22:31 +00:00
// create new article
2023-11-24 14:48:43 +00:00
let title = "Manu_Chao".to_string();
let create_res = create_article(data.hostname_beta, title.clone()).await?;
assert_eq!(title, create_res.title);
assert!(create_res.local);
2023-11-17 13:22:31 +00:00
// article should be federated to alpha
2023-11-24 14:48:43 +00:00
let get_res = get_article(data.hostname_alpha, &create_res.title).await?;
2023-11-17 13:22:31 +00:00
assert_eq!(create_res.title, get_res.title);
assert_eq!(1, get_res.edits.len());
assert!(!get_res.local);
2023-11-17 13:22:31 +00:00
assert_eq!(create_res.text, get_res.text);
// edit the article
2023-11-21 15:27:18 +00:00
let edit_form = EditArticleData {
2023-11-17 13:22:31 +00:00
ap_id: create_res.ap_id,
new_text: "Lorem Ipsum 2".to_string(),
2023-11-27 15:34:45 +00:00
previous_version: get_res.latest_version,
resolve_conflict_id: None,
2023-11-17 13:22:31 +00:00
};
2023-11-24 14:31:31 +00:00
let edit_res = edit_article(data.hostname_beta, &create_res.title, &edit_form).await?;
2023-11-17 13:22:31 +00:00
assert_eq!(edit_res.text, edit_form.new_text);
assert_eq!(edit_res.edits.len(), 2);
2023-11-21 15:27:18 +00:00
assert!(edit_res.edits[0]
.id
.to_string()
.starts_with(&edit_res.ap_id.to_string()));
2023-11-17 13:22:31 +00:00
// edit should be federated to alpha
2023-11-24 14:48:43 +00:00
let get_res = get_article(data.hostname_alpha, &edit_res.title).await?;
2023-11-17 13:22:31 +00:00
assert_eq!(edit_res.title, get_res.title);
assert_eq!(edit_res.edits.len(), 2);
assert_eq!(edit_res.text, get_res.text);
data.stop()
}
#[tokio::test]
#[serial]
async fn test_edit_remote_article() -> MyResult<()> {
let data = TestData::start();
follow_instance(data.hostname_alpha, data.hostname_beta).await?;
follow_instance(data.hostname_gamma, data.hostname_beta).await?;
// create new article
2023-11-24 14:48:43 +00:00
let title = "Manu_Chao".to_string();
let create_res = create_article(data.hostname_beta, title.clone()).await?;
assert_eq!(title, create_res.title);
assert!(create_res.local);
// article should be federated to alpha and gamma
2023-11-24 14:48:43 +00:00
let get_res = get_article(data.hostname_alpha, &title).await?;
assert_eq!(create_res.title, get_res.title);
assert_eq!(1, get_res.edits.len());
assert!(!get_res.local);
2023-11-24 14:48:43 +00:00
let get_res = get_article(data.hostname_gamma, &title).await?;
assert_eq!(create_res.title, get_res.title);
assert_eq!(create_res.text, get_res.text);
let edit_form = EditArticleData {
ap_id: create_res.ap_id,
new_text: "Lorem Ipsum 2".to_string(),
2023-11-27 15:34:45 +00:00
previous_version: get_res.latest_version,
resolve_conflict_id: None,
};
2023-11-24 14:48:43 +00:00
let edit_res = edit_article(data.hostname_alpha, &title, &edit_form).await?;
assert_eq!(edit_form.new_text, edit_res.text);
assert_eq!(2, edit_res.edits.len());
assert!(!edit_res.local);
assert!(edit_res.edits[0]
.id
.to_string()
.starts_with(&edit_res.ap_id.to_string()));
// edit should be federated to beta and gamma
2023-11-24 14:48:43 +00:00
let get_res = get_article(data.hostname_alpha, &title).await?;
assert_eq!(edit_res.title, get_res.title);
assert_eq!(edit_res.edits.len(), 2);
assert_eq!(edit_res.text, get_res.text);
2023-11-24 14:48:43 +00:00
let get_res = get_article(data.hostname_gamma, &title).await?;
assert_eq!(edit_res.title, get_res.title);
assert_eq!(edit_res.edits.len(), 2);
2023-11-17 13:22:31 +00:00
assert_eq!(edit_res.text, get_res.text);
2023-11-17 13:36:56 +00:00
data.stop()
2023-11-17 13:22:31 +00:00
}
2023-11-22 15:41:34 +00:00
#[tokio::test]
#[serial]
2023-11-27 15:34:45 +00:00
async fn test_local_edit_conflict() -> MyResult<()> {
let data = TestData::start();
// create new article
let title = "Manu_Chao".to_string();
let create_res = create_article(data.hostname_alpha, title.clone()).await?;
assert_eq!(title, create_res.title);
assert!(create_res.local);
// one user edits article
let edit_form = EditArticleData {
ap_id: create_res.ap_id.clone(),
new_text: "Lorem Ipsum\n".to_string(),
previous_version: create_res.latest_version.clone(),
resolve_conflict_id: None,
};
let edit_res = edit_article(data.hostname_alpha, &create_res.title, &edit_form).await?;
assert_eq!(edit_res.text, edit_form.new_text);
assert_eq!(2, edit_res.edits.len());
// another user edits article, without being aware of previous edit
let edit_form = EditArticleData {
ap_id: create_res.ap_id.clone(),
new_text: "Ipsum Lorem\n".to_string(),
previous_version: create_res.latest_version,
resolve_conflict_id: None,
};
let edit_res = edit_article_with_conflict(data.hostname_alpha, &edit_form)
.await?
.unwrap();
assert_eq!("<<<<<<< ours\nIpsum Lorem\n||||||| original\nsome\nexample\ntext\n=======\nLorem Ipsum\n>>>>>>> theirs\n", edit_res.three_way_merge);
2023-11-27 15:34:45 +00:00
2023-11-28 12:04:33 +00:00
let conflicts: Vec<ApiConflict> =
2023-11-27 15:34:45 +00:00
get_query(data.hostname_alpha, "edit_conflicts", None::<()>).await?;
assert_eq!(1, conflicts.len());
assert_eq!(conflicts[0], edit_res);
let edit_form = EditArticleData {
ap_id: create_res.ap_id.clone(),
new_text: "Lorem Ipsum and Ipsum Lorem\n".to_string(),
2023-11-28 12:04:33 +00:00
previous_version: edit_res.previous_version,
2023-11-27 15:34:45 +00:00
resolve_conflict_id: Some(edit_res.id),
};
let edit_res = edit_article(data.hostname_alpha, &create_res.title, &edit_form).await?;
assert_eq!(edit_form.new_text, edit_res.text);
2023-11-28 12:04:33 +00:00
let conflicts: Vec<ApiConflict> =
2023-11-27 15:34:45 +00:00
get_query(data.hostname_alpha, "edit_conflicts", None::<()>).await?;
assert_eq!(0, conflicts.len());
data.stop()
}
#[tokio::test]
#[serial]
async fn test_federated_edit_conflict() -> MyResult<()> {
2023-11-22 15:41:34 +00:00
let data = TestData::start();
follow_instance(data.hostname_alpha, data.hostname_beta).await?;
// create new article
2023-11-24 14:48:43 +00:00
let title = "Manu_Chao".to_string();
let create_res = create_article(data.hostname_beta, title.clone()).await?;
assert_eq!(title, create_res.title);
2023-11-22 15:41:34 +00:00
assert!(create_res.local);
2023-11-24 14:31:31 +00:00
// fetch article to gamma
let resolve_object = ResolveObject {
id: create_res.ap_id.inner().clone(),
};
let resolve_res: DbArticle =
get_query(data.hostname_gamma, "resolve_article", Some(resolve_object)).await?;
assert_eq!(create_res.text, resolve_res.text);
2023-11-22 15:41:34 +00:00
// alpha edits article
let edit_form = EditArticleData {
ap_id: create_res.ap_id.clone(),
new_text: "Lorem Ipsum\n".to_string(),
2023-11-27 15:34:45 +00:00
previous_version: create_res.latest_version.clone(),
resolve_conflict_id: None,
2023-11-22 15:41:34 +00:00
};
2023-11-24 14:31:31 +00:00
let edit_res = edit_article(data.hostname_alpha, &create_res.title, &edit_form).await?;
2023-11-22 15:41:34 +00:00
assert_eq!(edit_res.text, edit_form.new_text);
assert_eq!(2, edit_res.edits.len());
2023-11-22 15:41:34 +00:00
assert!(!edit_res.local);
assert!(edit_res.edits[1]
2023-11-22 15:41:34 +00:00
.id
.to_string()
.starts_with(&edit_res.ap_id.to_string()));
// gamma also edits, as its not the latest version there is a conflict. local version should
// not be updated with this conflicting version, instead user needs to handle the conflict
2023-11-22 15:41:34 +00:00
let edit_form = EditArticleData {
2023-11-27 15:34:45 +00:00
ap_id: create_res.ap_id.clone(),
new_text: "aaaa\n".to_string(),
2023-11-27 15:34:45 +00:00
previous_version: create_res.latest_version,
resolve_conflict_id: None,
2023-11-22 15:41:34 +00:00
};
2023-11-28 12:04:33 +00:00
let edit_res = edit_article(data.hostname_gamma, &title, &edit_form).await?;
assert_ne!(edit_form.new_text, edit_res.text);
assert_eq!(2, edit_res.edits.len());
2023-11-22 15:41:34 +00:00
assert!(!edit_res.local);
2023-11-28 12:04:33 +00:00
let conflicts: Vec<ApiConflict> =
get_query(data.hostname_gamma, "edit_conflicts", None::<()>).await?;
assert_eq!(1, conflicts.len());
2023-11-27 15:34:45 +00:00
// resolve the conflict
let edit_form = EditArticleData {
ap_id: create_res.ap_id,
new_text: "aaaa\n".to_string(),
2023-11-28 12:04:33 +00:00
previous_version: conflicts[0].previous_version.clone(),
resolve_conflict_id: Some(conflicts[0].id),
2023-11-27 15:34:45 +00:00
};
2023-11-28 12:04:33 +00:00
let edit_res = edit_article(data.hostname_gamma, &title, &edit_form).await?;
2023-11-27 15:34:45 +00:00
assert_eq!(edit_form.new_text, edit_res.text);
assert_eq!(3, edit_res.edits.len());
let conflicts: Vec<ApubEdit> =
get_query(data.hostname_gamma, "edit_conflicts", None::<()>).await?;
assert_eq!(0, conflicts.len());
2023-11-22 15:41:34 +00:00
data.stop()
}
#[tokio::test]
#[serial]
async fn test_overlapping_edits_no_conflict() -> MyResult<()> {
let data = TestData::start();
// create new article
let title = "Manu_Chao".to_string();
let create_res = create_article(data.hostname_alpha, title.clone()).await?;
assert_eq!(title, create_res.title);
assert!(create_res.local);
// one user edits article
let edit_form = EditArticleData {
ap_id: create_res.ap_id.clone(),
new_text: "my\nexample\ntext\n".to_string(),
previous_version: create_res.latest_version.clone(),
resolve_conflict_id: None,
};
let edit_res = edit_article(data.hostname_alpha, &create_res.title, &edit_form).await?;
assert_eq!(edit_res.text, edit_form.new_text);
assert_eq!(2, edit_res.edits.len());
// another user edits article, without being aware of previous edit
let edit_form = EditArticleData {
ap_id: create_res.ap_id.clone(),
new_text: "some\nexample\narticle\n".to_string(),
previous_version: create_res.latest_version,
resolve_conflict_id: None,
};
let edit_res = edit_article(data.hostname_alpha, &title, &edit_form).await?;
let conflicts: Vec<ApiConflict> =
get_query(data.hostname_alpha, "edit_conflicts", None::<()>).await?;
assert_eq!(0, conflicts.len());
assert_eq!(3, edit_res.edits.len());
assert_eq!("my\nexample\narticle\n", edit_res.text);
data.stop()
}