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}@filebrowser.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.filebrowser.invalid"]).await; |
| 375 | let org = pim.req("alice", "GET", ALICE_EVENT, &[], "").await.text(); |
| 376 | let room = "mailto:board@rooms.filebrowser.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 |