Skip to content
1        10        20        30        40        50        60        70        80        90        100       110       120       130       140       1501use actix_web::{App, http::StatusCode, test, web};2use base64::{Engine as _, engine::general_purpose::STANDARD};3use obsidian_sync::{config::Settings, webdav};4use tempfile::tempdir;56fn authorization() -> String {7    format!("Basic {}", STANDARD.encode("device:secret"))8}910fn app_state(path: std::path::PathBuf) -> web::Data<webdav::AppState> {11    web::Data::new(12        webdav::AppState::new(13            Settings {14                dav_username: "device".to_owned(),15                dav_password: "secret".to_owned(),16            },17            path,18        )19        .expect("construct app state"),20    )21}2223#[actix_web::test]24async fn health_is_unauthenticated() {25    let storage = tempdir().expect("create temporary storage directory");26    let app = test::init_service(27        App::new()28            .wrap(webdav::cors_policy())29            .app_data(app_state(storage.path().to_path_buf()))30            .configure(webdav::configure),31    )32    .await;3334    let response = test::call_service(&app, test::TestRequest::get().uri("/up").to_request()).await;35    assert_eq!(response.status(), StatusCode::OK);36}3738#[actix_web::test]39async fn dav_requires_basic_auth_challenge() {40    let storage = tempdir().expect("create temporary storage directory");41    let app = test::init_service(42        App::new()43            .wrap(webdav::cors_policy())44            .app_data(app_state(storage.path().to_path_buf()))45            .configure(webdav::configure),46    )47    .await;4849    let response = test::call_service(50        &app,51        test::TestRequest::put()52            .uri("/note.md")53            .set_payload("private note")54            .to_request(),55    )56    .await;57    assert_eq!(response.status(), StatusCode::UNAUTHORIZED);58    assert_eq!(59        response60            .headers()61            .get(actix_web::http::header::WWW_AUTHENTICATE)62            .expect("Basic challenge header")63            .to_str()64            .expect("valid challenge header"),65        r#"Basic realm="Obsidian WebDAV", charset="UTF-8""#66    );67}6869#[actix_web::test]70async fn authenticated_put_get_roundtrip() {71    let storage = tempdir().expect("create temporary storage directory");72    let app = test::init_service(73        App::new()74            .wrap(webdav::cors_policy())75            .app_data(app_state(storage.path().to_path_buf()))76            .configure(webdav::configure),77    )78    .await;7980    let put = test::TestRequest::put()81        .uri("/note.md")82        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))83        .set_payload("private note")84        .to_request();85    let response = test::call_service(&app, put).await;86    assert!(87        response.status() == StatusCode::CREATED || response.status() == StatusCode::NO_CONTENT,88        "unexpected PUT status: {}",89        response.status()90    );9192    let get = test::TestRequest::get()93        .uri("/note.md")94        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))95        .to_request();96    let response = test::call_and_read_body(&app, get).await;97    assert_eq!(response.as_ref(), b"private note");98}99100#[actix_web::test]101async fn propfind_lists_webdav_root() {102    let storage = tempdir().expect("create temporary storage directory");103    let app = test::init_service(104        App::new()105            .wrap(webdav::cors_policy())106            .app_data(app_state(storage.path().to_path_buf()))107            .configure(webdav::configure),108    )109    .await;110111    let request = test::TestRequest::default()112        .method(actix_web::http::Method::from_bytes(b"PROPFIND").expect("PROPFIND method"))113        .uri("/")114        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))115        .insert_header(("Depth", "1"))116        .insert_header(("Content-Type", "application/xml"))117        .set_payload(r#"<propfind xmlns="DAV:"><allprop/></propfind>"#)118        .to_request();119    let response = test::call_service(&app, request).await;120    assert_eq!(response.status().as_u16(), 207);121}122123#[actix_web::test]124async fn cors_preflight_allows_remotely_save_origins_and_headers() {125    let storage = tempdir().expect("create temporary storage directory");126    let app = test::init_service(127        App::new()128            .wrap(webdav::cors_policy())129            .app_data(app_state(storage.path().to_path_buf()))130            .configure(webdav::configure),131    )132    .await;133134    for origin in [135        "app://obsidian.md",136        "capacitor://localhost",137        "http://localhost",138    ] {139        let request = test::TestRequest::default()140            .method(actix_web::http::Method::OPTIONS)141            .uri("/TestVault/")142            .insert_header(("Origin", origin))143            .insert_header(("Access-Control-Request-Method", "PROPFIND"))144            .insert_header(("Access-Control-Request-Headers", "authorization,depth"))145            .to_request();146        let response = test::call_service(&app, request).await;147148        assert_eq!(response.status(), StatusCode::OK, "origin: {origin}");149        assert_eq!(150            response151                .headers()152                .get("Access-Control-Allow-Origin")153                .expect("allow-origin header")154                .to_str()155                .expect("valid allow-origin header"),156            origin157        );158        assert_eq!(159            response160                .headers()161                .get("Access-Control-Allow-Credentials")162                .expect("allow-credentials header"),163            "true"164        );165        let methods = response166            .headers()167            .get("Access-Control-Allow-Methods")168            .expect("allow-methods header")169            .to_str()170            .expect("valid allow-methods header")171            .to_ascii_uppercase();172        assert!(methods.contains("PROPFIND"), "allowed methods: {methods}");173        let headers = response174            .headers()175            .get("Access-Control-Allow-Headers")176            .expect("allow-headers header")177            .to_str()178            .expect("valid allow-headers header")179            .to_ascii_lowercase();180        assert!(181            headers.contains("authorization"),182            "allowed headers: {headers}"183        );184        assert!(headers.contains("depth"), "allowed headers: {headers}");185    }186}187188#[actix_web::test]189async fn cors_rejects_unlisted_origin() {190    let storage = tempdir().expect("create temporary storage directory");191    let app = test::init_service(192        App::new()193            .wrap(webdav::cors_policy())194            .app_data(app_state(storage.path().to_path_buf()))195            .configure(webdav::configure),196    )197    .await;198199    let request = test::TestRequest::default()200        .method(actix_web::http::Method::OPTIONS)201        .uri("/TestVault/")202        .insert_header(("Origin", "https://not-obsidian.example"))203        .insert_header(("Access-Control-Request-Method", "PROPFIND"))204        .insert_header(("Access-Control-Request-Headers", "authorization,depth"))205        .to_request();206    let response = test::call_service(&app, request).await;207208    assert_eq!(response.status(), StatusCode::BAD_REQUEST);209    assert!(210        !response211            .headers()212            .contains_key("Access-Control-Allow-Origin")213    );214}215216#[actix_web::test]217async fn cors_does_not_bypass_dav_auth() {218    let storage = tempdir().expect("create temporary storage directory");219    let app = test::init_service(220        App::new()221            .wrap(webdav::cors_policy())222            .app_data(app_state(storage.path().to_path_buf()))223            .configure(webdav::configure),224    )225    .await;226227    let request = test::TestRequest::put()228        .uri("/note.md")229        .insert_header(("Origin", "capacitor://localhost"))230        .set_payload("private note")231        .to_request();232    let response = test::call_service(&app, request).await;233234    assert_eq!(response.status(), StatusCode::UNAUTHORIZED);235    assert_eq!(236        response237            .headers()238            .get("Access-Control-Allow-Origin")239            .expect("CORS response header"),240        "capacitor://localhost"241    );242}243244#[actix_web::test]245async fn remotely_save_webdav_flow() {246    let storage = tempdir().expect("create temporary storage directory");247    let app = test::init_service(248        App::new()249            .wrap(webdav::cors_policy())250            .app_data(app_state(storage.path().to_path_buf()))251            .configure(webdav::configure),252    )253    .await;254255    let existence_probe = test::TestRequest::default()256        .method(actix_web::http::Method::from_bytes(b"PROPFIND").expect("PROPFIND method"))257        .uri("/TestVault/")258        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))259        .insert_header(("Origin", "capacitor://localhost"))260        .insert_header(("Depth", "0"))261        .insert_header(("Content-Type", "application/xml"))262        .set_payload(r#"<propfind xmlns="DAV:"><allprop/></propfind>"#)263        .to_request();264    assert_eq!(265        test::call_service(&app, existence_probe).await.status(),266        StatusCode::NOT_FOUND267    );268269    let create_vault = test::TestRequest::default()270        .method(actix_web::http::Method::from_bytes(b"MKCOL").expect("MKCOL method"))271        .uri("/TestVault/")272        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))273        .insert_header(("Origin", "capacitor://localhost"))274        .to_request();275    assert_eq!(276        test::call_service(&app, create_vault).await.status(),277        StatusCode::CREATED278    );279280    let options = test::TestRequest::default()281        .method(actix_web::http::Method::OPTIONS)282        .uri("/TestVault/")283        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))284        .insert_header(("Origin", "capacitor://localhost"))285        .to_request();286    let response = test::call_service(&app, options).await;287    assert_eq!(response.status(), StatusCode::OK);288    assert!(response.headers().contains_key("DAV"));289    assert!(290        response291            .headers()292            .get("Access-Control-Expose-Headers")293            .expect("expose headers")294            .to_str()295            .expect("valid expose header")296            .to_ascii_lowercase()297            .contains("dav")298    );299300    let listing = test::TestRequest::default()301        .method(actix_web::http::Method::from_bytes(b"PROPFIND").expect("PROPFIND method"))302        .uri("/TestVault/")303        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))304        .insert_header(("Origin", "capacitor://localhost"))305        .insert_header(("Depth", "1"))306        .insert_header(("Content-Type", "application/xml"))307        .set_payload(r#"<propfind xmlns="DAV:"><allprop/></propfind>"#)308        .to_request();309    assert_eq!(310        test::call_service(&app, listing).await.status().as_u16(),311        207312    );313314    let create_notes = test::TestRequest::default()315        .method(actix_web::http::Method::from_bytes(b"MKCOL").expect("MKCOL method"))316        .uri("/TestVault/notes/")317        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))318        .insert_header(("Origin", "capacitor://localhost"))319        .to_request();320    assert_eq!(321        test::call_service(&app, create_notes).await.status(),322        StatusCode::CREATED323    );324325    let put = test::TestRequest::put()326        .uri("/TestVault/notes/today.md")327        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))328        .insert_header(("Origin", "capacitor://localhost"))329        .set_payload("from Remotely Save profile")330        .to_request();331    assert_eq!(332        test::call_service(&app, put).await.status(),333        StatusCode::CREATED334    );335336    let copy_file = test::TestRequest::default()337        .method(actix_web::http::Method::from_bytes(b"COPY").expect("COPY method"))338        .uri("/TestVault/notes/today.md")339        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))340        .insert_header(("Origin", "capacitor://localhost"))341        .insert_header(("Destination", "http://localhost/TestVault/notes/copy.md"))342        .insert_header(("Overwrite", "T"))343        .to_request();344    let response = test::call_service(&app, copy_file).await;345    assert_eq!(response.status(), StatusCode::CREATED);346347    let copied = test::TestRequest::get()348        .uri("/TestVault/notes/copy.md")349        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))350        .insert_header(("Origin", "capacitor://localhost"))351        .to_request();352    let body = test::call_and_read_body(&app, copied).await;353    assert_eq!(body.as_ref(), b"from Remotely Save profile");354355    let proppatch = test::TestRequest::default()356        .method(actix_web::http::Method::from_bytes(b"PROPPATCH").expect("PROPPATCH method"))357        .uri("/TestVault/notes/copy.md")358        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))359        .insert_header(("Origin", "capacitor://localhost"))360        .insert_header(("Content-Type", "application/xml"))361        .set_payload(362            r#"<propertyupdate xmlns="DAV:" xmlns:X="urn:example"><set><prop><X:color>blue</X:color></prop></set></propertyupdate>"#,363        )364        .to_request();365    let response = test::call_service(&app, proppatch).await;366    assert_eq!(response.status().as_u16(), 207);367368    let lock = test::TestRequest::default()369        .method(actix_web::http::Method::from_bytes(b"LOCK").expect("LOCK method"))370        .uri("/TestVault/notes/copy.md")371        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))372        .insert_header(("Origin", "capacitor://localhost"))373        .insert_header(("Depth", "0"))374        .insert_header(("Timeout", "Second-600"))375        .insert_header(("Content-Type", "application/xml"))376        .set_payload(377            r#"<lockinfo xmlns="DAV:"><lockscope><exclusive/></lockscope><locktype><write/></locktype><owner><href>device</href></owner></lockinfo>"#,378        )379        .to_request();380    let response = test::call_service(&app, lock).await;381    assert_eq!(response.status(), StatusCode::OK);382    let lock_token = response383        .headers()384        .get("Lock-Token")385        .expect("lock token")386        .clone();387388    let unlock = test::TestRequest::default()389        .method(actix_web::http::Method::from_bytes(b"UNLOCK").expect("UNLOCK method"))390        .uri("/TestVault/notes/copy.md")391        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))392        .insert_header(("Origin", "capacitor://localhost"))393        .insert_header(("Lock-Token", lock_token))394        .to_request();395    assert_eq!(396        test::call_service(&app, unlock).await.status(),397        StatusCode::NO_CONTENT398    );399400    let move_file = test::TestRequest::default()401        .method(actix_web::http::Method::from_bytes(b"MOVE").expect("MOVE method"))402        .uri("/TestVault/notes/today.md")403        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))404        .insert_header(("Origin", "capacitor://localhost"))405        .insert_header(("Destination", "http://localhost/TestVault/notes/moved.md"))406        .insert_header(("Overwrite", "T"))407        .to_request();408    let response = test::call_service(&app, move_file).await;409    assert!(410        response.status() == StatusCode::CREATED || response.status() == StatusCode::NO_CONTENT,411        "unexpected MOVE status: {}",412        response.status()413    );414415    let get = test::TestRequest::get()416        .uri("/TestVault/notes/moved.md")417        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))418        .insert_header(("Origin", "capacitor://localhost"))419        .to_request();420    let body = test::call_and_read_body(&app, get).await;421    assert_eq!(body.as_ref(), b"from Remotely Save profile");422423    let delete = test::TestRequest::delete()424        .uri("/TestVault/notes/moved.md")425        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))426        .insert_header(("Origin", "capacitor://localhost"))427        .to_request();428    assert_eq!(429        test::call_service(&app, delete).await.status(),430        StatusCode::NO_CONTENT431    );432}433434#[actix_web::test]435async fn dead_properties_are_process_local() {436    let storage = tempdir().expect("create temporary storage directory");437    let app = test::init_service(438        App::new()439            .wrap(webdav::cors_policy())440            .app_data(app_state(storage.path().to_path_buf()))441            .configure(webdav::configure),442    )443    .await;444445    let put = test::TestRequest::put()446        .uri("/note.md")447        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))448        .set_payload("persistent file")449        .to_request();450    assert_eq!(451        test::call_service(&app, put).await.status(),452        StatusCode::CREATED453    );454455    let proppatch = test::TestRequest::default()456        .method(actix_web::http::Method::from_bytes(b"PROPPATCH").expect("PROPPATCH method"))457        .uri("/note.md")458        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))459        .insert_header(("Content-Type", "application/xml"))460        .set_payload(461            r#"<propertyupdate xmlns="DAV:" xmlns:X="urn:example"><set><prop><X:color>blue</X:color></prop></set></propertyupdate>"#,462        )463        .to_request();464    assert_eq!(465        test::call_service(&app, proppatch).await.status().as_u16(),466        207467    );468469    let get_prop = || {470        test::TestRequest::default()471            .method(actix_web::http::Method::from_bytes(b"PROPFIND").expect("PROPFIND method"))472            .uri("/note.md")473            .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))474            .insert_header(("Depth", "0"))475            .insert_header(("Content-Type", "application/xml"))476            .set_payload(477                r#"<propfind xmlns="DAV:" xmlns:X="urn:example"><prop><X:color/></prop></propfind>"#,478            )479            .to_request()480    };481    let body = test::call_and_read_body(&app, get_prop()).await;482    assert!(483        String::from_utf8_lossy(&body).contains("blue"),484        "custom property was not returned: {}",485        String::from_utf8_lossy(&body)486    );487    drop(app);488489    let restarted_app = test::init_service(490        App::new()491            .wrap(webdav::cors_policy())492            .app_data(app_state(storage.path().to_path_buf()))493            .configure(webdav::configure),494    )495    .await;496    let body = test::call_and_read_body(&restarted_app, get_prop()).await;497    assert!(498        !String::from_utf8_lossy(&body).contains("blue"),499        "custom property should clear after restart: {}",500        String::from_utf8_lossy(&body)501    );502503    let get_file = test::TestRequest::get()504        .uri("/note.md")505        .insert_header((actix_web::http::header::AUTHORIZATION, authorization()))506        .to_request();507    let body = test::call_and_read_body(&restarted_app, get_file).await;508    assert_eq!(body.as_ref(), b"persistent file");509}