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_sends_nothing_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 neither a copy nor a message. |
| 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 | |
| 310 | // One scheduling object per UID and owner (RFC 6638, 3.2.4.1). |
| 311 | io.dav("alice", "MKCALENDAR", "/pim/calendars/alice/work/", "") |
| 312 | .await; |
| 313 | let work = io.id("/pim/calendars/alice/work/").await; |
| 314 | let r = io.import(work, &meeting).await; |
| 315 | assert_eq!(r["created"], 0); |
| 316 | assert_eq!(r["skipped"][0]["uid"], "meet"); |
| 317 | assert_eq!( |
| 318 | r["skipped"][0]["reason"], |
| 319 | "unique-scheduling-object-resource" |
| 320 | ); |
| 321 | |
| 322 | // A read-only loan cannot be imported into. |
| 323 | let bob = login(&io.env, "bob", PW).await; |
| 324 | io.alice |
| 325 | .post_json( |
| 326 | &format!("/api/pim/collections/{cal}/shares"), |
| 327 | &json!({ "user": "bob", "mode": "ro" }), |
| 328 | ) |
| 329 | .await; |
| 330 | let r = bob |
| 331 | .raw( |
| 332 | Method::POST, |
| 333 | &format!("/api/pim/collections/{cal}/import"), |
| 334 | &[("content-type", "text/calendar")], |
| 335 | event("x", "x", "").into_bytes(), |
| 336 | ) |
| 337 | .await; |
| 338 | assert_eq!(r.status, StatusCode::FORBIDDEN); |
| 339 | // But exported. |
| 340 | let r = bob.get(&format!("/api/pim/collections/{cal}/export")).await; |
| 341 | assert_eq!(r.status, StatusCode::OK); |
| 342 | } |
| 343 | |
| 344 | #[tokio::test] |
| 345 | async fn export_round_trip() { |
| 346 | let io = Io::new().await; |
| 347 | let cal = io.id(CAL).await; |
| 348 | io.import(cal, &event("r1", "One", "")).await; |
| 349 | io.import(cal, &event("r2", "Two", "")).await; |
| 350 | |
| 351 | let url = format!("/api/pim/collections/{cal}/export"); |
| 352 | let r = io.alice.get(&url).await; |
| 353 | assert_eq!(r.status, StatusCode::OK); |
| 354 | let disposition = r.header("content-disposition").unwrap(); |
| 355 | assert!(disposition.starts_with("attachment;") && disposition.contains(".ics")); |
| 356 | let exported = r.text(); |
| 357 | // Download only: there is no saving into a folder. |
| 358 | let r = io.alice.post_json(&url, &json!({})).await; |
| 359 | assert_eq!(r.status, StatusCode::METHOD_NOT_ALLOWED); |
| 360 | |
| 361 | // The export imports back as the same objects. |
| 362 | io.dav("alice", "MKCALENDAR", "/pim/calendars/alice/copy/", "") |
| 363 | .await; |
| 364 | let copy = io.id("/pim/calendars/alice/copy/").await; |
| 365 | let r = io.import(copy, &exported).await; |
| 366 | assert_eq!(r["created"], 2, "{r}"); |
| 367 | |
| 368 | // vCards, one without UID. |
| 369 | let book = io.id("/pim/addressbooks/alice/default/").await; |
| 370 | let cards = "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:c1\r\nFN:One\r\nEND:VCARD\r\n\ |
| 371 | BEGIN:VCARD\r\nVERSION:4.0\r\nFN:Two\r\nEND:VCARD\r\n"; |
| 372 | let r = io.import(book, cards).await; |
| 373 | assert_eq!(r["created"], 2, "{r}"); |
| 374 | let r = io.import(book, cards).await; |
| 375 | assert_eq!(r["updated"], 2, "{r}"); |
| 376 | |
| 377 | // The system address book has no collection id of its own. |
| 378 | let r = io.alice.get("/api/pim/system/export").await; |
| 379 | assert_eq!(r.status, StatusCode::OK); |
| 380 | assert!(r.header("content-disposition").unwrap().contains(".vcf")); |
| 381 | assert!(r.text().contains("FN:alice")); |
| 382 | } |
| 383 | |
| 384 | #[tokio::test] |
| 385 | async fn import_as_a_new_collection() { |
| 386 | let io = Io::new().await; |
| 387 | let post = |query: &str, body: String| { |
| 388 | let (client, query) = (&io.alice, query.to_string()); |
| 389 | async move { |
| 390 | let r = client |
| 391 | .raw( |
| 392 | Method::POST, |
| 393 | &format!("/api/pim/import?{query}"), |
| 394 | &[("content-type", "text/calendar")], |
| 395 | body.into_bytes(), |
| 396 | ) |
| 397 | .await; |
| 398 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 399 | r.json() |
| 400 | } |
| 401 | }; |
| 402 | let before = collections(&io.alice).await; |
| 403 | let vevent = |uid: &str, summary: &str| { |
| 404 | format!( |
| 405 | "BEGIN:VEVENT\r\nUID:{uid}\r\nDTSTAMP:20260101T000000Z\r\nDTSTART:20260101T100000Z\r\nSUMMARY:{summary}\r\nEND:VEVENT\r\n" |
| 406 | ) |
| 407 | }; |
| 408 | |
| 409 | // The file names itself and its color. |
| 410 | let ics = format!( |
| 411 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nX-WR-CALNAME:Ferien\r\nX-APPLE-CALENDAR-COLOR:#FF2968FF\r\n{}END:VCALENDAR\r\n", |
| 412 | vevent("f1", "Sommer") |
| 413 | ); |
| 414 | let r = post("kind=calendar&file=holidays.ics&color=%2322c55e", ics).await; |
| 415 | assert_eq!(r["created"], 1, "{r}"); |
| 416 | assert_eq!(r["collection"]["name"], "Ferien", "{r}"); |
| 417 | assert_eq!(r["collection"]["color"], "#FF2968FF", "{r}"); |
| 418 | |
| 419 | // A typed name wins; without a color in the file the requested one counts. |
| 420 | let plain = format!( |
| 421 | "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nCOLOR:teal\r\n{}END:VCALENDAR\r\n", |
| 422 | vevent("f2", "Winter") |
| 423 | ); |
| 424 | let r = post("kind=calendar&name=Mine&file=x.ics&color=%2322c55e", plain).await; |
| 425 | assert_eq!(r["collection"]["name"], "Mine", "{r}"); |
| 426 | assert_eq!(r["collection"]["color"], "#22c55e", "{r}"); |
| 427 | |
| 428 | // An address book takes the file name. |
| 429 | let vcf = "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:c1\r\nFN:Ann\r\nN:;Ann;;;\r\nEND:VCARD\r\n"; |
| 430 | let r = post("kind=addressbook&file=Team%20contacts.vcf", vcf.to_string()).await; |
| 431 | assert_eq!(r["collection"]["name"], "Team contacts", "{r}"); |
| 432 | assert_eq!(r["collection"]["kind"], "addressbook", "{r}"); |
| 433 | assert_eq!(collections(&io.alice).await, before + 3); |
| 434 | |
| 435 | // Nothing importable: reported, and no empty collection is left behind. |
| 436 | 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"; |
| 437 | let r = post("kind=calendar&file=bad.ics", broken.to_string()).await; |
| 438 | assert_eq!(r["collection"], Value::Null, "{r}"); |
| 439 | assert_eq!(r["skipped_total"], 1, "{r}"); |
| 440 | assert_eq!(collections(&io.alice).await, before + 3); |
| 441 | } |
| 442 | |
| 443 | async fn collections(c: &Client) -> usize { |
| 444 | c.get("/api/pim/collections") |
| 445 | .await |
| 446 | .json() |
| 447 | .as_array() |
| 448 | .unwrap() |
| 449 | .len() |
| 450 | } |
| 451 | |
| 452 | #[tokio::test] |
| 453 | async fn share_candidates_leave_out_owner_borrowers_and_disabled() { |
| 454 | let io = Io::new().await; |
| 455 | let admin = login(&io.env, "admin", "admin1234").await; |
| 456 | create_user(&admin, "carol", PW, &[]).await; |
| 457 | create_user(&admin, "dave", PW, &[]).await; |
| 458 | let dave = user_id(&admin, "dave").await; |
| 459 | let r = admin |
| 460 | .put_json( |
| 461 | &format!("/api/admin/users/{dave}"), |
| 462 | &json!({"active": false}), |
| 463 | ) |
| 464 | .await; |
| 465 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 466 | let r = admin |
| 467 | .post_json( |
| 468 | "/api/admin/rooms", |
| 469 | &json!({"name": "atrium", "kind": "room"}), |
| 470 | ) |
| 471 | .await; |
| 472 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 473 | |
| 474 | let id = io.id(CAL).await; |
| 475 | let r = io |
| 476 | .alice |
| 477 | .post_json( |
| 478 | &format!("/api/pim/collections/{id}/shares"), |
| 479 | &json!({"user": "bob", "mode": "ro"}), |
| 480 | ) |
| 481 | .await; |
| 482 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 483 | |
| 484 | let names = |v: Value| -> Vec<String> { |
| 485 | v.as_array() |
| 486 | .unwrap() |
| 487 | .iter() |
| 488 | .map(|c| c["name"].as_str().unwrap().to_string()) |
| 489 | .collect() |
| 490 | }; |
| 491 | let r = io |
| 492 | .alice |
| 493 | .get(&format!("/api/pim/collections/{id}/shares/candidates")) |
| 494 | .await; |
| 495 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 496 | // Not alice herself, not bob (a borrower), not dave (disabled), no room. |
| 497 | assert_eq!(names(r.json()), ["admin", "carol"]); |
| 498 | |
| 499 | // Only the owner may ask. |
| 500 | let bob = login(&io.env, "bob", PW).await; |
| 501 | let r = bob |
| 502 | .get(&format!("/api/pim/collections/{id}/shares/candidates")) |
| 503 | .await; |
| 504 | assert_eq!(r.status, StatusCode::NOT_FOUND); |
| 505 | } |
| 506 | |
| 507 | #[tokio::test] |
| 508 | async fn a_skipped_import_into_a_new_user_leaves_only_the_default_calendar() { |
| 509 | use server::db::PimKind; |
| 510 | let io = Io::new().await; |
| 511 | let client = login(&io.env, "bob", PW).await; |
| 512 | 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"; |
| 513 | let r = client |
| 514 | .raw( |
| 515 | Method::POST, |
| 516 | "/api/pim/import?kind=calendar&file=bad.ics", |
| 517 | &[("content-type", "text/calendar")], |
| 518 | broken.as_bytes().to_vec(), |
| 519 | ) |
| 520 | .await; |
| 521 | assert_eq!(r.status, StatusCode::OK, "{}", r.text()); |
| 522 | assert_eq!(r.json()["collection"], Value::Null); |
| 523 | assert_eq!(r.json()["skipped_total"], 1); |
| 524 | |
| 525 | let db = &io.env.state.db; |
| 526 | let bob = db.find_user_by_name("bob").await.unwrap().unwrap(); |
| 527 | let pid = db.principal_of(bob.id).await.unwrap(); |
| 528 | let slugs: Vec<_> = db |
| 529 | .pim_collections(pid, PimKind::Calendar) |
| 530 | .await |
| 531 | .unwrap() |
| 532 | .into_iter() |
| 533 | .map(|c| c.slug) |
| 534 | .filter(|s| s != "inbox" && s != "outbox") |
| 535 | .collect(); |
| 536 | assert_eq!(slugs, ["default"]); |
| 537 | } |
| 538 |