Compare commits

..

2 Commits

Author SHA1 Message Date
Lennart K
008e40e17f integration tests: Also use health command 2026-01-29 11:26:20 +01:00
Lennart K
0703b7b470 Add another integration test 2026-01-29 10:25:00 +01:00
9 changed files with 149 additions and 49 deletions

1
Cargo.lock generated
View File

@@ -3344,6 +3344,7 @@ dependencies = [
"serde", "serde",
"similar-asserts", "similar-asserts",
"sqlx", "sqlx",
"tempfile",
"tokio", "tokio",
"tokio-util", "tokio-util",
"toml 0.9.11+spec-1.1.0", "toml 0.9.11+spec-1.1.0",

View File

@@ -156,6 +156,7 @@ rstest.workspace = true
rustical_store_sqlite = { workspace = true, features = ["test"] } rustical_store_sqlite = { workspace = true, features = ["test"] }
insta.workspace = true insta.workspace = true
similar-asserts.workspace = true similar-asserts.workspace = true
tempfile = "3.24"
[dependencies] [dependencies]
rustical_store.workspace = true rustical_store.workspace = true

View File

@@ -2,7 +2,7 @@ use crate::config::HttpConfig;
use clap::Parser; use clap::Parser;
use http::Method; use http::Method;
#[derive(Parser, Debug)] #[derive(Parser, Debug, Default)]
pub struct HealthArgs {} pub struct HealthArgs {}
/// Healthcheck for running rustical instance /// Healthcheck for running rustical instance

View File

@@ -8,7 +8,7 @@ use rustical_frontend::FrontendConfig;
mod health; mod health;
pub mod membership; pub mod membership;
mod principals; pub mod principals;
pub use health::{HealthArgs, cmd_health}; pub use health::{HealthArgs, cmd_health};
pub use principals::{PrincipalsArgs, cmd_principals}; pub use principals::{PrincipalsArgs, cmd_principals};

View File

@@ -1,56 +1,49 @@
use super::membership::MembershipArgs; use super::membership::MembershipArgs;
use crate::{config::Config, get_data_stores, membership::cmd_membership}; use crate::{config::Config, get_data_stores, membership::cmd_membership};
use clap::{Parser, Subcommand}; use clap::{Parser, Subcommand};
use figment::{
Figment,
providers::{Env, Format, Toml},
};
use password_hash::{PasswordHasher, SaltString, rand_core::OsRng}; use password_hash::{PasswordHasher, SaltString, rand_core::OsRng};
use rustical_store::auth::{AuthenticationProvider, Principal, PrincipalType}; use rustical_store::auth::{AuthenticationProvider, Principal, PrincipalType};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
pub struct PrincipalsArgs { pub struct PrincipalsArgs {
#[arg(short, long, env, default_value = "/etc/rustical/config.toml")]
config_file: String,
#[command(subcommand)] #[command(subcommand)]
command: Command, pub command: PrincipalsCommand,
} }
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
struct CreateArgs { pub struct CreateArgs {
id: String, pub id: String,
#[arg(value_enum, short, long)] #[arg(value_enum, short, long)]
principal_type: Option<PrincipalType>, pub principal_type: Option<PrincipalType>,
#[arg(short, long)] #[arg(short, long)]
name: Option<String>, pub name: Option<String>,
#[arg(long, help = "Ask for password input")] #[arg(long, help = "Ask for password input")]
password: bool, pub password: bool,
} }
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
struct RemoveArgs { pub struct RemoveArgs {
id: String, pub id: String,
} }
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
struct EditArgs { pub struct EditArgs {
id: String, pub id: String,
#[arg(long, help = "Ask for password input")] #[arg(long, help = "Ask for password input")]
password: bool, pub password: bool,
#[arg( #[arg(
long, long,
help = "Remove password (If you only want to use OIDC for example)" help = "Remove password (If you only want to use OIDC for example)"
)] )]
remove_password: bool, pub remove_password: bool,
#[arg(short, long, help = "Change principal displayname")] #[arg(short, long, help = "Change principal displayname")]
name: Option<String>, pub name: Option<String>,
#[arg(value_enum, short, long, help = "Change the principal type")] #[arg(value_enum, short, long, help = "Change the principal type")]
principal_type: Option<PrincipalType>, pub principal_type: Option<PrincipalType>,
} }
#[derive(Debug, Subcommand)] #[derive(Debug, Subcommand)]
enum Command { pub enum PrincipalsCommand {
List, List,
Create(CreateArgs), Create(CreateArgs),
Remove(RemoveArgs), Remove(RemoveArgs),
@@ -59,16 +52,11 @@ enum Command {
} }
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)] #[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub async fn cmd_principals(args: PrincipalsArgs) -> anyhow::Result<()> { pub async fn cmd_principals(args: PrincipalsArgs, config: Config) -> anyhow::Result<()> {
let config: Config = Figment::new()
.merge(Toml::file(&args.config_file))
.merge(Env::prefixed("RUSTICAL_").split("__"))
.extract()?;
let (_, _, _, principal_store, _) = get_data_stores(true, &config.data_store).await?; let (_, _, _, principal_store, _) = get_data_stores(true, &config.data_store).await?;
match args.command { match args.command {
Command::List => { PrincipalsCommand::List => {
for principal in principal_store.get_principals().await? { for principal in principal_store.get_principals().await? {
println!( println!(
"{} (displayname={}) [{}]", "{} (displayname={}) [{}]",
@@ -78,7 +66,7 @@ pub async fn cmd_principals(args: PrincipalsArgs) -> anyhow::Result<()> {
); );
} }
} }
Command::Create(CreateArgs { PrincipalsCommand::Create(CreateArgs {
id, id,
principal_type, principal_type,
name, name,
@@ -112,11 +100,11 @@ pub async fn cmd_principals(args: PrincipalsArgs) -> anyhow::Result<()> {
.await?; .await?;
println!("Principal created"); println!("Principal created");
} }
Command::Remove(RemoveArgs { id }) => { PrincipalsCommand::Remove(RemoveArgs { id }) => {
principal_store.remove_principal(&id).await?; principal_store.remove_principal(&id).await?;
println!("Principal {id} removed"); println!("Principal {id} removed");
} }
Command::Edit(EditArgs { PrincipalsCommand::Edit(EditArgs {
id, id,
remove_password, remove_password,
password, password,
@@ -152,7 +140,7 @@ pub async fn cmd_principals(args: PrincipalsArgs) -> anyhow::Result<()> {
principal_store.insert_principal(principal, true).await?; principal_store.insert_principal(principal, true).await?;
println!("Principal {id} updated"); println!("Principal {id} updated");
} }
Command::Membership(args) => { PrincipalsCommand::Membership(args) => {
cmd_membership(principal_store.as_ref(), args).await?; cmd_membership(principal_store.as_ref(), args).await?;
} }
} }

View File

@@ -110,8 +110,11 @@ pub async fn cmd_default(
args: Args, args: Args,
config: Config, config: Config,
start_notifier: Option<Arc<Notify>>, start_notifier: Option<Arc<Notify>>,
tracing: bool,
) -> Result<()> { ) -> Result<()> {
setup_tracing(&config.tracing); if tracing {
setup_tracing(&config.tracing);
}
let (addr_store, cal_store, subscription_store, principal_store, update_recv) = let (addr_store, cal_store, subscription_store, principal_store, update_recv) =
get_data_stores(!args.no_migrations, &config.data_store).await?; get_data_stores(!args.no_migrations, &config.data_store).await?;

View File

@@ -21,14 +21,16 @@ async fn main() -> Result<()> {
match args.command { match args.command {
Some(Command::GenConfig(gen_config_args)) => cmd_gen_config(gen_config_args), Some(Command::GenConfig(gen_config_args)) => cmd_gen_config(gen_config_args),
Some(Command::Principals(principals_args)) => cmd_principals(principals_args).await, Some(Command::Principals(principals_args)) => {
cmd_principals(principals_args, parse_config()?).await
}
Some(Command::Health(health_args)) => { Some(Command::Health(health_args)) => {
let config: Config = parse_config()?; let config: Config = parse_config()?;
cmd_health(config.http, health_args).await cmd_health(config.http, health_args).await
} }
None => { None => {
let config: Config = parse_config()?; let config: Config = parse_config()?;
cmd_default(args, config, None).await cmd_default(args, config, None, true).await
} }
} }
} }

View File

@@ -3,27 +3,38 @@ use rustical::{
config::{Config, DataStoreConfig, HttpConfig, SqliteDataStoreConfig}, config::{Config, DataStoreConfig, HttpConfig, SqliteDataStoreConfig},
}; };
use std::{ use std::{
collections::HashSet,
net::{Ipv4Addr, SocketAddrV4, TcpListener}, net::{Ipv4Addr, SocketAddrV4, TcpListener},
sync::Arc, sync::{Arc, Mutex, OnceLock},
thread::{self, JoinHandle}, thread::{self, JoinHandle},
}; };
use tokio::sync::Notify; use tokio::sync::Notify;
use tokio_util::sync::CancellationToken; use tokio_util::sync::CancellationToken;
// When running multiple integration tests we need to make sure that they don't get the same port
static BOUND_PORTS: OnceLock<Mutex<HashSet<u16>>> = OnceLock::new();
pub fn find_free_port() -> Option<u16> { pub fn find_free_port() -> Option<u16> {
let bound_ports = BOUND_PORTS.get_or_init(Mutex::default);
let mut bound_ports_write = bound_ports.lock().unwrap();
let mut port = 15000; let mut port = 15000;
// Frees the socket on drop such that this function returns a free port // Frees the socket on drop such that this function returns a free port
while TcpListener::bind(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port)).is_err() { while TcpListener::bind(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port)).is_err()
|| bound_ports_write.contains(&port)
{
port += 1; port += 1;
if port >= 16000 { if port >= 16000 {
return None; return None;
} }
} }
bound_ports_write.insert(port);
Some(port) Some(port)
} }
pub fn rustical_process() -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify>) { pub fn rustical_process(
db_url: Option<String>,
) -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify>) {
let port = find_free_port().unwrap(); let port = find_free_port().unwrap();
let token = CancellationToken::new(); let token = CancellationToken::new();
let cloned_token = token.clone(); let cloned_token = token.clone();
@@ -41,7 +52,7 @@ pub fn rustical_process() -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify
}, },
Config { Config {
data_store: DataStoreConfig::Sqlite(SqliteDataStoreConfig { data_store: DataStoreConfig::Sqlite(SqliteDataStoreConfig {
db_url: ":memory:".to_owned(), db_url: db_url.unwrap_or(":memory:".to_owned()),
run_repairs: true, run_repairs: true,
skip_broken: false, skip_broken: false,
}), }),
@@ -58,6 +69,7 @@ pub fn rustical_process() -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify
caldav: Default::default(), caldav: Default::default(),
}, },
Some(cloned_start_notify), Some(cloned_start_notify),
false,
) )
.await .await
}; };

View File

@@ -1,20 +1,26 @@
// This integration test checks whether the HTTP server works by actually running rustical in a new // This integration test checks whether the HTTP server works by actually running rustical in a new
// thread. // thread.
use common::rustical_process; use common::rustical_process;
use http::StatusCode; use http::{Method, StatusCode};
use rustical::{
PrincipalsArgs, cmd_health, cmd_principals,
config::{Config, DataStoreConfig, HttpConfig, SqliteDataStoreConfig},
principals::{CreateArgs, PrincipalsCommand},
};
use rustical_store::auth::{AuthenticationProvider, PrincipalType};
use rustical_store_sqlite::{create_db_pool, principal_store::SqlitePrincipalStore};
use std::time::Duration; use std::time::Duration;
mod common; mod common;
pub async fn test_runner<O, F>(inner: F) pub async fn test_runner<O, F>(db_path: Option<String>, inner: F)
where where
O: IntoFuture<Output = ()>, O: IntoFuture<Output = ()>,
// <O as IntoFuture>::IntoFuture: UnwindSafe, // <O as IntoFuture>::IntoFuture: UnwindSafe,
F: FnOnce(String) -> O, F: FnOnce(u16) -> O,
{ {
// Start RustiCal process // Start RustiCal process
let (token, port, main_process, start_notify) = rustical_process(); let (token, port, main_process, start_notify) = rustical_process(db_path);
let origin = format!("http://localhost:{port}");
// Wait for RustiCal server to listen // Wait for RustiCal server to listen
tokio::time::timeout(Duration::new(2, 0), start_notify.notified()) tokio::time::timeout(Duration::new(2, 0), start_notify.notified())
@@ -23,7 +29,7 @@ where
// We use catch_unwind to make sure we'll always correctly stop RustiCal // We use catch_unwind to make sure we'll always correctly stop RustiCal
// Otherwise, our process would just run indefinitely // Otherwise, our process would just run indefinitely
inner(origin).into_future().await; inner(port).into_future().await;
// Signal RustiCal to stop // Signal RustiCal to stop
token.cancel(); token.cancel();
@@ -32,13 +38,100 @@ where
#[tokio::test] #[tokio::test]
async fn test_ping() { async fn test_ping() {
test_runner(async |origin| { test_runner(None, async |port| {
let origin = format!("http://localhost:{port}");
let resp = reqwest::get(origin.clone() + "/ping").await.unwrap(); let resp = reqwest::get(origin.clone() + "/ping").await.unwrap();
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
// Ensure that path normalisation works as intended // Ensure that path normalisation works as intended
let resp = reqwest::get(origin + "/ping/").await.unwrap(); let resp = reqwest::get(origin + "/ping/").await.unwrap();
assert_eq!(resp.status(), StatusCode::OK); assert_eq!(resp.status(), StatusCode::OK);
cmd_health(
HttpConfig {
host: "localhost".to_owned(),
port,
..Default::default()
},
Default::default(),
)
.await
.unwrap();
}) })
.await .await
} }
// When setting a use password from the CLI we effectively have two processes accessing the same
// database: The server and the CLI.
// This test ensures that the server correctly picks up the changes made by the CLI.
#[tokio::test]
async fn test_initial_setup() {
let db_tempfile = tempfile::NamedTempFile::with_suffix(".rustical-test.sqlite3").unwrap();
let db_path = db_tempfile.path().to_string_lossy().into_owned();
test_runner(Some(db_path.clone()), async |port| {
let origin = format!("http://localhost:{port}");
// Create principal
cmd_principals(
PrincipalsArgs {
command: PrincipalsCommand::Create(CreateArgs {
id: "user".to_owned(),
name: Some("Test User".to_owned()),
password: false,
principal_type: Some(PrincipalType::Individual),
}),
},
Config {
data_store: DataStoreConfig::Sqlite(SqliteDataStoreConfig {
db_url: db_path.clone(),
run_repairs: true,
skip_broken: false,
}),
http: Default::default(),
frontend: Default::default(),
oidc: None,
tracing: Default::default(),
dav_push: Default::default(),
nextcloud_login: Default::default(),
caldav: Default::default(),
},
)
.await
.unwrap();
// Bodge to set password without using command (since that reads stdin)
let db = create_db_pool(&db_path, false).await.unwrap();
let principal_store = SqlitePrincipalStore::new(db);
let app_token = "token";
principal_store
.add_app_token("user", "Test Token".to_owned(), app_token.to_owned())
.await
.unwrap();
let url = origin.clone() + "/caldav/principal/user";
let resp = reqwest::Client::new()
.request(Method::from_bytes(b"PROPFIND").unwrap(), &url)
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
let resp = reqwest::Client::new()
.request(Method::from_bytes(b"PROPFIND").unwrap(), &url)
.basic_auth("user", Some("token"))
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::MULTI_STATUS);
principal_store.remove_principal("user").await.unwrap();
let resp = reqwest::Client::new()
.request(Method::from_bytes(b"PROPFIND").unwrap(), &url)
.send()
.await
.unwrap();
assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
})
.await;
}