1
0
Fork 0
mirror of https://github.com/Nutomic/ibis.git synced 2024-12-26 18:31:22 +00:00
ibis/tests/test.rs

812 lines
29 KiB
Rust
Raw Normal View History

2024-12-03 13:52:33 +00:00
#![expect(clippy::unwrap_used)]
2024-02-28 14:54:34 +00:00
2023-11-16 14:27:35 +00:00
mod common;
2024-01-17 16:14:16 +00:00
use crate::common::{TestData, TEST_ARTICLE_DEFAULT_TEXT};
2024-12-03 13:52:33 +00:00
use anyhow::Result;
use ibis::common::{
utils::extract_domain,
ArticleView,
CreateArticleForm,
EditArticleForm,
ForkArticleForm,
GetArticleForm,
GetUserForm,
ListArticlesForm,
LoginUserForm,
Notification,
ProtectArticleForm,
RegisterUserForm,
SearchArticleForm,
2024-02-01 11:23:51 +00:00
};
2023-12-01 13:04:51 +00:00
use pretty_assertions::{assert_eq, assert_ne};
2024-11-11 12:14:00 +00:00
use retry_future::{LinearRetryStrategy, RetryFuture, RetryPolicy};
2023-11-16 11:48:57 +00:00
use url::Url;
2023-11-15 12:05:07 +00:00
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_create_read_and_edit_local_article() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
2023-11-15 12:05:07 +00:00
2023-11-16 12:38:41 +00:00
// create article
2024-03-05 15:06:27 +00:00
let create_form = CreateArticleForm {
2024-01-30 15:06:02 +00:00
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = alpha.create_article(&create_form).await.unwrap();
2024-01-30 15:06:02 +00:00
assert_eq!(create_form.title, create_res.article.title);
2023-11-30 14:55:05 +00:00
assert!(create_res.article.local);
2023-11-16 12:38:41 +00:00
// now article can be read
2024-03-05 15:06:27 +00:00
let get_article_data = GetArticleForm {
2024-01-17 15:40:01 +00:00
title: Some(create_res.article.title.clone()),
2024-02-13 12:09:45 +00:00
domain: None,
2024-01-17 15:40:01 +00:00
id: None,
};
let get_res = alpha.get_article(get_article_data.clone()).await.unwrap();
2024-01-30 15:06:02 +00:00
assert_eq!(create_form.title, get_res.article.title);
2023-11-30 14:55:05 +00:00
assert_eq!(TEST_ARTICLE_DEFAULT_TEXT, get_res.article.text);
assert!(get_res.article.local);
2023-11-16 12:38:41 +00:00
// error on article which wasnt federated
let not_found = beta.get_article(get_article_data.clone()).await;
2024-12-03 13:52:33 +00:00
assert!(not_found.is_none());
// edit article
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2023-11-30 14:55:05 +00:00
article_id: create_res.article.id,
new_text: "Lorem Ipsum 2\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: get_res.latest_version,
2023-11-27 15:34:45 +00:00
resolve_conflict_id: None,
};
let edit_res = alpha.edit_article(&edit_form).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_form.new_text, edit_res.article.text);
let edits = alpha.get_article_edits(edit_res.article.id).await.unwrap();
assert_eq!(2, edits.len());
assert_eq!(edit_form.summary, edits[1].edit.summary);
2024-03-05 15:06:27 +00:00
let search_form = SearchArticleForm {
2024-01-30 15:06:02 +00:00
query: create_form.title.clone(),
};
let search_res = alpha.search(&search_form).await.unwrap();
assert_eq!(1, search_res.len());
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article, search_res[0]);
let list_articles = alpha
2024-03-05 15:06:27 +00:00
.list_articles(ListArticlesForm {
2024-02-01 11:23:51 +00:00
only_local: Some(false),
2024-11-08 11:26:06 +00:00
instance_id: None,
2024-02-01 11:23:51 +00:00
})
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2024-02-09 09:42:35 +00:00
assert_eq!(2, list_articles.len());
assert_eq!(edit_res.article, list_articles[0]);
TestData::stop(alpha, beta, gamma)
}
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_create_duplicate_article() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
// create article
2024-03-05 15:06:27 +00:00
let create_form = CreateArticleForm {
2024-01-30 15:06:02 +00:00
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = alpha.create_article(&create_form).await.unwrap();
2024-01-30 15:06:02 +00:00
assert_eq!(create_form.title, create_res.article.title);
2023-11-30 14:55:05 +00:00
assert!(create_res.article.local);
let create_res = alpha.create_article(&create_form).await;
assert!(create_res.is_err());
TestData::stop(alpha, beta, gamma)
2023-11-15 12:05:07 +00:00
}
2023-11-15 14:07:02 +00:00
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_follow_instance() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
2023-11-15 14:07:02 +00:00
2023-11-16 11:48:57 +00:00
// check initial state
let alpha_user = alpha.site().await.unwrap().my_profile.unwrap();
2024-02-07 15:54:43 +00:00
assert_eq!(0, alpha_user.following.len());
let beta_instance = beta.get_local_instance().await.unwrap();
2023-11-16 11:48:57 +00:00
assert_eq!(0, beta_instance.followers.len());
alpha
.follow_instance_with_resolve(&beta.hostname)
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2023-11-16 11:48:57 +00:00
// check that follow was federated
let alpha_user = alpha.site().await.unwrap().my_profile.unwrap();
2024-02-07 15:54:43 +00:00
assert_eq!(1, alpha_user.following.len());
assert_eq!(beta_instance.instance.ap_id, alpha_user.following[0].ap_id);
2023-12-04 14:10:07 +00:00
let beta_instance = beta.get_local_instance().await.unwrap();
2023-11-16 11:48:57 +00:00
assert_eq!(1, beta_instance.followers.len());
2024-02-07 15:54:43 +00:00
assert_eq!(alpha_user.person.ap_id, beta_instance.followers[0].ap_id);
2023-11-16 09:25:54 +00:00
TestData::stop(alpha, beta, gamma)
2023-11-16 09:25:54 +00:00
}
2023-12-01 11:11:19 +00:00
2023-11-16 15:40:43 +00:00
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_synchronize_articles() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
2023-11-16 15:40:43 +00:00
// create article on alpha
2024-03-05 15:06:27 +00:00
let create_form = CreateArticleForm {
2024-01-30 15:06:02 +00:00
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = alpha.create_article(&create_form).await.unwrap();
2024-01-30 15:06:02 +00:00
assert_eq!(create_form.title, create_res.article.title);
let edits = alpha
.get_article_edits(create_res.article.id)
.await
.unwrap();
assert_eq!(1, edits.len());
2023-11-30 14:55:05 +00:00
assert!(create_res.article.local);
2023-11-16 15:40:43 +00:00
// edit the article
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2023-11-30 14:55:05 +00:00
article_id: create_res.article.id,
new_text: "Lorem Ipsum 2\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: create_res.latest_version,
2023-11-27 15:34:45 +00:00
resolve_conflict_id: None,
};
let edit_res = alpha.edit_article(&edit_form).await.unwrap();
2023-11-16 15:40:43 +00:00
// fetch alpha instance on beta, articles are also fetched automatically
let instance = beta
.resolve_instance(Url::parse(&format!("http://{}", &alpha.hostname))?)
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2023-11-16 15:40:43 +00:00
2024-11-11 14:11:49 +00:00
let get_article_data = GetArticleForm {
title: Some(create_res.article.title.clone()),
..Default::default()
2024-01-17 15:40:01 +00:00
};
// try to read remote article by name, fails without domain
let get_res = beta.get_article(get_article_data.clone()).await;
2024-12-03 13:52:33 +00:00
assert!(get_res.is_none());
2024-01-17 15:40:01 +00:00
// get the article with instance id and compare
2024-11-11 14:11:49 +00:00
let get_res = RetryFuture::new(
|| async {
let get_article_data = GetArticleForm {
title: Some(create_res.article.title.clone()),
domain: Some(instance.domain.clone()),
id: None,
};
let res = beta.get_article(get_article_data).await;
2024-11-11 14:11:49 +00:00
match res {
2024-12-03 13:52:33 +00:00
None => Err(RetryPolicy::<String>::Retry(None)),
Some(a) if a.latest_version != edit_res.latest_version => {
Err(RetryPolicy::Retry(None))
}
2024-12-03 13:52:33 +00:00
Some(a) => Ok(a),
2024-11-11 14:11:49 +00:00
}
},
LinearRetryStrategy::new(),
)
.await?;
let beta_edits = beta.get_article_edits(create_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(create_res.article.ap_id, get_res.article.ap_id);
2024-01-30 15:06:02 +00:00
assert_eq!(create_form.title, get_res.article.title);
assert_eq!(2, beta_edits.len());
2023-11-30 14:55:05 +00:00
assert_eq!(edit_form.new_text, get_res.article.text);
assert!(!get_res.article.local);
2023-11-16 15:40:43 +00:00
TestData::stop(alpha, beta, gamma)
2023-11-16 15:40:43 +00:00
}
2023-11-17 13:22:31 +00:00
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_edit_local_article() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
2023-11-17 13:36:56 +00:00
let beta_instance = alpha
.follow_instance_with_resolve(&beta.hostname)
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2023-11-17 13:22:31 +00:00
// create new article
2024-03-05 15:06:27 +00:00
let create_form = CreateArticleForm {
2024-01-30 15:06:02 +00:00
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = beta.create_article(&create_form).await.unwrap();
2024-01-30 15:06:02 +00:00
assert_eq!(create_form.title, create_res.article.title);
2023-11-30 14:55:05 +00:00
assert!(create_res.article.local);
2023-11-17 13:22:31 +00:00
// article should be federated to alpha
2024-03-05 15:06:27 +00:00
let get_article_data = GetArticleForm {
2024-01-17 15:40:01 +00:00
title: Some(create_res.article.title.to_string()),
2024-02-13 12:09:45 +00:00
domain: Some(beta_instance.domain),
2024-01-17 15:40:01 +00:00
id: None,
};
let get_res = alpha.get_article(get_article_data.clone()).await.unwrap();
let edits = alpha.get_article_edits(get_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(create_res.article.title, get_res.article.title);
assert_eq!(1, edits.len());
2023-11-30 14:55:05 +00:00
assert!(!get_res.article.local);
assert_eq!(create_res.article.text, get_res.article.text);
2023-11-17 13:22:31 +00:00
// edit the article
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2023-11-30 14:55:05 +00:00
article_id: create_res.article.id,
new_text: "Lorem Ipsum 2\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: get_res.latest_version,
2023-11-27 15:34:45 +00:00
resolve_conflict_id: None,
2023-11-17 13:22:31 +00:00
};
let edit_res = beta.edit_article(&edit_form).await.unwrap();
let edits = beta.get_article_edits(edit_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article.text, edit_form.new_text);
assert_eq!(edits.len(), 2);
assert!(edits[0]
.edit
2023-11-30 14:55:05 +00:00
.ap_id
2023-11-21 15:27:18 +00:00
.to_string()
2023-11-30 14:55:05 +00:00
.starts_with(&edit_res.article.ap_id.to_string()));
2023-11-17 13:22:31 +00:00
// edit should be federated to alpha
let get_res = alpha.get_article(get_article_data).await.unwrap();
let edits = alpha.get_article_edits(get_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article.title, get_res.article.title);
assert_eq!(edits.len(), 2);
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article.text, get_res.article.text);
TestData::stop(alpha, beta, gamma)
}
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_edit_remote_article() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
let beta_id_on_alpha = alpha
.follow_instance_with_resolve(&beta.hostname)
2024-12-03 13:52:33 +00:00
.await
.unwrap();
let beta_id_on_gamma = gamma
.follow_instance_with_resolve(&beta.hostname)
2024-12-03 13:52:33 +00:00
.await
.unwrap();
// create new article
2024-03-05 15:06:27 +00:00
let create_form = CreateArticleForm {
2024-01-30 15:06:02 +00:00
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = beta.create_article(&create_form).await.unwrap();
2024-01-30 15:06:02 +00:00
assert_eq!(&create_form.title, &create_res.article.title);
2023-11-30 14:55:05 +00:00
assert!(create_res.article.local);
// article should be federated to alpha and gamma
2024-03-05 15:06:27 +00:00
let get_article_data_alpha = GetArticleForm {
2024-01-17 15:40:01 +00:00
title: Some(create_res.article.title.to_string()),
2024-02-13 12:09:45 +00:00
domain: Some(beta_id_on_alpha.domain),
2024-01-17 15:40:01 +00:00
id: None,
};
let get_res = alpha
2024-01-17 15:40:01 +00:00
.get_article(get_article_data_alpha.clone())
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(create_res.article.title, get_res.article.title);
let edits = alpha.get_article_edits(get_res.article.id).await.unwrap();
assert_eq!(1, edits.len());
2023-11-30 14:55:05 +00:00
assert!(!get_res.article.local);
2024-03-05 15:06:27 +00:00
let get_article_data_gamma = GetArticleForm {
2024-01-17 15:40:01 +00:00
title: Some(create_res.article.title.to_string()),
2024-02-13 12:09:45 +00:00
domain: Some(beta_id_on_gamma.domain),
2024-01-17 15:40:01 +00:00
id: None,
};
let get_res = gamma
2024-01-17 15:40:01 +00:00
.get_article(get_article_data_gamma.clone())
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(create_res.article.title, get_res.article.title);
assert_eq!(create_res.article.text, get_res.article.text);
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2024-01-17 15:40:01 +00:00
article_id: get_res.article.id,
new_text: "Lorem Ipsum 2\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: get_res.latest_version,
2023-11-27 15:34:45 +00:00
resolve_conflict_id: None,
};
let edit_res = alpha.edit_article(&edit_form).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_form.new_text, edit_res.article.text);
let edits = alpha.get_article_edits(edit_res.article.id).await.unwrap();
assert_eq!(2, edits.len());
2023-11-30 14:55:05 +00:00
assert!(!edit_res.article.local);
assert!(edits[0]
.edit
2023-11-30 14:55:05 +00:00
.ap_id
.to_string()
2023-11-30 14:55:05 +00:00
.starts_with(&edit_res.article.ap_id.to_string()));
// edit should be federated to beta and gamma
let get_res = alpha.get_article(get_article_data_alpha).await.unwrap();
let edits = alpha.get_article_edits(get_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article.title, get_res.article.title);
assert_eq!(edits.len(), 2);
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article.text, get_res.article.text);
let get_res = gamma.get_article(get_article_data_gamma).await.unwrap();
let edits = gamma.get_article_edits(edit_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article.title, get_res.article.title);
assert_eq!(edits.len(), 2);
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article.text, get_res.article.text);
2023-11-17 13:22:31 +00:00
TestData::stop(alpha, beta, gamma)
2023-11-17 13:22:31 +00:00
}
2023-11-22 15:41:34 +00:00
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_local_edit_conflict() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
2023-11-27 15:34:45 +00:00
// create new article
2024-03-05 15:06:27 +00:00
let create_form = CreateArticleForm {
2024-01-30 15:06:02 +00:00
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = alpha.create_article(&create_form).await.unwrap();
2024-01-30 15:06:02 +00:00
assert_eq!(create_form.title, create_res.article.title);
2023-11-30 14:55:05 +00:00
assert!(create_res.article.local);
2023-11-27 15:34:45 +00:00
// one user edits article
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2023-11-30 14:55:05 +00:00
article_id: create_res.article.id,
2023-11-27 15:34:45 +00:00
new_text: "Lorem Ipsum\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: create_res.latest_version.clone(),
2023-11-27 15:34:45 +00:00
resolve_conflict_id: None,
};
let edit_res = alpha.edit_article(&edit_form).await.unwrap();
let edits = alpha.get_article_edits(edit_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article.text, edit_form.new_text);
assert_eq!(2, edits.len());
2023-11-27 15:34:45 +00:00
// another user edits article, without being aware of previous edit
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2023-11-30 14:55:05 +00:00
article_id: create_res.article.id,
2023-11-27 15:34:45 +00:00
new_text: "Ipsum Lorem\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: create_res.latest_version,
2023-11-27 15:34:45 +00:00
resolve_conflict_id: None,
};
let edit_res = alpha
2024-01-17 16:14:16 +00:00
.edit_article_with_conflict(&edit_form)
2024-12-03 13:52:33 +00:00
.await
.unwrap()
2023-11-27 15:34:45 +00:00
.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
let notifications = alpha.notifications_list().await.unwrap();
assert_eq!(1, notifications.len());
let Notification::EditConflict(conflict) = &notifications[0] else {
panic!()
};
assert_eq!(conflict, &edit_res);
2023-11-27 15:34:45 +00:00
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2023-11-30 14:55:05 +00:00
article_id: create_res.article.id,
2023-11-27 15:34:45 +00:00
new_text: "Lorem Ipsum and Ipsum Lorem\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: edit_res.previous_version_id,
2023-11-27 15:34:45 +00:00
resolve_conflict_id: Some(edit_res.id),
};
let edit_res = alpha.edit_article(&edit_form).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_form.new_text, edit_res.article.text);
2023-11-27 15:34:45 +00:00
assert_eq!(0, alpha.notifications_count().await.unwrap());
2023-11-27 15:34:45 +00:00
TestData::stop(alpha, beta, gamma)
2023-11-27 15:34:45 +00:00
}
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_federated_edit_conflict() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
2023-11-22 15:41:34 +00:00
let beta_id_on_alpha = alpha
.follow_instance_with_resolve(&beta.hostname)
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2023-11-22 15:41:34 +00:00
// create new article
2024-03-05 15:06:27 +00:00
let create_form = CreateArticleForm {
2024-01-30 15:06:02 +00:00
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = beta.create_article(&create_form).await.unwrap();
let beta_edits = beta.get_article_edits(create_res.article.id).await.unwrap();
2024-01-30 15:06:02 +00:00
assert_eq!(create_form.title, create_res.article.title);
2023-11-30 14:55:05 +00:00
assert!(create_res.article.local);
2023-11-22 15:41:34 +00:00
2023-11-24 14:31:31 +00:00
// fetch article to gamma
let resolve_res: ArticleView = gamma
2024-01-18 11:18:17 +00:00
.resolve_article(create_res.article.ap_id.inner().clone())
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2023-12-01 13:04:51 +00:00
assert_eq!(create_res.article.text, resolve_res.article.text);
2023-11-24 14:31:31 +00:00
2023-11-22 15:41:34 +00:00
// alpha edits article
2024-03-05 15:06:27 +00:00
let get_article_data = GetArticleForm {
2024-01-30 15:06:02 +00:00
title: Some(create_form.title.to_string()),
2024-02-13 12:09:45 +00:00
domain: Some(beta_id_on_alpha.domain),
2024-01-17 15:40:01 +00:00
id: None,
};
let get_res = alpha.get_article(get_article_data).await.unwrap();
let alpha_edits = alpha.get_article_edits(get_res.article.id).await.unwrap();
assert_eq!(&beta_edits.len(), &alpha_edits.len());
assert_eq!(&beta_edits[0].edit.hash, &alpha_edits[0].edit.hash);
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2024-01-17 15:40:01 +00:00
article_id: get_res.article.id,
new_text: "Lorem Ipsum\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: create_res.latest_version.clone(),
2023-11-27 15:34:45 +00:00
resolve_conflict_id: None,
2023-11-22 15:41:34 +00:00
};
let edit_res = alpha.edit_article(&edit_form).await.unwrap();
let alpha_edits = alpha.get_article_edits(get_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article.text, edit_form.new_text);
assert_eq!(2, alpha_edits.len());
2023-11-30 14:55:05 +00:00
assert!(!edit_res.article.local);
assert!(alpha_edits[1]
.edit
2023-11-30 14:55:05 +00:00
.ap_id
2023-11-22 15:41:34 +00:00
.to_string()
2023-11-30 14:55:05 +00:00
.starts_with(&edit_res.article.ap_id.to_string()));
2023-11-22 15:41:34 +00:00
// 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
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2024-01-17 15:40:01 +00:00
article_id: resolve_res.article.id,
new_text: "aaaa\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: create_res.latest_version,
2023-11-27 15:34:45 +00:00
resolve_conflict_id: None,
2023-11-22 15:41:34 +00:00
};
let edit_res = gamma.edit_article(&edit_form).await.unwrap();
let gamma_edits = gamma.get_article_edits(edit_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_ne!(edit_form.new_text, edit_res.article.text);
assert_eq!(1, gamma_edits.len());
2023-11-30 14:55:05 +00:00
assert!(!edit_res.article.local);
2023-11-22 15:41:34 +00:00
assert_eq!(1, gamma.notifications_count().await.unwrap());
let notifications = gamma.notifications_list().await.unwrap();
assert_eq!(1, notifications.len());
let Notification::EditConflict(conflict) = &notifications[0] else {
panic!()
};
2023-11-27 15:34:45 +00:00
// resolve the conflict
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2024-01-17 15:40:01 +00:00
article_id: resolve_res.article.id,
2023-11-27 15:34:45 +00:00
new_text: "aaaa\n".to_string(),
summary: "summary".to_string(),
previous_version_id: conflict.previous_version_id.clone(),
resolve_conflict_id: Some(conflict.id),
2023-11-27 15:34:45 +00:00
};
let edit_res = gamma.edit_article(&edit_form).await.unwrap();
let gamma_edits = gamma.get_article_edits(edit_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_form.new_text, edit_res.article.text);
assert_eq!(3, gamma_edits.len());
2023-11-27 15:34:45 +00:00
assert_eq!(0, gamma.notifications_count().await.unwrap());
let notifications = gamma.notifications_list().await.unwrap();
assert_eq!(0, notifications.len());
TestData::stop(alpha, beta, gamma)
2023-11-22 15:41:34 +00:00
}
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_overlapping_edits_no_conflict() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
// create new article
2024-03-05 15:06:27 +00:00
let create_form = CreateArticleForm {
2024-01-30 15:06:02 +00:00
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = alpha.create_article(&create_form).await.unwrap();
2024-01-30 15:06:02 +00:00
assert_eq!(create_form.title, create_res.article.title);
2023-11-30 14:55:05 +00:00
assert!(create_res.article.local);
// one user edits article
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2023-11-30 14:55:05 +00:00
article_id: create_res.article.id,
new_text: "my\nexample\ntext\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: create_res.latest_version.clone(),
resolve_conflict_id: None,
};
let edit_res = alpha.edit_article(&edit_form).await.unwrap();
let alpha_edits = alpha.get_article_edits(edit_res.article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(edit_res.article.text, edit_form.new_text);
assert_eq!(2, alpha_edits.len());
// another user edits article, without being aware of previous edit
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
2023-11-30 14:55:05 +00:00
article_id: create_res.article.id,
new_text: "some\nexample\narticle\n".to_string(),
summary: "summary".to_string(),
2023-12-05 00:17:02 +00:00
previous_version_id: create_res.latest_version,
resolve_conflict_id: None,
};
let edit_res = alpha.edit_article(&edit_form).await.unwrap();
let alpha_edits = alpha.get_article_edits(edit_res.article.id).await.unwrap();
assert_eq!(0, alpha.notifications_count().await.unwrap());
assert_eq!(3, alpha_edits.len());
2023-11-30 14:55:05 +00:00
assert_eq!("my\nexample\narticle\n", edit_res.article.text);
TestData::stop(alpha, beta, gamma)
}
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_fork_article() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
// create article
2024-03-05 15:06:27 +00:00
let create_form = CreateArticleForm {
2024-01-30 15:06:02 +00:00
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = alpha.create_article(&create_form).await.unwrap();
let create_edits = alpha
.get_article_edits(create_res.article.id)
.await
.unwrap();
2024-01-30 15:06:02 +00:00
assert_eq!(create_form.title, create_res.article.title);
2023-11-30 14:55:05 +00:00
assert!(create_res.article.local);
// fetch on beta
let resolve_res = beta
2024-01-18 11:18:17 +00:00
.resolve_article(create_res.article.ap_id.into_inner())
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2023-11-30 14:55:05 +00:00
let resolved_article = resolve_res.article;
let resolve_edits = beta.get_article_edits(resolved_article.id).await.unwrap();
assert_eq!(create_edits.len(), resolve_edits.len());
// fork the article to local instance
2024-03-05 15:06:27 +00:00
let fork_form = ForkArticleForm {
2023-11-30 14:14:30 +00:00
article_id: resolved_article.id,
2024-02-14 11:22:24 +00:00
new_title: resolved_article.title.clone(),
};
let fork_res = beta.fork_article(&fork_form).await.unwrap();
2023-11-30 14:55:05 +00:00
let forked_article = fork_res.article;
let fork_edits = beta.get_article_edits(forked_article.id).await.unwrap();
2023-11-30 14:55:05 +00:00
assert_eq!(resolved_article.title, forked_article.title);
assert_eq!(resolved_article.text, forked_article.text);
assert_eq!(resolve_edits.len(), fork_edits.len());
assert_eq!(resolve_edits[0].edit.diff, fork_edits[0].edit.diff);
assert_eq!(resolve_edits[0].edit.hash, fork_edits[0].edit.hash);
assert_ne!(resolve_edits[0].edit.id, fork_edits[0].edit.id);
2023-12-01 13:04:51 +00:00
assert_eq!(resolve_res.latest_version, fork_res.latest_version);
2023-11-30 14:55:05 +00:00
assert_ne!(resolved_article.ap_id, forked_article.ap_id);
assert!(forked_article.local);
let beta_instance = beta.get_local_instance().await.unwrap();
assert_eq!(forked_article.instance_id, beta_instance.instance.id);
// now search returns two articles for this title (original and forked)
2024-03-05 15:06:27 +00:00
let search_form = SearchArticleForm {
2024-01-30 15:06:02 +00:00
query: create_form.title.clone(),
};
let search_res = beta.search(&search_form).await.unwrap();
assert_eq!(2, search_res.len());
TestData::stop(alpha, beta, gamma)
}
2023-12-08 21:21:31 +00:00
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_user_registration_login() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
2023-12-13 15:58:29 +00:00
let username = "my_user";
let password = "hunter2";
2024-03-05 15:06:27 +00:00
let register_data = RegisterUserForm {
2024-01-17 15:40:01 +00:00
username: username.to_string(),
password: password.to_string(),
};
alpha.register(register_data).await.unwrap();
2023-12-12 15:32:57 +00:00
2024-03-05 15:06:27 +00:00
let login_data = LoginUserForm {
2024-01-17 15:40:01 +00:00
username: username.to_string(),
password: "asd123".to_string(),
};
let invalid_login = alpha.login(login_data).await;
2023-12-12 15:32:57 +00:00
assert!(invalid_login.is_err());
2024-03-05 15:06:27 +00:00
let login_data = LoginUserForm {
2024-01-17 15:40:01 +00:00
username: username.to_string(),
password: password.to_string(),
};
alpha.login(login_data).await.unwrap();
2023-12-08 21:21:31 +00:00
let my_profile = alpha.site().await.unwrap().my_profile.unwrap();
2024-01-18 11:18:17 +00:00
assert_eq!(username, my_profile.person.username);
alpha.logout().await.unwrap();
2024-01-18 11:18:17 +00:00
let my_profile_after_logout = alpha.site().await.unwrap().my_profile;
2024-11-14 11:33:25 +00:00
assert!(my_profile_after_logout.is_none());
TestData::stop(alpha, beta, gamma)
2023-12-12 15:32:57 +00:00
}
2024-02-13 12:09:45 +00:00
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_user_profile() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
2024-02-13 12:09:45 +00:00
// Create an article and federate it, in order to federate the user who created it
2024-03-05 15:06:27 +00:00
let create_form = CreateArticleForm {
2024-02-13 12:09:45 +00:00
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = alpha.create_article(&create_form).await.unwrap();
beta.resolve_article(create_res.article.ap_id.into_inner())
2024-12-03 13:52:33 +00:00
.await
.unwrap();
let domain = extract_domain(&alpha.site().await.unwrap().my_profile.unwrap().person.ap_id);
2024-02-13 12:09:45 +00:00
// Now we can fetch the remote user from local api
2024-03-05 15:06:27 +00:00
let params = GetUserForm {
2024-02-13 12:09:45 +00:00
name: "alpha".to_string(),
2024-02-14 11:31:13 +00:00
domain: Some(domain),
2024-02-13 12:09:45 +00:00
};
let user = beta.get_user(params).await.unwrap();
2024-02-13 12:09:45 +00:00
assert_eq!("alpha", user.username);
assert!(!user.local);
TestData::stop(alpha, beta, gamma)
2024-02-13 12:09:45 +00:00
}
2024-03-05 15:06:27 +00:00
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_lock_article() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
2024-03-05 15:06:27 +00:00
// create article
let create_form = CreateArticleForm {
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = alpha.create_article(&create_form).await.unwrap();
2024-03-05 15:06:27 +00:00
assert!(!create_res.article.protected);
// lock from normal user fails
let lock_form = ProtectArticleForm {
article_id: create_res.article.id,
protected: true,
};
let lock_res = alpha.protect_article(&lock_form).await;
2024-03-05 15:06:27 +00:00
assert!(lock_res.is_err());
// login as admin to lock article
let form = LoginUserForm {
username: "ibis".to_string(),
password: "ibis".to_string(),
};
alpha.login(form).await.unwrap();
let lock_res = alpha.protect_article(&lock_form).await.unwrap();
2024-03-05 15:06:27 +00:00
assert!(lock_res.protected);
let resolve_res: ArticleView = gamma
2024-03-05 15:06:27 +00:00
.resolve_article(create_res.article.ap_id.inner().clone())
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2024-03-05 15:06:27 +00:00
let edit_form = EditArticleForm {
article_id: resolve_res.article.id,
new_text: "test".to_string(),
summary: "test".to_string(),
previous_version_id: resolve_res.latest_version,
resolve_conflict_id: None,
};
let edit_res = gamma.edit_article(&edit_form).await;
2024-12-03 13:52:33 +00:00
assert!(edit_res.is_none());
2024-03-05 15:06:27 +00:00
TestData::stop(alpha, beta, gamma)
2024-03-05 15:06:27 +00:00
}
2024-11-11 12:14:00 +00:00
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_synchronize_instances() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(false).await;
2024-11-11 12:14:00 +00:00
// fetch alpha instance on beta
beta.resolve_instance(Url::parse(&format!("http://{}", &alpha.hostname))?)
2024-12-03 13:52:33 +00:00
.await
.unwrap();
let beta_instances = beta.list_instances().await.unwrap();
2024-11-11 12:14:00 +00:00
assert_eq!(1, beta_instances.len());
// fetch beta instance on gamma
gamma
.resolve_instance(Url::parse(&format!("http://{}", &beta.hostname))?)
2024-12-03 13:52:33 +00:00
.await
.unwrap();
2024-11-11 12:14:00 +00:00
// wait until instance collection is fetched
let gamma_instances = RetryFuture::new(
|| async {
let res = gamma.list_instances().await;
2024-11-11 12:14:00 +00:00
match res {
2024-12-03 13:52:33 +00:00
None => Err(RetryPolicy::<String>::Retry(None)),
Some(i) if i.len() < 2 => Err(RetryPolicy::Retry(None)),
Some(i) => Ok(i),
2024-11-11 12:14:00 +00:00
}
},
LinearRetryStrategy::new(),
)
.await?;
// now gamma also knows about alpha
assert_eq!(2, gamma_instances.len());
assert!(gamma_instances.iter().any(|i| i.domain == alpha.hostname));
2024-11-11 12:14:00 +00:00
TestData::stop(alpha, beta, gamma)
2024-11-11 12:14:00 +00:00
}
2024-11-12 14:04:04 +00:00
#[tokio::test]
2024-12-03 13:52:33 +00:00
async fn test_article_approval_required() -> Result<()> {
let TestData(alpha, beta, gamma) = TestData::start(true).await;
2024-11-12 14:04:04 +00:00
// create article
let create_form = CreateArticleForm {
title: "Manu_Chao".to_string(),
text: TEST_ARTICLE_DEFAULT_TEXT.to_string(),
summary: "create article".to_string(),
};
let create_res = alpha.create_article(&create_form).await.unwrap();
2024-11-12 14:04:04 +00:00
assert!(!create_res.article.approved);
let list_all = alpha.list_articles(Default::default()).await.unwrap();
2024-11-12 14:04:04 +00:00
assert_eq!(1, list_all.len());
assert!(list_all.iter().all(|a| a.id != create_res.article.id));
// login as admin to handle approvals
let form = LoginUserForm {
username: "ibis".to_string(),
password: "ibis".to_string(),
};
alpha.login(form).await.unwrap();
2024-11-12 14:04:04 +00:00
assert_eq!(1, alpha.notifications_count().await.unwrap());
let notifications = alpha.notifications_list().await.unwrap();
assert_eq!(1, notifications.len());
let Notification::ArticleApprovalRequired(notif) = &notifications[0] else {
panic!()
};
assert_eq!(create_res.article.id, notif.id);
2024-11-12 14:04:04 +00:00
alpha.approve_article(notif.id, true).await.unwrap();
2024-11-14 15:42:49 +00:00
let form = GetArticleForm {
id: Some(create_res.article.id),
..Default::default()
};
let approved = alpha.get_article(form).await.unwrap();
2024-11-14 15:42:49 +00:00
assert_eq!(create_res.article.id, approved.article.id);
assert!(approved.article.approved);
2024-11-12 14:04:04 +00:00
let list_all = alpha.list_articles(Default::default()).await.unwrap();
2024-11-12 14:04:04 +00:00
assert_eq!(2, list_all.len());
assert!(list_all.iter().any(|a| a.id == create_res.article.id));
TestData::stop(alpha, beta, gamma)
2024-11-12 14:04:04 +00:00
}