/* * SPDX-FileCopyrightText: 2020 Stalwart Labs LLC * * SPDX-License-Identifier: Apache-2.0 OR MIT */ use encodify::base64::{LENIENT, STANDARD}; use inbuxa_migrate::archive::sources::SourceKind; use inbuxa_migrate::exchange::ews::EwsClient; use inbuxa_migrate::exchange::ews::autodiscover::{Autodiscover, DiscoverySource}; use inbuxa_migrate::exchange::ews::error::EwsError; use inbuxa_migrate::exchange::ews::parse::{ EnvelopeKind, FindFolderResponse, FindItemResponse, GetAttachmentInline, ResponseMessage, SyncFolderItemsResponse, }; use inbuxa_migrate::exchange::ews::types::{ FolderClass, FolderId, ItemId, ResponseCode, ServerVersion, }; use inbuxa_migrate::exchange::ews::xml::{EwsRequest, FolderRef, ItemShape, Traversal}; use inbuxa_migrate::logging::{Level, Logger}; use inbuxa_migrate::net::auth::Auth; use inbuxa_migrate::net::http::{ClientOptions, TlsPolicy}; use inbuxa_migrate::net::retry::RetryPolicy; use mockito::Matcher; const TXT_XML: &str = "text/xml; charset=utf-8"; const APP_JSON: &str = "application/json"; fn client(retries: u32) -> EwsClient { EwsClient::new( Auth::Bearer { token: "t".to_owned(), }, ClientOptions { retry: RetryPolicy::new(retries), tls: TlsPolicy::Verify, logger: Logger::new(Level::Quiet), }, ) } const NS: &str = " xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" \ xmlns:t=\"http://schemas.microsoft.com/exchange/services/2006/types\" \ xmlns:m=\"http://schemas.microsoft.com/exchange/services/2006/messages\""; fn envelope(inner: &str) -> String { format!( "{inner}" ) } #[test] fn autodiscover_v2_returns_global_endpoint() { let mut server = mockito::Server::new(); let _m = server .mock("GET", "/autodiscover/autodiscover.json") .match_query(Matcher::AllOf(vec![ Matcher::UrlEncoded("Email".into(), "alice@contoso.com".into()), Matcher::UrlEncoded("Protocol".into(), "Ews".into()), ])) .with_status(200) .with_header("content-type", APP_JSON) .with_body(r#"{"Protocol":"Ews","Url":"https://outlook.office365.com/EWS/Exchange.asmx"}"#) .create(); let _ = server; let url = "https://outlook.office365.com/EWS/Exchange.asmx"; assert!(Autodiscover::is_fully_qualified_ews_url(url)); let r = Autodiscover::new(ClientOptions { retry: RetryPolicy::new(0), tls: TlsPolicy::Verify, logger: Logger::new(Level::Quiet), }) .discover(Some(url), None, None) .unwrap(); assert_eq!(r.source, DiscoverySource::SuppliedUrl); assert_eq!(r.ews_url, url); } #[test] fn find_folder_pagination_and_classification() { let body = format!( "\ NoError\ \ \ \ IPF.NoteInbox\ \ IPF.AppointmentCalendar\ \ IPF.ContactContacts\ " ); let parsed = FindFolderResponse::parse(envelope(&body).as_bytes()).unwrap(); assert_eq!(parsed.folders.len(), 3); let inbox = &parsed.folders[0]; assert_eq!(inbox.folder_id.id, "FMAIL"); assert_eq!(inbox.folder_class, "IPF.Note"); } #[test] fn find_folder_classifies_workmail_folder_class_set() { let body = format!( "\ NoError\ \ IPF.NoteInbox\ SyncFolder\ IPF.Note.OutlookHomepageRSS Feeds\ IPF.ConfigurationQuick Step Settings\ IPF.TaskTasks\ IPF.AppointmentCalendar\ IPF.ContactContacts\ " ); let parsed = FindFolderResponse::parse(envelope(&body).as_bytes()).unwrap(); let by_id = |id: &str| { parsed .folders .iter() .find(|f| f.folder_id.id == id) .map(|f| FolderClass::from_ipf(&f.folder_class)) .unwrap() }; assert_eq!(by_id("INBOX"), FolderClass::Mail); assert_eq!( by_id("CUSTOM"), FolderClass::Mail, "a user folder with no FolderClass must import as mail, never be dropped" ); assert_eq!( by_id("RSS"), FolderClass::Skipped, "RSS Feeds (IPF.Note.OutlookHomepage) holds feed posts, not mail" ); assert_eq!(by_id("CFG"), FolderClass::Skipped); assert_eq!(by_id("TASKS"), FolderClass::Skipped); assert_eq!(by_id("CAL"), FolderClass::Calendar); assert_eq!(by_id("CON"), FolderClass::Contacts); } #[test] fn find_item_distribution_list_does_not_clobber_preceding_contact() { let body = envelope(&format!( "\ NoError\ \ \ \ \ " )); let parsed = FindItemResponse::parse(body.as_bytes()).unwrap(); assert_eq!( parsed.items.len(), 3, "DistributionList must be its own item, not clobber C2" ); let ids: Vec<&str> = parsed.items.iter().map(|i| i.id.id.as_str()).collect(); assert_eq!(ids, ["C1", "C2", "DL1"]); assert_eq!( parsed.items[2].element.to_ascii_lowercase(), "distributionlist" ); } #[test] fn find_item_offset_loop_terminates_on_includes_last_true() { let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let page1 = envelope(&format!( "\ NoError\ \ \ \ " )); let page2 = envelope(&format!( "\ NoError\ \ \ \ " )); let _m1 = server .mock("POST", "/EWS/Exchange.asmx") .match_body(Matcher::Regex("Offset=\"0\"".into())) .with_status(200) .with_header("content-type", TXT_XML) .with_body(page1) .create(); let _m2 = server .mock("POST", "/EWS/Exchange.asmx") .match_body(Matcher::Regex("Offset=\"2\"".into())) .with_status(200) .with_header("content-type", TXT_XML) .with_body(page2) .create(); let c = client(0); let folder = FolderId::new("FID", "FCK"); let body0 = EwsRequest::FindItem { parent: FolderRef::Concrete(&folder), traversal: Traversal::Shallow, offset: 0, page_size: 2, }; let r0 = c.call(&url, &body0).unwrap(); let parsed0 = FindItemResponse::parse(&r0.body).unwrap(); assert_eq!(parsed0.items.len(), 2); assert!(parsed0.more); let body1 = EwsRequest::FindItem { parent: FolderRef::Concrete(&folder), traversal: Traversal::Shallow, offset: 2, page_size: 2, }; let r1 = c.call(&url, &body1).unwrap(); let parsed1 = FindItemResponse::parse(&r1.body).unwrap(); assert_eq!(parsed1.items.len(), 2); assert!(!parsed1.more); } #[test] fn get_item_mixed_success_and_per_item_error() { let body = envelope(&format!( "\ \ NoError\ \ S\ SGVsbG8=\ \ \ ErrorItemNotFound\ not found\ \ " )); let r = ResponseMessage::parse_all(body.as_bytes(), "GetItemResponseMessage").unwrap(); assert_eq!(r.len(), 2); assert!(r[0].success); assert!(r[0].inner_xml.contains("SGVsbG8=")); assert!(!r[1].success); assert!(matches!(r[1].response_code, ResponseCode::ItemNotFound)); } #[test] fn server_busy_with_back_off_triggers_retry_and_eventually_succeeds() { let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let busy_body = format!( "\ soap:Serverbusy\ ErrorServerBusy\ 200\ " ); let ok_body = envelope(&format!( "\ NoError\ \ " )); let _m1 = server .mock("POST", "/EWS/Exchange.asmx") .with_status(200) .with_header("content-type", TXT_XML) .with_body(busy_body) .expect(1) .create(); let _m2 = server .mock("POST", "/EWS/Exchange.asmx") .with_status(200) .with_header("content-type", TXT_XML) .with_body(ok_body) .expect_at_least(1) .create(); let c = client(3); let body = EwsRequest::FindFolder { parent: FolderRef::Distinguished( inbuxa_migrate::exchange::ews::types::DistinguishedFolderId::MsgFolderRoot, ), traversal: Traversal::Deep, }; let r = c .call(&url, &body) .expect("call should retry past busy fault"); let parsed = FindFolderResponse::parse(&r.body).unwrap(); assert!(parsed.folders.is_empty()); assert!(c.stats().retries_observed >= 1); } #[test] fn http_401_surfaces_as_auth_error() { let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let _m = server .mock("POST", "/EWS/Exchange.asmx") .with_status(401) .with_header("www-authenticate", "Bearer error=\"invalid_token\"") .with_body("unauthorized") .create(); let c = client(0); let body = EwsRequest::FindFolder { parent: FolderRef::Distinguished( inbuxa_migrate::exchange::ews::types::DistinguishedFolderId::MsgFolderRoot, ), traversal: Traversal::Deep, }; let err = c.call(&url, &body).unwrap_err(); assert!(matches!(err, EwsError::Auth(_)), "got {err:?}"); } #[test] fn mime_content_round_trips_through_base64_decode() { let original = b"From: alice@x\r\nSubject: hi\r\n\r\nbody"; let encoded = STANDARD.encode(original); let body = envelope(&format!( "\ \ NoError\ \ hi\ {encoded}\ \ " )); let r = ResponseMessage::parse_all(body.as_bytes(), "GetItemResponseMessage").unwrap(); let item = inbuxa_migrate::exchange::ews::parse::MessageItem::parse(&r[0].inner_xml).unwrap(); let s = item.mime_content.unwrap(); let bytes = LENIENT.decode(&s).unwrap(); assert_eq!(bytes, original); } #[test] fn get_attachment_inline_decodes_photo_blob() { let body = envelope(&format!( "\ \ NoError\ \ p.png\ image/png\ true\ iVBORw0KGgo=\ " )); let items = GetAttachmentInline::parse_all(body.as_bytes()).unwrap(); assert_eq!(items.len(), 1); assert!(items[0].is_contact_photo); assert_eq!(items[0].content_base64, "iVBORw0KGgo="); assert_eq!(items[0].content_type.as_deref(), Some("image/png")); } #[test] fn calendar_item_master_inlines_modified_and_deleted_occurrences() { let body = "\ \ \ Daily\ uid-1\ 2025-06-15T14:00:00Z\ 2025-06-15T15:00:00Z\ RecurringMaster\ \ 1\ 2025-06-153\ \ \ 2025-06-16T15:00:00Z2025-06-16T16:30:00Z2025-06-16T14:00:00Z\ \ \ 2025-06-17T14:00:00Z\ \ "; let item = inbuxa_migrate::exchange::ews::parse::CalendarItemRaw::parse(body).unwrap(); assert_eq!(item.id.id, "M1"); assert_eq!(item.uid.as_deref(), Some("uid-1")); assert_eq!(item.modified_occurrences.len(), 1); assert_eq!( item.modified_occurrences[0].item_id.id, "OCC1", "occurrence ItemId must populate the occurrence, not overwrite the master" ); assert_eq!(item.deleted_occurrences.len(), 1); } #[test] fn organizer_and_attendee_addresses_survive_an_ex_routing_type() { let body = "\ \ \ Review\ uid-3\ 2025-06-15T14:00:00Z\ 2025-06-15T15:00:00Z\ Alice\ alice@example.com\ EXMailbox\ Kristina Morgental\ /o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=bdc77b18152647a29d28ce1188376dc9-kristina\ EXUnknown\ "; let item = inbuxa_migrate::exchange::ews::parse::CalendarItemRaw::parse(body).unwrap(); let event = item.to_jscalendar(); assert_eq!( event.data["organizerCalendarAddress"], "mailto:alice@example.com", "a usable address must be kept even when RoutingType says EX" ); let participants = event.data["participants"].as_object().unwrap(); let organizer = participants .values() .find(|p| p["calendarAddress"] == "mailto:alice@example.com") .expect("organizer participant"); assert_eq!(organizer["email"], "alice@example.com"); let attendee = participants .values() .find(|p| p["name"] == "Kristina Morgental") .expect("attendee participant"); assert!( attendee["calendarAddress"] .as_str() .unwrap() .starts_with("urn:x-inbuxa-migrate:attendee:"), "a legacy directory reference cannot be resolved and stays synthetic" ); assert!( attendee.get("email").is_none(), "no email is invented for a directory reference" ); } #[test] fn recurrence_end_date_with_a_timezone_offset_yields_a_bounded_series() { let body = "\ \ \ Biweekly\ uid-2\ 2021-08-04T21:15:00Z\ 2021-08-04T22:15:00Z\ RecurringMaster\ \ 2Wednesday\ 2021-08-04-06:002021-09-30-06:00\ \ "; let item = inbuxa_migrate::exchange::ews::parse::CalendarItemRaw::parse(body).unwrap(); let event = item.to_jscalendar(); let rule = &event.data["recurrenceRule"]; assert_eq!(rule["frequency"], "weekly"); assert_eq!( rule["until"], "2021-09-30T23:59:59", "an EndDate carrying a UTC offset must still produce a valid LocalDateTime" ); } #[test] fn sync_folder_items_creates_updates_deletes_round_trip() { let body = envelope(&format!( "\ NoError\ STATE-1\ true\ \ \ \ \ \ true\ " )); let parsed = SyncFolderItemsResponse::parse(body.as_bytes()).unwrap(); assert_eq!(parsed.sync_state, "STATE-1"); assert!(!parsed.more); assert_eq!(parsed.changes.len(), 5); } #[test] fn invalid_sync_state_data_fault_is_surfaced() { let body = format!( "\ soap:Serverbad state\ ErrorInvalidSyncStateData\ " ); let env = EnvelopeKind::read(body.as_bytes()).unwrap(); match env { EnvelopeKind::Fault { fault, .. } => { assert!(matches!( fault.response_code, ResponseCode::InvalidSyncStateData )); } _ => panic!("expected fault"), } } #[test] fn coordinator_diff_classifies_new_vanished_changed_unchanged() { use inbuxa_migrate::archive::LocalId; use inbuxa_migrate::archive::ids::ews::ItemRow; use inbuxa_migrate::import::ews::items::{DiffPlan, EnumeratedItem}; let server = vec![ EnumeratedItem { element: "Message".into(), id: ItemId::new("A", "ck-1"), item_class: None, }, EnumeratedItem { element: "Message".into(), id: ItemId::new("B", "ck-2"), item_class: None, }, EnumeratedItem { element: "Message".into(), id: ItemId::new("C", "ck-1"), item_class: None, }, ]; let local = vec![ ItemRow { item_id: "A".into(), change_key: "ck-1".into(), local_id: LocalId::from_raw(1), }, ItemRow { item_id: "B".into(), change_key: "ck-1".into(), local_id: LocalId::from_raw(2), }, ItemRow { item_id: "Z".into(), change_key: "ck-9".into(), local_id: LocalId::from_raw(99), }, ]; let plan = DiffPlan::full(&server, &local); assert_eq!(plan.new.len(), 1); assert_eq!(plan.new[0].id, "C"); assert_eq!(plan.present_unchanged, 1); assert_eq!(plan.present_changed.len(), 1); assert_eq!(plan.present_changed[0].0.id, "B"); assert_eq!(plan.vanished.len(), 1); assert_eq!(plan.vanished[0].0, "Z"); } #[test] fn source_change_protection_refuses_different_account() { use inbuxa_migrate::archive::Archive; use inbuxa_migrate::archive::sources::SourceKey; let db = Archive::open_in_memory().unwrap(); let sources = db.store().sources(); let key = SourceKey { kind: SourceKind::ExchangeEws, url: "https://outlook.office365.com/EWS/Exchange.asmx", account_id: "alice@contoso.com", }; sources.upsert(&key, None, "alice@contoso.com").unwrap(); let conflict = sources .conflicting(&SourceKey { kind: SourceKind::ExchangeEws, url: "https://outlook.office365.com/EWS/Exchange.asmx", account_id: "bob@contoso.com", }) .unwrap(); assert!(conflict.is_some(), "different account_id must conflict"); } #[test] fn mailbox_kinds_are_three_separate_sources() { use inbuxa_migrate::archive::Archive; use inbuxa_migrate::archive::sources::SourceKey; let db = Archive::open_in_memory().unwrap(); let sources = db.store().sources(); let primary = SourceKey { kind: SourceKind::ExchangeEws, url: "https://x/EWS/Exchange.asmx", account_id: "alice@contoso.com", }; let archive = SourceKey { kind: SourceKind::ExchangeEws, url: "https://x/EWS/Exchange.asmx", account_id: "alice@contoso.com#archive", }; let public = SourceKey { kind: SourceKind::ExchangeEws, url: "https://x/EWS/Exchange.asmx", account_id: "__public_folders__@contoso.com", }; let p = sources.upsert(&primary, None, "alice@contoso.com").unwrap(); let a = sources.upsert(&archive, None, "alice@contoso.com").unwrap(); let q = sources.upsert(&public, None, "alice@contoso.com").unwrap(); assert_ne!(p, a); assert_ne!(p, q); assert_ne!(a, q); } #[test] fn get_item_batches_chunk_the_id_list() { let ids: Vec = (0..7).map(|i| ItemId::new(format!("I{i}"), "K")).collect(); let chunks: Vec<&[ItemId]> = ids.chunks(3).collect(); assert_eq!(chunks.len(), 3); for chunk in &chunks { let body = EwsRequest::GetItem { shape: ItemShape::Message, ids: chunk, version: ServerVersion::Exchange2013Sp1, } .body(); assert!(body.contains("")); for id in *chunk { assert!(body.contains(&format!("Id=\"{}\"", id.id))); } } } #[test] fn sync_folder_items_request_body_carries_state_and_max() { let folder = FolderId::new("FID", "FCK"); let body = EwsRequest::SyncFolderItems { folder: &folder, sync_state: "OPAQUE", max_changes: 512, version: ServerVersion::Exchange2013Sp1, } .body(); assert!(body.contains("OPAQUE")); assert!(body.contains("512")); assert!(body.contains("NormalItems")); assert!(body.contains("")); } #[test] fn get_attachment_body_carries_inline_request() { let body = EwsRequest::GetAttachment { ids: &["A1", "A2"] }.body(); assert!(body.contains("")); assert!(body.contains("")); assert!(body.contains("true")); } #[test] fn http_500_with_server_busy_body_is_treated_as_fault_and_retried() { let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let busy_body = format!( "\ soap:Serverbusy\ ErrorServerBusy\ 100\ " ); let ok_body = envelope(&format!( "\ NoError\ \ " )); let _m1 = server .mock("POST", "/EWS/Exchange.asmx") .with_status(500) .with_header("content-type", TXT_XML) .with_body(busy_body) .expect(1) .create(); let _m2 = server .mock("POST", "/EWS/Exchange.asmx") .with_status(200) .with_header("content-type", TXT_XML) .with_body(ok_body) .create(); let c = client(3); let body = EwsRequest::FindFolder { parent: FolderRef::Distinguished( inbuxa_migrate::exchange::ews::types::DistinguishedFolderId::MsgFolderRoot, ), traversal: Traversal::Deep, }; let r = c.call(&url, &body).expect("should retry"); let parsed = FindFolderResponse::parse(&r.body).unwrap(); assert!(parsed.folders.is_empty()); } #[test] fn invalid_server_version_fault_downgrades_and_succeeds() { let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let version_fault = format!( "\ soap:Server\ The specified server version is invalid.\ ErrorInvalidServerVersion\ " ); let ok_body = format!( "\ \ NoError\ \ " ); let m_sp1 = server .mock("POST", "/EWS/Exchange.asmx") .match_body(Matcher::Regex("Exchange2013_SP1".into())) .with_status(500) .with_header("content-type", TXT_XML) .with_body(&version_fault) .expect(1) .create(); let m_2013 = server .mock("POST", "/EWS/Exchange.asmx") .match_body(Matcher::Regex("Exchange2013\"".into())) .with_status(500) .with_header("content-type", TXT_XML) .with_body(&version_fault) .expect(1) .create(); let m_2010 = server .mock("POST", "/EWS/Exchange.asmx") .match_body(Matcher::Regex("Exchange2010_SP2".into())) .with_status(200) .with_header("content-type", TXT_XML) .with_body(&ok_body) .expect_at_least(1) .create(); let c = client(0); assert_eq!(c.server_version(), ServerVersion::Exchange2013Sp1); let body = EwsRequest::FindFolder { parent: FolderRef::Distinguished( inbuxa_migrate::exchange::ews::types::DistinguishedFolderId::MsgFolderRoot, ), traversal: Traversal::Deep, }; let r = c .call(&url, &body) .expect("call should walk the version ladder down to a version the server accepts"); let parsed = FindFolderResponse::parse(&r.body).unwrap(); assert!(parsed.folders.is_empty()); assert_eq!(c.server_version(), ServerVersion::Exchange2010Sp2); assert_eq!(c.stats().retries_observed, 0); m_sp1.assert(); m_2013.assert(); m_2010.assert(); } #[test] fn unsupported_version_floor_surfaces_as_soap_fault() { let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let version_fault = format!( "\ soap:Server\ The specified server version is invalid.\ ErrorInvalidServerVersion\ " ); let _m = server .mock("POST", "/EWS/Exchange.asmx") .with_status(500) .with_header("content-type", TXT_XML) .with_body(&version_fault) .expect_at_least(1) .create(); let c = client(0); let body = EwsRequest::FindFolder { parent: FolderRef::Distinguished( inbuxa_migrate::exchange::ews::types::DistinguishedFolderId::MsgFolderRoot, ), traversal: Traversal::Deep, }; let err = c.call(&url, &body).unwrap_err(); assert!( matches!( err, EwsError::SoapFault { code: ResponseCode::InvalidServerVersion, .. } ), "got {err:?}" ); assert_eq!(c.server_version(), ServerVersion::Exchange2007); } #[test] fn server_affinity_cookie_is_captured_and_resent() { let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let ok_body = envelope(&format!( "\ NoError\ \ " )); let first = server .mock("POST", "/EWS/Exchange.asmx") .match_header("x-preferserveraffinity", "True") .match_header("x-backendoverridecookie", Matcher::Missing) .with_status(200) .with_header("content-type", TXT_XML) .with_header( "set-cookie", "X-BackEndOverrideCookie=AFFIN123; path=/; HttpOnly", ) .with_body(&ok_body) .expect(1) .create(); let second = server .mock("POST", "/EWS/Exchange.asmx") .match_header("x-backendoverridecookie", "AFFIN123") .with_status(200) .with_header("content-type", TXT_XML) .with_body(&ok_body) .expect(1) .create(); let c = client(0); let body = EwsRequest::FindFolder { parent: FolderRef::Distinguished( inbuxa_migrate::exchange::ews::types::DistinguishedFolderId::MsgFolderRoot, ), traversal: Traversal::Deep, }; c.call(&url, &body).expect("first call ok"); c.call(&url, &body).expect("second call ok"); first.assert(); second.assert(); } #[test] fn http_456_surfaces_as_account_locked_auth_error() { let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let _m = server .mock("POST", "/EWS/Exchange.asmx") .with_status(456) .with_body("Account locked. Unlock at https://unlock.example/") .create(); let c = client(0); let body = EwsRequest::FindFolder { parent: FolderRef::Distinguished( inbuxa_migrate::exchange::ews::types::DistinguishedFolderId::MsgFolderRoot, ), traversal: Traversal::Deep, }; let err = c.call(&url, &body).unwrap_err(); match err { EwsError::Auth(m) => assert!(m.contains("locked"), "got {m}"), other => panic!("expected Auth error, got {other:?}"), } } #[test] fn for_each_fetched_item_streams_every_id_across_windows() { use inbuxa_migrate::import::ews::items::{ItemEnumeration, ItemRunCtx, SyncBatch}; use inbuxa_migrate::logging::{Level, Logger}; let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let one_message = envelope(&format!( "\ NoError\ " )); let _m = server .mock("POST", "/EWS/Exchange.asmx") .with_status(200) .with_header("content-type", TXT_XML) .with_body(&one_message) .expect(5) .create(); let c = client(0); let ctx = ItemRunCtx { client: &c, url: &url, owner_smtp: None, batch_size: 1, attachment_batch: 1, connections: 2, enumeration: ItemEnumeration::FindItem, sync_batch: SyncBatch::MAX, logger: Logger::new(Level::Quiet), }; let ids: Vec = (0..5).map(|i| ItemId::new(format!("I{i}"), "K")).collect(); let mut delivered = 0usize; let failed = ctx .for_each_fetched_item(ItemShape::Message, &ids, |msg| { assert!(msg.success); delivered += 1; Ok(()) }) .expect("streaming fetch should succeed"); assert_eq!(delivered, 5, "every id must be delivered exactly once"); assert_eq!(failed, 0); _m.assert(); } #[test] fn warning_response_class_is_treated_as_success_in_mock() { let body = envelope(&format!( "\ \ NoError\ \ " )); let r = ResponseMessage::parse_all(body.as_bytes(), "GetItemResponseMessage").unwrap(); assert!(r[0].success); } #[test] fn default_namespace_envelope_parses_via_resolved_names() { let body = "\ \ \ \ NoError\ \ Inbox\ \ \ "; let parsed = FindFolderResponse::parse(body.as_bytes()).unwrap(); assert_eq!(parsed.folders.len(), 1); assert_eq!(parsed.folders[0].folder_id.id, "D1"); } #[test] fn get_folder_messages_preserve_position_when_one_errors() { let body = envelope(&format!( "\ \ NoError\ Inbox\ \ \ ErrorAccessDenied\ \ \ NoError\ Drafts\ \ " )); let msgs = ResponseMessage::parse_all(body.as_bytes(), "GetFolderResponseMessage").unwrap(); assert_eq!(msgs.len(), 3, "all three messages must be present"); assert!(msgs[0].success); assert!(!msgs[1].success); assert!(msgs[2].success); } fn plan_folders_against_mock( kind: inbuxa_migrate::exchange::ews::types::MailboxKind, mailbox: Option<&str>, body_matcher: Matcher, ) { use inbuxa_migrate::import::ews::folders::FolderPlan; use inbuxa_migrate::logging::{Level, Logger}; let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let find_folder = envelope(&format!( "\ NoError\ \ " )); let get_folder = envelope(&format!( "\ \ NoError\ \ " )); let find = server .mock("POST", "/EWS/Exchange.asmx") .match_header("soapaction", Matcher::Regex("/FindFolder\"$".into())) .match_body(body_matcher.clone()) .with_status(200) .with_header("content-type", TXT_XML) .with_body(find_folder) .expect(1) .create(); let get = server .mock("POST", "/EWS/Exchange.asmx") .match_header("soapaction", Matcher::Regex("/GetFolder\"$".into())) .match_body(body_matcher) .with_status(200) .with_header("content-type", TXT_XML) .with_body(get_folder) .expect_at_least(1) .create(); let plan = FolderPlan::discover(&client(0), &url, kind, mailbox, Logger::new(Level::Quiet)) .expect("folder planning should succeed"); assert_eq!(plan.root_folder_id, "ROOT"); find.assert(); get.assert(); } #[test] fn explicit_mailbox_addresses_distinguished_folders_in_that_mailbox() { // Regression for importing a FullAccess mailbox with Basic auth: folders must name --mailbox. plan_folders_against_mock( inbuxa_migrate::exchange::ews::types::MailboxKind::Primary, Some("bob@corp.example"), Matcher::Regex( "bob@corp.example".into(), ), ); } #[test] fn public_folders_ignore_explicit_mailbox() { // Public folder roots cannot carry a Mailbox element. plan_folders_against_mock( inbuxa_migrate::exchange::ews::types::MailboxKind::PublicFolders, Some("bob@corp.example"), Matcher::Regex( "^(?s)[^@]*[^@]*$".into(), ), ); } // A dry run against an archive of another EWS account fails like the real run (R6). #[test] fn dry_run_reports_source_change_like_the_real_run() { use inbuxa_migrate::archive::Archive; use inbuxa_migrate::archive::sources::{SourceChange, SourceKey}; use inbuxa_migrate::exchange::ews::types::MailboxKind; use inbuxa_migrate::import::ews::{EwsAuth, EwsImport, ItemEnumeration}; use inbuxa_migrate::sync::{RunMode, RunOptions}; const URL: &str = "https://ews.example.invalid/EWS/Exchange.asmx"; let path = tempfile::NamedTempFile::new().unwrap().path().to_owned(); let archive = Archive::open(&path).unwrap(); archive .store() .sources() .upsert( &SourceKey { kind: SourceKind::ExchangeEws, url: URL, account_id: "old@x.com", }, None, "old@x.com", ) .unwrap(); drop(archive); let import = EwsImport { url: Some(URL.to_owned()), mailbox: Some("new@x.com".to_owned()), mailbox_kind: MailboxKind::Primary, auth: EwsAuth::Bearer { token: "t".to_owned(), }, ews_connections: 1, getitem_batch: 10, attachment_batch: 10, enumeration: ItemEnumeration::SyncFolderItems, source_change: SourceChange::Reject, }; let options = RunOptions { archive: path, threads: 1, mode: RunMode::DryRun, max_retries: 0, tls: TlsPolicy::Verify, logger: Logger::new(Level::Quiet), }; let err = import.run(options).into_result().unwrap_err(); assert_eq!(err.exit_status().code(), 4); assert_eq!( err.to_string(), "source-change protection: archive already records exchange_ews source \ (https://ews.example.invalid/EWS/Exchange.asmx, account old@x.com); \ pass --allow-source-change to import a different account" ); } // A vanished message whose row cannot be deleted is counted and kept, the rest are swept (R8). #[test] fn vanished_item_delete_failure_is_counted_and_rolled_back() { use inbuxa_migrate::archive::Archive; use inbuxa_migrate::archive::sources::SourceKey; use inbuxa_migrate::exchange::ews::parse::FolderEntry; use inbuxa_migrate::import::ews::coordinator::EwsCoordinator; use inbuxa_migrate::import::ews::folder_sync::ItemKind; use inbuxa_migrate::import::ews::folders::{ClassifiedFolder, FolderPlan}; use inbuxa_migrate::import::ews::items::{ItemEnumeration, ItemRunCtx, SyncBatch}; use inbuxa_migrate::sync::TypeCounts; use inbuxa_migrate::types::ObjectType; let mut server = mockito::Server::new(); let url = format!("{}/EWS/Exchange.asmx", server.url()); let soap = |server: &mut mockito::Server, action: &str, inner: &str| { server .mock("POST", "/EWS/Exchange.asmx") .match_header("soapaction", Matcher::Regex(format!("/{action}\"$"))) .with_status(200) .with_header("content-type", TXT_XML) .with_body(envelope(inner)) .create() }; let find_items = |ids: &[&str]| { let items: String = ids .iter() .map(|id| { format!( "\ IPM.Note" ) }) .collect(); format!( "\ NoError\ {items}\ ", ids.len() ) }; let mime = |id: &str| { STANDARD.encode(format!("Message-ID: <{id}@x>\r\nSubject: {id}\r\n\r\nbody\r\n").as_bytes()) }; let get_items: String = ["M1", "M2"] .iter() .map(|id| { format!( "NoError\ \ {}\ ", mime(id) ) }) .collect(); soap(&mut server, "FindItem", &find_items(&["M1", "M2"])); soap( &mut server, "GetItem", &format!( "{get_items}" ), ); let archive = Archive::open_in_memory().unwrap(); let source = archive .store() .sources() .upsert( &SourceKey { kind: SourceKind::ExchangeEws, url: &url, account_id: "alice@x.com", }, None, "alice@x.com", ) .unwrap(); let c = client(0); let ctx = EwsCoordinator { store: archive.store(), source, items: ItemRunCtx { client: &c, url: &url, owner_smtp: None, batch_size: 10, attachment_batch: 10, connections: 1, enumeration: ItemEnumeration::FindItem, sync_batch: SyncBatch::MAX, logger: Logger::new(Level::Quiet), }, }; let plan = FolderPlan { mail: vec![ClassifiedFolder { folder: FolderEntry { folder_id: FolderId::new("INBOX", "K"), parent_id: Some("ROOT".to_owned()), display_name: "Inbox".to_owned(), folder_class: "IPF.Note".to_owned(), ..FolderEntry::default() }, kind: FolderClass::Mail, }], calendar: Vec::new(), contacts: Vec::new(), well_known_roles: Default::default(), root_folder_id: "ROOT".to_owned(), }; let mut mailboxes = TypeCounts::default(); let mut emails = TypeCounts::default(); ctx.reconcile_folders(ItemKind::Message, &plan, &mut mailboxes, &mut emails) .unwrap(); ctx.reconcile_items(ItemKind::Message, &plan, &mut emails) .unwrap(); assert_eq!(emails.created, 2); let pinned = ctx .ids() .local_id(ObjectType::Email, "M1") .unwrap() .unwrap(); archive .connection() .execute_batch(&format!( "CREATE TEMP TRIGGER pin BEFORE DELETE ON emails WHEN old.id = {pinned} \ BEGIN SELECT RAISE(ABORT, 'pinned'); END" )) .unwrap(); server.reset(); soap(&mut server, "FindItem", &find_items(&[])); let mut emails = TypeCounts::default(); ctx.reconcile_items(ItemKind::Message, &plan, &mut emails) .unwrap(); assert_eq!((emails.deleted, emails.failed), (1, 1)); assert_eq!( ctx.ids().local_id(ObjectType::Email, "M1").unwrap(), Some(pinned), "the failed row's id mapping must be rolled back with it" ); assert_eq!(ctx.ids().local_id(ObjectType::Email, "M2").unwrap(), None); } // A 2xx device-code poll with an unreadable body reports the body error, as before the rewrite. #[test] fn device_code_poll_with_unreadable_2xx_body_reports_token_body_error() { use inbuxa_migrate::exchange::ews::oauth::SCOPE_DELEGATED; use inbuxa_migrate::exchange::oauth::{Authority, Dialect, OAuthClient}; let mut server = mockito::Server::new(); let _device = server .mock("POST", "/common/oauth2/v2.0/devicecode") .with_status(200) .with_header("content-type", "application/json") .with_body(r#"{"device_code":"DC","user_code":"U","interval":0,"expires_in":30}"#) .create(); let _token = server .mock("POST", "/common/oauth2/v2.0/token") .with_status(200) .with_header("content-type", "text/html") .with_body("proxy") .create(); let authority = Authority::new(format!("{}/common", server.url())); let err = OAuthClient::new( ClientOptions { retry: RetryPolicy::new(0), tls: TlsPolicy::Verify, logger: Logger::new(Level::Quiet), }, &authority, "client-id", SCOPE_DELEGATED, Dialect::Ews, ) .device_code() .unwrap_err(); assert!(err.0.starts_with("token body: "), "got {:?}", err.0); }