api_pim_io.rs
| 1 | //! Public feeds, import and export of calendars and address books. |
| 2 | |
| 3 | mod common; |
| 4 | |
| 5 | use axum::http::{Method, StatusCode}; |
| 6 | use common::*; |
| 7 | use serde_json::{Value, json}; |
| 8 | |
| 9 | const PW: &str = "secret12345"; |
| 10 | const CAL: &str = "/pim/calendars/alice/default/"; |
| 11 | |
| 12 | struct Io { |
| 13 | env: Env, |
| 14 | alice: Client, |
| 15 | } |
| 16 | |
| 17 | impl Io { |
| 18 | async fn new() -> Self { |
| 19 | let env = Env::new().await; |
| 20 | let admin = env.admin().await; |
| 21 | create_user(&admin, "alice", PW, &[]).await; |
| 22 | create_user(&admin, "bob", PW, &[]).await; |
| 23 | let alice = login(&env, "alice", PW).await; |
| 24 | Io { env, alice } |
| 25 | } |
| 26 | |
| 27 | async fn dav(&self, user: &str, verb: &str, path: &str, body: &str) -> Resp { |
| 28 | let auth = basic(user, PW); |
| 29 | // Without Depth, PROPFIND lists only the collection itself. |
| 30 | Client::new(self.env.app.clone()) |
| 31 | .raw( |
| 32 | Method::from_bytes(verb.as_bytes()).unwrap(), |
| 33 | path, |
| 34 | &[("authorization", &auth), ("depth", "1")], |
| 35 | body.as_bytes().to_vec(), |
| 36 | ) |
| 37 | .await |
| 38 | } |
| 39 | |
| 40 | async fn anon(&self, path: &str, headers: &[(&str, &str)]) -> Resp { |
| 41 | Client::new(self.env.app.clone()) |
| 42 | .raw(Method::GET, path, headers, Vec::new()) |
| 43 | .await |
| 44 | } |
| 45 | |
| 46 | /// The id of alice's collection with this CalDAV/CardDAV URL. |
| 47 | async fn id(&self, url: &str) -> i64 { |
| 48 | let list = self.alice.get("/api/pim/collections").await.json(); |
| 49 | list.as_array() |
| 50 | .unwrap() |
| 51 | .iter() |
| 52 | .find(|c| c["url"] == url) |
| 53 | .unwrap_or_else(|| panic!("no collection {url}: {list}"))["id"] |
| 54 | .as_i64() |
| 55 | .unwrap() |
| 56 | } |
| 57 | |
| 58 | async fn link(&self, id: i64, body: Value) -> String { |
| 59 | let r = self |
| 60 | .alice |
| 61 | .post_json(&format!("/api/pim/collections/{id}/links"), &body) |
| 62 | .await; |
| 63 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 64 | r.json()["path"].as_str().unwrap().to_string() |
| 65 | } |
| 66 | |
| 67 | async fn import(&self, id: i64, body: &str) -> Value { |
| 68 | let r = self |
| 69 | .alice |
| 70 | .raw( |
| 71 | Method::POST, |
| 72 | &format!("/api/pim/collections/{id}/import"), |
| 73 | &[("content-type", "text/calendar")], |
| 74 | body.as_bytes().to_vec(), |
| 75 | ) |
| 76 | .await; |
| 77 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 78 | r.json() |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | fn event(uid: &str, summary: &str, extra: &str) -> String { |
| 83 | format!( |
| 84 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\nBEGIN:VEVENT\r\nUID:{uid}\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20260101T100000Z\r\nSUMMARY:{summary}\r\n{extra}END:VEVENT\r\nEND:VCALENDAR\r\n" |
| 85 | ) |
| 86 | } |
| 87 | |
| 88 | #[tokio::test] |
| 89 | async fn feeds() { |
| 90 | let io = Io::new().await; |
| 91 | let cal = io.id(CAL).await; |
| 92 | let book = io.id("/pim/addressbooks/alice/default/").await; |
| 93 | for uid in ["a", "b"] { |
| 94 | let r = io |
| 95 | .dav( |
| 96 | "alice", |
| 97 | "PUT", |
| 98 | &format!("{CAL}{uid}.ics"), |
| 99 | &event( |
| 100 | uid, |
| 101 | "Secret plan", |
| 102 | "BEGIN:VALARM\r\nACTION:DISPLAY\r\nTRIGGER:-PT5M\r\nEND:VALARM\r\n", |
| 103 | ), |
| 104 | ) |
| 105 | .await; |
| 106 | assert_eq!(r.status, StatusCode::CREATED, "{}", r.text()); |
| 107 | } |
| 108 | |
| 109 | let r = io |
| 110 | .dav( |
| 111 | "alice", |
| 112 | "PUT", |
| 113 | &format!("{CAL}p.ics"), |
| 114 | &event("p", "Doctor", "CLASS:PRIVATE\r\n"), |
| 115 | ) |
| 116 | .await; |
| 117 | assert_eq!(r.status, StatusCode::CREATED); |
| 118 | |
| 119 | let full = io.link(cal, json!({})).await; |
| 120 | assert!( |
| 121 | full.starts_with("/feed/") && full.ends_with(".ics"), |
| 122 | "{full}" |
| 123 | ); |
| 124 | let r = io.anon(&full, &[]).await; |
| 125 | assert_eq!(r.status, StatusCode::OK); |
| 126 | assert!( |
| 127 | r.header("content-type") |
| 128 | .unwrap() |
| 129 | .starts_with("text/calendar") |
| 130 | ); |
| 131 | let text = r.text(); |
| 132 | assert!( |
| 133 | text.contains("UID:a\r\n") && text.contains("UID:b\r\n"), |
| 134 | "{text}" |
| 135 | ); |
| 136 | assert!(text.contains("X-WR-CALNAME:")); |
| 137 | assert!(text.contains("Secret plan")); |
| 138 | // A public link shows private events as busy time only. |
| 139 | assert!( |
| 140 | !text.contains("Doctor") && text.contains("SUMMARY:Busy"), |
| 141 | "{text}" |
| 142 | ); |
| 143 | let etag = r.header("etag").unwrap(); |
| 144 | let r = io.anon(&full, &[("if-none-match", &etag)]).await; |
| 145 | assert_eq!(r.status, StatusCode::NOT_MODIFIED); |
| 146 | // The extension is optional. |
| 147 | let bare = full.trim_end_matches(".ics"); |
| 148 | assert_eq!(io.anon(bare, &[]).await.status, StatusCode::OK); |
| 149 | |
| 150 | // A change gives a new ETag. |
| 151 | io.dav("alice", "PUT", &format!("{CAL}c.ics"), &event("c", "x", "")) |
| 152 | .await; |
| 153 | let r = io.anon(&full, &[("if-none-match", &etag)]).await; |
| 154 | assert_eq!(r.status, StatusCode::OK); |
| 155 | |
| 156 | let busy = io.link(cal, json!({ "busy_only": true })).await; |
| 157 | let text = io.anon(&busy, &[]).await.text(); |
| 158 | assert!(text.contains("SUMMARY:Busy"), "{text}"); |
| 159 | assert!( |
| 160 | !text.contains("Secret plan") && !text.contains("VALARM"), |
| 161 | "{text}" |
| 162 | ); |
| 163 | assert!(!text.contains("UID:a\r\n"), "UIDs are hashed: {text}"); |
| 164 | // The owner's export keeps everything. |
| 165 | let export = io |
| 166 | .alice |
| 167 | .get(&format!("/api/pim/collections/{cal}/export")) |
| 168 | .await |
| 169 | .text(); |
| 170 | assert!(export.contains("Doctor")); |
| 171 | |
| 172 | let locked = io.link(cal, json!({ "password": "feedpass123" })).await; |
| 173 | let r = io.anon(&locked, &[]).await; |
| 174 | assert_eq!(r.status, StatusCode::UNAUTHORIZED); |
| 175 | assert!(r.header("www-authenticate").is_some()); |
| 176 | let wrong = basic("", "nope-nope"); |
| 177 | for _ in 0..3 { |
| 178 | let r = io.anon(&locked, &[("authorization", &wrong)]).await; |
| 179 | assert_eq!(r.status, StatusCode::UNAUTHORIZED); |
| 180 | } |
| 181 | let right = basic("anyone", "feedpass123"); |
| 182 | assert_eq!( |
| 183 | io.anon(&locked, &[("authorization", &right)]).await.status, |
| 184 | StatusCode::OK |
| 185 | ); |
| 186 | |
| 187 | let expired = io |
| 188 | .link(cal, json!({ "expires_at": "2000-01-01T00:00:00Z" })) |
| 189 | .await; |
| 190 | assert_eq!(io.anon(&expired, &[]).await.status, StatusCode::GONE); |
| 191 | |
| 192 | let cards = io.link(book, json!({})).await; |
| 193 | assert!(cards.ends_with(".vcf")); |
| 194 | let r = io.anon(&cards, &[]).await; |
| 195 | assert!(r.header("content-type").unwrap().starts_with("text/vcard")); |
| 196 | let r = io |
| 197 | .alice |
| 198 | .post_json( |
| 199 | &format!("/api/pim/collections/{book}/links"), |
| 200 | &json!({ "busy_only": true }), |
| 201 | ) |
| 202 | .await; |
| 203 | assert_eq!(r.status, StatusCode::BAD_REQUEST); |
| 204 | |
| 205 | // Only the owner sees and manages the links. |
| 206 | let bob = login(&io.env, "bob", PW).await; |
| 207 | assert_eq!( |
| 208 | bob.get(&format!("/api/pim/collections/{cal}/links")) |
| 209 | .await |
| 210 | .status, |
| 211 | StatusCode::NOT_FOUND |
| 212 | ); |
| 213 | let list = io |
| 214 | .alice |
| 215 | .get(&format!("/api/pim/collections/{cal}/links")) |
| 216 | .await |
| 217 | .json(); |
| 218 | assert_eq!(list.as_array().unwrap().len(), 4); |
| 219 | let first = list[0]["id"].as_i64().unwrap(); |
| 220 | let r = io |
| 221 | .alice |
| 222 | .delete(&format!("/api/pim/collections/{cal}/links/{first}")) |
| 223 | .await; |
| 224 | assert_eq!(r.status, StatusCode::OK); |
| 225 | assert_eq!(io.anon(&full, &[]).await.status, StatusCode::NOT_FOUND); |
| 226 | |
| 227 | // A deleted collection takes its links along. |
| 228 | let r = io |
| 229 | .dav("alice", "MKCALENDAR", "/pim/calendars/alice/work/", "") |
| 230 | .await; |
| 231 | assert_eq!(r.status, StatusCode::CREATED); |
| 232 | let work = io.id("/pim/calendars/alice/work/").await; |
| 233 | let gone = io.link(work, json!({})).await; |
| 234 | assert_eq!(io.anon(&gone, &[]).await.status, StatusCode::OK); |
| 235 | io.dav("alice", "DELETE", "/pim/calendars/alice/work/", "") |
| 236 | .await; |
| 237 | assert_eq!(io.anon(&gone, &[]).await.status, StatusCode::NOT_FOUND); |
| 238 | } |
| 239 | |
| 240 | #[tokio::test] |
| 241 | async fn import_splits_and_updates() { |
| 242 | let io = Io::new().await; |
| 243 | let cal = io.id(CAL).await; |
| 244 | let file = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:x\r\nMETHOD:PUBLISH\r\n\ |
| 245 | BEGIN:VEVENT\r\nUID:m\r\nDTSTART:20260105T100000Z\r\nRRULE:FREQ=DAILY;COUNT=3\r\nEND:VEVENT\r\n\ |
| 246 | BEGIN:VEVENT\r\nUID:m\r\nRECURRENCE-ID:20260106T100000Z\r\nDTSTART:20260106T120000Z\r\nEND:VEVENT\r\n\ |
| 247 | BEGIN:VEVENT\r\nDTSTART:20260107T100000Z\r\nSUMMARY:no uid\r\nEND:VEVENT\r\n\ |
| 248 | BEGIN:VEVENT\r\nUID:nostart\r\nSUMMARY:no start\r\nEND:VEVENT\r\n\ |
| 249 | BEGIN:VFREEBUSY\r\nUID:fb\r\nEND:VFREEBUSY\r\nEND:VCALENDAR\r\n"; |
| 250 | let r = io.import(cal, file).await; |
| 251 | assert_eq!(r["created"], 2, "{r}"); |
| 252 | assert_eq!(r["updated"], 0); |
| 253 | assert_eq!(r["skipped_total"], 2); |
| 254 | let reasons: Vec<&str> = r["skipped"] |
| 255 | .as_array() |
| 256 | .unwrap() |
| 257 | .iter() |
| 258 | .map(|s| s["reason"].as_str().unwrap()) |
| 259 | .collect(); |
| 260 | assert!(reasons.contains(&"supported-calendar-component"), "{r}"); |
| 261 | assert!(reasons.contains(&"valid-calendar-data"), "{r}"); |
| 262 | |
| 263 | // The same file again updates, also the event that had no UID. |
| 264 | let r = io.import(cal, file).await; |
| 265 | assert_eq!( |
| 266 | (r["created"].as_i64(), r["updated"].as_i64()), |
| 267 | (Some(0), Some(2)) |
| 268 | ); |
| 269 | |
| 270 | let r = io.dav("alice", "PROPFIND", CAL, "").await; |
| 271 | let listing = r.text(); |
| 272 | assert_eq!(listing.matches(".ics</").count(), 2, "{listing}"); |
| 273 | let export = io |
| 274 | .alice |
| 275 | .get(&format!("/api/pim/collections/{cal}/export")) |
| 276 | .await; |
| 277 | let text = export.text(); |
| 278 | assert!(!text.contains("METHOD")); |
| 279 | assert!(text.contains("RECURRENCE-ID:20260106T100000Z")); |
| 280 | assert!(text.contains("DTSTAMP:"), "import adds DTSTAMP: {text}"); |
| 281 | } |
| 282 | |
| 283 | #[tokio::test] |
| 284 | async fn import_schedules_like_a_put_and_keeps_uniqueness() { |
| 285 | let io = Io::new().await; |
| 286 | let cal = io.id(CAL).await; |
| 287 | let meeting = event( |
| 288 | "meet", |
| 289 | "Meeting", |
| 290 | "ORGANIZER:mailto:alice@dovenest.invalid\r\nATTENDEE:mailto:bob@dovenest.invalid\r\n", |
| 291 | ); |
| 292 | let r = io.import(cal, &meeting).await; |
| 293 | assert_eq!(r["created"], 1, "{r}"); |
| 294 | // Stored as a scheduling object, with a Schedule-Tag like after a PUT. |
| 295 | let listing = io.dav("alice", "PROPFIND", CAL, "").await.text(); |
| 296 | let href = listing |
| 297 | .split("<d:href>") |
| 298 | .filter_map(|s| s.split("</d:href>").next()) |
| 299 | .find(|h| h.ends_with(".ics")) |
| 300 | .unwrap_or_else(|| panic!("{listing}")) |
| 301 | .to_string(); |
| 302 | let r = io.dav("alice", "GET", &href, "").await; |
| 303 | assert!(r.header("schedule-tag").is_some()); |
| 304 | // bob got a copy and the REQUEST, as after a PUT. |
| 305 | for path in ["/pim/calendars/bob/default/", "/pim/calendars/bob/inbox/"] { |
| 306 | let r = io.dav("bob", "PROPFIND", path, "").await; |
| 307 | assert!(r.text().contains(".ics</"), "{path}: {}", r.text()); |
| 308 | } |
| 309 | // One scheduling object per UID and owner (RFC 6638, 3.2.4.1). |
| 310 | io.dav("alice", "MKCALENDAR", "/pim/calendars/alice/work/", "") |
| 311 | .await; |
| 312 | let work = io.id("/pim/calendars/alice/work/").await; |
| 313 | let r = io.import(work, &meeting).await; |
| 314 | assert_eq!(r["created"], 0); |
| 315 | assert_eq!(r["skipped"][0]["uid"], "meet"); |
| 316 | assert_eq!( |
| 317 | r["skipped"][0]["reason"], |
| 318 | "unique-scheduling-object-resource" |
| 319 | ); |
| 320 | |
| 321 | // Imported again without bob, the meeting is cancelled for him. |
| 322 | let r = io.import(cal, &event("meet", "Meeting", "")).await; |
| 323 | assert_eq!(r["updated"], 1, "{r}"); |
| 324 | let copy = io |
| 325 | .dav("bob", "PROPFIND", "/pim/calendars/bob/default/", "") |
| 326 | .await |
| 327 | .text(); |
| 328 | let copy = copy |
| 329 | .split("<d:href>") |
| 330 | .filter_map(|s| s.split("</d:href>").next()) |
| 331 | .find(|h| h.ends_with(".ics")) |
| 332 | .unwrap_or_else(|| panic!("{copy}")) |
| 333 | .to_string(); |
| 334 | let r = io.dav("bob", "GET", ©, "").await; |
| 335 | assert!(r.text().contains("STATUS:CANCELLED"), "{}", r.text()); |
| 336 | |
| 337 | // A read-only loan cannot be imported into. |
| 338 | let bob = login(&io.env, "bob", PW).await; |
| 339 | io.alice |
| 340 | .post_json( |
| 341 | &format!("/api/pim/collections/{cal}/shares"), |
| 342 | &json!({ "user": "bob", "mode": "ro" }), |
| 343 | ) |
| 344 | .await; |
| 345 | let r = bob |
| 346 | .raw( |
| 347 | Method::POST, |
| 348 | &format!("/api/pim/collections/{cal}/import"), |
| 349 | &[("content-type", "text/calendar")], |
| 350 | event("x", "x", "").into_bytes(), |
| 351 | ) |
| 352 | .await; |
| 353 | assert_eq!(r.status, StatusCode::FORBIDDEN); |
| 354 | // But exported. |
| 355 | let r = bob.get(&format!("/api/pim/collections/{cal}/export")).await; |
| 356 | assert_eq!(r.status, StatusCode::OK); |
| 357 | } |
| 358 | |
| 359 | #[tokio::test] |
| 360 | async fn export_round_trip() { |
| 361 | let io = Io::new().await; |
| 362 | let cal = io.id(CAL).await; |
| 363 | io.import(cal, &event("r1", "One", "")).await; |
| 364 | io.import(cal, &event("r2", "Two", "")).await; |
| 365 | |
| 366 | let url = format!("/api/pim/collections/{cal}/export"); |
| 367 | let r = io.alice.get(&url).await; |
| 368 | assert_eq!(r.status, StatusCode::OK); |
| 369 | let disposition = r.header("content-disposition").unwrap(); |
| 370 | assert!(disposition.starts_with("attachment;") && disposition.contains(".ics")); |
| 371 | let exported = r.text(); |
| 372 | // Download only: there is no saving into a folder. |
| 373 | let r = io.alice.post_json(&url, &json!({})).await; |
| 374 | assert_eq!(r.status, StatusCode::METHOD_NOT_ALLOWED); |
| 375 | |
| 376 | // The export imports back as the same objects. |
| 377 | io.dav("alice", "MKCALENDAR", "/pim/calendars/alice/copy/", "") |
| 378 | .await; |
| 379 | let copy = io.id("/pim/calendars/alice/copy/").await; |
| 380 | let r = io.import(copy, &exported).await; |
| 381 | assert_eq!(r["created"], 2, "{r}"); |
| 382 | |
| 383 | // vCards, one without UID. |
| 384 | let book = io.id("/pim/addressbooks/alice/default/").await; |
| 385 | let cards = "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:c1\r\nFN:One\r\nEND:VCARD\r\n\ |
| 386 | BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Two\r\nEND:VCARD\r\n"; |
| 387 | let r = io.import(book, cards).await; |
| 388 | assert_eq!(r["created"], 2, "{r}"); |
| 389 | let r = io.import(book, cards).await; |
| 390 | assert_eq!(r["updated"], 2, "{r}"); |
| 391 | |
| 392 | // The system address book has no collection id of its own. |
| 393 | let r = io.alice.get("/api/pim/system/export").await; |
| 394 | assert_eq!(r.status, StatusCode::OK); |
| 395 | assert!(r.header("content-disposition").unwrap().contains(".vcf")); |
| 396 | assert!(r.text().contains("FN:alice")); |
| 397 | } |
| 398 | |
| 399 | #[tokio::test] |
| 400 | async fn import_as_a_new_collection() { |
| 401 | let io = Io::new().await; |
| 402 | let post = |query: &str, body: String| { |
| 403 | let (client, query) = (&io.alice, query.to_string()); |
| 404 | async move { |
| 405 | let r = client |
| 406 | .raw( |
| 407 | Method::POST, |
| 408 | &format!("/api/pim/import?{query}"), |
| 409 | &[("content-type", "text/calendar")], |
| 410 | body.into_bytes(), |
| 411 | ) |
| 412 | .await; |
| 413 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 414 | r.json() |
| 415 | } |
| 416 | }; |
| 417 | let before = collections(&io.alice).await; |
| 418 | let vevent = |uid: &str, summary: &str| { |
| 419 | format!( |
| 420 | "BEGIN:VEVENT\r\nUID:{uid}\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20260101T100000Z\r\nSUMMARY:{summary}\r\nEND:VEVENT\r\n" |
| 421 | ) |
| 422 | }; |
| 423 | |
| 424 | // The file names itself and its color. |
| 425 | let ics = format!( |
| 426 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nX-WR-CALNAME:Ferien\r\nX-APPLE-CALENDAR-COLOR:#FF2968FF\r\n{}END:VCALENDAR\r\n", |
| 427 | vevent("f1", "Sommer") |
| 428 | ); |
| 429 | let r = post("kind=calendar&file=holidays.ics&color=%2322c55e", ics).await; |
| 430 | assert_eq!(r["created"], 1, "{r}"); |
| 431 | assert_eq!(r["collection"]["name"], "Ferien", "{r}"); |
| 432 | assert_eq!(r["collection"]["color"], "#FF2968FF", "{r}"); |
| 433 | |
| 434 | // A typed name wins; without a color in the file the requested one counts. |
| 435 | let plain = format!( |
| 436 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nCOLOR:teal\r\n{}END:VCALENDAR\r\n", |
| 437 | vevent("f2", "Winter") |
| 438 | ); |
| 439 | let r = post("kind=calendar&name=Mine&file=x.ics&color=%2322c55e", plain).await; |
| 440 | assert_eq!(r["collection"]["name"], "Mine", "{r}"); |
| 441 | assert_eq!(r["collection"]["color"], "#22c55e", "{r}"); |
| 442 | |
| 443 | // An address book takes the file name. |
| 444 | let vcf = "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:c1\r\nFN:Ann\r\nN:;Ann;;;\r\nEND:VCARD\r\n"; |
| 445 | let r = post("kind=addressbook&file=Team%20contacts.vcf", vcf.to_string()).await; |
| 446 | assert_eq!(r["collection"]["name"], "Team contacts", "{r}"); |
| 447 | assert_eq!(r["collection"]["kind"], "addressbook", "{r}"); |
| 448 | assert_eq!(collections(&io.alice).await, before + 3); |
| 449 | |
| 450 | // Nothing importable: reported, and no empty collection is left behind. |
| 451 | let broken = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:bad\r\nSUMMARY:no start\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"; |
| 452 | let r = post("kind=calendar&file=bad.ics", broken.to_string()).await; |
| 453 | assert_eq!(r["collection"], Value::Null, "{r}"); |
| 454 | assert_eq!(r["skipped_total"], 1, "{r}"); |
| 455 | assert_eq!(collections(&io.alice).await, before + 3); |
| 456 | } |
| 457 | |
| 458 | async fn collections(c: &Client) -> usize { |
| 459 | c.get("/api/pim/collections") |
| 460 | .await |
| 461 | .json() |
| 462 | .as_array() |
| 463 | .unwrap() |
| 464 | .len() |
| 465 | } |
| 466 | |
| 467 | #[tokio::test] |
| 468 | async fn share_candidates_leave_out_owner_borrowers_and_disabled() { |
| 469 | let io = Io::new().await; |
| 470 | let admin = login(&io.env, "admin", "admin1234").await; |
| 471 | create_user(&admin, "carol", PW, &[]).await; |
| 472 | create_user(&admin, "dave", PW, &[]).await; |
| 473 | let dave = user_id(&admin, "dave").await; |
| 474 | let r = admin |
| 475 | .put_json( |
| 476 | &format!("/api/admin/users/{dave}"), |
| 477 | &json!({"active": false}), |
| 478 | ) |
| 479 | .await; |
| 480 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 481 | let r = admin |
| 482 | .post_json( |
| 483 | "/api/admin/rooms", |
| 484 | &json!({"name": "atrium", "kind": "room"}), |
| 485 | ) |
| 486 | .await; |
| 487 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 488 | |
| 489 | let id = io.id(CAL).await; |
| 490 | let r = io |
| 491 | .alice |
| 492 | .post_json( |
| 493 | &format!("/api/pim/collections/{id}/shares"), |
| 494 | &json!({"user": "bob", "mode": "ro"}), |
| 495 | ) |
| 496 | .await; |
| 497 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 498 | |
| 499 | let names = |v: Value| -> Vec<String> { |
| 500 | v.as_array() |
| 501 | .unwrap() |
| 502 | .iter() |
| 503 | .map(|c| c["name"].as_str().unwrap().to_string()) |
| 504 | .collect() |
| 505 | }; |
| 506 | let r = io |
| 507 | .alice |
| 508 | .get(&format!("/api/pim/collections/{id}/shares/candidates")) |
| 509 | .await; |
| 510 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 511 | // Not alice herself, not bob (a borrower), not dave (disabled), no room. |
| 512 | assert_eq!(names(r.json()), ["admin", "carol"]); |
| 513 | |
| 514 | // Only the owner may ask. |
| 515 | let bob = login(&io.env, "bob", PW).await; |
| 516 | let r = bob |
| 517 | .get(&format!("/api/pim/collections/{id}/shares/candidates")) |
| 518 | .await; |
| 519 | assert_eq!(r.status, StatusCode::NOT_FOUND); |
| 520 | } |
| 521 | |
| 522 | #[tokio::test] |
| 523 | async fn a_skipped_import_into_a_new_user_leaves_only_the_default_calendar() { |
| 524 | use server::db::PimKind; |
| 525 | let io = Io::new().await; |
| 526 | let client = login(&io.env, "bob", PW).await; |
| 527 | let broken = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nBEGIN:VEVENT\r\nUID:bad\r\nSUMMARY:no start\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"; |
| 528 | let r = client |
| 529 | .raw( |
| 530 | Method::POST, |
| 531 | "/api/pim/import?kind=calendar&file=bad.ics", |
| 532 | &[("content-type", "text/calendar")], |
| 533 | broken.as_bytes().to_vec(), |
| 534 | ) |
| 535 | .await; |
| 536 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 537 | assert_eq!(r.json()["collection"], Value::Null); |
| 538 | assert_eq!(r.json()["skipped_total"], 1); |
| 539 | |
| 540 | let db = &io.env.state.db; |
| 541 | let bob = db.find_user_by_name("bob").await.unwrap().unwrap(); |
| 542 | let pid = db.principal_of(bob.id).await.unwrap(); |
| 543 | let slugs: Vec<_> = db |
| 544 | .pim_collections(pid, PimKind::Calendar) |
| 545 | .await |
| 546 | .unwrap() |
| 547 | .into_iter() |
| 548 | .map(|c| c.slug) |
| 549 | .filter(|s| s != "inbox" && s != "outbox") |
| 550 | .collect(); |
| 551 | assert_eq!(slugs, ["default"]); |
| 552 | } |
| 553 | |
| 554 | #[tokio::test] |
| 555 | async fn collection_names_and_descriptions_are_checked() { |
| 556 | let io = Io::new().await; |
| 557 | let create = |body: Value| { |
| 558 | let client = &io.alice; |
| 559 | async move { client.post_json("/api/pim/collections", &body).await } |
| 560 | }; |
| 561 | let long = "x".repeat(257); |
| 562 | for name in [long.as_str(), "bell\u{7}"] { |
| 563 | let r = create(json!({"kind": "calendar", "name": name})).await; |
| 564 | assert_eq!(r.status, StatusCode::BAD_REQUEST, "{name:?}"); |
| 565 | } |
| 566 | let r = |
| 567 | create(json!({"kind": "calendar", "name": "Ok", "description": "x".repeat(1025)})).await; |
| 568 | assert_eq!(r.status, StatusCode::BAD_REQUEST); |
| 569 | let r = create(json!({"kind": "calendar", "name": "Ok", "description": "two\nlines"})).await; |
| 570 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 571 | let id = r.json()["id"].as_i64().unwrap(); |
| 572 | let url = format!("/api/pim/collections/{id}"); |
| 573 | for body in [ |
| 574 | json!({"name": long}), |
| 575 | json!({"description": "x".repeat(1025)}), |
| 576 | ] { |
| 577 | let r = io.alice.put_json(&url, &body).await; |
| 578 | assert_eq!(r.status, StatusCode::BAD_REQUEST, "{body}"); |
| 579 | } |
| 580 | |
| 581 | // A file's own name that cannot be stored gives way to the file name. |
| 582 | let ics = format!( |
| 583 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nX-WR-CALNAME:{long}\r\nBEGIN:VEVENT\r\nUID:n1\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20260101T100000Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n" |
| 584 | ); |
| 585 | let r = io |
| 586 | .alice |
| 587 | .raw( |
| 588 | Method::POST, |
| 589 | "/api/pim/import?kind=calendar&file=Trips.ics", |
| 590 | &[("content-type", "text/calendar")], |
| 591 | ics.into_bytes(), |
| 592 | ) |
| 593 | .await; |
| 594 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 595 | assert_eq!(r.json()["collection"]["name"], "Trips"); |
| 596 | } |
| 597 | |
| 598 | #[tokio::test] |
| 599 | async fn a_feed_password_matches_with_edge_spaces() { |
| 600 | let io = Io::new().await; |
| 601 | let cal = io.id(CAL).await; |
| 602 | let locked = io.link(cal, json!({ "password": " feedpass123 " })).await; |
| 603 | for pw in [" feedpass123 ", "feedpass123"] { |
| 604 | let r = io.anon(&locked, &[("authorization", &basic("", pw))]).await; |
| 605 | assert_eq!(r.status, StatusCode::OK, "{pw:?}"); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | #[tokio::test] |
| 610 | async fn feeds_of_a_disabled_owner_stop() { |
| 611 | let io = Io::new().await; |
| 612 | let admin = login(&io.env, "admin", "admin1234").await; |
| 613 | let feed = io.link(io.id(CAL).await, json!({})).await; |
| 614 | let alice = user_id(&admin, "alice").await; |
| 615 | for (active, status) in [(false, StatusCode::NOT_FOUND), (true, StatusCode::OK)] { |
| 616 | let r = admin |
| 617 | .put_json( |
| 618 | &format!("/api/admin/users/{alice}"), |
| 619 | &json!({ "active": active }), |
| 620 | ) |
| 621 | .await; |
| 622 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 623 | assert_eq!(io.anon(&feed, &[]).await.status, status); |
| 624 | } |
| 625 | } |
| 626 | |
| 627 | #[tokio::test] |
| 628 | async fn a_loan_to_a_disabled_user_takes_a_new_mode() { |
| 629 | let io = Io::new().await; |
| 630 | let admin = login(&io.env, "admin", "admin1234").await; |
| 631 | create_user(&admin, "carol", PW, &[]).await; |
| 632 | let id = io.id(CAL).await; |
| 633 | let shares = format!("/api/pim/collections/{id}/shares"); |
| 634 | let r = io |
| 635 | .alice |
| 636 | .post_json(&shares, &json!({"user": "bob", "mode": "ro"})) |
| 637 | .await; |
| 638 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 639 | for name in ["bob", "carol"] { |
| 640 | let uid = user_id(&admin, name).await; |
| 641 | let r = admin |
| 642 | .put_json( |
| 643 | &format!("/api/admin/users/{uid}"), |
| 644 | &json!({"active": false}), |
| 645 | ) |
| 646 | .await; |
| 647 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 648 | } |
| 649 | |
| 650 | let r = io |
| 651 | .alice |
| 652 | .post_json(&shares, &json!({"user": "bob", "mode": "rw"})) |
| 653 | .await; |
| 654 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 655 | assert_eq!(r.json()["user_name"], "bob"); |
| 656 | let list = io.alice.get(&shares).await.json(); |
| 657 | assert_eq!(list[0]["mode"], "rw", "{list}"); |
| 658 | |
| 659 | // No new loan goes to a disabled account. |
| 660 | let r = io |
| 661 | .alice |
| 662 | .post_json(&shares, &json!({"user": "carol", "mode": "ro"})) |
| 663 | .await; |
| 664 | assert_eq!(r.status, StatusCode::NOT_FOUND); |
| 665 | } |
| 666 | |
| 667 | #[tokio::test] |
| 668 | async fn an_import_racing_the_collection_delete_never_fails_with_500() { |
| 669 | let io = Io::new().await; |
| 670 | let url = "/pim/calendars/alice/race/"; |
| 671 | for i in 0..20 { |
| 672 | let r = io.dav("alice", "MKCALENDAR", url, "").await; |
| 673 | assert_eq!(r.status, StatusCode::CREATED, "{}", r.text()); |
| 674 | let path = format!("/api/pim/collections/{}/import", io.id(url).await); |
| 675 | let (import, delete) = tokio::join!( |
| 676 | io.alice.raw( |
| 677 | Method::POST, |
| 678 | &path, |
| 679 | &[("content-type", "text/calendar")], |
| 680 | event(&format!("race{i}"), "x", "").into_bytes(), |
| 681 | ), |
| 682 | io.dav("alice", "DELETE", url, ""), |
| 683 | ); |
| 684 | assert_eq!(delete.status, StatusCode::NO_CONTENT); |
| 685 | assert!( |
| 686 | [StatusCode::OK, StatusCode::NOT_FOUND].contains(&import.status), |
| 687 | "{}: {}", |
| 688 | import.status, |
| 689 | import.text() |
| 690 | ); |
| 691 | } |
| 692 | } |
| 693 | |
| 694 | #[tokio::test] |
| 695 | async fn the_web_api_keeps_the_dav_limits() { |
| 696 | let io = Io::new().await; |
| 697 | let cal = io.id(CAL).await; |
| 698 | // An object over the 10 MiB a PUT takes is skipped. |
| 699 | let big = event( |
| 700 | "big", |
| 701 | "Big", |
| 702 | &format!("DESCRIPTION:{}\r\n", "x".repeat(11 << 20)), |
| 703 | ); |
| 704 | let r = io.import(cal, &(big + &event("small", "Small", ""))).await; |
| 705 | assert_eq!(r["created"], 1, "{r}"); |
| 706 | assert_eq!(r["skipped"][0]["uid"], "big", "{r}"); |
| 707 | |
| 708 | // One large time zone copied into every event is refused up front. |
| 709 | let zone = format!( |
| 710 | "BEGIN:VTIMEZONE\r\nTZID:Big\r\n{}BEGIN:STANDARD\r\nDTSTART:19700101T000000\r\n\ |
| 711 | TZOFFSETFROM:+0000\r\nTZOFFSETTO:+0000\r\nEND:STANDARD\r\nEND:VTIMEZONE\r\n", |
| 712 | format!("X-PAD:{}\r\n", "x".repeat(70)).repeat(15_000) |
| 713 | ); |
| 714 | let events: String = (0..200) |
| 715 | .map(|i| { |
| 716 | format!( |
| 717 | "BEGIN:VEVENT\r\nUID:z{i}\r\nDTSTAMP:20260101T000000Z\r\n\ |
| 718 | DTSTART;TZID=Big:20260101T100000\r\nEND:VEVENT\r\n" |
| 719 | ) |
| 720 | }) |
| 721 | .collect(); |
| 722 | let file = format!( |
| 723 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\n{zone}{events}END:VCALENDAR\r\n" |
| 724 | ); |
| 725 | let r = io |
| 726 | .alice |
| 727 | .raw( |
| 728 | Method::POST, |
| 729 | &format!("/api/pim/collections/{cal}/import"), |
| 730 | &[("content-type", "text/calendar")], |
| 731 | file.into_bytes(), |
| 732 | ) |
| 733 | .await; |
| 734 | assert_eq!(r.status, StatusCode::PAYLOAD_TOO_LARGE, "{}", r.text()); |
| 735 | |
| 736 | // A zone no event names is not copied, and alarms are no objects. |
| 737 | let events: String = (0..150) |
| 738 | .map(|i| { |
| 739 | format!( |
| 740 | "BEGIN:VEVENT\r\nUID:a{i}\r\nDTSTAMP:20260101T000000Z\r\n\ |
| 741 | DTSTART:20260101T100000Z\r\nBEGIN:VALARM\r\nACTION:DISPLAY\r\n\ |
| 742 | TRIGGER:-PT5M\r\nEND:VALARM\r\nEND:VEVENT\r\n" |
| 743 | ) |
| 744 | }) |
| 745 | .collect(); |
| 746 | let file = format!( |
| 747 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\n{zone}{events}END:VCALENDAR\r\n" |
| 748 | ); |
| 749 | let r = io.import(cal, &file).await; |
| 750 | assert_eq!(r["created"], 150, "{r}"); |
| 751 | |
| 752 | // A suffixed slug never takes the lent form "shared-<id>". |
| 753 | for _ in 0..2 { |
| 754 | let r = io |
| 755 | .alice |
| 756 | .post_json( |
| 757 | "/api/pim/collections", |
| 758 | &json!({"kind": "calendar", "name": "Shared"}), |
| 759 | ) |
| 760 | .await; |
| 761 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 762 | assert!(!r.text().contains("/shared-"), "{}", r.text()); |
| 763 | } |
| 764 | |
| 765 | // No more collections than MKCOL allows. |
| 766 | let create = async |name: String| { |
| 767 | io.alice |
| 768 | .post_json( |
| 769 | "/api/pim/collections", |
| 770 | &json!({"kind": "addressbook", "name": name}), |
| 771 | ) |
| 772 | .await |
| 773 | .status |
| 774 | }; |
| 775 | let mut made = 0; |
| 776 | while create(format!("Book {made}")).await == StatusCode::OK { |
| 777 | made += 1; |
| 778 | assert!(made <= 100); |
| 779 | } |
| 780 | assert_eq!(create("One more".into()).await, StatusCode::FORBIDDEN); |
| 781 | } |
| 782 |