From 64755ff47b465949de4e7255dbda5ce329c8f921 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Fri, 30 Oct 2026 09:43:49 -0700 Subject: [PATCH] Test against inbuxa itself; rename Sieve names for an inbuxa session tests/integration_inbuxa.rs runs against the 27.1-rc.4 server image, pinned by digest. Its rig starts the server in recovery mode to make a domain and an administrator, then normally on the same volume, where the administrator makes an API key. One test runs a migrate plan that sets up two accounts with import-directory and exports each with that one key: each account's mail lands in its own mailbox and nowhere else, a message filed in two folders arrives once in both, and a final --again pass sends nothing. The other checks that a second export carries over a flag changed at the source, and that Sieve scripts get inbuxa's names. That second check found a bug: export renamed vnd.stalwart.* to vnd.inbuxa.* only when the target listed vnd.inbuxa.* among its sieveExtensions. inbuxa runs them without listing them, as Stalwart does its own, so scripts went into inbuxa unchanged. The target now also counts as inbuxa when its session has urn:inbuxa:jmap. --- CHANGELOG.md | 5 + docs/usage.md | 16 ++ src/jmap/request.rs | 3 + src/sync/export/sieve.rs | 7 +- src/sync/export/sieve_names.rs | 31 ++-- tests/integration/inbuxa.rs | 279 +++++++++++++++++++++++++++++++ tests/integration/mod.rs | 1 + tests/integration_inbuxa.rs | 291 +++++++++++++++++++++++++++++++++ tests/mock_sync.rs | 31 ++++ 9 files changed, 648 insertions(+), 16 deletions(-) create mode 100644 tests/integration/inbuxa.rs create mode 100644 tests/integration_inbuxa.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e6b865..be6e1e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,11 @@ candidates before a series ships (`27.1-rc.1`). The tag is the version with a plan. ### Fixed +- `export` into inbuxa renames Stalwart's Sieve extensions + (`vnd.stalwart.while` and the rest) to inbuxa's. It looked for inbuxa's + names in the server's list of Sieve extensions, which doesn't name them, + so scripts went in unchanged. It now goes by the session, which says the + server is inbuxa. - `inspect` opens the archive read-only. It used to create an empty archive at a mistyped path and bring an older archive's layout up to date; now the first is an error and the second is refused, and reading a closed diff --git a/docs/usage.md b/docs/usage.md index 1b403ad..3fdec43 100644 --- a/docs/usage.md +++ b/docs/usage.md @@ -675,6 +675,9 @@ cargo test --test integration_export_imap -- --ignored --test-threads=1 cargo test --test integration_export_managesieve -- --ignored --test-threads=1 cargo test --test integration_export_dav -- --ignored --test-threads=1 +# inbuxa itself, an image pinned by digest (run before each release): +cargo test --test integration_inbuxa -- --ignored + # Slow tests cargo test --test mock_jmap -- --ignored ``` @@ -685,3 +688,16 @@ container, and each one creates and removes the same disposable domain, Separate binaries each boot their own container on their own ports, so running them one after another is safe. +`integration_inbuxa` is the exception: each of its tests starts its own +inbuxa server on its own volume, so its tests run in parallel. A test starts +the server once in recovery mode to make a domain and an administrator, then +again normally, where the administrator makes an API key. It then checks: +- a `migrate --plan` run that sets up the accounts with `import-directory` + and exports each one with that one key; +- a message in two folders arriving once, in both; +- a second export carrying over a changed flag; +- Sieve scripts getting inbuxa's names. + +To test another release, change the digest in +`tests/integration/inbuxa.rs`. + diff --git a/src/jmap/request.rs b/src/jmap/request.rs index 0e2ad33..0a88ac6 100644 --- a/src/jmap/request.rs +++ b/src/jmap/request.rs @@ -18,6 +18,9 @@ use crate::logging::Logger; pub const URN_CORE: &str = "urn:ietf:params:jmap:core"; +/// The session capability every inbuxa server has. +pub const URN_INBUXA: &str = "urn:inbuxa:jmap"; + /// The capability inbuxa advertises for its registry types (`x:Account`, /// `x:Domain` and the rest of the `x:` objects). pub const URN_INBUXA_REGISTRY: &str = "urn:inbuxa:jmap:registry"; diff --git a/src/sync/export/sieve.rs b/src/sync/export/sieve.rs index f4bdd04..11f560d 100644 --- a/src/sync/export/sieve.rs +++ b/src/sync/export/sieve.rs @@ -15,7 +15,7 @@ use super::{Maps, Net, Plan, Uploader}; use crate::error::Error; use crate::jmap::blobxfer; use crate::jmap::error::JmapError; -use crate::jmap::request::{Request, check_method_error}; +use crate::jmap::request::{Request, URN_INBUXA, check_method_error}; use crate::logging::{LEVEL_DEFAULT, Logger}; use crate::sync::import_jmap::mapping::BlobBytes; use crate::sync::import_jmap::mapping::{SIEVE_SELECT, row_to_sieve_script}; @@ -66,7 +66,10 @@ pub fn reconcile( let mut active_target: Option = None; let mut deactivate = false; let mut uploader = Uploader::new(net, &ctx.conn); - let rename_vendor = sieve_names::target_uses_inbuxa_names(&target_sieve_extensions(net)); + let rename_vendor = sieve_names::target_uses_inbuxa_names( + net.session.capabilities.contains_key(URN_INBUXA), + &target_sieve_extensions(net), + ); let wanted_active = locals .iter() .find(|(_, _, a, _)| *a) diff --git a/src/sync/export/sieve_names.rs b/src/sync/export/sieve_names.rs index 1439cb0..238b52b 100644 --- a/src/sync/export/sieve_names.rs +++ b/src/sync/export/sieve_names.rs @@ -20,11 +20,12 @@ const NEW: &str = "vnd.inbuxa."; const OLD_ENV_REF: &str = "${env.vnd.stalwart."; const NEW_ENV_REF: &str = "${env.vnd.inbuxa."; -/// Whether the target advertises inbuxa's vendor extensions, from the -/// `sieveExtensions` list of its `urn:ietf:params:jmap:sieve` account -/// capability. -pub fn target_uses_inbuxa_names(sieve_extensions: &[String]) -> bool { - sieve_extensions.iter().any(|e| e.starts_with(NEW)) +/// Whether the target takes inbuxa's vendor names: it is an inbuxa server +/// (its session has `urn:inbuxa:jmap`), or its `sieveExtensions` list +/// names them. inbuxa runs `vnd.inbuxa.while` and the rest without listing +/// them, as Stalwart does its own, so the list alone misses it. +pub fn target_uses_inbuxa_names(inbuxa_session: bool, sieve_extensions: &[String]) -> bool { + inbuxa_session || sieve_extensions.iter().any(|e| e.starts_with(NEW)) } /// The script with Stalwart's vendor names renamed, and the old names that @@ -329,14 +330,16 @@ mod tests { #[test] fn target_detection() { - assert!(target_uses_inbuxa_names(&[ - "fileinto".to_owned(), - "vnd.inbuxa.while".to_owned() - ])); - assert!(!target_uses_inbuxa_names(&[ - "fileinto".to_owned(), - "vnd.stalwart.while".to_owned() - ])); - assert!(!target_uses_inbuxa_names(&[])); + assert!(target_uses_inbuxa_names( + false, + &["fileinto".to_owned(), "vnd.inbuxa.while".to_owned()] + )); + assert!(!target_uses_inbuxa_names( + false, + &["fileinto".to_owned(), "vnd.stalwart.while".to_owned()] + )); + assert!(!target_uses_inbuxa_names(false, &[])); + // inbuxa lists none of its vendor extensions. + assert!(target_uses_inbuxa_names(true, &["fileinto".to_owned()])); } } diff --git a/tests/integration/inbuxa.rs b/tests/integration/inbuxa.rs new file mode 100644 index 0000000..742d9d8 --- /dev/null +++ b/tests/integration/inbuxa.rs @@ -0,0 +1,279 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + */ + +//! An inbuxa server, pinned by digest, set up the way a test needs it: +//! started once in recovery mode to make a domain and an administrator, +//! then again normally on the same data, where the administrator makes an +//! API key. The second start lets HTTP Basic through +//! (`INBUXA_HTTP_BASIC_AUTH=all`) so the test can sign in as the users it +//! checks; the migration itself uses the key. +//! +//! Recovery mode makes no roles, and a server seeds its built-in ones on +//! its first normal start only if it has none, so nothing here creates a +//! role before that start. + +use std::net::TcpListener; +use std::process::Command; +use std::time::{Duration, Instant}; + +use encodify::base64::STANDARD; +use serde_json::{Value, json}; +use testcontainers::core::{IntoContainerPort, Mount}; +use testcontainers::runners::SyncRunner; +use testcontainers::{Container, GenericImage, ImageExt}; +use ureq::Agent; + +use super::OWNER_LABEL; +use super::error::{ContainerError, ContainerResult}; + +/// 27.1-rc.4. `GenericImage` joins name and tag with `:`, so the digest is +/// split across the two. +const IMAGE_NAME: &str = "ghcr.io/inbuxa/inbuxa-server@sha256"; +const IMAGE_DIGEST: &str = "f43be9a78d3155eaddae7bd95033d5b833eddd11761bab996c1d80913f48b4a6"; + +const HTTP_PORT: u16 = 8080; +const CONFIG_PATH: &str = "/etc/inbuxa/config.json"; +const CONFIG_JSON: &str = r#"{"@type":"RocksDb","path":"/var/lib/inbuxa"}"#; +const RECOVERY_ADMIN: (&str, &str) = ("admin", "admin"); + +pub const DOMAIN: &str = "example.org"; +pub const ADMIN_EMAIL: &str = "boss@example.org"; +pub const ADMIN_PASSWORD: &str = "Boss-pass-Xq7v2k9w"; + +pub struct Inbuxa { + container: Option>, + volume: String, + pub base_url: String, + /// The administrator's API key. + pub token: String, +} + +impl Inbuxa { + pub fn start() -> ContainerResult { + let port = free_port()?; + let base_url = format!("http://127.0.0.1:{port}"); + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map_or(0, |d| d.as_nanos()); + let volume = format!("inbuxa-migrate-itest-{}-{nanos}", std::process::id()); + + let recovery = run(&volume, port, &base_url, true)?; + let admin = basic(RECOVERY_ADMIN.0, RECOVERY_ADMIN.1); + let session = wait_ready(&base_url, &admin)?; + let account = registry_account(&session)?; + let created = jmap( + &base_url, + &admin, + json!([["x:Domain/set", {"accountId": account, "create": {"d": {"name": DOMAIN}}}, "0"]]), + )?; + let domain_id = created[0][1]["created"]["d"]["id"] + .as_str() + .ok_or_else(|| ContainerError::Seed(format!("domain not created: {created}")))? + .to_owned(); + let created = jmap( + &base_url, + &admin, + json!([["x:Account/set", {"accountId": account, "create": {"boss": { + "@type": "User", + "name": "boss", + "domainId": domain_id, + "credentials": {"0": {"@type": "Password", "secret": ADMIN_PASSWORD}}, + "roles": {"@type": "Admin"}, + }}}, "0"]]), + )?; + if created[0][1]["created"]["boss"].is_null() { + return Err(ContainerError::Seed(format!( + "administrator not created: {created}" + ))); + } + drop(recovery); + + let container = run(&volume, port, &base_url, false)?; + let admin = basic(ADMIN_EMAIL, ADMIN_PASSWORD); + let session = wait_ready(&base_url, &admin)?; + let account = registry_account(&session)?; + let created = jmap( + &base_url, + &admin, + json!([["x:ApiKey/set", {"accountId": account, "create": {"k": { + "description": "inbuxa-migrate live test", + "permissions": {"@type": "Inherit"}, + }}}, "0"]]), + )?; + let token = created[0][1]["created"]["k"]["secret"] + .as_str() + .ok_or_else(|| ContainerError::Seed(format!("no API key: {created}")))? + .to_owned(); + Ok(Inbuxa { + container: Some(container), + volume, + base_url, + token, + }) + } + + /// The account a user's own session names, signed in as that user. + pub fn account_of(&self, email: &str, password: &str) -> ContainerResult { + let session = session(&self.base_url, &basic(email, password))?; + session["primaryAccounts"]["urn:ietf:params:jmap:mail"] + .as_str() + .map(str::to_owned) + .ok_or_else(|| ContainerError::Protocol(format!("no mail account: {session}"))) + } + + /// A blob's bytes, signed in as `email`. + pub fn download( + &self, + email: &str, + password: &str, + account: &str, + blob: &str, + ) -> ContainerResult> { + let auth = basic(email, password); + let template = session(&self.base_url, &auth)?["downloadUrl"] + .as_str() + .unwrap_or_default() + .to_owned(); + let url = template + .replace("{accountId}", account) + .replace("{blobId}", blob) + .replace("{name}", "blob") + .replace("{type}", "application/octet-stream"); + let mut resp = agent().get(&url).header("Authorization", &auth).call()?; + resp.body_mut() + .read_to_vec() + .map_err(|e| ContainerError::Protocol(format!("download: {e}"))) + } + + /// Method calls, signed in as `email`. + pub fn call(&self, email: &str, password: &str, calls: Value) -> ContainerResult { + jmap(&self.base_url, &basic(email, password), calls) + } +} + +impl Drop for Inbuxa { + fn drop(&mut self) { + drop(self.container.take()); + let _ = Command::new("docker") + .args(["volume", "rm", "-f", &self.volume]) + .output(); + } +} + +fn run( + volume: &str, + port: u16, + base_url: &str, + recovery: bool, +) -> ContainerResult> { + let mut request = GenericImage::new(IMAGE_NAME, IMAGE_DIGEST) + .with_exposed_port(HTTP_PORT.tcp()) + .with_env_var("INBUXA_PUBLIC_URL", base_url) + .with_labels([(OWNER_LABEL, "1")]) + .with_copy_to(CONFIG_PATH, CONFIG_JSON.as_bytes().to_vec()) + .with_mount(Mount::volume_mount(volume, "/var/lib/inbuxa")) + .with_mapped_port(port, HTTP_PORT.tcp()) + .with_startup_timeout(Duration::from_secs(180)); + request = if recovery { + request + .with_env_var("INBUXA_RECOVERY_MODE", "1") + .with_env_var( + "INBUXA_RECOVERY_ADMIN", + format!("{}:{}", RECOVERY_ADMIN.0, RECOVERY_ADMIN.1), + ) + } else { + request.with_env_var("INBUXA_HTTP_BASIC_AUTH", "all") + }; + Ok(request.start()?) +} + +fn wait_ready(base_url: &str, auth: &str) -> ContainerResult { + let deadline = Instant::now() + Duration::from_secs(90); + loop { + match session(base_url, auth) { + Ok(s) if s.get("apiUrl").is_some() => return Ok(s), + Ok(s) if Instant::now() > deadline => { + return Err(ContainerError::Protocol(format!("no session: {s}"))); + } + Err(e) if Instant::now() > deadline => return Err(e), + _ => std::thread::sleep(Duration::from_millis(500)), + } + } +} + +fn session(base_url: &str, auth: &str) -> ContainerResult { + let mut resp = agent() + .get(format!("{base_url}/jmap/session")) + .header("Authorization", auth) + .call()?; + let status = resp.status().as_u16(); + let text = resp + .body_mut() + .read_to_string() + .map_err(|e| ContainerError::Protocol(format!("body read: {e}")))?; + if status != 200 { + return Err(ContainerError::Protocol(format!( + "session status {status}: {text}" + ))); + } + serde_json::from_str(&text).map_err(|e| ContainerError::Protocol(format!("session: {e}"))) +} + +/// The account that carries the registry: an administrator's own. +fn registry_account(session: &Value) -> ContainerResult { + session["accounts"] + .as_object() + .and_then(|accounts| { + accounts.iter().find(|(_, a)| { + a["accountCapabilities"] + .get("urn:inbuxa:jmap:registry") + .is_some() + }) + }) + .map(|(id, _)| id.clone()) + .ok_or_else(|| ContainerError::Protocol(format!("no registry account: {session}"))) +} + +fn jmap(base_url: &str, auth: &str, calls: Value) -> ContainerResult { + let body = json!({ + "using": [ + "urn:ietf:params:jmap:core", + "urn:ietf:params:jmap:mail", + "urn:ietf:params:jmap:sieve", + "urn:inbuxa:jmap:registry", + ], + "methodCalls": calls, + }); + let mut resp = agent() + .post(format!("{base_url}/jmap/")) + .header("Authorization", auth) + .header("Content-Type", "application/json") + .send(body.to_string())?; + let text = resp + .body_mut() + .read_to_string() + .map_err(|e| ContainerError::Protocol(format!("body read: {e}")))?; + let reply: Value = serde_json::from_str(&text) + .map_err(|e| ContainerError::Protocol(format!("reply {text}: {e}")))?; + Ok(reply["methodResponses"].clone()) +} + +fn free_port() -> ContainerResult { + Ok(TcpListener::bind("127.0.0.1:0")?.local_addr()?.port()) +} + +fn agent() -> Agent { + Agent::config_builder() + .http_status_as_error(false) + .build() + .new_agent() +} + +pub fn basic(user: &str, password: &str) -> String { + let mut header = String::from("Basic "); + STANDARD.encode_append(format!("{user}:{password}"), &mut header); + header +} diff --git a/tests/integration/mod.rs b/tests/integration/mod.rs index 5de15cd..f0fcef0 100644 --- a/tests/integration/mod.rs +++ b/tests/integration/mod.rs @@ -14,6 +14,7 @@ pub mod dav_client; pub mod dovecot; pub mod error; pub mod imap_client; +pub mod inbuxa; pub mod layouts; pub mod radicale; pub mod sieve_client; diff --git a/tests/integration_inbuxa.rs b/tests/integration_inbuxa.rs new file mode 100644 index 0000000..5214e3f --- /dev/null +++ b/tests/integration_inbuxa.rs @@ -0,0 +1,291 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: Apache-2.0 OR MIT + */ + +//! Exports into inbuxa itself, an image pinned by digest. Run before each +//! release: `cargo test --test integration_inbuxa -- --ignored`. + +mod integration; + +use std::fs; +use std::path::Path; +use std::process::Command; + +use inbuxa_migrate::db; +use integration::inbuxa::{ADMIN_EMAIL, ADMIN_PASSWORD, Inbuxa}; +use serde_json::{Value, json}; +use tempfile::TempDir; + +const BIN: &str = env!("CARGO_BIN_EXE_inbuxa-migrate"); + +fn message(folder: &Path, file: &str, id: &str, subject: &str) { + for sub in ["cur", "new", "tmp"] { + fs::create_dir_all(folder.join(sub)).unwrap(); + } + fs::write( + folder.join("cur").join(file), + format!( + "From: x@example.org\r\nTo: y@example.org\r\nSubject: {subject}\r\n\ + Message-ID: <{id}@example.org>\r\nDate: Mon, 4 Mar 2024 09:00:00 +0000\r\n\r\nHi\r\n" + ), + ) + .unwrap(); +} + +fn user(email: &str) -> Value { + let (name, domain) = email.split_once('@').unwrap(); + json!({ + "email": email, "name": name, "domain": domain, "description": null, + "roles": "user", "permissions": null, "aliases": [], "quotas": {}, + "tenant": null, "locale": null, "timeZone": null, "externalId": null, "groups": [], + }) +} + +/// Each email in the account as (subject, number of mailboxes, keywords). +fn emails(server: &Inbuxa, email: &str, password: &str) -> Vec<(String, usize, Vec)> { + let account = server.account_of(email, password).unwrap(); + let r = server + .call( + email, + password, + json!([ + ["Email/query", {"accountId": account}, "q"], + ["Email/get", {"accountId": account, + "#ids": {"resultOf": "q", "name": "Email/query", "path": "/ids"}, + "properties": ["subject", "mailboxIds", "keywords"]}, "g"], + ]), + ) + .unwrap(); + let mut out: Vec<_> = r[1][1]["list"] + .as_array() + .unwrap_or_else(|| panic!("{r}")) + .iter() + .map(|e| { + let mut keywords: Vec = e["keywords"] + .as_object() + .map(|k| k.keys().cloned().collect()) + .unwrap_or_default(); + keywords.sort(); + ( + e["subject"].as_str().unwrap_or_default().to_owned(), + e["mailboxIds"].as_object().map_or(0, |m| m.len()), + keywords, + ) + }) + .collect(); + out.sort(); + out +} + +fn report(work: &Path) -> Vec { + fs::read_to_string(work.join("report.csv")) + .unwrap() + .lines() + .skip(1) + .map(|l| l.split(',').take(9).collect::>().join(",")) + .collect() +} + +#[test] +#[ignore = "requires Docker"] +fn a_plan_sets_up_the_accounts_and_moves_each_with_one_admin_token() { + let server = Inbuxa::start().expect("start inbuxa"); + let dir = TempDir::new().unwrap(); + let root = dir.path(); + + fs::create_dir_all(root.join("directory")).unwrap(); + let directory = json!({ + "format": "inbuxa-migrate-directory", "formatVersion": 1, + "domains": [{"name": "other.test", "enabled": true, "description": null, "aliases": [], + "catchAllAddress": null, "subAddressing": "enabled", "tenant": null}], + "tenants": [], "roles": [], "groups": [], "lists": [], + "users": [user("ann@example.org"), user("bob@other.test")], + }); + fs::write(root.join("directory/directory.json"), directory.to_string()).unwrap(); + // Ann's message is in her inbox and in Archive: one email, two mailboxes. + message(&root.join("mail/ann"), "1:2,S", "ann-1", "For Ann"); + message(&root.join("mail/ann/.Archive"), "1:2,S", "ann-1", "For Ann"); + message(&root.join("mail/bob"), "1:2,", "bob-1", "For Bob"); + + let url = server.base_url.as_str(); + let plan = json!({ + "format": "inbuxa-migrate-plan", "formatVersion": 1, + "setup": [{"name": "directory", + "args": ["import-directory", "--url", url, "--auth-bearer", + "--passwords", "passwords.csv", "directory"], + "secrets": {"token": "ITEST_INBUXA_TOKEN"}}], + "accounts": {"directory": "directory/directory.json"}, + "steps": [ + {"name": "mail", "args": ["import", "maildir", "mail/{local}", "{archive}"]}, + {"name": "inbuxa", + "args": ["export", "--url", url, "--auth-bearer", "--account-name", "{email}", + "--objects", "mailbox,email", "{archive}"], + "secrets": {"token": "ITEST_INBUXA_TOKEN"}}, + ], + }); + fs::write(root.join("plan.json"), plan.to_string()).unwrap(); + let work = root.join("work"); + let migrate = |again: bool| { + let mut command = Command::new(BIN); + command + .args(["migrate", "-q", "--plan"]) + .arg(root.join("plan.json")) + .arg(&work) + .env("ITEST_INBUXA_TOKEN", &server.token); + if again { + command.arg("--again"); + } + let out = command.output().unwrap(); + assert!( + out.status.success(), + "{}\n{}", + String::from_utf8_lossy(&out.stderr), + fs::read_to_string(work.join("report.csv")).unwrap_or_default() + ); + }; + + migrate(false); + let rows = report(&work); + assert_eq!(rows[0], "(setup),directory,done,0,3,0,0,0,0", "{rows:?}"); + let passwords = fs::read_to_string(root.join("passwords.csv")).unwrap(); + let password = |email: &str| { + passwords + .lines() + .find_map(|l| l.strip_prefix(&format!("{email},"))) + .unwrap_or_else(|| panic!("{email} not in {passwords}")) + .to_owned() + }; + let (ann, bob) = (password("ann@example.org"), password("bob@other.test")); + let seen = vec!["$seen".to_owned()]; + assert_eq!( + emails(&server, "ann@example.org", &ann), + [("For Ann".to_owned(), 2, seen)] + ); + assert_eq!( + emails(&server, "bob@other.test", &bob), + [("For Bob".to_owned(), 1, Vec::new())] + ); + // The administrator's own mailbox got nothing. + assert_eq!(emails(&server, ADMIN_EMAIL, ADMIN_PASSWORD), []); + + // The final pass finds nothing new and sends nothing. + migrate(true); + let rows = report(&work); + assert!( + rows.iter() + .filter(|r| r.contains(",inbuxa,")) + .all(|r| r.contains(",inbuxa,done,0,0,0,0,")), + "{rows:?}" + ); + assert_eq!(emails(&server, "bob@other.test", &bob).len(), 1); +} + +#[test] +#[ignore = "requires Docker"] +fn a_second_export_updates_flags_and_sieve_names_are_renamed() { + let server = Inbuxa::start().expect("start inbuxa"); + let dir = TempDir::new().unwrap(); + let maildir = dir.path().join("mail"); + let archive = dir.path().join("archive.sqlite"); + message(&maildir, "1:2,S", "m-1", "Flag me"); + + let run = |args: &[&str]| { + let out = Command::new(BIN) + .args(args) + .env("INBUXA_MIGRATE_API_KEY", &server.token) + .output() + .unwrap(); + let stderr = String::from_utf8_lossy(&out.stderr).into_owned(); + assert!(out.status.success(), "{args:?}: {stderr}"); + (String::from_utf8_lossy(&out.stdout).into_owned(), stderr) + }; + let import = || { + run(&[ + "import", + "maildir", + maildir.to_str().unwrap(), + archive.to_str().unwrap(), + ]) + }; + let export = || { + run(&[ + "export", + "--url", + &server.base_url, + "--auth-bearer", + "--account-name", + ADMIN_EMAIL, + archive.to_str().unwrap(), + ]) + }; + + import(); + { + let conn = db::init::open(&archive).unwrap(); + let blob = db::blobs::intern_blob( + &conn, + b"require [\"fileinto\", \"vnd.stalwart.while\"];\nkeep;\n", + ) + .unwrap(); + conn.execute( + "INSERT INTO sieve_scripts (name, is_active, blob_id) VALUES ('main', 1, ?1)", + rusqlite::params![blob], + ) + .unwrap(); + } + let (_, stderr) = export(); + assert!( + stderr.contains("renamed vnd.stalwart.while to vnd.inbuxa.while"), + "{stderr}" + ); + assert_eq!( + emails(&server, ADMIN_EMAIL, ADMIN_PASSWORD), + [("Flag me".to_owned(), 1, vec!["$seen".to_owned()])] + ); + let account = server.account_of(ADMIN_EMAIL, ADMIN_PASSWORD).unwrap(); + let r = server + .call( + ADMIN_EMAIL, + ADMIN_PASSWORD, + json!([["SieveScript/get", {"accountId": account}, "s"]]), + ) + .unwrap(); + let script = r[0][1]["list"] + .as_array() + .and_then(|l| l.iter().find(|s| s["name"] == "main")) + .unwrap_or_else(|| panic!("no script: {r}")) + .clone(); + assert_eq!(script["isActive"], true, "{script}"); + let body = server + .download( + ADMIN_EMAIL, + ADMIN_PASSWORD, + &account, + script["blobId"].as_str().unwrap(), + ) + .unwrap(); + let body = String::from_utf8_lossy(&body); + assert!( + body.contains("vnd.inbuxa.while") && !body.contains("vnd.stalwart"), + "{body}" + ); + + // Flagged at the source: the next import and export carry it over. + fs::rename(maildir.join("cur/1:2,S"), maildir.join("cur/1:2,FS")).unwrap(); + import(); + let (stdout, _) = export(); + assert!( + stdout.contains("Email: created=0 fetched=0 updated=1"), + "{stdout}" + ); + assert_eq!( + emails(&server, ADMIN_EMAIL, ADMIN_PASSWORD), + [( + "Flag me".to_owned(), + 1, + vec!["$flagged".to_owned(), "$seen".to_owned()] + )] + ); +} diff --git a/tests/mock_sync.rs b/tests/mock_sync.rs index 25bef55..f528466 100644 --- a/tests/mock_sync.rs +++ b/tests/mock_sync.rs @@ -2767,6 +2767,37 @@ fn export_sieve_renames_stalwart_names_for_an_inbuxa_target() { let _ = std::fs::remove_file(&archive); } +/// inbuxa runs its vendor extensions without listing them; its session's +/// `urn:inbuxa:jmap` is what says the target is inbuxa. +#[test] +fn export_sieve_renames_for_an_inbuxa_session_that_lists_no_vendor_names() { + let mut server = mockito::Server::new(); + let base = server.url(); + let archive = archive_with_active_sieve( + "loop", + b"require [\"fileinto\", \"vnd.stalwart.while\"];\nkeep;\n", + ); + let mut session: serde_json::Value = + serde_json::from_str(&session_body_sieve(&base, &["fileinto"])).unwrap(); + session["capabilities"]["urn:inbuxa:jmap"] = json!({}); + let (upload, activate, _keep) = mock_sieve_export( + &mut server, + session.to_string(), + Matcher::Exact("require [\"fileinto\", \"vnd.inbuxa.while\"];\nkeep;\n".into()), + true, + json!(["SieveScript/set", {"accountId":"w"}, "a"]), + ); + let summary = sync::export::run( + common(&archive), + export_cfg_objects(&base, vec![ObjectType::SieveScript]), + ) + .expect("export"); + upload.assert(); + activate.assert(); + assert_eq!(sieve_counts(&summary).created, 1); + let _ = std::fs::remove_file(&archive); +} + #[test] fn export_sieve_keeps_stalwart_names_for_a_target_without_inbuxa_names() { let mut server = mockito::Server::new(); -- 2.54.0