Compare commits

...

5 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
Lennart K
233cf2ea37 fix test snapshots 2026-01-28 21:05:56 +01:00
Lennart K
494f31f992 Add more abstract integration test 2026-01-28 20:54:55 +01:00
Lennart K
c1758e2cba cmd_default: Add notifier to detect when rustical has started 2026-01-28 20:16:41 +01:00
38 changed files with 472 additions and 40 deletions

4
Cargo.lock generated
View File

@@ -3319,6 +3319,7 @@ dependencies = [
"caldata",
"clap",
"figment",
"futures-util",
"headers",
"http",
"insta",
@@ -3343,7 +3344,9 @@ dependencies = [
"serde",
"similar-asserts",
"sqlx",
"tempfile",
"tokio",
"tokio-util",
"toml 0.9.11+spec-1.1.0",
"tower",
"tower-http",
@@ -4471,6 +4474,7 @@ dependencies = [
"bytes",
"futures-core",
"futures-sink",
"futures-util",
"pin-project-lite",
"tokio",
]

View File

@@ -73,6 +73,7 @@ tokio = { version = "1.48", features = [
"rt-multi-thread",
"full",
] }
tokio-util = { version = "0.7", features = ["rt"] }
url = "2.5"
base64 = "0.22"
thiserror = "2.0"
@@ -155,6 +156,7 @@ rstest.workspace = true
rustical_store_sqlite = { workspace = true, features = ["test"] }
insta.workspace = true
similar-asserts.workspace = true
tempfile = "3.24"
[dependencies]
rustical_store.workspace = true
@@ -166,6 +168,7 @@ caldata.workspace = true
toml.workspace = true
serde.workspace = true
tokio.workspace = true
tokio-util.workspace = true
tracing.workspace = true
anyhow.workspace = true
clap.workspace = true
@@ -204,3 +207,4 @@ tower-http.workspace = true
axum-extra.workspace = true
headers.workspace = true
http.workspace = true
futures-util.workspace = true

View File

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

View File

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

View File

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

View File

@@ -17,6 +17,7 @@ use rustical_store_sqlite::principal_store::SqlitePrincipalStore;
use rustical_store_sqlite::{SqliteStore, create_db_pool};
use setup_tracing::setup_tracing;
use std::sync::Arc;
use tokio::sync::Notify;
use tokio::sync::mpsc::Receiver;
use tower::Layer;
use tower_http::normalize_path::NormalizePathLayer;
@@ -105,8 +106,15 @@ pub async fn get_data_stores(
}
#[allow(clippy::missing_errors_doc, clippy::missing_panics_doc)]
pub async fn cmd_default(args: Args, config: Config) -> Result<()> {
setup_tracing(&config.tracing);
pub async fn cmd_default(
args: Args,
config: Config,
start_notifier: Option<Arc<Notify>>,
tracing: bool,
) -> Result<()> {
if tracing {
setup_tracing(&config.tracing);
}
let (addr_store, cal_store, subscription_store, principal_store, update_recv) =
get_data_stores(!args.no_migrations, &config.data_store).await?;
@@ -144,6 +152,9 @@ pub async fn cmd_default(args: Args, config: Config) -> Result<()> {
let listener = tokio::net::TcpListener::bind(&address).await?;
tasks.push(tokio::spawn(async move {
info!("RustiCal serving on http://{address}");
if let Some(start_notifier) = start_notifier {
start_notifier.notify_waiters();
}
axum::serve(listener, app).await.unwrap();
}));

View File

@@ -21,14 +21,16 @@ async fn main() -> Result<()> {
match args.command {
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)) => {
let config: Config = parse_config()?;
cmd_health(config.http, health_args).await
}
None => {
let config: Config = parse_config()?;
cmd_default(args, config).await
cmd_default(args, config, None, true).await
}
}
}

View File

@@ -0,0 +1,84 @@
use rustical::{
Args, cmd_default,
config::{Config, DataStoreConfig, HttpConfig, SqliteDataStoreConfig},
};
use std::{
collections::HashSet,
net::{Ipv4Addr, SocketAddrV4, TcpListener},
sync::{Arc, Mutex, OnceLock},
thread::{self, JoinHandle},
};
use tokio::sync::Notify;
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> {
let bound_ports = BOUND_PORTS.get_or_init(Mutex::default);
let mut bound_ports_write = bound_ports.lock().unwrap();
let mut port = 15000;
// Frees the socket on drop such that this function returns a free port
while TcpListener::bind(SocketAddrV4::new(Ipv4Addr::UNSPECIFIED, port)).is_err()
|| bound_ports_write.contains(&port)
{
port += 1;
if port >= 16000 {
return None;
}
}
bound_ports_write.insert(port);
Some(port)
}
pub fn rustical_process(
db_url: Option<String>,
) -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify>) {
let port = find_free_port().unwrap();
let token = CancellationToken::new();
let cloned_token = token.clone();
let start_notify = Arc::new(Notify::new());
let cloned_start_notify = start_notify.clone();
let main_process = thread::spawn(move || {
let rt = tokio::runtime::Runtime::new().unwrap();
let fut = async {
cmd_default(
Args {
config_file: "asldajldakjsdkj".to_owned(),
no_migrations: false,
command: None,
},
Config {
data_store: DataStoreConfig::Sqlite(SqliteDataStoreConfig {
db_url: db_url.unwrap_or(":memory:".to_owned()),
run_repairs: true,
skip_broken: false,
}),
http: HttpConfig {
host: "127.0.0.1".to_owned(),
port,
..Default::default()
},
frontend: Default::default(),
oidc: None,
tracing: Default::default(),
dav_push: Default::default(),
nextcloud_login: Default::default(),
caldav: Default::default(),
},
Some(cloned_start_notify),
false,
)
.await
};
rt.block_on(async {
tokio::select! {
_ = cloned_token.cancelled() => {},
_ = fut => {}
}
});
});
(token, port, main_process, start_notify)
}

137
tests/http_integration.rs Normal file
View File

@@ -0,0 +1,137 @@
// This integration test checks whether the HTTP server works by actually running rustical in a new
// thread.
use common::rustical_process;
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;
mod common;
pub async fn test_runner<O, F>(db_path: Option<String>, inner: F)
where
O: IntoFuture<Output = ()>,
// <O as IntoFuture>::IntoFuture: UnwindSafe,
F: FnOnce(u16) -> O,
{
// Start RustiCal process
let (token, port, main_process, start_notify) = rustical_process(db_path);
// Wait for RustiCal server to listen
tokio::time::timeout(Duration::new(2, 0), start_notify.notified())
.await
.unwrap();
// We use catch_unwind to make sure we'll always correctly stop RustiCal
// Otherwise, our process would just run indefinitely
inner(port).into_future().await;
// Signal RustiCal to stop
token.cancel();
main_process.join().unwrap();
}
#[tokio::test]
async fn test_ping() {
test_runner(None, async |port| {
let origin = format!("http://localhost:{port}");
let resp = reqwest::get(origin.clone() + "/ping").await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
// Ensure that path normalisation works as intended
let resp = reqwest::get(origin + "/ping/").await.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
cmd_health(
HttpConfig {
host: "localhost".to_owned(),
port,
..Default::default()
},
Default::default(),
)
.await
.unwrap();
})
.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;
}

View File

@@ -0,0 +1,5 @@
---
source: tests/integration_tests/carddav/addressbook.rs
expression: body
---

View File

@@ -0,0 +1,5 @@
---
source: tests/integration_tests/carddav/addressbook.rs
expression: body
---

View File

@@ -0,0 +1,5 @@
---
source: tests/integration_tests/carddav/addressbook.rs
expression: body
---

View File

@@ -0,0 +1,35 @@
---
source: tests/integration_tests/carddav/addressbook.rs
expression: body
---
<?xml version="1.0" encoding="utf-8"?>
<multistatus xmlns="DAV:" xmlns:CAL="urn:ietf:params:xml:ns:caldav" xmlns:CARD="urn:ietf:params:xml:ns:carddav" xmlns:CS="http://calendarserver.org/ns/" xmlns:PUSH="https://bitfire.at/webdav-push">
<response>
<href>/carddav/principal/user/contacts/newcard.vcf</href>
<propstat>
<prop>
<getetag>&quot;ea0bf4a2ce7ef84606a4cf9235776dbc11b3e7ce351ddf35f27cbc0088acca7e&quot;</getetag>
<CARD:address-data>BEGIN:VCARD
VERSION:3.0
FN:Cyrus Daboo
N:Daboo;Cyrus
ADR;TYPE=POSTAL:;2822 Email HQ;Suite 2821;RFCVille;PA;15213;USA
EMAIL;TYPE=INTERNET,PREF:cyrus@example.com
NICKNAME:me
NOTE:Example VCard.
ORG:Self Employed
TEL;TYPE=WORK,VOICE:412 605 0499
TEL;TYPE=FAX:412 605 0705
URL:http://www.example.com
UID:1234-5678-9000-1
END:VCARD
</CARD:address-data>
</prop>
<status>HTTP/1.1 200 OK</status>
</propstat>
</response>
<response>
<href>/home/bernard/addressbook/vcf1.vcf</href>
<status>HTTP/1.1 404 Not Found</status>
</response>
</multistatus>

View File

@@ -0,0 +1,68 @@
---
source: tests/integration_tests/carddav/addressbook.rs
expression: body
---
<?xml version="1.0" encoding="utf-8"?>
<multistatus xmlns="DAV:" xmlns:CAL="urn:ietf:params:xml:ns:caldav" xmlns:CARD="urn:ietf:params:xml:ns:carddav" xmlns:CS="http://calendarserver.org/ns/" xmlns:PUSH="https://bitfire.at/webdav-push">
<response>
<href>/carddav/principal/user/contacts/</href>
<propstat>
<prop>
<CARD:addressbook-description>Amazing contacts!</CARD:addressbook-description>
<CARD:supported-address-data>
<CARD:address-data-type content-type="text/vcard" version="3.0"/>
<CARD:address-data-type content-type="text/vcard" version="4.0"/>
</CARD:supported-address-data>
<CARD:supported-collation-set>
<CARD:supported-collation>i;ascii-casemap</CARD:supported-collation>
<CARD:supported-collation>i;unicode-casemap</CARD:supported-collation>
<CARD:supported-collation>i;octet</CARD:supported-collation>
</CARD:supported-collation-set>
<supported-report-set>
<supported-report>
<report>
<CARD:addressbook-multiget/>
</report>
</supported-report>
<supported-report>
<report>
<sync-collection/>
</report>
</supported-report>
</supported-report-set>
<CARD:max-resource-size>10000000</CARD:max-resource-size>
<sync-token>github.com/lennart-k/rustical/ns/0</sync-token>
<CS:getctag>github.com/lennart-k/rustical/ns/0</CS:getctag>
<PUSH:transports>
<PUSH:web-push/>
</PUSH:transports>
<PUSH:topic>[PUSH_TOPIC]</PUSH:topic>
<PUSH:supported-triggers>
<PUSH:content-update>
<depth>1</depth>
</PUSH:content-update>
<PUSH:property-update>
<depth>1</depth>
</PUSH:property-update>
</PUSH:supported-triggers>
<resourcetype>
<collection/>
<CARD:addressbook/>
</resourcetype>
<displayname>Contacts</displayname>
<current-user-principal>
<href>/carddav/principal/user/</href>
</current-user-principal>
<current-user-privilege-set>
<privilege>
<all/>
</privilege>
</current-user-privilege-set>
<owner>
<href>/carddav/principal/user/</href>
</owner>
</prop>
<status>HTTP/1.1 200 OK</status>
</propstat>
</response>
</multistatus>

View File

@@ -0,0 +1,28 @@
---
source: tests/integration_tests/carddav/addressbook.rs
expression: body
---
<?xml version="1.0" encoding="utf-8"?>
<multistatus xmlns="DAV:" xmlns:CAL="urn:ietf:params:xml:ns:caldav" xmlns:CARD="urn:ietf:params:xml:ns:carddav" xmlns:CS="http://calendarserver.org/ns/" xmlns:PUSH="https://bitfire.at/webdav-push">
<response>
<href>/carddav/principal/user/contacts</href>
<propstat>
<prop>
<displayname xmlns="DAV:"/>
<addressbook-description xmlns="urn:ietf:params:xml:ns:carddav"/>
<addressbook-description/>
</prop>
<status>HTTP/1.1 200 OK</status>
</propstat>
<propstat>
<prop>
</prop>
<status>HTTP/1.1 404 Not Found</status>
</propstat>
<propstat>
<prop>
</prop>
<status>HTTP/1.1 409 Conflict</status>
</propstat>
</response>
</multistatus>

View File

@@ -0,0 +1,12 @@
---
source: tests/integration_tests/carddav/addressbook_import.rs
expression: body
---
BEGIN:VCARD
VERSION:4.0
FN:John Doe
N:Doe;John;;;,
BDAY:--0203
GENDER:M
UID:[UID]
END:VCARD

View File

@@ -0,0 +1,5 @@
---
source: tests/integration_tests/carddav/addressbook_import.rs
expression: body
---

View File

@@ -0,0 +1,5 @@
---
source: tests/integration_tests/carddav/mod.rs
expression: body
---

View File

@@ -0,0 +1,27 @@
---
source: tests/integration_tests/carddav/mod.rs
expression: body
---
<?xml version="1.0" encoding="utf-8"?>
<multistatus xmlns="DAV:" xmlns:CAL="urn:ietf:params:xml:ns:caldav" xmlns:CARD="urn:ietf:params:xml:ns:carddav" xmlns:CS="http://calendarserver.org/ns/" xmlns:PUSH="https://bitfire.at/webdav-push">
<response>
<href>/carddav/</href>
<propstat>
<prop>
<resourcetype>
<collection/>
</resourcetype>
<displayname>RustiCal DAV root</displayname>
<current-user-principal>
<href>/carddav/principal/user/</href>
</current-user-principal>
<current-user-privilege-set>
<privilege>
<all/>
</privilege>
</current-user-privilege-set>
</prop>
<status>HTTP/1.1 200 OK</status>
</propstat>
</response>
</multistatus>

View File

@@ -0,0 +1,5 @@
---
source: tests/integration_tests/carddav/mod.rs
expression: body
---

View File

@@ -1 +0,0 @@
mod integration_tests;

View File

@@ -0,0 +1,3 @@
// This file is just an entrypoint for integration_tests
// since the test runner only looks for files to run.
mod integration_tests;