api_pim_schedule.rs
| 1 | //! Implicit scheduling (RFC 6638) between the accounts and rooms of one |
| 2 | //! server. |
| 3 | |
| 4 | mod common; |
| 5 | |
| 6 | use axum::http::{Method, StatusCode}; |
| 7 | use common::*; |
| 8 | use pimdav::xml::{self, CALDAV, DAV, Name}; |
| 9 | use serde_json::json; |
| 10 | use xmltree::Element; |
| 11 | |
| 12 | const USERS: [&str; 3] = ["alice", "bob", "carol"]; |
| 13 | const PW: &str = "secret12345"; |
| 14 | |
| 15 | struct Pim { |
| 16 | env: Env, |
| 17 | admin: Client, |
| 18 | } |
| 19 | |
| 20 | impl Pim { |
| 21 | async fn new() -> Self { |
| 22 | let env = Env::new().await; |
| 23 | let admin = env.admin().await; |
| 24 | for u in USERS { |
| 25 | create_user(&admin, u, PW, &[]).await; |
| 26 | } |
| 27 | Pim { env, admin } |
| 28 | } |
| 29 | |
| 30 | async fn req( |
| 31 | &self, |
| 32 | user: &str, |
| 33 | verb: &str, |
| 34 | path: &str, |
| 35 | extra: &[(&str, &str)], |
| 36 | body: &str, |
| 37 | ) -> Resp { |
| 38 | let auth = basic(user, PW); |
| 39 | let mut headers = vec![("authorization", auth.as_str())]; |
| 40 | headers.extend_from_slice(extra); |
| 41 | Client::new(self.env.app.clone()) |
| 42 | .raw( |
| 43 | Method::from_bytes(verb.as_bytes()).unwrap(), |
| 44 | path, |
| 45 | &headers, |
| 46 | body.as_bytes().to_vec(), |
| 47 | ) |
| 48 | .await |
| 49 | } |
| 50 | |
| 51 | /// The hrefs of the members of a collection. |
| 52 | async fn members(&self, user: &str, collection: &str) -> Vec<String> { |
| 53 | let r = self |
| 54 | .req(user, "PROPFIND", collection, &[("depth", "1")], "") |
| 55 | .await; |
| 56 | assert_eq!(r.status, StatusCode::MULTI_STATUS, "{}", r.text()); |
| 57 | let root = Element::parse(r.body.as_slice()).unwrap(); |
| 58 | xml::elements(&root) |
| 59 | .map(|resp| xml::text(xml::child(resp, DAV, "href").unwrap())) |
| 60 | .filter(|h| h != collection) |
| 61 | .collect() |
| 62 | } |
| 63 | |
| 64 | /// The only object of a user's default calendar. |
| 65 | async fn copy(&self, user: &str) -> (String, Resp) { |
| 66 | let members = self.members(user, &cal(user)).await; |
| 67 | assert_eq!(members.len(), 1, "{members:?}"); |
| 68 | let href = members[0].clone(); |
| 69 | let r = self.req(user, "GET", &href, &[], "").await; |
| 70 | assert_eq!(r.status, StatusCode::OK); |
| 71 | (href, r) |
| 72 | } |
| 73 | |
| 74 | async fn inbox(&self, user: &str) -> Vec<String> { |
| 75 | let mut out = Vec::new(); |
| 76 | for href in self |
| 77 | .members(user, &format!("/pim/calendars/{user}/inbox/")) |
| 78 | .await |
| 79 | { |
| 80 | out.push(unfold(&self.req(user, "GET", &href, &[], "").await.text())); |
| 81 | } |
| 82 | out |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | fn cal(user: &str) -> String { |
| 87 | format!("/pim/calendars/{user}/default/") |
| 88 | } |
| 89 | |
| 90 | fn addr(user: &str) -> String { |
| 91 | format!("mailto:{user}@dovenest.invalid") |
| 92 | } |
| 93 | |
| 94 | fn unfold(s: &str) -> String { |
| 95 | s.replace("\r\n ", "") |
| 96 | } |
| 97 | |
| 98 | /// A parameter of the ATTENDEE property of `who`. |
| 99 | fn attendee_param(ics: &str, who: &str, param: &str) -> Option<String> { |
| 100 | let suffix = format!(":{who}"); |
| 101 | let unfolded = unfold(ics); |
| 102 | let line = unfolded |
| 103 | .lines() |
| 104 | .find(|l| l.starts_with("ATTENDEE") && l.ends_with(&suffix))?; |
| 105 | line.strip_suffix(&suffix)? |
| 106 | .split(';') |
| 107 | .find_map(|p| p.strip_prefix(&format!("{param}=")).map(str::to_string)) |
| 108 | } |
| 109 | |
| 110 | fn meeting(start: &str, attendees: &[&str]) -> String { |
| 111 | let attendees: String = attendees |
| 112 | .iter() |
| 113 | .map(|a| format!("ATTENDEE;RSVP=TRUE:{a}\r\n")) |
| 114 | .collect(); |
| 115 | format!( |
| 116 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\nBEGIN:VEVENT\r\nUID:meet-1\r\n\ |
| 117 | DTSTAMP:20260101T000000Z\r\nDTSTART:{start}\r\nDURATION:PT1H\r\nSUMMARY:Planning\r\n\ |
| 118 | ORGANIZER:{}\r\nATTENDEE;PARTSTAT=ACCEPTED:{}\r\n{attendees}END:VEVENT\r\nEND:VCALENDAR\r\n", |
| 119 | addr("alice"), |
| 120 | addr("alice") |
| 121 | ) |
| 122 | } |
| 123 | |
| 124 | const ALICE_EVENT: &str = "/pim/calendars/alice/default/meet.ics"; |
| 125 | |
| 126 | async fn invite(pim: &Pim, attendees: &[&str]) -> Resp { |
| 127 | let r = pim |
| 128 | .req( |
| 129 | "alice", |
| 130 | "PUT", |
| 131 | ALICE_EVENT, |
| 132 | &[], |
| 133 | &meeting("20260301T100000Z", attendees), |
| 134 | ) |
| 135 | .await; |
| 136 | assert_eq!(r.status, StatusCode::CREATED, "{}", r.text()); |
| 137 | r |
| 138 | } |
| 139 | |
| 140 | fn error_condition(r: &Resp) -> Name { |
| 141 | let root = Element::parse(r.body.as_slice()).unwrap_or_else(|_| panic!("{}", r.text())); |
| 142 | Name::of(xml::elements(&root).next().unwrap()) |
| 143 | } |
| 144 | |
| 145 | #[tokio::test] |
| 146 | async fn discovery_names_inbox_and_outbox() { |
| 147 | let pim = Pim::new().await; |
| 148 | let r = pim.req("alice", "OPTIONS", "/pim/", &[], "").await; |
| 149 | assert!(r.header("dav").unwrap().contains("calendar-auto-schedule")); |
| 150 | let body = "<d:propfind xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"><d:prop>\ |
| 151 | <c:schedule-inbox-URL/><c:schedule-outbox-URL/></d:prop></d:propfind>"; |
| 152 | let r = pim |
| 153 | .req("alice", "PROPFIND", "/pim/principals/alice/", &[], body) |
| 154 | .await; |
| 155 | let text = r.text(); |
| 156 | assert!(text.contains("/pim/calendars/alice/inbox/"), "{text}"); |
| 157 | assert!(text.contains("/pim/calendars/alice/outbox/"), "{text}"); |
| 158 | } |
| 159 | |
| 160 | #[tokio::test] |
| 161 | async fn invite_and_answer() { |
| 162 | let pim = Pim::new().await; |
| 163 | let r = invite( |
| 164 | &pim, |
| 165 | &[ |
| 166 | &addr("bob"), |
| 167 | &addr("carol"), |
| 168 | "mailto:dave@example.com", |
| 169 | &addr("nobody"), |
| 170 | ], |
| 171 | ) |
| 172 | .await; |
| 173 | // The server added SCHEDULE-STATUS, so the stored bytes differ. |
| 174 | assert!(r.header("etag").is_none()); |
| 175 | assert!(r.header("schedule-tag").is_some()); |
| 176 | |
| 177 | let org = unfold(&pim.req("alice", "GET", ALICE_EVENT, &[], "").await.text()); |
| 178 | for (who, status) in [ |
| 179 | (addr("bob"), "1.2"), |
| 180 | (addr("carol"), "1.2"), |
| 181 | ("mailto:dave@example.com".into(), "5.2"), |
| 182 | (addr("nobody"), "3.7"), |
| 183 | ] { |
| 184 | let got = attendee_param(&org, &who, "SCHEDULE-STATUS"); |
| 185 | assert_eq!(got.as_deref(), Some(status), "{org}"); |
| 186 | } |
| 187 | |
| 188 | let (bob_href, got) = pim.copy("bob").await; |
| 189 | let bob_copy = unfold(&got.text()); |
| 190 | let partstat = attendee_param(&bob_copy, &addr("bob"), "PARTSTAT"); |
| 191 | assert_eq!(partstat.as_deref(), Some("NEEDS-ACTION"), "{bob_copy}"); |
| 192 | assert!( |
| 193 | !bob_copy.contains("METHOD") && !bob_copy.contains("SCHEDULE-STATUS"), |
| 194 | "{bob_copy}" |
| 195 | ); |
| 196 | let inbox = pim.inbox("bob").await; |
| 197 | assert_eq!(inbox.len(), 1); |
| 198 | assert!(inbox[0].contains("METHOD:REQUEST")); |
| 199 | |
| 200 | // bob accepts, conditional on what he read. |
| 201 | let alice_tag = pim |
| 202 | .req("alice", "GET", ALICE_EVENT, &[], "") |
| 203 | .await |
| 204 | .header("schedule-tag"); |
| 205 | let carol_tag = pim.copy("carol").await.1.header("schedule-tag"); |
| 206 | let tag = got.header("schedule-tag").unwrap(); |
| 207 | let accepted = bob_copy.replace("PARTSTAT=NEEDS-ACTION", "PARTSTAT=ACCEPTED"); |
| 208 | let r = pim |
| 209 | .req( |
| 210 | "bob", |
| 211 | "PUT", |
| 212 | &bob_href, |
| 213 | &[("if-schedule-tag-match", &tag)], |
| 214 | &accepted, |
| 215 | ) |
| 216 | .await; |
| 217 | assert_eq!(r.status, StatusCode::NO_CONTENT, "{}", r.text()); |
| 218 | let bob_copy = unfold(&pim.req("bob", "GET", &bob_href, &[], "").await.text()); |
| 219 | assert!( |
| 220 | bob_copy.contains("ORGANIZER;SCHEDULE-STATUS=1.2"), |
| 221 | "{bob_copy}" |
| 222 | ); |
| 223 | |
| 224 | // alice sees the answer; her Schedule-Tag stays, so her pending edit |
| 225 | // would still go through. |
| 226 | let r = pim.req("alice", "GET", ALICE_EVENT, &[], "").await; |
| 227 | assert_eq!(r.header("schedule-tag"), alice_tag); |
| 228 | let org = r.text(); |
| 229 | assert_eq!( |
| 230 | attendee_param(&org, &addr("bob"), "SCHEDULE-STATUS").as_deref(), |
| 231 | Some("2.0") |
| 232 | ); |
| 233 | assert_eq!( |
| 234 | attendee_param(&org, &addr("bob"), "PARTSTAT").as_deref(), |
| 235 | Some("ACCEPTED") |
| 236 | ); |
| 237 | let replies = pim.inbox("alice").await; |
| 238 | assert_eq!(replies.len(), 1); |
| 239 | assert!(replies[0].contains("METHOD:REPLY")); |
| 240 | |
| 241 | // carol's copy learns it quietly: same Schedule-Tag, no inbox entry. |
| 242 | let (_, carol) = pim.copy("carol").await; |
| 243 | assert_eq!(carol.header("schedule-tag"), carol_tag); |
| 244 | let seen = attendee_param(&carol.text(), &addr("bob"), "PARTSTAT"); |
| 245 | assert_eq!(seen.as_deref(), Some("ACCEPTED"), "{}", carol.text()); |
| 246 | assert_eq!(pim.inbox("carol").await.len(), 1); |
| 247 | |
| 248 | // Deleting her copy declines for carol. |
| 249 | let (carol_href, _) = pim.copy("carol").await; |
| 250 | assert_eq!( |
| 251 | pim.req("carol", "DELETE", &carol_href, &[], "") |
| 252 | .await |
| 253 | .status, |
| 254 | StatusCode::NO_CONTENT |
| 255 | ); |
| 256 | let org = pim.req("alice", "GET", ALICE_EVENT, &[], "").await.text(); |
| 257 | assert_eq!( |
| 258 | attendee_param(&org, &addr("carol"), "PARTSTAT").as_deref(), |
| 259 | Some("DECLINED") |
| 260 | ); |
| 261 | } |
| 262 | |
| 263 | #[tokio::test] |
| 264 | async fn organizer_changes_reach_attendees() { |
| 265 | let pim = Pim::new().await; |
| 266 | invite(&pim, &[&addr("bob"), &addr("carol")]).await; |
| 267 | let (bob_href, got) = pim.copy("bob").await; |
| 268 | let accepted = unfold(&got.text()).replace("PARTSTAT=NEEDS-ACTION", "PARTSTAT=ACCEPTED"); |
| 269 | assert_eq!( |
| 270 | pim.req("bob", "PUT", &bob_href, &[], &accepted) |
| 271 | .await |
| 272 | .status, |
| 273 | StatusCode::NO_CONTENT |
| 274 | ); |
| 275 | let org = pim.req("alice", "GET", ALICE_EVENT, &[], "").await.text(); |
| 276 | assert_eq!( |
| 277 | attendee_param(&org, &addr("bob"), "PARTSTAT").as_deref(), |
| 278 | Some("ACCEPTED") |
| 279 | ); |
| 280 | let bob_tag = pim.copy("bob").await.1.header("schedule-tag"); |
| 281 | |
| 282 | // Moving the meeting asks everyone again. |
| 283 | let moved = meeting("20260301T140000Z", &[&addr("bob"), &addr("carol")]); |
| 284 | assert_eq!( |
| 285 | pim.req("alice", "PUT", ALICE_EVENT, &[], &moved) |
| 286 | .await |
| 287 | .status, |
| 288 | StatusCode::NO_CONTENT |
| 289 | ); |
| 290 | let (_, got) = pim.copy("bob").await; |
| 291 | let bob_copy = unfold(&got.text()); |
| 292 | assert!(bob_copy.contains("DTSTART:20260301T140000Z"), "{bob_copy}"); |
| 293 | let partstat = attendee_param(&bob_copy, &addr("bob"), "PARTSTAT"); |
| 294 | assert_eq!(partstat.as_deref(), Some("NEEDS-ACTION"), "{bob_copy}"); |
| 295 | assert_ne!(got.header("schedule-tag"), bob_tag); |
| 296 | assert_eq!(pim.inbox("bob").await.len(), 2); |
| 297 | |
| 298 | // Dropping bob cancels his copy. |
| 299 | let without_bob = meeting("20260301T140000Z", &[&addr("carol")]); |
| 300 | pim.req("alice", "PUT", ALICE_EVENT, &[], &without_bob) |
| 301 | .await; |
| 302 | assert!(unfold(&pim.copy("bob").await.1.text()).contains("STATUS:CANCELLED")); |
| 303 | assert!( |
| 304 | pim.inbox("bob") |
| 305 | .await |
| 306 | .iter() |
| 307 | .any(|m| m.contains("METHOD:CANCEL")) |
| 308 | ); |
| 309 | |
| 310 | // Deleting the meeting cancels it for carol. |
| 311 | assert_eq!( |
| 312 | pim.req("alice", "DELETE", ALICE_EVENT, &[], "") |
| 313 | .await |
| 314 | .status, |
| 315 | StatusCode::NO_CONTENT |
| 316 | ); |
| 317 | assert!(unfold(&pim.copy("carol").await.1.text()).contains("STATUS:CANCELLED")); |
| 318 | } |
| 319 | |
| 320 | #[tokio::test] |
| 321 | async fn attendees_have_limits() { |
| 322 | let pim = Pim::new().await; |
| 323 | invite(&pim, &[&addr("bob")]).await; |
| 324 | let (bob_href, got) = pim.copy("bob").await; |
| 325 | let bob_copy = unfold(&got.text()); |
| 326 | |
| 327 | let moved = bob_copy.replace("DTSTART:20260301T100000Z", "DTSTART:20260301T120000Z"); |
| 328 | let r = pim.req("bob", "PUT", &bob_href, &[], &moved).await; |
| 329 | assert_eq!(r.status, StatusCode::FORBIDDEN); |
| 330 | assert!(error_condition(&r).is(CALDAV, "allowed-attendee-scheduling-object-change")); |
| 331 | |
| 332 | let r = pim |
| 333 | .req( |
| 334 | "bob", |
| 335 | "PUT", |
| 336 | &bob_href, |
| 337 | &[("if-schedule-tag-match", "\"stale\"")], |
| 338 | &bob_copy, |
| 339 | ) |
| 340 | .await; |
| 341 | assert_eq!(r.status, StatusCode::PRECONDITION_FAILED); |
| 342 | let into_inbox = pim |
| 343 | .req( |
| 344 | "bob", |
| 345 | "PUT", |
| 346 | "/pim/calendars/bob/inbox/x.ics", |
| 347 | &[], |
| 348 | &bob_copy, |
| 349 | ) |
| 350 | .await; |
| 351 | assert_eq!(into_inbox.status, StatusCode::FORBIDDEN); |
| 352 | |
| 353 | // A silent removal answers nothing. |
| 354 | let r = pim |
| 355 | .req("bob", "DELETE", &bob_href, &[("schedule-reply", "F")], "") |
| 356 | .await; |
| 357 | assert_eq!(r.status, StatusCode::NO_CONTENT); |
| 358 | let org = unfold(&pim.req("alice", "GET", ALICE_EVENT, &[], "").await.text()); |
| 359 | assert!(!org.contains("DECLINED"), "{org}"); |
| 360 | assert!(pim.inbox("alice").await.is_empty()); |
| 361 | } |
| 362 | |
| 363 | #[tokio::test] |
| 364 | async fn rooms_receive_bookings() { |
| 365 | let pim = Pim::new().await; |
| 366 | let r = pim |
| 367 | .admin |
| 368 | .post_json( |
| 369 | "/api/admin/rooms", |
| 370 | &json!({"name": "board", "display_name": "Board", "kind": "room"}), |
| 371 | ) |
| 372 | .await; |
| 373 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 374 | invite(&pim, &["mailto:board@rooms.dovenest.invalid"]).await; |
| 375 | let org = pim.req("alice", "GET", ALICE_EVENT, &[], "").await.text(); |
| 376 | let room = "mailto:board@rooms.dovenest.invalid"; |
| 377 | assert_eq!( |
| 378 | attendee_param(&org, room, "SCHEDULE-STATUS").as_deref(), |
| 379 | Some("1.2"), |
| 380 | "{org}" |
| 381 | ); |
| 382 | // Everyone reads a room's bookings. |
| 383 | let members = pim.members("bob", "/pim/calendars/board/default/").await; |
| 384 | assert_eq!(members.len(), 1, "{members:?}"); |
| 385 | } |
| 386 | |
| 387 | const ROOM: &str = "mailto:board@rooms.dovenest.invalid"; |
| 388 | |
| 389 | /// `days` from now at `hour` UTC, as an iCalendar date-time. Rooms only |
| 390 | /// check instances from now on. |
| 391 | fn future(days: i64, hour: u32) -> String { |
| 392 | (chrono::Utc::now() + chrono::TimeDelta::days(days)) |
| 393 | .date_naive() |
| 394 | .and_hms_opt(hour, 0, 0) |
| 395 | .unwrap() |
| 396 | .format("%Y%m%dT%H%M%SZ") |
| 397 | .to_string() |
| 398 | } |
| 399 | |
| 400 | fn booking(uid: &str, organizer: &str, start: &str, extra: &str, attendees: &[&str]) -> String { |
| 401 | let attendees: String = attendees |
| 402 | .iter() |
| 403 | .map(|a| format!("ATTENDEE:{a}\r\n")) |
| 404 | .collect(); |
| 405 | format!( |
| 406 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\nBEGIN:VEVENT\r\nUID:{uid}\r\n\ |
| 407 | DTSTAMP:20260101T000000Z\r\nDTSTART:{start}\r\nDURATION:PT1H\r\n{extra}\ |
| 408 | ORGANIZER:{o}\r\nATTENDEE;PARTSTAT=ACCEPTED:{o}\r\n{attendees}END:VEVENT\r\nEND:VCALENDAR\r\n", |
| 409 | o = addr(organizer), |
| 410 | ) |
| 411 | } |
| 412 | |
| 413 | impl Pim { |
| 414 | async fn room(&self) { |
| 415 | let r = self |
| 416 | .admin |
| 417 | .post_json( |
| 418 | "/api/admin/rooms", |
| 419 | &json!({"name": "board", "display_name": "Board", "kind": "room"}), |
| 420 | ) |
| 421 | .await; |
| 422 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 423 | } |
| 424 | |
| 425 | async fn put_ok(&self, user: &str, path: &str, body: &str) { |
| 426 | let r = self.req(user, "PUT", path, &[], body).await; |
| 427 | assert!(r.status.is_success(), "{}: {}", r.status, r.text()); |
| 428 | } |
| 429 | |
| 430 | async fn get(&self, user: &str, path: &str) -> String { |
| 431 | unfold(&self.req(user, "GET", path, &[], "").await.text()) |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | #[tokio::test] |
| 436 | async fn outbox_answers_free_busy() { |
| 437 | let pim = Pim::new().await; |
| 438 | let event = |uid: &str, start: &str| { |
| 439 | format!( |
| 440 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\nBEGIN:VEVENT\r\nUID:{uid}\r\n\ |
| 441 | DTSTAMP:20260101T000000Z\r\nDTSTART:{start}\r\nDURATION:PT1H\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n" |
| 442 | ) |
| 443 | }; |
| 444 | pim.put_ok( |
| 445 | "bob", |
| 446 | &format!("{}busy.ics", cal("bob")), |
| 447 | &event("busy", "20260310T100000Z"), |
| 448 | ) |
| 449 | .await; |
| 450 | // A calendar marked transparent adds no busy time. |
| 451 | let mk = "<c:mkcalendar xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"><d:set><d:prop>\ |
| 452 | <c:schedule-calendar-transp><c:transparent/></c:schedule-calendar-transp>\ |
| 453 | </d:prop></d:set></c:mkcalendar>"; |
| 454 | let r = pim |
| 455 | .req("bob", "MKCALENDAR", "/pim/calendars/bob/side/", &[], mk) |
| 456 | .await; |
| 457 | assert_eq!(r.status, StatusCode::CREATED, "{}", r.text()); |
| 458 | pim.put_ok( |
| 459 | "bob", |
| 460 | "/pim/calendars/bob/side/x.ics", |
| 461 | &event("side", "20260310T120000Z"), |
| 462 | ) |
| 463 | .await; |
| 464 | let body = "<d:propfind xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"><d:prop>\ |
| 465 | <c:schedule-calendar-transp/></d:prop></d:propfind>"; |
| 466 | let r = pim |
| 467 | .req("bob", "PROPFIND", "/pim/calendars/bob/side/", &[], body) |
| 468 | .await; |
| 469 | assert!(r.text().contains("<c:transparent/>"), "{}", r.text()); |
| 470 | |
| 471 | let outbox = "/pim/calendars/alice/outbox/"; |
| 472 | let r = pim.req("alice", "OPTIONS", outbox, &[], "").await; |
| 473 | assert!(r.header("allow").unwrap().contains("POST")); |
| 474 | let request = |organizer: &str| { |
| 475 | format!( |
| 476 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\nMETHOD:REQUEST\r\n\ |
| 477 | BEGIN:VFREEBUSY\r\nUID:fb\r\nDTSTAMP:20260101T000000Z\r\n\ |
| 478 | DTSTART:20260310T000000Z\r\nDTEND:20260311T000000Z\r\nORGANIZER:{organizer}\r\n\ |
| 479 | ATTENDEE:{}\r\nATTENDEE:{}\r\nATTENDEE:mailto:dave@example.com\r\n\ |
| 480 | END:VFREEBUSY\r\nEND:VCALENDAR\r\n", |
| 481 | addr("bob"), |
| 482 | addr("nobody"), |
| 483 | ) |
| 484 | }; |
| 485 | let headers = [("content-type", "text/calendar")]; |
| 486 | let r = pim |
| 487 | .req("alice", "POST", outbox, &headers, &request(&addr("alice"))) |
| 488 | .await; |
| 489 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 490 | let root = Element::parse(r.body.as_slice()).unwrap(); |
| 491 | let answers: Vec<(String, String, String)> = xml::elements(&root) |
| 492 | .map(|resp| { |
| 493 | let recipient = xml::child(resp, CALDAV, "recipient").unwrap(); |
| 494 | let get = |local: &str| { |
| 495 | xml::child(resp, CALDAV, local) |
| 496 | .map(xml::text) |
| 497 | .unwrap_or_default() |
| 498 | }; |
| 499 | ( |
| 500 | xml::text(xml::child(recipient, DAV, "href").unwrap()), |
| 501 | get("request-status"), |
| 502 | unfold(&get("calendar-data")), |
| 503 | ) |
| 504 | }) |
| 505 | .collect(); |
| 506 | assert_eq!(answers.len(), 3, "{answers:?}"); |
| 507 | let (_, status, data) = &answers[0]; |
| 508 | assert!(status.starts_with("2.0"), "{status}"); |
| 509 | assert!( |
| 510 | data.contains("FREEBUSY;FBTYPE=BUSY:20260310T100000Z/20260310T110000Z"), |
| 511 | "{data}" |
| 512 | ); |
| 513 | assert!(!data.contains("20260310T120000Z"), "{data}"); |
| 514 | assert!(answers[1].1.starts_with("3.7"), "{answers:?}"); |
| 515 | assert!(answers[2].1.starts_with("5.2"), "{answers:?}"); |
| 516 | |
| 517 | // Only for the own addresses. |
| 518 | let r = pim |
| 519 | .req("alice", "POST", outbox, &headers, &request(&addr("bob"))) |
| 520 | .await; |
| 521 | assert_eq!(r.status, StatusCode::FORBIDDEN); |
| 522 | assert!(error_condition(&r).is(CALDAV, "organizer-allowed")); |
| 523 | } |
| 524 | |
| 525 | #[tokio::test] |
| 526 | async fn rooms_answer_from_their_bookings() { |
| 527 | let pim = Pim::new().await; |
| 528 | pim.room().await; |
| 529 | let slot = future(30, 10); |
| 530 | let alice_event = format!("{}a1.ics", cal("alice")); |
| 531 | pim.put_ok( |
| 532 | "alice", |
| 533 | &alice_event, |
| 534 | &booking("a1", "alice", &slot, "", &[ROOM]), |
| 535 | ) |
| 536 | .await; |
| 537 | let org = pim.get("alice", &alice_event).await; |
| 538 | assert_eq!( |
| 539 | attendee_param(&org, ROOM, "PARTSTAT").as_deref(), |
| 540 | Some("ACCEPTED"), |
| 541 | "{org}" |
| 542 | ); |
| 543 | assert!( |
| 544 | pim.inbox("alice") |
| 545 | .await |
| 546 | .iter() |
| 547 | .any(|m| m.contains("METHOD:REPLY")) |
| 548 | ); |
| 549 | |
| 550 | // A second meeting in the same slot is turned down. |
| 551 | let carol_event = format!("{}c1.ics", cal("carol")); |
| 552 | pim.put_ok( |
| 553 | "carol", |
| 554 | &carol_event, |
| 555 | &booking("c1", "carol", &slot, "", &[ROOM]), |
| 556 | ) |
| 557 | .await; |
| 558 | let org = pim.get("carol", &carol_event).await; |
| 559 | assert_eq!( |
| 560 | attendee_param(&org, ROOM, "PARTSTAT").as_deref(), |
| 561 | Some("DECLINED"), |
| 562 | "{org}" |
| 563 | ); |
| 564 | |
| 565 | // Cancelling the first frees the slot for the next request. |
| 566 | let r = pim.req("alice", "DELETE", &alice_event, &[], "").await; |
| 567 | assert_eq!(r.status, StatusCode::NO_CONTENT); |
| 568 | let renamed = booking("c1", "carol", &slot, "SUMMARY:Again\r\n", &[ROOM]); |
| 569 | pim.put_ok("carol", &carol_event, &renamed).await; |
| 570 | let org = pim.get("carol", &carol_event).await; |
| 571 | assert_eq!( |
| 572 | attendee_param(&org, ROOM, "PARTSTAT").as_deref(), |
| 573 | Some("ACCEPTED"), |
| 574 | "{org}" |
| 575 | ); |
| 576 | |
| 577 | // A series declines only the instance that collides. |
| 578 | pim.put_ok( |
| 579 | "carol", |
| 580 | &format!("{}c2.ics", cal("carol")), |
| 581 | &booking("c2", "carol", &future(67, 9), "", &[ROOM]), |
| 582 | ) |
| 583 | .await; |
| 584 | let bob_event = format!("{}b1.ics", cal("bob")); |
| 585 | let weekly = booking( |
| 586 | "b1", |
| 587 | "bob", |
| 588 | &future(60, 9), |
| 589 | "RRULE:FREQ=WEEKLY;COUNT=3\r\n", |
| 590 | &[ROOM], |
| 591 | ); |
| 592 | pim.put_ok("bob", &bob_event, &weekly).await; |
| 593 | let org = pim.get("bob", &bob_event).await; |
| 594 | assert!( |
| 595 | org.contains(&format!("RECURRENCE-ID:{}", future(67, 9))), |
| 596 | "{org}" |
| 597 | ); |
| 598 | let answers = |partstat: &str| { |
| 599 | org.lines() |
| 600 | .filter(|l| l.ends_with(ROOM) && l.contains(&format!("PARTSTAT={partstat}"))) |
| 601 | .count() |
| 602 | }; |
| 603 | assert_eq!((answers("ACCEPTED"), answers("DECLINED")), (1, 1), "{org}"); |
| 604 | } |
| 605 | |
| 606 | #[tokio::test] |
| 607 | async fn rooms_check_series_to_their_horizon() { |
| 608 | let pim = Pim::new().await; |
| 609 | pim.room().await; |
| 610 | let org = |user: &'static str, uid: &'static str| { |
| 611 | let pim = &pim; |
| 612 | async move { pim.get(user, &format!("{}{uid}.ics", cal(user))).await } |
| 613 | }; |
| 614 | let declined = |org: &str| { |
| 615 | org.lines() |
| 616 | .filter(|l| l.ends_with(ROOM) && l.contains("PARTSTAT=DECLINED")) |
| 617 | .count() |
| 618 | }; |
| 619 | let weekly = |uid: &str, who: &str, start: &str, rule: &str| { |
| 620 | booking( |
| 621 | uid, |
| 622 | who, |
| 623 | start, |
| 624 | &format!("RRULE:FREQ=WEEKLY;{rule}\r\n"), |
| 625 | &[ROOM], |
| 626 | ) |
| 627 | }; |
| 628 | for (uid, start) in [ |
| 629 | ("c1", future(800, 9)), |
| 630 | ("c2", future(800, 14)), |
| 631 | ("c3", future(3700, 9)), |
| 632 | ] { |
| 633 | pim.put_ok( |
| 634 | "carol", |
| 635 | &format!("{}{uid}.ics", cal("carol")), |
| 636 | &booking(uid, "carol", &start, "", &[ROOM]), |
| 637 | ) |
| 638 | .await; |
| 639 | } |
| 640 | |
| 641 | // A bounded series is checked to its end, past two years. |
| 642 | pim.put_ok( |
| 643 | "bob", |
| 644 | &format!("{}b1.ics", cal("bob")), |
| 645 | &weekly("b1", "bob", &future(786, 9), "COUNT=3"), |
| 646 | ) |
| 647 | .await; |
| 648 | assert_eq!(declined(&org("bob", "b1").await), 1); |
| 649 | // A series without end is checked for two years only. |
| 650 | pim.put_ok( |
| 651 | "alice", |
| 652 | &format!("{}a1.ics", cal("alice")), |
| 653 | &weekly("a1", "alice", &future(786, 14), "INTERVAL=1"), |
| 654 | ) |
| 655 | .await; |
| 656 | assert_eq!(declined(&org("alice", "a1").await), 0); |
| 657 | // Ten years is the limit even for a bounded series. |
| 658 | pim.put_ok( |
| 659 | "bob", |
| 660 | &format!("{}b2.ics", cal("bob")), |
| 661 | &weekly("b2", "bob", &future(3693, 9), "COUNT=2"), |
| 662 | ) |
| 663 | .await; |
| 664 | assert_eq!(declined(&org("bob", "b2").await), 0); |
| 665 | } |
| 666 | |
| 667 | #[tokio::test] |
| 668 | async fn sharees_schedule_only_when_allowed() { |
| 669 | let pim = Pim::new().await; |
| 670 | let alice = login(&pim.env, "alice", PW).await; |
| 671 | let listed = alice.get("/api/pim/collections").await.json(); |
| 672 | let id = listed |
| 673 | .as_array() |
| 674 | .unwrap() |
| 675 | .iter() |
| 676 | .find(|c| c["kind"] == "calendar" && c["mode"].is_null()) |
| 677 | .unwrap()["id"] |
| 678 | .as_i64() |
| 679 | .unwrap(); |
| 680 | let shares = format!("/api/pim/collections/{id}/shares"); |
| 681 | let lend = |mode: &'static str| { |
| 682 | let alice = &alice; |
| 683 | let shares = &shares; |
| 684 | async move { |
| 685 | let r = alice |
| 686 | .post_json(shares, &json!({"user": "bob", "mode": mode})) |
| 687 | .await; |
| 688 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 689 | } |
| 690 | }; |
| 691 | lend("rw").await; |
| 692 | let shared = format!("/pim/calendars/bob/shared-{id}/"); |
| 693 | let invite = booking("s1", "alice", "20260401T100000Z", "", &[&addr("carol")]); |
| 694 | |
| 695 | // Plain write access edits, but sends nothing as alice. |
| 696 | let r = pim |
| 697 | .req("bob", "PUT", &format!("{shared}s1.ics"), &[], &invite) |
| 698 | .await; |
| 699 | assert_eq!(r.status, StatusCode::FORBIDDEN, "{}", r.text()); |
| 700 | assert!(error_condition(&r).is(DAV, "need-privileges")); |
| 701 | assert!(r.text().contains("schedule-send-invite"), "{}", r.text()); |
| 702 | let plain = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\nBEGIN:VEVENT\r\nUID:p1\r\n\ |
| 703 | DTSTAMP:20260101T000000Z\r\nDTSTART:20260401T120000Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"; |
| 704 | pim.put_ok("bob", &format!("{shared}p1.ics"), plain).await; |
| 705 | |
| 706 | // With the schedule level, bob invites for alice, and says so. |
| 707 | lend("rw+schedule").await; |
| 708 | let body = "<d:propfind xmlns:d=\"DAV:\"><d:prop><d:current-user-privilege-set/></d:prop></d:propfind>"; |
| 709 | let r = pim.req("bob", "PROPFIND", &shared, &[], body).await; |
| 710 | assert!(r.text().contains("schedule-send-invite"), "{}", r.text()); |
| 711 | pim.put_ok("bob", &format!("{shared}s1.ics"), &invite).await; |
| 712 | let sent_by = format!("ORGANIZER;SENT-BY=\"{}\":{}", addr("bob"), addr("alice")); |
| 713 | let org = pim.get("alice", &format!("{}s1.ics", cal("alice"))).await; |
| 714 | assert!(org.contains(&sent_by), "{org}"); |
| 715 | let (_, copy) = pim.copy("carol").await; |
| 716 | assert!(unfold(©.text()).contains(&sent_by), "{}", copy.text()); |
| 717 | |
| 718 | // An edit that sends nothing is stored as sent: alice's own edit keeps |
| 719 | // bob's SENT-BY, and the PUT keeps its ETag. |
| 720 | let s1 = format!("{}s1.ics", cal("alice")); |
| 721 | let edited = pim.req("alice", "GET", &s1, &[], "").await.text().replacen( |
| 722 | "UID:s1\r\n", |
| 723 | "UID:s1\r\nX-NOTE:quiet\r\n", |
| 724 | 1, |
| 725 | ); |
| 726 | let r = pim.req("alice", "PUT", &s1, &[], &edited).await; |
| 727 | assert!(r.status.is_success(), "{}", r.text()); |
| 728 | assert!(r.header("etag").is_some(), "{edited}"); |
| 729 | assert_eq!(pim.req("alice", "GET", &s1, &[], "").await.text(), edited); |
| 730 | |
| 731 | // Answering for alice needs it too. |
| 732 | lend("rw").await; |
| 733 | pim.put_ok( |
| 734 | "carol", |
| 735 | &format!("{}c9.ics", cal("carol")), |
| 736 | &booking("c9", "carol", "20260402T100000Z", "", &[&addr("alice")]), |
| 737 | ) |
| 738 | .await; |
| 739 | let members = pim.members("alice", &cal("alice")).await; |
| 740 | let href = members |
| 741 | .iter() |
| 742 | .find(|h| !h.ends_with("s1.ics") && !h.ends_with("p1.ics")) |
| 743 | .unwrap(); |
| 744 | let name = href.rsplit('/').next().unwrap(); |
| 745 | let got = pim.get("alice", href).await; |
| 746 | let accepted = got.replace( |
| 747 | &format!("PARTSTAT=NEEDS-ACTION:{}", addr("alice")), |
| 748 | &format!("PARTSTAT=ACCEPTED:{}", addr("alice")), |
| 749 | ); |
| 750 | assert_ne!(got, accepted, "{got}"); |
| 751 | let r = pim |
| 752 | .req("bob", "PUT", &format!("{shared}{name}"), &[], &accepted) |
| 753 | .await; |
| 754 | assert_eq!(r.status, StatusCode::FORBIDDEN, "{}", r.text()); |
| 755 | assert!(r.text().contains("schedule-send-reply"), "{}", r.text()); |
| 756 | lend("rw+schedule").await; |
| 757 | pim.put_ok("bob", &format!("{shared}{name}"), &accepted) |
| 758 | .await; |
| 759 | let org = pim.get("carol", &format!("{}c9.ics", cal("carol"))).await; |
| 760 | assert_eq!( |
| 761 | attendee_param(&org, &addr("alice"), "PARTSTAT").as_deref(), |
| 762 | Some("ACCEPTED"), |
| 763 | "{org}" |
| 764 | ); |
| 765 | let reply = pim.inbox("carol").await; |
| 766 | let reply = reply.iter().find(|m| m.contains("METHOD:REPLY")).unwrap(); |
| 767 | assert!( |
| 768 | reply.contains(&format!("SENT-BY=\"{}\"", addr("bob"))), |
| 769 | "{reply}" |
| 770 | ); |
| 771 | } |
| 772 | |
| 773 | #[tokio::test] |
| 774 | async fn one_resource_per_scheduled_uid() { |
| 775 | let pim = Pim::new().await; |
| 776 | invite(&pim, &[&addr("bob")]).await; |
| 777 | let mk = "<c:mkcalendar xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"/>"; |
| 778 | let r = pim |
| 779 | .req("alice", "MKCALENDAR", "/pim/calendars/alice/work/", &[], mk) |
| 780 | .await; |
| 781 | assert_eq!(r.status, StatusCode::CREATED); |
| 782 | let r = pim |
| 783 | .req( |
| 784 | "alice", |
| 785 | "PUT", |
| 786 | "/pim/calendars/alice/work/again.ics", |
| 787 | &[], |
| 788 | &meeting("20260301T100000Z", &[&addr("bob")]), |
| 789 | ) |
| 790 | .await; |
| 791 | assert_eq!(r.status, StatusCode::FORBIDDEN, "{}", r.text()); |
| 792 | assert!(error_condition(&r).is(CALDAV, "unique-scheduling-object-resource")); |
| 793 | assert!(r.text().contains(ALICE_EVENT), "{}", r.text()); |
| 794 | } |
| 795 | |
| 796 | #[tokio::test] |
| 797 | async fn foreign_uids_are_not_overwritten() { |
| 798 | let pim = Pim::new().await; |
| 799 | pim.room().await; |
| 800 | // alice organizes meet-1, keeps a plain event and books the room. |
| 801 | invite(&pim, &[&addr("bob")]).await; |
| 802 | let plain = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\nBEGIN:VEVENT\r\nUID:plain-1\r\n\ |
| 803 | DTSTAMP:20260101T000000Z\r\nDTSTART:20260310T100000Z\r\nSUMMARY:Mine\r\n\ |
| 804 | END:VEVENT\r\nEND:VCALENDAR\r\n"; |
| 805 | let alice_plain = "/pim/calendars/alice/default/plain.ics"; |
| 806 | pim.put_ok("alice", alice_plain, plain).await; |
| 807 | let slot = future(3, 9); |
| 808 | pim.put_ok( |
| 809 | "alice", |
| 810 | "/pim/calendars/alice/default/book.ics", |
| 811 | &booking("book-1", "alice", &slot, "", &[ROOM]), |
| 812 | ) |
| 813 | .await; |
| 814 | let room_copy = pim.members("bob", "/pim/calendars/board/default/").await; |
| 815 | assert_eq!(room_copy.len(), 1, "{room_copy:?}"); |
| 816 | let before = [ |
| 817 | pim.get("alice", ALICE_EVENT).await, |
| 818 | pim.get("alice", alice_plain).await, |
| 819 | pim.get("bob", &room_copy[0]).await, |
| 820 | ]; |
| 821 | let alice_inbox = pim.inbox("alice").await.len(); |
| 822 | |
| 823 | // carol reuses those UIDs as organizer, inviting alice and the room. |
| 824 | let other = future(5, 14); |
| 825 | for (uid, to) in [ |
| 826 | ("meet-1", addr("alice")), |
| 827 | ("plain-1", addr("alice")), |
| 828 | ("book-1", ROOM.to_string()), |
| 829 | ] { |
| 830 | let path = format!("/pim/calendars/carol/default/{uid}.ics"); |
| 831 | pim.put_ok("carol", &path, &booking(uid, "carol", &other, "", &[&to])) |
| 832 | .await; |
| 833 | let sent = pim.get("carol", &path).await; |
| 834 | assert_eq!( |
| 835 | attendee_param(&sent, &to, "SCHEDULE-STATUS").as_deref(), |
| 836 | Some("3.8"), |
| 837 | "{sent}" |
| 838 | ); |
| 839 | // Removing the attendee would cancel for them. |
| 840 | let r = pim.req("carol", "DELETE", &path, &[], "").await; |
| 841 | assert_eq!(r.status, StatusCode::NO_CONTENT); |
| 842 | } |
| 843 | // carol brings in a copy that names alice as organizer of plain-1 and |
| 844 | // answers it: alice's plain event takes no reply. |
| 845 | let fake = plain.replace( |
| 846 | "SUMMARY:Mine\r\n", |
| 847 | &format!( |
| 848 | "ORGANIZER:{}\r\nATTENDEE:{}\r\n", |
| 849 | addr("alice"), |
| 850 | addr("carol") |
| 851 | ), |
| 852 | ); |
| 853 | let fake_path = "/pim/calendars/carol/default/fake.ics"; |
| 854 | pim.put_ok("carol", fake_path, &fake).await; |
| 855 | let answered = fake.replace( |
| 856 | &format!("ATTENDEE:{}", addr("carol")), |
| 857 | &format!("ATTENDEE;PARTSTAT=ACCEPTED:{}", addr("carol")), |
| 858 | ); |
| 859 | pim.put_ok("carol", fake_path, &answered).await; |
| 860 | |
| 861 | let after = [ |
| 862 | pim.get("alice", ALICE_EVENT).await, |
| 863 | pim.get("alice", alice_plain).await, |
| 864 | pim.get("bob", &room_copy[0]).await, |
| 865 | ]; |
| 866 | assert_eq!(before, after); |
| 867 | assert_eq!(pim.inbox("alice").await.len(), alice_inbox); |
| 868 | |
| 869 | // The real organizer still reaches an attendee who deleted their copy. |
| 870 | let (bob_copy, _) = pim.copy("bob").await; |
| 871 | let r = pim |
| 872 | .req("bob", "DELETE", &bob_copy, &[("schedule-reply", "F")], "") |
| 873 | .await; |
| 874 | assert_eq!(r.status, StatusCode::NO_CONTENT); |
| 875 | let moved = pim |
| 876 | .get("alice", ALICE_EVENT) |
| 877 | .await |
| 878 | .replace("DTSTART:20260301T100000Z", "DTSTART:20260302T100000Z"); |
| 879 | pim.put_ok("alice", ALICE_EVENT, &moved).await; |
| 880 | let (_, again) = pim.copy("bob").await; |
| 881 | assert!( |
| 882 | again.text().contains("20260302T100000Z"), |
| 883 | "{}", |
| 884 | again.text() |
| 885 | ); |
| 886 | } |
| 887 | |
| 888 | #[tokio::test] |
| 889 | async fn move_stays_with_one_owner() { |
| 890 | let pim = Pim::new().await; |
| 891 | let alice = login(&pim.env, "alice", PW).await; |
| 892 | let listed = alice.get("/api/pim/collections").await.json(); |
| 893 | let id = listed |
| 894 | .as_array() |
| 895 | .unwrap() |
| 896 | .iter() |
| 897 | .find(|c| c["kind"] == "calendar" && c["mode"].is_null()) |
| 898 | .unwrap()["id"] |
| 899 | .as_i64() |
| 900 | .unwrap(); |
| 901 | let r = alice |
| 902 | .post_json( |
| 903 | &format!("/api/pim/collections/{id}/shares"), |
| 904 | &json!({"user": "bob", "mode": "rw"}), |
| 905 | ) |
| 906 | .await; |
| 907 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 908 | let mk = "<c:mkcalendar xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"/>"; |
| 909 | let r = pim |
| 910 | .req("bob", "MKCALENDAR", "/pim/calendars/bob/work/", &[], mk) |
| 911 | .await; |
| 912 | assert_eq!(r.status, StatusCode::CREATED); |
| 913 | let own = "/pim/calendars/bob/default/m.ics"; |
| 914 | pim.put_ok( |
| 915 | "bob", |
| 916 | own, |
| 917 | &booking("m1", "bob", "20260401T100000Z", "", &[&addr("carol")]), |
| 918 | ) |
| 919 | .await; |
| 920 | let to = |path: &str| format!("http://localhost{path}"); |
| 921 | // Into alice's lent calendar: refused, the object stays. |
| 922 | let lent = format!("/pim/calendars/bob/shared-{id}/m.ics"); |
| 923 | let r = pim |
| 924 | .req("bob", "MOVE", own, &[("destination", &to(&lent))], "") |
| 925 | .await; |
| 926 | assert_eq!(r.status, StatusCode::FORBIDDEN, "{}", r.text()); |
| 927 | assert_eq!(pim.members("bob", &cal("bob")).await.len(), 1); |
| 928 | // Between bob's own calendars as before. |
| 929 | let work = "/pim/calendars/bob/work/m.ics"; |
| 930 | let r = pim |
| 931 | .req("bob", "MOVE", own, &[("destination", &to(work))], "") |
| 932 | .await; |
| 933 | assert_eq!(r.status, StatusCode::CREATED, "{}", r.text()); |
| 934 | } |
| 935 | |
| 936 | #[tokio::test] |
| 937 | async fn deleting_a_calendar_cancels_its_meetings() { |
| 938 | let pim = Pim::new().await; |
| 939 | let mk = "<c:mkcalendar xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"/>"; |
| 940 | let r = pim |
| 941 | .req("alice", "MKCALENDAR", "/pim/calendars/alice/work/", &[], mk) |
| 942 | .await; |
| 943 | assert_eq!(r.status, StatusCode::CREATED); |
| 944 | pim.put_ok( |
| 945 | "alice", |
| 946 | "/pim/calendars/alice/work/m.ics", |
| 947 | &booking("w1", "alice", "20260401T100000Z", "", &[&addr("bob")]), |
| 948 | ) |
| 949 | .await; |
| 950 | let r = pim |
| 951 | .req("alice", "DELETE", "/pim/calendars/alice/work/", &[], "") |
| 952 | .await; |
| 953 | assert_eq!(r.status, StatusCode::NO_CONTENT, "{}", r.text()); |
| 954 | let r = pim |
| 955 | .req( |
| 956 | "alice", |
| 957 | "PROPFIND", |
| 958 | "/pim/calendars/alice/work/", |
| 959 | &[("depth", "0")], |
| 960 | "", |
| 961 | ) |
| 962 | .await; |
| 963 | assert_eq!(r.status, StatusCode::NOT_FOUND); |
| 964 | let (_, copy) = pim.copy("bob").await; |
| 965 | assert!(copy.text().contains("STATUS:CANCELLED"), "{}", copy.text()); |
| 966 | } |
| 967 | |
| 968 | #[tokio::test] |
| 969 | async fn invitations_go_to_the_chosen_calendar() { |
| 970 | let pim = Pim::new().await; |
| 971 | let mk = "<c:mkcalendar xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"/>"; |
| 972 | let r = pim |
| 973 | .req("bob", "MKCALENDAR", "/pim/calendars/bob/work/", &[], mk) |
| 974 | .await; |
| 975 | assert_eq!(r.status, StatusCode::CREATED); |
| 976 | let inbox = "/pim/calendars/bob/inbox/"; |
| 977 | let set = |href: &str| { |
| 978 | format!( |
| 979 | "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"><d:set><d:prop>\ |
| 980 | <c:schedule-default-calendar-URL><d:href>{href}</d:href></c:schedule-default-calendar-URL>\ |
| 981 | </d:prop></d:set></d:propertyupdate>" |
| 982 | ) |
| 983 | }; |
| 984 | // Not one of bob's calendars: refused with the RFC 6638 precondition. |
| 985 | for bad in [ |
| 986 | "/pim/calendars/alice/default/", |
| 987 | "/pim/calendars/bob/inbox/", |
| 988 | "/pim/calendars/bob/birthdays/", |
| 989 | ] { |
| 990 | let r = pim.req("bob", "PROPPATCH", inbox, &[], &set(bad)).await; |
| 991 | assert_eq!(r.status, StatusCode::MULTI_STATUS); |
| 992 | assert!( |
| 993 | r.text().contains("valid-schedule-default-calendar-URL"), |
| 994 | "{bad}: {}", |
| 995 | r.text() |
| 996 | ); |
| 997 | } |
| 998 | let r = pim |
| 999 | .req( |
| 1000 | "bob", |
| 1001 | "PROPPATCH", |
| 1002 | inbox, |
| 1003 | &[], |
| 1004 | &set("/pim/calendars/bob/work/"), |
| 1005 | ) |
| 1006 | .await; |
| 1007 | assert_eq!(r.status, StatusCode::MULTI_STATUS); |
| 1008 | assert!(r.text().contains("200"), "{}", r.text()); |
| 1009 | let props = "<d:propfind xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"><d:prop>\ |
| 1010 | <c:schedule-default-calendar-URL/></d:prop></d:propfind>"; |
| 1011 | let r = pim |
| 1012 | .req("bob", "PROPFIND", inbox, &[("depth", "0")], props) |
| 1013 | .await; |
| 1014 | assert!( |
| 1015 | r.text().contains("/pim/calendars/bob/work/"), |
| 1016 | "{}", |
| 1017 | r.text() |
| 1018 | ); |
| 1019 | |
| 1020 | invite(&pim, &[&addr("bob")]).await; |
| 1021 | assert_eq!( |
| 1022 | pim.members("bob", "/pim/calendars/bob/work/").await.len(), |
| 1023 | 1 |
| 1024 | ); |
| 1025 | assert!(pim.members("bob", &cal("bob")).await.is_empty()); |
| 1026 | // The chosen calendar is the one that must stay. |
| 1027 | let r = pim |
| 1028 | .req("bob", "DELETE", "/pim/calendars/bob/work/", &[], "") |
| 1029 | .await; |
| 1030 | assert_eq!(r.status, StatusCode::FORBIDDEN); |
| 1031 | assert!(error_condition(&r).is(CALDAV, "default-calendar-needed")); |
| 1032 | // Removing the property goes back to the oldest calendar. |
| 1033 | let remove = "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:c=\"urn:ietf:params:xml:ns:caldav\"><d:remove><d:prop>\ |
| 1034 | <c:schedule-default-calendar-URL/></d:prop></d:remove></d:propertyupdate>"; |
| 1035 | let r = pim.req("bob", "PROPPATCH", inbox, &[], remove).await; |
| 1036 | assert_eq!(r.status, StatusCode::MULTI_STATUS); |
| 1037 | let r = pim |
| 1038 | .req("bob", "PROPFIND", inbox, &[("depth", "0")], props) |
| 1039 | .await; |
| 1040 | assert!( |
| 1041 | r.text().contains("/pim/calendars/bob/default/"), |
| 1042 | "{}", |
| 1043 | r.text() |
| 1044 | ); |
| 1045 | } |
| 1046 | |
| 1047 | #[tokio::test] |
| 1048 | async fn replies_need_a_listed_attendee() { |
| 1049 | let pim = Pim::new().await; |
| 1050 | invite(&pim, &[&addr("bob")]).await; |
| 1051 | // carol brings in her own copy of alice's meeting, then answers it. |
| 1052 | let copy = meeting("20260301T100000Z", &[&addr("carol")]); |
| 1053 | let path = format!("{}meet.ics", cal("carol")); |
| 1054 | pim.put_ok("carol", &path, ©).await; |
| 1055 | let answered = copy.replace("ATTENDEE;RSVP=TRUE:", "ATTENDEE;PARTSTAT=ACCEPTED:"); |
| 1056 | pim.put_ok("carol", &path, &answered).await; |
| 1057 | assert!( |
| 1058 | pim.get("carol", &path) |
| 1059 | .await |
| 1060 | .contains("ORGANIZER;SCHEDULE-STATUS=3.8") |
| 1061 | ); |
| 1062 | assert!(pim.inbox("alice").await.is_empty()); |
| 1063 | let org = pim.get("alice", ALICE_EVENT).await; |
| 1064 | assert!(!org.contains(&addr("carol")), "{org}"); |
| 1065 | } |
| 1066 | |
| 1067 | #[tokio::test] |
| 1068 | async fn move_does_not_overwrite_a_meeting() { |
| 1069 | let pim = Pim::new().await; |
| 1070 | invite(&pim, &[&addr("bob")]).await; |
| 1071 | let plain = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\nBEGIN:VEVENT\r\nUID:p1\r\n\ |
| 1072 | DTSTAMP:20260101T000000Z\r\nDTSTART:20260401T120000Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"; |
| 1073 | let from = format!("{}p1.ics", cal("alice")); |
| 1074 | pim.put_ok("alice", &from, plain).await; |
| 1075 | let r = pim |
| 1076 | .req("alice", "MOVE", &from, &[("destination", ALICE_EVENT)], "") |
| 1077 | .await; |
| 1078 | assert_eq!(r.status, StatusCode::FORBIDDEN, "{}", r.text()); |
| 1079 | assert!(pim.get("alice", ALICE_EVENT).await.contains("UID:meet-1")); |
| 1080 | } |
| 1081 | |
| 1082 | #[tokio::test] |
| 1083 | async fn imports_on_a_plain_loan_skip_meetings() { |
| 1084 | let pim = Pim::new().await; |
| 1085 | invite(&pim, &[&addr("bob")]).await; |
| 1086 | let alice = login(&pim.env, "alice", PW).await; |
| 1087 | let listed = alice.get("/api/pim/collections").await.json(); |
| 1088 | let id = listed |
| 1089 | .as_array() |
| 1090 | .unwrap() |
| 1091 | .iter() |
| 1092 | .find(|c| c["kind"] == "calendar" && c["mode"].is_null()) |
| 1093 | .unwrap()["id"] |
| 1094 | .as_i64() |
| 1095 | .unwrap(); |
| 1096 | let r = alice |
| 1097 | .post_json( |
| 1098 | &format!("/api/pim/collections/{id}/shares"), |
| 1099 | &json!({"user": "carol", "mode": "rw"}), |
| 1100 | ) |
| 1101 | .await; |
| 1102 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 1103 | |
| 1104 | let carol = login(&pim.env, "carol", PW).await; |
| 1105 | let moved = meeting("20260302T100000Z", &[]); |
| 1106 | let plain = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//t//EN\r\nBEGIN:VEVENT\r\nUID:p1\r\n\ |
| 1107 | DTSTAMP:20260101T000000Z\r\nDTSTART:20260401T120000Z\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n"; |
| 1108 | let r = carol |
| 1109 | .raw( |
| 1110 | Method::POST, |
| 1111 | &format!("/api/pim/collections/{id}/import"), |
| 1112 | &[], |
| 1113 | format!("{moved}{plain}").into_bytes(), |
| 1114 | ) |
| 1115 | .await; |
| 1116 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 1117 | let result = r.json(); |
| 1118 | assert_eq!( |
| 1119 | (result["created"].as_i64(), result["updated"].as_i64()), |
| 1120 | (Some(1), Some(0)) |
| 1121 | ); |
| 1122 | assert_eq!(result["skipped"][0]["reason"], "need-privileges"); |
| 1123 | let org = pim.get("alice", ALICE_EVENT).await; |
| 1124 | assert!(org.contains("DTSTART:20260301T100000Z"), "{org}"); |
| 1125 | } |
| 1126 | |
| 1127 | #[tokio::test] |
| 1128 | async fn deleting_an_organizer_cancels_and_forgets_it() { |
| 1129 | let pim = Pim::new().await; |
| 1130 | invite(&pim, &[&addr("bob")]).await; |
| 1131 | let id = user_id(&pim.admin, "alice").await; |
| 1132 | let r = pim.admin.delete(&format!("/api/admin/users/{id}")).await; |
| 1133 | assert_eq!(r.status, StatusCode::OK); |
| 1134 | let (_, copy) = pim.copy("bob").await; |
| 1135 | let copy = unfold(©.text()); |
| 1136 | assert!(copy.contains("STATUS:CANCELLED"), "{copy}"); |
| 1137 | assert!(!copy.contains("alice@dovenest.invalid"), "{copy}"); |
| 1138 | assert!(copy.contains("@deleted."), "{copy}"); |
| 1139 | let inbox = pim.inbox("bob").await; |
| 1140 | assert!(!inbox.is_empty()); |
| 1141 | assert!( |
| 1142 | inbox.iter().all(|m| !m.contains("alice@dovenest.invalid")), |
| 1143 | "{inbox:?}" |
| 1144 | ); |
| 1145 | } |
| 1146 | |
| 1147 | #[tokio::test] |
| 1148 | async fn deleting_a_disabled_user_retracts_its_meetings() { |
| 1149 | let disable_and_delete = async |pim: &Pim, user: &str| { |
| 1150 | let id = user_id(&pim.admin, user).await; |
| 1151 | let r = pim |
| 1152 | .admin |
| 1153 | .put_json( |
| 1154 | &format!("/api/admin/users/{id}"), |
| 1155 | &json!({ "active": false }), |
| 1156 | ) |
| 1157 | .await; |
| 1158 | assert_eq!(r.status, StatusCode::OK); |
| 1159 | let r = pim.admin.delete(&format!("/api/admin/users/{id}")).await; |
| 1160 | assert_eq!(r.status, StatusCode::OK); |
| 1161 | }; |
| 1162 | let pim = Pim::new().await; |
| 1163 | invite(&pim, &[&addr("bob")]).await; |
| 1164 | disable_and_delete(&pim, "bob").await; |
| 1165 | let org = unfold(&pim.req("alice", "GET", ALICE_EVENT, &[], "").await.text()); |
| 1166 | assert!(org.contains("PARTSTAT=DECLINED"), "{org}"); |
| 1167 | assert!( |
| 1168 | pim.inbox("alice") |
| 1169 | .await |
| 1170 | .iter() |
| 1171 | .any(|m| m.contains("METHOD:REPLY")), |
| 1172 | "the decline reaches the organizer's inbox" |
| 1173 | ); |
| 1174 | |
| 1175 | let pim = Pim::new().await; |
| 1176 | invite(&pim, &[&addr("bob")]).await; |
| 1177 | disable_and_delete(&pim, "alice").await; |
| 1178 | assert!( |
| 1179 | pim.inbox("bob") |
| 1180 | .await |
| 1181 | .iter() |
| 1182 | .any(|m| m.contains("METHOD:CANCEL")), |
| 1183 | "the cancel reaches the attendee's inbox" |
| 1184 | ); |
| 1185 | } |
| 1186 | |
| 1187 | #[tokio::test] |
| 1188 | async fn a_delete_that_matches_nothing_writes_nothing() { |
| 1189 | use server::db::{PimObject, PimOp}; |
| 1190 | let pim = Pim::new().await; |
| 1191 | let db = &pim.env.state.db; |
| 1192 | let pid = db |
| 1193 | .principal_of(user_id(&pim.admin, "bob").await) |
| 1194 | .await |
| 1195 | .unwrap(); |
| 1196 | db.pim_ensure_defaults(pid).await.unwrap(); |
| 1197 | let cal = db |
| 1198 | .pim_collection(pid, server::db::PimKind::Calendar, "default") |
| 1199 | .await |
| 1200 | .unwrap() |
| 1201 | .unwrap(); |
| 1202 | let ops = [PimOp::Put { |
| 1203 | collection_id: cal.id, |
| 1204 | obj: PimObject { |
| 1205 | name: "x.ics".into(), |
| 1206 | uid: "x".into(), |
| 1207 | component: "VEVENT".into(), |
| 1208 | etag: "\"e\"".into(), |
| 1209 | ..Default::default() |
| 1210 | }, |
| 1211 | data: b"x".to_vec(), |
| 1212 | }]; |
| 1213 | assert!(!db.delete_user(i64::MAX, &ops).await.unwrap()); |
| 1214 | assert!(!db.delete_room(i64::MAX, &ops).await.unwrap()); |
| 1215 | assert!(db.pim_object(cal.id, "x.ics").await.unwrap().is_none()); |
| 1216 | } |
| 1217 | |
| 1218 | #[tokio::test] |
| 1219 | async fn find_uid_prefers_the_scheduling_copy() { |
| 1220 | use server::db::{PimCollection, PimKind, PimObject, PimOp}; |
| 1221 | let pim = Pim::new().await; |
| 1222 | let db = &pim.env.state.db; |
| 1223 | let pid = db |
| 1224 | .principal_of(user_id(&pim.admin, "bob").await) |
| 1225 | .await |
| 1226 | .unwrap(); |
| 1227 | db.pim_ensure_defaults(pid).await.unwrap(); |
| 1228 | let work = PimCollection { |
| 1229 | slug: "work".into(), |
| 1230 | components: "VEVENT".into(), |
| 1231 | ..Default::default() |
| 1232 | }; |
| 1233 | assert!( |
| 1234 | db.pim_create_collection(pid, PimKind::Calendar, &work, &[]) |
| 1235 | .await |
| 1236 | .unwrap() |
| 1237 | ); |
| 1238 | let default = db |
| 1239 | .pim_collection(pid, PimKind::Calendar, "default") |
| 1240 | .await |
| 1241 | .unwrap() |
| 1242 | .unwrap(); |
| 1243 | let work = db |
| 1244 | .pim_collection(pid, PimKind::Calendar, "work") |
| 1245 | .await |
| 1246 | .unwrap() |
| 1247 | .unwrap(); |
| 1248 | let put = |collection_id: i64, name: &str, schedule_tag: Option<&str>| PimOp::Put { |
| 1249 | collection_id, |
| 1250 | obj: PimObject { |
| 1251 | name: name.into(), |
| 1252 | uid: "meet-1".into(), |
| 1253 | component: "VEVENT".into(), |
| 1254 | etag: "\"e\"".into(), |
| 1255 | schedule_tag: schedule_tag.map(Into::into), |
| 1256 | ..Default::default() |
| 1257 | }, |
| 1258 | data: b"x".to_vec(), |
| 1259 | }; |
| 1260 | // The plain copy is older, so only the schedule tag can rank the other first. |
| 1261 | db.pim_apply(&[put(default.id, "plain.ics", None)]) |
| 1262 | .await |
| 1263 | .unwrap(); |
| 1264 | db.pim_apply(&[put(work.id, "meeting.ics", Some("\"s\""))]) |
| 1265 | .await |
| 1266 | .unwrap(); |
| 1267 | |
| 1268 | let (id, obj, _) = db.pim_find_uid(pid, "meet-1").await.unwrap().unwrap(); |
| 1269 | assert_eq!((id, obj.name.as_str()), (work.id, "meeting.ics")); |
| 1270 | } |
| 1271 |