| 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 delete_is_soft_without_a_body() { |
| 114 | let mock = Mock::new(&[200]); |
| 115 | messages(&mock) |
| 116 | .soft_delete_message(&ConversationRef::chat("19:a@thread.v2"), "m1") |
| 117 | .await |
| 118 | .unwrap(); |
| 119 | let requests = mock.requests(); |
| 120 | assert_eq!(requests[0].method, Method::Delete); |
| 121 | assert_eq!( |
| 122 | requests[0].url, |
| 123 | format!("{BASE}/19%3Aa%40thread.v2/messages/m1?behavior=softDelete") |
| 124 | ); |
| 125 | assert!(requests[0].body.is_none()); |
| 126 | } |
| 127 | |
| 128 | #[tokio::test] |
| 129 | async fn undelete_clears_the_delete_time_property_like_teams() { |
| 130 | let mock = Mock::new(&[200]); |
| 131 | messages(&mock) |
| 132 | .undelete_message(&ConversationRef::chat("19:a@thread.v2"), "m1") |
| 133 | .await |
| 134 | .unwrap(); |
| 135 | let requests = mock.requests(); |
| 136 | assert_eq!(requests.len(), 1); |
| 137 | assert_eq!(requests[0].method, Method::Delete); |
| 138 | assert_eq!( |
| 139 | requests[0].url, |
| 140 | format!("{BASE}/19%3Aa%40thread.v2/messages/m1/properties?name=deletetime") |
| 141 | ); |
| 142 | assert_eq!( |
| 143 | requests[0].body, |
| 144 | Some(serde_json::json!({"deletetime": null})) |
| 145 | ); |
| 146 | } |
| 147 | |
| 148 | #[tokio::test] |
| 149 | async fn edit_retries_once_on_404() { |
| 150 | let mock = Mock::new(&[404, 200]); |
| 151 | messages(&mock) |
| 152 | .edit_message(&ConversationRef::chat("19:a"), "m1", "x") |
| 153 | .await |
| 154 | .unwrap(); |
| 155 | let requests = mock.requests(); |
| 156 | assert_eq!(requests.len(), 2); |
| 157 | assert_eq!(requests[0].url, requests[1].url); |
| 158 | } |
| 159 | |
| 160 | #[tokio::test] |
| 161 | async fn edit_gives_up_after_the_second_404() { |
| 162 | let mock = Mock::new(&[404, 404]); |
| 163 | let error = messages(&mock) |
| 164 | .edit_message(&ConversationRef::chat("19:a"), "m1", "x") |
| 165 | .await |
| 166 | .unwrap_err(); |
| 167 | assert!(matches!( |
| 168 | error, |
| 169 | Error::Session(session::Error::Api { status: 404, .. }) |
| 170 | )); |
| 171 | assert_eq!(mock.requests().len(), 2); |
| 172 | } |
| 173 | |
| 174 | #[tokio::test] |
| 175 | async fn delete_does_not_retry_on_404() { |
| 176 | let mock = Mock::new(&[404]); |
| 177 | assert!( |
| 178 | messages(&mock) |
| 179 | .soft_delete_message(&ConversationRef::chat("19:a"), "m1") |
| 180 | .await |
| 181 | .is_err() |
| 182 | ); |
| 183 | assert_eq!(mock.requests().len(), 1); |
| 184 | } |
| 185 | |
| 186 | #[test] |
| 187 | fn messages_use_the_ic3_scope() { |
| 188 | let scope = chatsvc::messages::messages_scope(); |
| 189 | assert_eq!(scope.resource, session::IC3); |
| 190 | assert_eq!(scope.name, "Teams.AccessAsUser.All"); |
| 191 | } |
| 192 | |
| 193 | #[tokio::test] |
| 194 | async fn set_emotion_puts_key_and_numeric_timestamp() { |
| 195 | let mock = Mock::new(&[200]); |
| 196 | messages(&mock) |
| 197 | .set_emotion( |
| 198 | &ConversationRef::chat("19:a@thread.v2"), |
| 199 | "m1", |
| 200 | "1f603_grinningfacewithbigeyes", |
| 201 | ) |
| 202 | .await |
| 203 | .unwrap(); |
| 204 | let requests = mock.requests(); |
| 205 | assert_eq!(requests[0].method, Method::Put); |
| 206 | assert!( |
| 207 | requests[0] |
| 208 | .url |
| 209 | .ends_with("/messages/m1/properties?name=emotions") |
| 210 | ); |
| 211 | let emotions = &requests[0].body.as_ref().unwrap()["emotions"]; |
| 212 | assert_eq!(emotions["key"], "1f603_grinningfacewithbigeyes"); |
| 213 | assert!(emotions["value"].is_number()); |
| 214 | } |
| 215 | |
| 216 | #[tokio::test] |
| 217 | async fn unset_emotion_deletes_with_key_body_and_retries_a_500() { |
| 218 | let mock = Mock::new(&[500, 200]); |
| 219 | messages(&mock) |
| 220 | .unset_emotion(&ConversationRef::chat("19:a@thread.v2"), "m1", "like") |
| 221 | .await |
| 222 | .unwrap(); |
| 223 | let requests = mock.requests(); |
| 224 | assert_eq!(requests.len(), 2); |
| 225 | assert_eq!(requests[1].method, Method::Delete); |
| 226 | assert!(requests[1].url.ends_with("/properties?name=emotions")); |
| 227 | assert_eq!( |
| 228 | requests[1].body.as_ref().unwrap(), |
| 229 | &serde_json::json!({"emotions": {"key": "like"}}) |
| 230 | ); |
| 231 | } |
| 232 | |
| 233 | #[tokio::test] |
| 234 | async fn emotion_gives_up_after_the_second_500() { |
| 235 | let mock = Mock::new(&[500, 500]); |
| 236 | assert!( |
| 237 | messages(&mock) |
| 238 | .set_emotion(&ConversationRef::chat("19:a"), "m1", "like") |
| 239 | .await |
| 240 | .is_err() |
| 241 | ); |
| 242 | assert_eq!(mock.requests().len(), 2); |
| 243 | } |
| 244 | |
| 245 | #[tokio::test] |
| 246 | async fn unset_emotions_tries_every_key_and_succeeds_if_one_does() { |
| 247 | let mock = Mock::new(&[404, 200]); |
| 248 | messages(&mock) |
| 249 | .unset_emotions(&ConversationRef::chat("19:a"), "m1", &["primary", "alias"]) |
| 250 | .await |
| 251 | .unwrap(); |
| 252 | let requests = mock.requests(); |
| 253 | assert_eq!(requests.len(), 2); |
| 254 | assert_eq!( |
| 255 | requests[0].body.as_ref().unwrap()["emotions"]["key"], |
| 256 | "primary" |
| 257 | ); |
| 258 | assert_eq!( |
| 259 | requests[1].body.as_ref().unwrap()["emotions"]["key"], |
| 260 | "alias" |
| 261 | ); |
| 262 | } |
| 263 | |
| 264 | #[tokio::test] |
| 265 | async fn unset_emotions_fails_when_every_key_fails() { |
| 266 | let mock = Mock::new(&[404, 404]); |
| 267 | assert!( |
| 268 | messages(&mock) |
| 269 | .unset_emotions(&ConversationRef::chat("19:a"), "m1", &["primary", "alias"]) |
| 270 | .await |
| 271 | .is_err() |
| 272 | ); |
| 273 | } |
| 274 | |
| 275 | #[tokio::test] |
| 276 | async fn send_typing_posts_a_control_message_without_retrying() { |
| 277 | let mock = Mock::new(&[201, 201, 500]); |
| 278 | let chat = ConversationRef::chat("19:a@thread.v2"); |
| 279 | messages(&mock).send_typing(&chat, true).await.unwrap(); |
| 280 | messages(&mock).send_typing(&chat, false).await.unwrap(); |
| 281 | assert!(messages(&mock).send_typing(&chat, true).await.is_err()); |
| 282 | let requests = mock.requests(); |
| 283 | assert_eq!(requests.len(), 3); |
| 284 | assert_eq!(requests[0].method, Method::Post); |
| 285 | assert_eq!( |
| 286 | requests[0].url, |
| 287 | format!("{BASE}/19%3Aa%40thread.v2/messages") |
| 288 | ); |
| 289 | let start = requests[0].body.as_ref().unwrap(); |
| 290 | assert_eq!(start["messagetype"], "Control/Typing"); |
| 291 | assert_eq!(start["content"], ""); |
| 292 | assert_eq!(start["contenttype"], "Application/Message"); |
| 293 | assert_eq!( |
| 294 | requests[1].body.as_ref().unwrap()["messagetype"], |
| 295 | "Control/ClearTyping" |
| 296 | ); |
| 297 | } |
| 298 | |
| 299 | #[tokio::test] |
| 300 | async fn send_typing_addresses_a_channel_reply_thread() { |
| 301 | let mock = Mock::new(&[201]); |
| 302 | messages(&mock) |
| 303 | .send_typing( |
| 304 | &ConversationRef::channel_reply("19:c@thread.tacv2", "99"), |
| 305 | true, |
| 306 | ) |
| 307 | .await |
| 308 | .unwrap(); |
| 309 | assert_eq!( |
| 310 | mock.requests()[0].url, |
| 311 | format!("{BASE}/19%3Ac%40thread.tacv2%3Bmessageid%3D99/messages") |
| 312 | ); |
| 313 | } |
| 314 | |
| 315 | #[tokio::test] |
| 316 | async fn set_links_puts_the_array_as_a_string() { |
| 317 | let mock = Mock::new(&[200]); |
| 318 | let links = r#"[{"itemid":"0","url":"https://a.example"}]"#; |
| 319 | messages(&mock) |
| 320 | .set_links(&ConversationRef::chat("19:a@thread.v2"), "m1", links) |
| 321 | .await |
| 322 | .unwrap(); |
| 323 | let requests = mock.requests(); |
| 324 | assert_eq!(requests[0].method, Method::Put); |
| 325 | assert_eq!( |
| 326 | requests[0].url, |
| 327 | format!("{BASE}/19%3Aa%40thread.v2/messages/m1/properties?name=links") |
| 328 | ); |
| 329 | assert_eq!(requests[0].body.as_ref().unwrap()["links"], links); |
| 330 | } |
| 331 | |
| 332 | #[tokio::test] |
| 333 | async fn set_links_in_a_channel_reply_targets_the_thread_conversation() { |
| 334 | let mock = Mock::new(&[200]); |
| 335 | messages(&mock) |
| 336 | .set_links( |
| 337 | &ConversationRef::channel_reply("19:c@thread.tacv2", "root1"), |
| 338 | "r1", |
| 339 | "[]", |
| 340 | ) |
| 341 | .await |
| 342 | .unwrap(); |
| 343 | assert_eq!( |
| 344 | mock.requests()[0].url, |
| 345 | format!( |
| 346 | "{BASE}/19%3Ac%40thread.tacv2%3Bmessageid%3Droot1/messages/r1/properties?name=links" |
| 347 | ) |
| 348 | ); |
| 349 | } |
| 350 | |
| 351 | #[tokio::test] |
| 352 | async fn link_info_encodes_the_url_for_the_preview_service() { |
| 353 | let mock = Mock::new(&[200]); |
| 354 | messages(&mock) |
| 355 | .link_info("https://a.example/x?y=1&z=2") |
| 356 | .await |
| 357 | .unwrap(); |
| 358 | assert_eq!(mock.requests()[0].method, Method::Get); |
| 359 | assert_eq!( |
| 360 | mock.requests()[0].url, |
| 361 | "https://de-prod.asyncgw.teams.microsoft.com/urlp/v1/url/info?url=https%3A%2F%2Fa.example%2Fx%3Fy%3D1%26z%3D2" |
| 362 | ); |
| 363 | } |
| 364 | |
| 365 | #[tokio::test] |
| 366 | async fn link_images_outside_the_preview_service_are_refused_without_a_request() { |
| 367 | let mock = Mock::new(&[]); |
| 368 | assert!( |
| 369 | messages(&mock) |
| 370 | .link_image("https://evil.example/a.png") |
| 371 | .await |
| 372 | .is_err() |
| 373 | ); |
| 374 | assert!(mock.requests().is_empty()); |
| 375 | } |
| 376 | |
| 377 | #[tokio::test] |
| 378 | async fn list_message_links_asks_for_the_msnp24_view() { |
| 379 | let mock = Mock::new(&[200]); |
| 380 | messages(&mock) |
| 381 | .list_message_links(&ConversationRef::channel_root("19:c@thread.tacv2"), 50) |
| 382 | .await |
| 383 | .unwrap(); |
| 384 | assert_eq!( |
| 385 | mock.requests()[0].url, |
| 386 | format!("{BASE}/19%3Ac%40thread.tacv2/messages?view=msnp24Equivalent&pageSize=50") |
| 387 | ); |
| 388 | } |
| 389 | |
| 390 | struct ArrivalMock { |
| 391 | requests: Mutex<Vec<Request>>, |
| 392 | } |
| 393 | |
| 394 | impl chatsvc::messages::MessageTransport for &ArrivalMock { |
| 395 | async fn send(&self, request: Request) -> Result<ApiResponse> { |
| 396 | self.requests.lock().unwrap().push(request); |
| 397 | Ok(ApiResponse { |
| 398 | status: 201, |
| 399 | body: serde_json::json!({"OriginalArrivalTime": 1727203440000.0}), |
| 400 | retry_after: None, |
| 401 | }) |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | #[tokio::test] |
| 406 | async fn post_message_sends_rich_text_to_the_reply_thread_and_returns_the_arrival_id() { |
| 407 | let mock = ArrivalMock { |
| 408 | requests: Mutex::new(Vec::new()), |
| 409 | }; |
| 410 | let properties = |
| 411 | serde_json::json!({"importance": "", "mentions": "[]", "formatVariant": "TEAMS"}); |
| 412 | let message_id = Messages::with_transport(&mock, "emea") |
| 413 | .post_message( |
| 414 | &ConversationRef::channel_reply("19:c@thread.tacv2", "99"), |
| 415 | "<p>hi</p>", |
| 416 | "Daniel", |
| 417 | &properties, |
| 418 | ) |
| 419 | .await |
| 420 | .unwrap(); |
| 421 | assert_eq!(message_id, "1727203440000"); |
| 422 | let requests = mock.requests.lock().unwrap().clone(); |
| 423 | assert_eq!(requests[0].method, Method::Post); |
| 424 | assert_eq!( |
| 425 | requests[0].url, |
| 426 | format!("{BASE}/19%3Ac%40thread.tacv2%3Bmessageid%3D99/messages") |
| 427 | ); |
| 428 | let body = requests[0].body.as_ref().unwrap(); |
| 429 | assert_eq!(body["messagetype"], "RichText/Html"); |
| 430 | assert_eq!(body["contenttype"], "text"); |
| 431 | assert_eq!(body["content"], "<p>hi</p>"); |
| 432 | assert_eq!(body["imdisplayname"], "Daniel"); |
| 433 | assert_eq!(body["properties"], properties); |
| 434 | assert_eq!(body["amsreferences"], serde_json::json!([])); |
| 435 | let client_message_id = body["clientmessageid"].as_str().unwrap(); |
| 436 | assert!((2..=19).contains(&client_message_id.len())); |
| 437 | assert!( |
| 438 | client_message_id |
| 439 | .chars() |
| 440 | .all(|character| character.is_ascii_digit()) |
| 441 | ); |
| 442 | } |
| 443 | |
| 444 | #[tokio::test] |
| 445 | async fn cards_go_to_the_cards_property_of_the_post() { |
| 446 | let mock = Mock::new(&[404, 200]); |
| 447 | messages(&mock) |
| 448 | .set_cards( |
| 449 | &ConversationRef::ChannelRoot { |
| 450 | channel_id: "19:c@thread.skype".into(), |
| 451 | }, |
| 452 | "17", |
| 453 | "[]", |
| 454 | ) |
| 455 | .await |
| 456 | .unwrap(); |
| 457 | let requests = mock.requests(); |
| 458 | assert_eq!(requests.len(), 2); |
| 459 | assert_eq!(requests[1].method, Method::Put); |
| 460 | assert_eq!( |
| 461 | requests[1].url, |
| 462 | format!("{BASE}/19%3Ac%40thread.skype/messages/17/properties?name=cards") |
| 463 | ); |
| 464 | assert_eq!(requests[1].body.as_ref().unwrap()["cards"], "[]"); |
| 465 | } |