mirror of
https://github.com/lennart-k/rustical.git
synced 2026-01-30 06:58:26 +00:00
Add another integration test
This commit is contained in:
@@ -3,27 +3,38 @@ use rustical::{
|
||||
config::{Config, DataStoreConfig, HttpConfig, SqliteDataStoreConfig},
|
||||
};
|
||||
use std::{
|
||||
collections::HashSet,
|
||||
net::{Ipv4Addr, SocketAddrV4, TcpListener},
|
||||
sync::Arc,
|
||||
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() {
|
||||
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() -> (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 token = CancellationToken::new();
|
||||
let cloned_token = token.clone();
|
||||
@@ -41,7 +52,7 @@ pub fn rustical_process() -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify
|
||||
},
|
||||
Config {
|
||||
data_store: DataStoreConfig::Sqlite(SqliteDataStoreConfig {
|
||||
db_url: ":memory:".to_owned(),
|
||||
db_url: db_url.unwrap_or(":memory:".to_owned()),
|
||||
run_repairs: true,
|
||||
skip_broken: false,
|
||||
}),
|
||||
@@ -58,6 +69,7 @@ pub fn rustical_process() -> (CancellationToken, u16, JoinHandle<()>, Arc<Notify
|
||||
caldav: Default::default(),
|
||||
},
|
||||
Some(cloned_start_notify),
|
||||
false,
|
||||
)
|
||||
.await
|
||||
};
|
||||
|
||||
@@ -1,19 +1,26 @@
|
||||
// This integration test checks whether the HTTP server works by actually running rustical in a new
|
||||
// thread.
|
||||
use common::rustical_process;
|
||||
use http::StatusCode;
|
||||
use http::{Method, StatusCode};
|
||||
use rustical::{
|
||||
PrincipalsArgs, cmd_principals,
|
||||
config::{Config, DataStoreConfig, 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>(inner: F)
|
||||
pub async fn test_runner<O, F>(db_path: Option<String>, inner: F)
|
||||
where
|
||||
O: IntoFuture<Output = ()>,
|
||||
// <O as IntoFuture>::IntoFuture: UnwindSafe,
|
||||
F: FnOnce(String) -> O,
|
||||
{
|
||||
// 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
|
||||
@@ -32,7 +39,7 @@ where
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_ping() {
|
||||
test_runner(async |origin| {
|
||||
test_runner(None, async |origin| {
|
||||
let resp = reqwest::get(origin.clone() + "/ping").await.unwrap();
|
||||
assert_eq!(resp.status(), StatusCode::OK);
|
||||
|
||||
@@ -42,3 +49,77 @@ async fn test_ping() {
|
||||
})
|
||||
.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 |origin| {
|
||||
// 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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user