| 1 | use std::collections::VecDeque; |
| 2 | use std::sync::Mutex; |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | use chatsvc::messages::{ConversationRef, Messages}; |
| 6 | use chatsvc::{Error, Result}; |
| 7 | use serde_json::Value; |
| 8 | use session::{ApiResponse, Method, Request}; |
| 9 | |
| 10 | struct Mock { |
| 11 | answers: Mutex<VecDeque<u16>>, |
| 12 | requests: Mutex<Vec<Request>>, |
| 13 | } |
| 14 | |
| 15 | impl Mock { |
| 16 | fn new(statuses: &[u16]) -> Self { |
| 17 | Mock { |
| 18 | answers: Mutex::new(statuses.iter().copied().collect()), |
| 19 | requests: Mutex::new(Vec::new()), |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | fn requests(&self) -> Vec<Request> { |
| 24 | self.requests.lock().unwrap().clone() |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl chatsvc::messages::MessageTransport for &Mock { |
| 29 | async fn send(&self, request: Request) -> Result<ApiResponse> { |
| 30 | self.requests.lock().unwrap().push(request); |
| 31 | Ok(ApiResponse { |
| 32 | status: self |
| 33 | .answers |
| 34 | .lock() |
| 35 | .unwrap() |
| 36 | .pop_front() |
| 37 | .expect("mock exhausted"), |
| 38 | body: Value::Null, |
| 39 | retry_after: None, |
| 40 | }) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | fn messages(mock: &Mock) -> Messages<&Mock> { |
| 45 | Messages::with_transport(mock, "emea").with_retry_delay(Duration::ZERO) |
| 46 | } |
| 47 | |
| 48 | const BASE: &str = "https://teams.cloud.microsoft/api/chatsvc/emea/v1/users/ME/conversations"; |
| 49 | |
| 50 | #[tokio::test] |
| 51 | async fn edit_chat_puts_the_message_object() { |
| 52 | let mock = Mock::new(&[200]); |
| 53 | messages(&mock) |
| 54 | .edit_message(&ConversationRef::chat("19:a@thread.v2"), "m1", "<p>hi</p>") |
| 55 | .await |
| 56 | .unwrap(); |
| 57 | let requests = mock.requests(); |
| 58 | assert_eq!(requests.len(), 1); |
| 59 | assert_eq!(requests[0].method, Method::Put); |
| 60 | assert_eq!( |
| 61 | requests[0].url, |
| 62 | format!("{BASE}/19%3Aa%40thread.v2/messages/m1") |
| 63 | ); |
| 64 | let body = requests[0].body.as_ref().unwrap(); |
| 65 | assert_eq!(body["id"], "m1"); |
| 66 | assert_eq!(body["content"], "<p>hi</p>"); |
| 67 | assert_eq!(body["messagetype"], "RichText/Html"); |
| 68 | assert_eq!(body["contenttype"], "text"); |
| 69 | assert!( |
| 70 | body["properties"]["edittime"] |
| 71 | .as_str() |
| 72 | .unwrap() |
| 73 | .parse::<i64>() |
| 74 | .is_ok() |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | #[tokio::test] |
| 79 | async fn channel_reply_encodes_the_messageid_suffix() { |
| 80 | let mock = Mock::new(&[200]); |
| 81 | messages(&mock) |
| 82 | .edit_message( |
| 83 | &ConversationRef::channel_reply("19:c@thread.tacv2", "root1"), |
| 84 | "r1", |
| 85 | "x", |
| 86 | ) |
| 87 | .await |
| 88 | .unwrap(); |
| 89 | assert_eq!( |
| 90 | mock.requests()[0].url, |
| 91 | format!("{BASE}/19%3Ac%40thread.tacv2%3Bmessageid%3Droot1/messages/r1") |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | #[tokio::test] |
| 96 | async fn channel_root_uses_the_channel_id() { |
| 97 | let mock = Mock::new(&[200]); |
| 98 | messages(&mock) |
| 99 | .edit_message( |
| 100 | &ConversationRef::channel_root("19:c@thread.tacv2"), |
| 101 | "m1", |
| 102 | "x", |
| 103 | ) |
| 104 | .await |
| 105 | .unwrap(); |
| 106 | assert_eq!( |
| 107 | mock.requests()[0].url, |
| 108 | format!("{BASE}/19%3Ac%40thread.tacv2/messages/m1") |
| 109 | ); |
| 110 | } |
| 111 | |
| 112 | #[tokio::test] |
| 113 | async fn editing_a_post_sends_its_subject_again() { |
| 114 | let mock = Mock::new(&[200]); |
| 115 | messages(&mock) |
| 116 | .edit_post( |
| 117 | &ConversationRef::channel_root("19:c@thread.tacv2"), |
| 118 | "m1", |
| 119 | "x", |
| 120 | None, |
| 121 | Some("Plan"), |
| 122 | ) |
| 123 | .await |
| 124 | .unwrap(); |
| 125 | let body = mock.requests()[0].body.clone().unwrap(); |
| 126 | assert_eq!(body["properties"]["subject"], "Plan"); |
| 127 | assert!(body["properties"]["edittime"].is_string()); |
| 128 | } |
| 129 | |
| 130 | #[tokio::test] |
| 131 | async fn delete_is_soft_without_a_body() { |
| 132 | let mock = Mock::new(&[200]); |
| 133 | messages(&mock) |
| 134 | .soft_delete_message(&ConversationRef::chat("19:a@thread.v2"), "m1") |
| 135 | .await |
| 136 | .unwrap(); |
| 137 | let requests = mock.requests(); |
| 138 | assert_eq!(requests[0].method, Method::Delete); |
| 139 | assert_eq!( |
| 140 | requests[0].url, |
| 141 | format!("{BASE}/19%3Aa%40thread.v2/messages/m1?behavior=softDelete") |
| 142 | ); |
| 143 | assert!(requests[0].body.is_none()); |
| 144 | } |
| 145 | |
| 146 | #[tokio::test] |
| 147 | async fn undelete_clears_the_delete_time_property_like_teams() { |
| 148 | let mock = Mock::new(&[200]); |
| 149 | messages(&mock) |
| 150 | .undelete_message(&ConversationRef::chat("19:a@thread.v2"), "m1") |
| 151 | .await |
| 152 | .unwrap(); |
| 153 | let requests = mock.requests(); |
| 154 | assert_eq!(requests.len(), 1); |
| 155 | assert_eq!(requests[0].method, Method::Delete); |
| 156 | assert_eq!( |
| 157 | requests[0].url, |
| 158 | format!("{BASE}/19%3Aa%40thread.v2/messages/m1/properties?name=deletetime") |
| 159 | ); |
| 160 | assert_eq!( |
| 161 | requests[0].body, |
| 162 | Some(serde_json::json!({"deletetime": null})) |
| 163 | ); |
| 164 | } |
| 165 | |
| 166 | #[tokio::test] |
| 167 | async fn edit_retries_once_on_404() { |
| 168 | let mock = Mock::new(&[404, 200]); |
| 169 | messages(&mock) |
| 170 | .edit_message(&ConversationRef::chat("19:a"), "m1", "x") |
| 171 | .await |
| 172 | .unwrap(); |
| 173 | let requests = mock.requests(); |
| 174 | assert_eq!(requests.len(), 2); |
| 175 | assert_eq!(requests[0].url, requests[1].url); |
| 176 | } |
| 177 | |
| 178 | #[tokio::test] |
| 179 | async fn edit_gives_up_after_the_second_404() { |
| 180 | let mock = Mock::new(&[404, 404]); |
| 181 | let error = messages(&mock) |
| 182 | .edit_message(&ConversationRef::chat("19:a"), "m1", "x") |
| 183 | .await |
| 184 | .unwrap_err(); |
| 185 | assert!(matches!( |
| 186 | error, |
| 187 | Error::Session(session::Error::Api { status: 404, .. }) |
| 188 | )); |
| 189 | assert_eq!(mock.requests().len(), 2); |
| 190 | } |
| 191 | |
| 192 | #[tokio::test] |
| 193 | async fn delete_does_not_retry_on_404() { |
| 194 | let mock = Mock::new(&[404]); |
| 195 | assert!( |
| 196 | messages(&mock) |
| 197 | .soft_delete_message(&ConversationRef::chat("19:a"), "m1") |
| 198 | .await |
| 199 | .is_err() |
| 200 | ); |
| 201 | assert_eq!(mock.requests().len(), 1); |
| 202 | } |
| 203 | |
| 204 | #[test] |
| 205 | fn messages_use_the_ic3_scope() { |
| 206 | let scope = chatsvc::messages::messages_scope(); |
| 207 | assert_eq!(scope.resource, session::IC3); |
| 208 | assert_eq!(scope.name, "Teams.AccessAsUser.All"); |
| 209 | } |
| 210 | |
| 211 | #[tokio::test] |
| 212 | async fn set_emotion_puts_key_and_numeric_timestamp() { |
| 213 | let mock = Mock::new(&[200]); |
| 214 | messages(&mock) |
| 215 | .set_emotion( |
| 216 | &ConversationRef::chat("19:a@thread.v2"), |
| 217 | "m1", |
| 218 | "1f603_grinningfacewithbigeyes", |
| 219 | ) |
| 220 | .await |
| 221 | .unwrap(); |
| 222 | let requests = mock.requests(); |
| 223 | assert_eq!(requests[0].method, Method::Put); |
| 224 | assert!( |
| 225 | requests[0] |
| 226 | .url |
| 227 | .ends_with("/messages/m1/properties?name=emotions") |
| 228 | ); |
| 229 | let emotions = &requests[0].body.as_ref().unwrap()["emotions"]; |
| 230 | assert_eq!(emotions["key"], "1f603_grinningfacewithbigeyes"); |
| 231 | assert!(emotions["value"].is_number()); |
| 232 | } |
| 233 | |
| 234 | #[tokio::test] |
| 235 | async fn unset_emotion_deletes_with_key_body_and_retries_a_500() { |
| 236 | let mock = Mock::new(&[500, 200]); |
| 237 | messages(&mock) |
| 238 | .unset_emotion(&ConversationRef::chat("19:a@thread.v2"), "m1", "like") |
| 239 | .await |
| 240 | .unwrap(); |
| 241 | let requests = mock.requests(); |
| 242 | assert_eq!(requests.len(), 2); |
| 243 | assert_eq!(requests[1].method, Method::Delete); |
| 244 | assert!(requests[1].url.ends_with("/properties?name=emotions")); |
| 245 | assert_eq!( |
| 246 | requests[1].body.as_ref().unwrap(), |
| 247 | &serde_json::json!({"emotions": {"key": "like"}}) |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | #[tokio::test] |
| 252 | async fn emotion_gives_up_after_the_second_500() { |
| 253 | let mock = Mock::new(&[500, 500]); |
| 254 | assert!( |
| 255 | messages(&mock) |
| 256 | .set_emotion(&ConversationRef::chat("19:a"), "m1", "like") |
| 257 | .await |
| 258 | .is_err() |
| 259 | ); |
| 260 | assert_eq!(mock.requests().len(), 2); |
| 261 | } |
| 262 | |
| 263 | #[tokio::test] |
| 264 | async fn unset_emotions_tries_every_key_and_succeeds_if_one_does() { |
| 265 | let mock = Mock::new(&[404, 200]); |
| 266 | messages(&mock) |
| 267 | .unset_emotions(&ConversationRef::chat("19:a"), "m1", &["primary", "alias"]) |
| 268 | .await |
| 269 | .unwrap(); |
| 270 | let requests = mock.requests(); |
| 271 | assert_eq!(requests.len(), 2); |
| 272 | assert_eq!( |
| 273 | requests[0].body.as_ref().unwrap()["emotions"]["key"], |
| 274 | "primary" |
| 275 | ); |
| 276 | assert_eq!( |
| 277 | requests[1].body.as_ref().unwrap()["emotions"]["key"], |
| 278 | "alias" |
| 279 | ); |
| 280 | } |
| 281 | |
| 282 | #[tokio::test] |
| 283 | async fn unset_emotions_fails_when_every_key_fails() { |
| 284 | let mock = Mock::new(&[404, 404]); |
| 285 | assert!( |
| 286 | messages(&mock) |
| 287 | .unset_emotions(&ConversationRef::chat("19:a"), "m1", &["primary", "alias"]) |
| 288 | .await |
| 289 | .is_err() |
| 290 | ); |
| 291 | } |
| 292 | |
| 293 | #[tokio::test] |
| 294 | async fn send_typing_posts_a_control_message_without_retrying() { |
| 295 | let mock = Mock::new(&[201, 201, 500]); |
| 296 | let chat = ConversationRef::chat("19:a@thread.v2"); |
| 297 | messages(&mock).send_typing(&chat, true).await.unwrap(); |
| 298 | messages(&mock).send_typing(&chat, false).await.unwrap(); |
| 299 | assert!(messages(&mock).send_typing(&chat, true).await.is_err()); |
| 300 | let requests = mock.requests(); |
| 301 | assert_eq!(requests.len(), 3); |
| 302 | assert_eq!(requests[0].method, Method::Post); |
| 303 | assert_eq!( |
| 304 | requests[0].url, |
| 305 | format!("{BASE}/19%3Aa%40thread.v2/messages") |
| 306 | ); |
| 307 | let start = requests[0].body.as_ref().unwrap(); |
| 308 | assert_eq!(start["messagetype"], "Control/Typing"); |
| 309 | assert_eq!(start["content"], ""); |
| 310 | assert_eq!(start["contenttype"], "Application/Message"); |
| 311 | assert_eq!( |
| 312 | requests[1].body.as_ref().unwrap()["messagetype"], |
| 313 | "Control/ClearTyping" |
| 314 | ); |
| 315 | } |
| 316 | |
| 317 | #[tokio::test] |
| 318 | async fn send_typing_addresses_a_channel_reply_thread() { |
| 319 | let mock = Mock::new(&[201]); |
| 320 | messages(&mock) |
| 321 | .send_typing( |
| 322 | &ConversationRef::channel_reply("19:c@thread.tacv2", "99"), |
| 323 | true, |
| 324 | ) |
| 325 | .await |
| 326 | .unwrap(); |
| 327 | assert_eq!( |
| 328 | mock.requests()[0].url, |
| 329 | format!("{BASE}/19%3Ac%40thread.tacv2%3Bmessageid%3D99/messages") |
| 330 | ); |
| 331 | } |
| 332 | |
| 333 | #[tokio::test] |
| 334 | async fn set_links_puts_the_array_as_a_string() { |
| 335 | let mock = Mock::new(&[200]); |
| 336 | let links = r#"[{"itemid":"0","url":"https://a.example"}]"#; |
| 337 | messages(&mock) |
| 338 | .set_links(&ConversationRef::chat("19:a@thread.v2"), "m1", links) |
| 339 | .await |
| 340 | .unwrap(); |
| 341 | let requests = mock.requests(); |
| 342 | assert_eq!(requests[0].method, Method::Put); |
| 343 | assert_eq!( |
| 344 | requests[0].url, |
| 345 | format!("{BASE}/19%3Aa%40thread.v2/messages/m1/properties?name=links") |
| 346 | ); |
| 347 | assert_eq!(requests[0].body.as_ref().unwrap()["links"], links); |
| 348 | } |
| 349 | |
| 350 | #[tokio::test] |
| 351 | async fn set_links_in_a_channel_reply_targets_the_thread_conversation() { |
| 352 | let mock = Mock::new(&[200]); |
| 353 | messages(&mock) |
| 354 | .set_links( |
| 355 | &ConversationRef::channel_reply("19:c@thread.tacv2", "root1"), |
| 356 | "r1", |
| 357 | "[]", |
| 358 | ) |
| 359 | .await |
| 360 | .unwrap(); |
| 361 | assert_eq!( |
| 362 | mock.requests()[0].url, |
| 363 | format!( |
| 364 | "{BASE}/19%3Ac%40thread.tacv2%3Bmessageid%3Droot1/messages/r1/properties?name=links" |
| 365 | ) |
| 366 | ); |
| 367 | } |
| 368 | |
| 369 | #[tokio::test] |
| 370 | async fn link_info_encodes_the_url_for_the_preview_service() { |
| 371 | let mock = Mock::new(&[200]); |
| 372 | messages(&mock) |
| 373 | .link_info("https://a.example/x?y=1&z=2") |
| 374 | .await |
| 375 | .unwrap(); |
| 376 | assert_eq!(mock.requests()[0].method, Method::Get); |
| 377 | assert_eq!( |
| 378 | mock.requests()[0].url, |
| 379 | "https://de-prod.asyncgw.teams.microsoft.com/urlp/v1/url/info?url=https%3A%2F%2Fa.example%2Fx%3Fy%3D1%26z%3D2" |
| 380 | ); |
| 381 | } |
| 382 | |
| 383 | #[tokio::test] |
| 384 | async fn link_images_outside_the_preview_service_are_refused_without_a_request() { |
| 385 | let mock = Mock::new(&[]); |
| 386 | assert!( |
| 387 | messages(&mock) |
| 388 | .link_image("https://evil.example/a.png") |
| 389 | .await |
| 390 | .is_err() |
| 391 | ); |
| 392 | assert!(mock.requests().is_empty()); |
| 393 | } |
| 394 | |
| 395 | #[tokio::test] |
| 396 | async fn list_message_links_asks_for_the_msnp24_view() { |
| 397 | let mock = Mock::new(&[200]); |
| 398 | messages(&mock) |
| 399 | .list_message_links(&ConversationRef::channel_root("19:c@thread.tacv2"), 50) |
| 400 | .await |
| 401 | .unwrap(); |
| 402 | assert_eq!( |
| 403 | mock.requests()[0].url, |
| 404 | format!("{BASE}/19%3Ac%40thread.tacv2/messages?view=msnp24Equivalent&pageSize=50") |
| 405 | ); |
| 406 | } |
| 407 | |
| 408 | struct ArrivalMock { |
| 409 | requests: Mutex<Vec<Request>>, |
| 410 | } |
| 411 | |
| 412 | impl chatsvc::messages::MessageTransport for &ArrivalMock { |
| 413 | async fn send(&self, request: Request) -> Result<ApiResponse> { |
| 414 | self.requests.lock().unwrap().push(request); |
| 415 | Ok(ApiResponse { |
| 416 | status: 201, |
| 417 | body: serde_json::json!({"OriginalArrivalTime": 1727203440000.0}), |
| 418 | retry_after: None, |
| 419 | }) |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | #[tokio::test] |
| 424 | async fn post_message_sends_rich_text_to_the_reply_thread_and_returns_the_arrival_id() { |
| 425 | let mock = ArrivalMock { |
| 426 | requests: Mutex::new(Vec::new()), |
| 427 | }; |
| 428 | let properties = |
| 429 | serde_json::json!({"importance": "", "mentions": "[]", "formatVariant": "TEAMS"}); |
| 430 | let message_id = Messages::with_transport(&mock, "emea") |
| 431 | .post_message( |
| 432 | &ConversationRef::channel_reply("19:c@thread.tacv2", "99"), |
| 433 | "<p>hi</p>", |
| 434 | "Daniel", |
| 435 | &properties, |
| 436 | ) |
| 437 | .await |
| 438 | .unwrap(); |
| 439 | assert_eq!(message_id, "1727203440000"); |
| 440 | let requests = mock.requests.lock().unwrap().clone(); |
| 441 | assert_eq!(requests[0].method, Method::Post); |
| 442 | assert_eq!( |
| 443 | requests[0].url, |
| 444 | format!("{BASE}/19%3Ac%40thread.tacv2%3Bmessageid%3D99/messages") |
| 445 | ); |
| 446 | let body = requests[0].body.as_ref().unwrap(); |
| 447 | assert_eq!(body["messagetype"], "RichText/Html"); |
| 448 | assert_eq!(body["contenttype"], "text"); |
| 449 | assert_eq!(body["content"], "<p>hi</p>"); |
| 450 | assert_eq!(body["imdisplayname"], "Daniel"); |
| 451 | assert_eq!(body["properties"], properties); |
| 452 | assert_eq!(body["amsreferences"], serde_json::json!([])); |
| 453 | let client_message_id = body["clientmessageid"].as_str().unwrap(); |
| 454 | assert!((2..=19).contains(&client_message_id.len())); |
| 455 | assert!( |
| 456 | client_message_id |
| 457 | .chars() |
| 458 | .all(|character| character.is_ascii_digit()) |
| 459 | ); |
| 460 | } |
| 461 | |
| 462 | #[tokio::test] |
| 463 | async fn cards_go_to_the_cards_property_of_the_post() { |
| 464 | let mock = Mock::new(&[404, 200]); |
| 465 | messages(&mock) |
| 466 | .set_cards( |
| 467 | &ConversationRef::ChannelRoot { |
| 468 | channel_id: "19:c@thread.skype".into(), |
| 469 | }, |
| 470 | "17", |
| 471 | "[]", |
| 472 | ) |
| 473 | .await |
| 474 | .unwrap(); |
| 475 | let requests = mock.requests(); |
| 476 | assert_eq!(requests.len(), 2); |
| 477 | assert_eq!(requests[1].method, Method::Put); |
| 478 | assert_eq!( |
| 479 | requests[1].url, |
| 480 | format!("{BASE}/19%3Ac%40thread.skype/messages/17/properties?name=cards") |
| 481 | ); |
| 482 | assert_eq!(requests[1].body.as_ref().unwrap()["cards"], "[]"); |
| 483 | } |