ibis/tests/test.rs

120 lines
3.2 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 11:48:57 +00:00
use fediwiki::api::{FollowInstance, 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;
2023-11-16 11:48:57 +00:00
use fediwiki::federation::objects::instance::DbInstance;
2023-11-15 12:05:07 +00:00
use fediwiki::start;
2023-11-16 11:48:57 +00:00
use once_cell::sync::Lazy;
use reqwest::Client;
use serial_test::serial;
use std::sync::Once;
use tracing::log::LevelFilter;
use url::Url;
fn setup() {
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();
});
}
static CLIENT: Lazy<Client> = Lazy::new(|| Client::new());
2023-11-15 12:05:07 +00:00
#[tokio::test]
2023-11-16 11:48:57 +00:00
#[serial]
2023-11-16 09:25:54 +00:00
async fn test_get_article() -> MyResult<()> {
2023-11-16 11:48:57 +00:00
setup();
2023-11-15 12:05:07 +00:00
let hostname = "localhost:8131";
2023-11-15 14:07:02 +00:00
let handle = tokio::task::spawn(async {
2023-11-15 12:05:07 +00:00
start(hostname).await.unwrap();
});
let title = "Manu_Chao";
let res: DbArticle = reqwest::get(format!("http://{hostname}/api/v1/article/{title}"))
2023-11-16 09:25:54 +00:00
.await?
2023-11-15 12:05:07 +00:00
.json()
2023-11-16 09:25:54 +00:00
.await?;
2023-11-15 12:05:07 +00:00
assert_eq!(title, res.title);
assert!(res.local);
2023-11-15 14:07:02 +00:00
handle.abort();
2023-11-16 09:25:54 +00:00
Ok(())
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-16 11:48:57 +00:00
setup();
2023-11-15 14:07:02 +00:00
let hostname_alpha = "localhost:8131";
let hostname_beta = "localhost:8132";
let handle_alpha = tokio::task::spawn(async {
start(hostname_alpha).await.unwrap();
});
let handle_beta = tokio::task::spawn(async {
start(hostname_beta).await.unwrap();
});
2023-11-16 11:48:57 +00:00
// check initial state
let alpha_instance: DbInstance = CLIENT
.get(format!("http://{hostname_alpha}/api/v1/instance"))
.send()
.await?
.json()
.await?;
assert_eq!(0, alpha_instance.follows.len());
let beta_instance: DbInstance = CLIENT
.get(format!("http://{hostname_beta}/api/v1/instance"))
.send()
.await?
.json()
.await?;
assert_eq!(0, beta_instance.followers.len());
// fetch beta instance on alpha
let resolve_object = ResolveObject {
id: Url::parse(&format!("http://{hostname_beta}"))?,
};
let beta_instance_resolved: DbInstance = CLIENT
.get(format!("http://{hostname_alpha}/api/v1/resolve_object"))
.query(&resolve_object)
.send()
.await?
.json()
.await?;
// send follow
let follow_instance = FollowInstance {
instance_id: beta_instance_resolved.ap_id,
};
CLIENT
.post(format!("http://{hostname_alpha}/api/v1/instance/follow"))
.form(&follow_instance)
.send()
.await?;
// check that follow was federated
let beta_instance: DbInstance = CLIENT
.get(format!("http://{hostname_beta}/api/v1/instance"))
.send()
.await?
.json()
.await?;
assert_eq!(1, beta_instance.followers.len());
let alpha_instance: DbInstance = CLIENT
.get(format!("http://{hostname_alpha}/api/v1/instance"))
.send()
.await?
.json()
.await?;
assert_eq!(1, alpha_instance.follows.len());
2023-11-16 09:25:54 +00:00
2023-11-15 14:07:02 +00:00
handle_alpha.abort();
handle_beta.abort();
2023-11-16 09:25:54 +00:00
Ok(())
}