Files
inbuxa-migrate/tests/mock_guide.rs
T
jcoffey-dev 6ca40a68b5
ci / test (pull_request) Skipped
github/ci (branch) GitHub Actions
ci / github (pull_request) Successful in 3m31s
ci / announce (pull_request) Skipped
guide: Microsoft 365 and Google Workspace as sources
Microsoft 365 reads every mailbox as an Entra app with a client secret
(--app-only); Check signs the app in and names any Graph application
permission it lacks. Google Workspace reads Gmail as a service account
with domain-wide delegation (--google-key); Check asks Google for a token
for the first account.
2026-10-30 14:50:13 -07:00

205 lines
7.0 KiB
Rust

/*
* SPDX-FileCopyrightText: 2026 Coffey Labs LLC <[email protected]>
*
* SPDX-License-Identifier: Apache-2.0 OR MIT
*/
//! `guide` as the browser sees it: the real binary serving its page on
//! 127.0.0.1, answering only its own address and token, and running
//! `migrate` on the plan it writes. Maildir in, offline; the inbuxa side
//! is a port nothing listens on, so the export fails as it should.
mod integration;
use std::fs;
use integration::guide::{get, post, send, settled, start, start_unheard};
use serde_json::{Value, json};
use tempfile::TempDir;
const BIN: &str = env!("CARGO_BIN_EXE_inbuxa-migrate");
#[test]
fn the_page_answers_only_its_own_address_and_token() {
let dir = TempDir::new().unwrap();
let guide = start(BIN, dir.path());
let (status, headers, page) = get(&guide, "");
assert_eq!(status, 200);
assert!(page.contains("Move to inbuxa"), "{page}");
assert!(headers.contains("Content-Security-Policy: default-src 'self'"));
assert!(headers.contains("Referrer-Policy: no-referrer"));
assert_eq!(get(&guide, "guide.js").0, 200);
let wrong_token = format!("GET /{}x/ HTTP/1.1\r\nHost: {}", guide.token, guide.host);
assert_eq!(send(&guide, &wrong_token, "").0, 404);
let port = guide.host.rsplit(':').next().unwrap();
let rebound = format!(
"GET /{}/ HTTP/1.1\r\nHost: attacker.example:{port}",
guide.token
);
assert_eq!(send(&guide, &rebound, "").0, 403);
let form_post = format!(
"POST /{}/api/job HTTP/1.1\r\nHost: {}\r\nContent-Type: text/plain",
guide.token, guide.host
);
assert_eq!(send(&guide, &form_post, "{}").0, 415);
let elsewhere = format!(
"POST /{}/api/job HTTP/1.1\r\nHost: {}\r\nContent-Type: application/json\r\nOrigin: https://attacker.example",
guide.token, guide.host
);
assert_eq!(send(&guide, &elsewhere, "{}").0, 403);
}
#[test]
fn a_maildir_move_previews_runs_and_reports() {
let dir = TempDir::new().unwrap();
let root = dir.path();
let mail = root.join("mail/ann");
for sub in ["cur", "new", "tmp"] {
fs::create_dir_all(mail.join(sub)).unwrap();
}
fs::write(
mail.join("cur/1:2,S"),
"From: [email protected]\r\nSubject: Hi\r\nMessage-ID: <[email protected]>\r\n\r\nHi\r\n",
)
.unwrap();
let guide = start(BIN, root);
let (_, parsed) = post(
&guide,
"api/accounts",
&json!({"text": "email\n[email protected]\nnot one\n"}),
);
assert_eq!(parsed["accounts"], json!(["[email protected]"]));
assert_eq!(parsed["rejected"], json!(["not one"]));
let form = json!({
"source": {"kind": "maildir", "path": "mail/{local}"},
"accounts": ["[email protected]"],
"create": false,
"target": {"url": "http://127.0.0.1:9", "token": "t0ken-for-the-test"},
"parallel": 2,
});
let (_, found) = post(&guide, "api/check-source", &json!({"form": form}));
assert_eq!(found["ok"], true, "{found}");
let (status, _) = post(&guide, "api/job", &json!({"kind": "preview", "form": form}));
assert_eq!(status, 200);
let state = settled(&guide);
let report = state["job"]["result"]["report"].as_str().unwrap();
assert!(
report.contains("[email protected],import,done,2,2"),
"{report}"
);
assert_eq!(state["job"]["exit"], 0, "{state}");
// The token came once; an empty one keeps it.
let mut again = form.clone();
again["target"]["token"] = json!("");
let (status, _) = post(&guide, "api/job", &json!({"kind": "run", "form": again}));
assert_eq!(status, 200);
let state = settled(&guide);
assert_eq!(state["job"]["exit"], 5, "{state}");
let report = state["job"]["result"]["report"].as_str().unwrap();
assert!(
report.contains("[email protected],import,done,2,2"),
"{report}"
);
assert!(report.contains("[email protected],export,failed"), "{report}");
assert!(
state["command"]
.as_str()
.unwrap()
.contains("export MIGRATE_INBUXA_TOKEN=…"),
"{state}"
);
// The plan is an ordinary one, and nothing written holds the token.
let plan: Value =
serde_json::from_str(&fs::read_to_string(root.join("plan.json")).unwrap()).unwrap();
assert_eq!(
plan["steps"][0]["args"],
json!(["import", "maildir", "mail/{local}", "{archive}"])
);
for name in [
"plan.json",
"preview-plan.json",
"guide.json",
"work/report.csv",
] {
let text = fs::read_to_string(root.join(name)).unwrap();
assert!(!text.contains("t0ken-for-the-test"), "{name}: {text}");
}
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let mode = fs::metadata(root.join("guide.json"))
.unwrap()
.permissions()
.mode();
assert_eq!(mode & 0o777, 0o600);
}
let saved: Value =
serde_json::from_str(&fs::read_to_string(root.join("guide.json")).unwrap()).unwrap();
assert_eq!(saved["form"]["target"]["url"], "http://127.0.0.1:9");
}
#[test]
fn a_closed_stdout_doesnt_end_the_guide() {
let dir = TempDir::new().unwrap();
let port = std::net::TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port();
let guide = start_unheard(BIN, dir.path(), port);
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(30);
while std::net::TcpStream::connect(&guide.host).is_err() {
assert!(
std::time::Instant::now() < deadline,
"the guide never listened"
);
std::thread::sleep(std::time::Duration::from_millis(50));
}
std::thread::sleep(std::time::Duration::from_millis(300));
let head = format!("GET /x/ HTTP/1.1\r\nHost: {}", guide.host);
assert_eq!(send(&guide, &head, "").0, 404);
}
#[test]
fn the_guide_checks_a_google_key_by_asking_for_a_token() {
let mut google = mockito::Server::new();
let (key, _) = integration::google::service_account_key(&format!("{}/token", google.url()));
let _token = google
.mock("POST", "/token")
.match_body(mockito::Matcher::Regex("assertion=".to_owned()))
.with_status(200)
.with_body(r#"{"access_token":"ya29.x","expires_in":3599}"#)
.create();
let dir = TempDir::new().unwrap();
fs::write(dir.path().join("sa.json"), key).unwrap();
let guide = start(BIN, dir.path());
let form = |key_file: &str| {
json!({"form": {
"source": {"kind": "google", "keyFile": key_file},
"accounts": ["[email protected]"],
}})
};
let (_, missing) = post(&guide, "api/check-source", &form("nope.json"));
assert_eq!(missing["ok"], false, "{missing}");
assert!(
missing["message"].as_str().unwrap().contains("nope.json"),
"{missing}"
);
let (_, found) = post(&guide, "api/check-source", &form("sa.json"));
assert_eq!(found["ok"], true, "{found}");
assert!(
found["message"]
.as_str()
.unwrap()
.contains("act for [email protected]"),
"{found}"
);
}