From 32901da35fef6a79de686cd545c2a5a8547be0fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Thu, 23 Feb 2023 19:01:42 +0100 Subject: [PATCH] events: Reorganize tests Move tests to the proper files, streamline test names and remove duplicates. --- crates/ruma-common/tests/events/audio.rs | 39 +- crates/ruma-common/tests/events/call.rs | 883 ++++++++++-------- crates/ruma-common/tests/events/file.rs | 118 +-- crates/ruma-common/tests/events/image.rs | 39 +- crates/ruma-common/tests/events/location.rs | 37 +- crates/ruma-common/tests/events/message.rs | 103 +- .../ruma-common/tests/events/message_event.rs | 203 ---- crates/ruma-common/tests/events/mod.rs | 1 - .../ruma-common/tests/events/room_message.rs | 436 +++++++-- crates/ruma-common/tests/events/sticker.rs | 111 ++- crates/ruma-common/tests/events/video.rs | 39 +- 11 files changed, 977 insertions(+), 1032 deletions(-) delete mode 100644 crates/ruma-common/tests/events/message_event.rs diff --git a/crates/ruma-common/tests/events/audio.rs b/crates/ruma-common/tests/events/audio.rs index d8c428b1..265c3e90 100644 --- a/crates/ruma-common/tests/events/audio.rs +++ b/crates/ruma-common/tests/events/audio.rs @@ -12,10 +12,7 @@ use ruma_common::{ file::{EncryptedContentInit, FileContent, FileContentInfo}, message::MessageContent, relation::InReplyTo, - room::{ - message::{AudioMessageEventContent, MessageType, Relation, RoomMessageEventContent}, - JsonWebKeyInit, MediaSource, - }, + room::{message::Relation, JsonWebKeyInit}, AnyMessageLikeEvent, MessageLikeEvent, }, mxc_uri, @@ -329,37 +326,3 @@ fn message_event_deserialization() { assert_eq!(content.audio.duration, Some(Duration::from_millis(5_300))); assert_matches!(content.audio.waveform, None); } - -#[test] -fn room_message_serialization() { - let message_event_content = - RoomMessageEventContent::new(MessageType::Audio(AudioMessageEventContent::plain( - "Upload: my_song.mp3".to_owned(), - mxc_uri!("mxc://notareal.hs/file").to_owned(), - None, - ))); - - assert_eq!( - to_json_value(&message_event_content).unwrap(), - json!({ - "body": "Upload: my_song.mp3", - "url": "mxc://notareal.hs/file", - "msgtype": "m.audio", - }) - ); -} - -#[test] -fn room_message_deserialization() { - let json_data = json!({ - "body": "Upload: my_song.mp3", - "url": "mxc://notareal.hs/file", - "msgtype": "m.audio", - }); - - let event_content = from_json_value::(json_data).unwrap(); - let content = assert_matches!(event_content.msgtype, MessageType::Audio(content) => content); - assert_eq!(content.body, "Upload: my_song.mp3"); - let url = assert_matches!(content.source, MediaSource::Plain(url) => url); - assert_eq!(url, "mxc://notareal.hs/file"); -} diff --git a/crates/ruma-common/tests/events/call.rs b/crates/ruma-common/tests/events/call.rs index 4b3aaf43..7e8ad353 100644 --- a/crates/ruma-common/tests/events/call.rs +++ b/crates/ruma-common/tests/events/call.rs @@ -1,108 +1,23 @@ -#![cfg(feature = "unstable-msc2746")] - use assert_matches::assert_matches; -use assign::assign; use js_int::uint; + use ruma_common::{ events::{ call::{ answer::CallAnswerEventContent, candidates::{CallCandidatesEventContent, Candidate}, - hangup::{CallHangupEventContent, Reason}, + hangup::CallHangupEventContent, invite::CallInviteEventContent, - negotiate::CallNegotiateEventContent, - reject::CallRejectEventContent, - select_answer::CallSelectAnswerEventContent, - AnswerSessionDescription, CallCapabilities, OfferSessionDescription, - SessionDescription, SessionDescriptionType, + AnswerSessionDescription, OfferSessionDescription, }, - AnyMessageLikeEvent, MessageLikeEvent, + AnyMessageLikeEvent, AnySyncMessageLikeEvent, MessageLikeEvent, }, - VoipVersionId, + room_id, + serde::CanBeEmpty, + MilliSecondsSinceUnixEpoch, VoipVersionId, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; -#[test] -fn invite_content_serialization() { - let event_content = CallInviteEventContent::version_0( - "abcdef".into(), - uint!(30000), - OfferSessionDescription::new("not a real sdp".to_owned()), - ); - - assert_eq!( - to_json_value(&event_content).unwrap(), - json!({ - "call_id": "abcdef", - "lifetime": 30000, - "version": 0, - "offer": { - "type": "offer", - "sdp": "not a real sdp", - }, - }) - ); -} - -#[test] -fn invite_event_serialization() { - let content = CallInviteEventContent::version_1( - "abcdef".into(), - "9876".into(), - uint!(60000), - OfferSessionDescription::new("not a real sdp".to_owned()), - CallCapabilities::new(), - ); - - assert_eq!( - to_json_value(&content).unwrap(), - json!({ - "call_id": "abcdef", - "party_id": "9876", - "lifetime": 60000, - "version": "1", - "offer": { - "type": "offer", - "sdp": "not a real sdp", - }, - }) - ); -} - -#[test] -fn invite_event_deserialization() { - let json_data = json!({ - "content": { - "call_id": "abcdef", - "party_id": "9876", - "lifetime": 60000, - "version": "1", - "offer": { - "type": "offer", - "sdp": "not a real sdp", - }, - }, - "event_id": "$event:notareal.hs", - "origin_server_ts": 134_829_848, - "room_id": "!roomid:notareal.hs", - "sender": "@user:notareal.hs", - "type": "m.call.invite", - }); - - let event = from_json_value::(json_data).unwrap(); - let message_event = assert_matches!( - event, - AnyMessageLikeEvent::CallInvite(MessageLikeEvent::Original(message_event)) => message_event - ); - let content = message_event.content; - assert_eq!(content.call_id, "abcdef"); - assert_eq!(content.party_id.unwrap(), "9876"); - assert_eq!(content.lifetime, uint!(60000)); - assert_eq!(content.version, VoipVersionId::V1); - assert_eq!(content.offer.sdp, "not a real sdp"); - assert!(!content.capabilities.dtmf); -} - #[test] fn answer_content_serialization() { let event_content = CallAnswerEventContent::version_0( @@ -124,64 +39,113 @@ fn answer_content_serialization() { } #[test] -fn answer_event_serialization() { - let content = CallAnswerEventContent::version_1( - AnswerSessionDescription::new("not a real sdp".to_owned()), - "abcdef".into(), - "9876".into(), - assign!(CallCapabilities::new(), { dtmf: true }), - ); +fn answer_content_deserialization() { + let json_data = json!({ + "answer": { + "type": "answer", + "sdp": "Hello" + }, + "call_id": "foofoo", + "version": 0 + }); - assert_eq!( - to_json_value(&content).unwrap(), - json!({ - "call_id": "abcdef", - "party_id": "9876", - "version": "1", - "answer": { - "type": "answer", - "sdp": "not a real sdp", - }, - "capabilities": { - "m.call.dtmf": true, - }, - }) - ); + let content = from_json_value::(json_data).unwrap(); + + assert_eq!(content.answer.sdp, "Hello"); + assert_eq!(content.call_id, "foofoo"); + assert_eq!(content.version, VoipVersionId::V0); } #[test] fn answer_event_deserialization() { let json_data = json!({ "content": { - "call_id": "abcdef", - "party_id": "9876", - "version": "org.matrix.1b", "answer": { "type": "answer", - "sdp": "not a real sdp", - }, - "capabilities": { - "m.call.dtmf": true, + "sdp": "Hello" }, + "call_id": "foofoo", + "version": 0 }, - "event_id": "$event:notareal.hs", - "origin_server_ts": 134_829_848, - "room_id": "!roomid:notareal.hs", - "sender": "@user:notareal.hs", + "event_id": "$h29iv0s8:example.com", + "origin_server_ts": 1, + "room_id": "!roomid:room.com", + "sender": "@carl:example.com", + "type": "m.call.answer" + }); + + let message_event = assert_matches!( + from_json_value::(json_data).unwrap(), + AnyMessageLikeEvent::CallAnswer(MessageLikeEvent::Original(message_event)) => message_event + ); + assert_eq!(message_event.event_id, "$h29iv0s8:example.com"); + assert_eq!(message_event.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1))); + assert_eq!(message_event.room_id, "!roomid:room.com"); + assert_eq!(message_event.sender, "@carl:example.com"); + assert!(message_event.unsigned.is_empty()); + + let content = message_event.content; + assert_eq!(content.answer.sdp, "Hello"); + assert_eq!(content.call_id, "foofoo"); + assert_eq!(content.version, VoipVersionId::V0); +} + +#[test] +fn answer_event_deserialization_then_convert_to_full() { + let rid = room_id!("!roomid:room.com"); + let json_data = json!({ + "content": { + "answer": { + "type": "answer", + "sdp": "Hello", + }, + "call_id": "foofoo", + "version": 0, + }, + "event_id": "$h29iv0s8:example.com", + "origin_server_ts": 1, + "sender": "@carl:example.com", "type": "m.call.answer", }); - let event = from_json_value::(json_data).unwrap(); + let sync_ev: AnySyncMessageLikeEvent = from_json_value(json_data).unwrap(); + let message_event = assert_matches!( - event, + sync_ev.into_full_event(rid.to_owned()), AnyMessageLikeEvent::CallAnswer(MessageLikeEvent::Original(message_event)) => message_event ); + assert_eq!(message_event.event_id, "$h29iv0s8:example.com"); + assert_eq!(message_event.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1))); + assert_eq!(message_event.room_id, "!roomid:room.com"); + assert_eq!(message_event.sender, "@carl:example.com"); + assert!(message_event.unsigned.is_empty()); + let content = message_event.content; - assert_eq!(content.call_id, "abcdef"); - assert_eq!(content.party_id.unwrap(), "9876"); - assert_eq!(content.version.as_ref(), "org.matrix.1b"); - assert_eq!(content.answer.sdp, "not a real sdp"); - assert!(content.capabilities.dtmf); + assert_eq!(content.answer.sdp, "Hello"); + assert_eq!(content.call_id, "foofoo"); + assert_eq!(content.version, VoipVersionId::V0); +} + +#[test] +fn invite_content_serialization() { + let event_content = CallInviteEventContent::version_0( + "abcdef".into(), + uint!(30000), + OfferSessionDescription::new("not a real sdp".to_owned()), + ); + + assert_eq!( + to_json_value(&event_content).unwrap(), + json!({ + "call_id": "abcdef", + "lifetime": 30000, + "version": 0, + "offer": { + "type": "offer", + "sdp": "not a real sdp", + }, + }) + ); } #[test] @@ -207,84 +171,6 @@ fn candidates_content_serialization() { ); } -#[test] -fn candidates_event_serialization() { - let content = CallCandidatesEventContent::version_1( - "abcdef".into(), - "9876".into(), - vec![ - Candidate::new("not a real candidate".to_owned(), "0".to_owned(), uint!(0)), - Candidate::new("another fake candidate".to_owned(), "0".to_owned(), uint!(1)), - ], - ); - - assert_eq!( - to_json_value(&content).unwrap(), - json!({ - "call_id": "abcdef", - "party_id": "9876", - "version": "1", - "candidates": [ - { - "candidate": "not a real candidate", - "sdpMid": "0", - "sdpMLineIndex": 0, - }, - { - "candidate": "another fake candidate", - "sdpMid": "0", - "sdpMLineIndex": 1, - }, - ], - }) - ); -} - -#[test] -fn candidates_event_deserialization() { - let json_data = json!({ - "content": { - "call_id": "abcdef", - "party_id": "9876", - "version": "1", - "candidates": [ - { - "candidate": "not a real candidate", - "sdpMid": "0", - "sdpMLineIndex": 0, - }, - { - "candidate": "another fake candidate", - "sdpMid": "0", - "sdpMLineIndex": 1, - }, - ], - }, - "event_id": "$event:notareal.hs", - "origin_server_ts": 134_829_848, - "room_id": "!roomid:notareal.hs", - "sender": "@user:notareal.hs", - "type": "m.call.candidates", - }); - - let event = from_json_value::(json_data).unwrap(); - let message_event = assert_matches!( - event, - AnyMessageLikeEvent::CallCandidates(MessageLikeEvent::Original(message_event)) => message_event - ); - let content = message_event.content; - assert_eq!(content.call_id, "abcdef"); - assert_eq!(content.party_id.unwrap(), "9876"); - assert_eq!(content.version, VoipVersionId::V1); - assert_eq!(content.candidates.len(), 2); - assert_eq!(content.candidates[0].candidate, "not a real candidate"); - assert_eq!(content.candidates[0].sdp_mid, "0"); - assert_eq!(content.candidates[0].sdp_m_line_index, uint!(0)); - assert_eq!(content.candidates[1].candidate, "another fake candidate"); - assert_eq!(content.candidates[1].sdp_mid, "0"); - assert_eq!(content.candidates[1].sdp_m_line_index, uint!(1)); -} - #[test] fn hangup_content_serialization() { let event_content = CallHangupEventContent::version_0("abcdef".into()); @@ -299,184 +185,407 @@ fn hangup_content_serialization() { ); } -#[test] -fn hangup_event_serialization() { - let content = - CallHangupEventContent::version_1("abcdef".into(), "9876".into(), Reason::IceFailed); - - assert_eq!( - to_json_value(&content).unwrap(), - json!({ - "call_id": "abcdef", - "party_id": "9876", - "version": "1", - "reason": "ice_failed", - }) - ); -} - -#[test] -fn hangup_event_deserialization() { - let json_data = json!({ - "content": { - "call_id": "abcdef", - "party_id": "9876", - "version": "1", - }, - "event_id": "$event:notareal.hs", - "origin_server_ts": 134_829_848, - "room_id": "!roomid:notareal.hs", - "sender": "@user:notareal.hs", - "type": "m.call.hangup", - }); - - let event = from_json_value::(json_data).unwrap(); - let message_event = assert_matches!( - event, - AnyMessageLikeEvent::CallHangup(MessageLikeEvent::Original(message_event)) => message_event - ); - let content = message_event.content; - assert_eq!(content.call_id, "abcdef"); - assert_eq!(content.party_id.unwrap(), "9876"); - assert_eq!(content.version, VoipVersionId::V1); - assert_eq!(content.reason, Some(Reason::UserHangup)); -} - -#[test] -fn negotiate_event_serialization() { - let content = CallNegotiateEventContent::new( - "abcdef".into(), - "9876".into(), - uint!(30000), - SessionDescription::new(SessionDescriptionType::Offer, "not a real sdp".to_owned()), - ); - - assert_eq!( - to_json_value(&content).unwrap(), - json!({ - "call_id": "abcdef", - "party_id": "9876", - "lifetime": 30000, - "description": { - "type": "offer", - "sdp": "not a real sdp", +#[cfg(feature = "unstable-msc2746")] +mod msc2746 { + use assert_matches::assert_matches; + use assign::assign; + use js_int::uint; + use ruma_common::{ + events::{ + call::{ + answer::CallAnswerEventContent, + candidates::{CallCandidatesEventContent, Candidate}, + hangup::{CallHangupEventContent, Reason}, + invite::CallInviteEventContent, + negotiate::CallNegotiateEventContent, + reject::CallRejectEventContent, + select_answer::CallSelectAnswerEventContent, + AnswerSessionDescription, CallCapabilities, OfferSessionDescription, + SessionDescription, SessionDescriptionType, }, - }) - ); -} - -#[test] -fn negotiate_event_deserialization() { - let json_data = json!({ - "content": { - "call_id": "abcdef", - "party_id": "9876", - "lifetime": 30000, - "description": { - "type": "pranswer", - "sdp": "not a real sdp", - } + AnyMessageLikeEvent, MessageLikeEvent, }, - "event_id": "$event:notareal.hs", - "origin_server_ts": 134_829_848, - "room_id": "!roomid:notareal.hs", - "sender": "@user:notareal.hs", - "type": "m.call.negotiate", - }); + VoipVersionId, + }; + use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; - let event = from_json_value::(json_data).unwrap(); - let message_event = assert_matches!( - event, - AnyMessageLikeEvent::CallNegotiate(MessageLikeEvent::Original(message_event)) => message_event - ); - let content = message_event.content; - assert_eq!(content.call_id, "abcdef"); - assert_eq!(content.party_id, "9876"); - assert_eq!(content.lifetime, uint!(30000)); - assert_eq!(content.description.session_type, SessionDescriptionType::PrAnswer); - assert_eq!(content.description.sdp, "not a real sdp"); -} - -#[test] -fn reject_event_serialization() { - let content = CallRejectEventContent::version_1("abcdef".into(), "9876".into()); - - assert_eq!( - to_json_value(&content).unwrap(), - json!({ - "call_id": "abcdef", - "party_id": "9876", - "version": "1", - }) - ); -} - -#[test] -fn reject_event_deserialization() { - let json_data = json!({ - "content": { - "call_id": "abcdef", - "party_id": "9876", - "version": "1", - }, - "event_id": "$event:notareal.hs", - "origin_server_ts": 134_829_848, - "room_id": "!roomid:notareal.hs", - "sender": "@user:notareal.hs", - "type": "m.call.reject", - }); - - let event = from_json_value::(json_data).unwrap(); - let message_event = assert_matches!( - event, - AnyMessageLikeEvent::CallReject(MessageLikeEvent::Original(message_event)) => message_event - ); - let content = message_event.content; - assert_eq!(content.call_id, "abcdef"); - assert_eq!(content.party_id, "9876"); - assert_eq!(content.version, VoipVersionId::V1); -} - -#[test] -fn select_answer_event_serialization() { - let content = - CallSelectAnswerEventContent::version_1("abcdef".into(), "9876".into(), "6336".into()); - - assert_eq!( - to_json_value(&content).unwrap(), - json!({ - "call_id": "abcdef", - "party_id": "9876", - "selected_party_id": "6336", - "version": "1", - }) - ); -} - -#[test] -fn select_answer_event_deserialization() { - let json_data = json!({ - "content": { - "call_id": "abcdef", - "party_id": "9876", - "selected_party_id": "6336", - "version": "1", - }, - "event_id": "$event:notareal.hs", - "origin_server_ts": 134_829_848, - "room_id": "!roomid:notareal.hs", - "sender": "@user:notareal.hs", - "type": "m.call.select_answer", - }); - - let event = from_json_value::(json_data).unwrap(); - let message_event = assert_matches!( - event, - AnyMessageLikeEvent::CallSelectAnswer(MessageLikeEvent::Original(message_event)) => message_event - ); - let content = message_event.content; - assert_eq!(content.call_id, "abcdef"); - assert_eq!(content.party_id, "9876"); - assert_eq!(content.selected_party_id, "6336"); - assert_eq!(content.version, VoipVersionId::V1); + #[test] + fn invite_event_serialization() { + let content = CallInviteEventContent::version_1( + "abcdef".into(), + "9876".into(), + uint!(60000), + OfferSessionDescription::new("not a real sdp".to_owned()), + CallCapabilities::new(), + ); + + assert_eq!( + to_json_value(&content).unwrap(), + json!({ + "call_id": "abcdef", + "party_id": "9876", + "lifetime": 60000, + "version": "1", + "offer": { + "type": "offer", + "sdp": "not a real sdp", + }, + }) + ); + } + + #[test] + fn invite_event_deserialization() { + let json_data = json!({ + "content": { + "call_id": "abcdef", + "party_id": "9876", + "lifetime": 60000, + "version": "1", + "offer": { + "type": "offer", + "sdp": "not a real sdp", + }, + }, + "event_id": "$event:notareal.hs", + "origin_server_ts": 134_829_848, + "room_id": "!roomid:notareal.hs", + "sender": "@user:notareal.hs", + "type": "m.call.invite", + }); + + let event = from_json_value::(json_data).unwrap(); + let message_event = assert_matches!( + event, + AnyMessageLikeEvent::CallInvite(MessageLikeEvent::Original(message_event)) => message_event + ); + let content = message_event.content; + assert_eq!(content.call_id, "abcdef"); + assert_eq!(content.party_id.unwrap(), "9876"); + assert_eq!(content.lifetime, uint!(60000)); + assert_eq!(content.version, VoipVersionId::V1); + assert_eq!(content.offer.sdp, "not a real sdp"); + assert!(!content.capabilities.dtmf); + } + + #[test] + fn answer_event_serialization() { + let content = CallAnswerEventContent::version_1( + AnswerSessionDescription::new("not a real sdp".to_owned()), + "abcdef".into(), + "9876".into(), + assign!(CallCapabilities::new(), { dtmf: true }), + ); + + assert_eq!( + to_json_value(&content).unwrap(), + json!({ + "call_id": "abcdef", + "party_id": "9876", + "version": "1", + "answer": { + "type": "answer", + "sdp": "not a real sdp", + }, + "capabilities": { + "m.call.dtmf": true, + }, + }) + ); + } + + #[test] + fn answer_event_deserialization() { + let json_data = json!({ + "content": { + "call_id": "abcdef", + "party_id": "9876", + "version": "org.matrix.1b", + "answer": { + "type": "answer", + "sdp": "not a real sdp", + }, + "capabilities": { + "m.call.dtmf": true, + }, + }, + "event_id": "$event:notareal.hs", + "origin_server_ts": 134_829_848, + "room_id": "!roomid:notareal.hs", + "sender": "@user:notareal.hs", + "type": "m.call.answer", + }); + + let event = from_json_value::(json_data).unwrap(); + let message_event = assert_matches!( + event, + AnyMessageLikeEvent::CallAnswer(MessageLikeEvent::Original(message_event)) => message_event + ); + let content = message_event.content; + assert_eq!(content.call_id, "abcdef"); + assert_eq!(content.party_id.unwrap(), "9876"); + assert_eq!(content.version.as_ref(), "org.matrix.1b"); + assert_eq!(content.answer.sdp, "not a real sdp"); + assert!(content.capabilities.dtmf); + } + + #[test] + fn candidates_event_serialization() { + let content = CallCandidatesEventContent::version_1( + "abcdef".into(), + "9876".into(), + vec![ + Candidate::new("not a real candidate".to_owned(), "0".to_owned(), uint!(0)), + Candidate::new("another fake candidate".to_owned(), "0".to_owned(), uint!(1)), + ], + ); + + assert_eq!( + to_json_value(&content).unwrap(), + json!({ + "call_id": "abcdef", + "party_id": "9876", + "version": "1", + "candidates": [ + { + "candidate": "not a real candidate", + "sdpMid": "0", + "sdpMLineIndex": 0, + }, + { + "candidate": "another fake candidate", + "sdpMid": "0", + "sdpMLineIndex": 1, + }, + ], + }) + ); + } + + #[test] + fn candidates_event_deserialization() { + let json_data = json!({ + "content": { + "call_id": "abcdef", + "party_id": "9876", + "version": "1", + "candidates": [ + { + "candidate": "not a real candidate", + "sdpMid": "0", + "sdpMLineIndex": 0, + }, + { + "candidate": "another fake candidate", + "sdpMid": "0", + "sdpMLineIndex": 1, + }, + ], + }, + "event_id": "$event:notareal.hs", + "origin_server_ts": 134_829_848, + "room_id": "!roomid:notareal.hs", + "sender": "@user:notareal.hs", + "type": "m.call.candidates", + }); + + let event = from_json_value::(json_data).unwrap(); + let message_event = assert_matches!( + event, + AnyMessageLikeEvent::CallCandidates(MessageLikeEvent::Original(message_event)) => message_event + ); + let content = message_event.content; + assert_eq!(content.call_id, "abcdef"); + assert_eq!(content.party_id.unwrap(), "9876"); + assert_eq!(content.version, VoipVersionId::V1); + assert_eq!(content.candidates.len(), 2); + assert_eq!(content.candidates[0].candidate, "not a real candidate"); + assert_eq!(content.candidates[0].sdp_mid, "0"); + assert_eq!(content.candidates[0].sdp_m_line_index, uint!(0)); + assert_eq!(content.candidates[1].candidate, "another fake candidate"); + assert_eq!(content.candidates[1].sdp_mid, "0"); + assert_eq!(content.candidates[1].sdp_m_line_index, uint!(1)); + } + + #[test] + fn hangup_event_serialization() { + let content = + CallHangupEventContent::version_1("abcdef".into(), "9876".into(), Reason::IceFailed); + + assert_eq!( + to_json_value(&content).unwrap(), + json!({ + "call_id": "abcdef", + "party_id": "9876", + "version": "1", + "reason": "ice_failed", + }) + ); + } + + #[test] + fn hangup_event_deserialization() { + let json_data = json!({ + "content": { + "call_id": "abcdef", + "party_id": "9876", + "version": "1", + }, + "event_id": "$event:notareal.hs", + "origin_server_ts": 134_829_848, + "room_id": "!roomid:notareal.hs", + "sender": "@user:notareal.hs", + "type": "m.call.hangup", + }); + + let event = from_json_value::(json_data).unwrap(); + let message_event = assert_matches!( + event, + AnyMessageLikeEvent::CallHangup(MessageLikeEvent::Original(message_event)) => message_event + ); + let content = message_event.content; + assert_eq!(content.call_id, "abcdef"); + assert_eq!(content.party_id.unwrap(), "9876"); + assert_eq!(content.version, VoipVersionId::V1); + assert_eq!(content.reason, Some(Reason::UserHangup)); + } + + #[test] + fn negotiate_event_serialization() { + let content = CallNegotiateEventContent::new( + "abcdef".into(), + "9876".into(), + uint!(30000), + SessionDescription::new(SessionDescriptionType::Offer, "not a real sdp".to_owned()), + ); + + assert_eq!( + to_json_value(&content).unwrap(), + json!({ + "call_id": "abcdef", + "party_id": "9876", + "lifetime": 30000, + "description": { + "type": "offer", + "sdp": "not a real sdp", + }, + }) + ); + } + + #[test] + fn negotiate_event_deserialization() { + let json_data = json!({ + "content": { + "call_id": "abcdef", + "party_id": "9876", + "lifetime": 30000, + "description": { + "type": "pranswer", + "sdp": "not a real sdp", + } + }, + "event_id": "$event:notareal.hs", + "origin_server_ts": 134_829_848, + "room_id": "!roomid:notareal.hs", + "sender": "@user:notareal.hs", + "type": "m.call.negotiate", + }); + + let event = from_json_value::(json_data).unwrap(); + let message_event = assert_matches!( + event, + AnyMessageLikeEvent::CallNegotiate(MessageLikeEvent::Original(message_event)) => message_event + ); + let content = message_event.content; + assert_eq!(content.call_id, "abcdef"); + assert_eq!(content.party_id, "9876"); + assert_eq!(content.lifetime, uint!(30000)); + assert_eq!(content.description.session_type, SessionDescriptionType::PrAnswer); + assert_eq!(content.description.sdp, "not a real sdp"); + } + + #[test] + fn reject_event_serialization() { + let content = CallRejectEventContent::version_1("abcdef".into(), "9876".into()); + + assert_eq!( + to_json_value(&content).unwrap(), + json!({ + "call_id": "abcdef", + "party_id": "9876", + "version": "1", + }) + ); + } + + #[test] + fn reject_event_deserialization() { + let json_data = json!({ + "content": { + "call_id": "abcdef", + "party_id": "9876", + "version": "1", + }, + "event_id": "$event:notareal.hs", + "origin_server_ts": 134_829_848, + "room_id": "!roomid:notareal.hs", + "sender": "@user:notareal.hs", + "type": "m.call.reject", + }); + + let event = from_json_value::(json_data).unwrap(); + let message_event = assert_matches!( + event, + AnyMessageLikeEvent::CallReject(MessageLikeEvent::Original(message_event)) => message_event + ); + let content = message_event.content; + assert_eq!(content.call_id, "abcdef"); + assert_eq!(content.party_id, "9876"); + assert_eq!(content.version, VoipVersionId::V1); + } + + #[test] + fn select_answer_event_serialization() { + let content = + CallSelectAnswerEventContent::version_1("abcdef".into(), "9876".into(), "6336".into()); + + assert_eq!( + to_json_value(&content).unwrap(), + json!({ + "call_id": "abcdef", + "party_id": "9876", + "selected_party_id": "6336", + "version": "1", + }) + ); + } + + #[test] + fn select_answer_event_deserialization() { + let json_data = json!({ + "content": { + "call_id": "abcdef", + "party_id": "9876", + "selected_party_id": "6336", + "version": "1", + }, + "event_id": "$event:notareal.hs", + "origin_server_ts": 134_829_848, + "room_id": "!roomid:notareal.hs", + "sender": "@user:notareal.hs", + "type": "m.call.select_answer", + }); + + let event = from_json_value::(json_data).unwrap(); + let message_event = assert_matches!( + event, + AnyMessageLikeEvent::CallSelectAnswer(MessageLikeEvent::Original(message_event)) => message_event + ); + let content = message_event.content; + assert_eq!(content.call_id, "abcdef"); + assert_eq!(content.party_id, "9876"); + assert_eq!(content.selected_party_id, "6336"); + assert_eq!(content.version, VoipVersionId::V1); + } } diff --git a/crates/ruma-common/tests/events/file.rs b/crates/ruma-common/tests/events/file.rs index 25402a59..7ced0f42 100644 --- a/crates/ruma-common/tests/events/file.rs +++ b/crates/ruma-common/tests/events/file.rs @@ -9,10 +9,7 @@ use ruma_common::{ file::{EncryptedContentInit, FileContentInfo, FileEventContent}, message::MessageContent, relation::InReplyTo, - room::{ - message::{FileMessageEventContent, MessageType, Relation, RoomMessageEventContent}, - EncryptedFileInit, JsonWebKeyInit, MediaSource, - }, + room::{message::Relation, JsonWebKeyInit}, AnyMessageLikeEvent, MessageLikeEvent, }, mxc_uri, @@ -217,116 +214,3 @@ fn message_event_deserialization() { assert_eq!(message_event.sender, "@user:notareal.hs"); assert!(message_event.unsigned.is_empty()); } - -#[test] -fn room_message_plain_content_serialization() { - let message_event_content = - RoomMessageEventContent::new(MessageType::File(FileMessageEventContent::plain( - "Upload: my_file.txt".to_owned(), - mxc_uri!("mxc://notareal.hs/file").to_owned(), - None, - ))); - - assert_eq!( - to_json_value(&message_event_content).unwrap(), - json!({ - "body": "Upload: my_file.txt", - "url": "mxc://notareal.hs/file", - "msgtype": "m.file", - }) - ); -} - -#[test] -fn room_message_encrypted_content_serialization() { - let message_event_content = - RoomMessageEventContent::new(MessageType::File(FileMessageEventContent::encrypted( - "Upload: my_file.txt".to_owned(), - EncryptedFileInit { - url: mxc_uri!("mxc://notareal.hs/file").to_owned(), - key: JsonWebKeyInit { - kty: "oct".to_owned(), - key_ops: vec!["encrypt".to_owned(), "decrypt".to_owned()], - alg: "A256CTR".to_owned(), - k: Base64::parse("TLlG_OpX807zzQuuwv4QZGJ21_u7weemFGYJFszMn9A").unwrap(), - ext: true, - } - .into(), - iv: Base64::parse("S22dq3NAX8wAAAAAAAAAAA").unwrap(), - hashes: [( - "sha256".to_owned(), - Base64::parse("aWOHudBnDkJ9IwaR1Nd8XKoI7DOrqDTwt6xDPfVGN6Q").unwrap(), - )] - .into(), - v: "v2".to_owned(), - } - .into(), - ))); - - assert_eq!( - to_json_value(&message_event_content).unwrap(), - json!({ - "body": "Upload: my_file.txt", - "file": { - "url": "mxc://notareal.hs/file", - "key": { - "kty": "oct", - "key_ops": ["encrypt", "decrypt"], - "alg": "A256CTR", - "k": "TLlG_OpX807zzQuuwv4QZGJ21_u7weemFGYJFszMn9A", - "ext": true - }, - "iv": "S22dq3NAX8wAAAAAAAAAAA", - "hashes": { - "sha256": "aWOHudBnDkJ9IwaR1Nd8XKoI7DOrqDTwt6xDPfVGN6Q" - }, - "v": "v2", - }, - "msgtype": "m.file", - }) - ); -} - -#[test] -fn room_message_plain_content_deserialization() { - let json_data = json!({ - "body": "Upload: my_file.txt", - "url": "mxc://notareal.hs/file", - "msgtype": "m.file", - }); - - let event_content = from_json_value::(json_data).unwrap(); - let content = assert_matches!(event_content.msgtype, MessageType::File(content) => content); - assert_eq!(content.body, "Upload: my_file.txt"); - let url = assert_matches!(content.source, MediaSource::Plain(url) => url); - assert_eq!(url, "mxc://notareal.hs/file"); -} - -#[test] -fn room_message_encrypted_content_deserialization() { - let json_data = json!({ - "body": "Upload: my_file.txt", - "file": { - "url": "mxc://notareal.hs/file", - "key": { - "kty": "oct", - "key_ops": ["encrypt", "decrypt"], - "alg": "A256CTR", - "k": "TLlG_OpX807zzQuuwv4QZGJ21_u7weemFGYJFszMn9A", - "ext": true - }, - "iv": "S22dq3NAX8wAAAAAAAAAAA", - "hashes": { - "sha256": "aWOHudBnDkJ9IwaR1Nd8XKoI7DOrqDTwt6xDPfVGN6Q" - }, - "v": "v2", - }, - "msgtype": "m.file", - }); - - let event_content = from_json_value::(json_data).unwrap(); - let content = assert_matches!(event_content.msgtype, MessageType::File(content) => content); - assert_eq!(content.body, "Upload: my_file.txt"); - let encrypted_file = assert_matches!(content.source, MediaSource::Encrypted(f) => f); - assert_eq!(encrypted_file.url, "mxc://notareal.hs/file"); -} diff --git a/crates/ruma-common/tests/events/image.rs b/crates/ruma-common/tests/events/image.rs index 486ed508..5ced9044 100644 --- a/crates/ruma-common/tests/events/image.rs +++ b/crates/ruma-common/tests/events/image.rs @@ -13,10 +13,7 @@ use ruma_common::{ }, message::MessageContent, relation::InReplyTo, - room::{ - message::{ImageMessageEventContent, MessageType, Relation, RoomMessageEventContent}, - JsonWebKeyInit, MediaSource, - }, + room::{message::Relation, JsonWebKeyInit}, AnyMessageLikeEvent, MessageLikeEvent, }, mxc_uri, @@ -285,37 +282,3 @@ fn message_event_deserialization() { assert_eq!(content.image.height, Some(uint!(837))); assert_eq!(content.thumbnail.len(), 0); } - -#[test] -fn room_message_serialization() { - let message_event_content = - RoomMessageEventContent::new(MessageType::Image(ImageMessageEventContent::plain( - "Upload: my_image.jpg".to_owned(), - mxc_uri!("mxc://notareal.hs/file").to_owned(), - None, - ))); - - assert_eq!( - to_json_value(&message_event_content).unwrap(), - json!({ - "body": "Upload: my_image.jpg", - "url": "mxc://notareal.hs/file", - "msgtype": "m.image", - }) - ); -} - -#[test] -fn room_message_deserialization() { - let json_data = json!({ - "body": "Upload: my_image.jpg", - "url": "mxc://notareal.hs/file", - "msgtype": "m.image", - }); - - let event_content = from_json_value::(json_data).unwrap(); - let content = assert_matches!(event_content.msgtype, MessageType::Image(content) => content); - assert_eq!(content.body, "Upload: my_image.jpg"); - let url = assert_matches!(content.source, MediaSource::Plain(url) => url); - assert_eq!(url, "mxc://notareal.hs/file"); -} diff --git a/crates/ruma-common/tests/events/location.rs b/crates/ruma-common/tests/events/location.rs index cac21027..a1d3013b 100644 --- a/crates/ruma-common/tests/events/location.rs +++ b/crates/ruma-common/tests/events/location.rs @@ -9,9 +9,7 @@ use ruma_common::{ location::{AssetType, LocationContent, LocationEventContent, ZoomLevel, ZoomLevelError}, message::MessageContent, relation::InReplyTo, - room::message::{ - LocationMessageEventContent, MessageType, Relation, RoomMessageEventContent, - }, + room::message::Relation, AnyMessageLikeEvent, MessageLikeEvent, }, room_id, @@ -178,36 +176,3 @@ fn message_event_deserialization() { assert_eq!(ev.sender, user_id!("@user:notareal.hs")); assert!(ev.unsigned.is_empty()); } - -#[test] -fn room_message_serialization() { - let message_event_content = - RoomMessageEventContent::new(MessageType::Location(LocationMessageEventContent::new( - "Alice was at geo:51.5008,0.1247;u=35".to_owned(), - "geo:51.5008,0.1247;u=35".to_owned(), - ))); - - assert_eq!( - to_json_value(&message_event_content).unwrap(), - json!({ - "body": "Alice was at geo:51.5008,0.1247;u=35", - "geo_uri": "geo:51.5008,0.1247;u=35", - "msgtype": "m.location", - }) - ); -} - -#[test] -fn room_message_deserialization() { - let json_data = json!({ - "body": "Alice was at geo:51.5008,0.1247;u=35", - "geo_uri": "geo:51.5008,0.1247;u=35", - "msgtype": "m.location", - }); - - let event_content = from_json_value::(json_data).unwrap(); - let content = assert_matches!(event_content.msgtype, MessageType::Location(c) => c); - - assert_eq!(content.body, "Alice was at geo:51.5008,0.1247;u=35"); - assert_eq!(content.geo_uri, "geo:51.5008,0.1247;u=35"); -} diff --git a/crates/ruma-common/tests/events/message.rs b/crates/ruma-common/tests/events/message.rs index fe894a86..bb5fa63b 100644 --- a/crates/ruma-common/tests/events/message.rs +++ b/crates/ruma-common/tests/events/message.rs @@ -10,7 +10,7 @@ use ruma_common::{ message::{MessageContent, MessageEventContent, Text}, notice::NoticeEventContent, relation::InReplyTo, - room::message::{EmoteMessageEventContent, MessageType, Relation, RoomMessageEventContent}, + room::message::Relation, AnyMessageLikeEvent, MessageLikeEvent, }, serde::CanBeEmpty, @@ -295,44 +295,6 @@ fn message_event_deserialization() { assert!(message_event.unsigned.is_empty()); } -#[test] -fn room_message_plain_text_deserialization() { - let json_data = json!({ - "body": "test", - "msgtype": "m.text", - }); - - let content = assert_matches!( - from_json_value::(json_data), - Ok(RoomMessageEventContent { - msgtype: MessageType::Text(content), - .. - }) => content - ); - assert_eq!(content.body, "test"); -} - -#[test] -fn room_message_html_and_text_deserialization() { - let json_data = json!({ - "body": "test", - "formatted_body": "

test

", - "format": "org.matrix.custom.html", - "msgtype": "m.text", - }); - - let content = assert_matches!( - from_json_value::(json_data), - Ok(RoomMessageEventContent { - msgtype: MessageType::Text(content), - .. - }) => content - ); - assert_eq!(content.body, "test"); - let formatted = content.formatted.unwrap(); - assert_eq!(formatted.body, "

test

"); -} - #[test] fn notice_event_serialization() { let content = NoticeEventContent::plain("Hello, I'm a robot!"); @@ -342,20 +304,6 @@ fn notice_event_serialization() { ); } -#[test] -fn room_message_notice_serialization() { - let message_event_content = - RoomMessageEventContent::notice_plain("> <@test:example.com> test\n\ntest reply"); - - assert_eq!( - to_json_value(&message_event_content).unwrap(), - json!({ - "body": "> <@test:example.com> test\n\ntest reply", - "msgtype": "m.notice", - }) - ); -} - #[test] fn notice_event_stable_deserialization() { let json_data = json!({ @@ -420,23 +368,6 @@ fn notice_event_unstable_deserialization() { assert_eq!(message.find_html(), Some("Hello, I'm a robot!")); } -#[test] -fn room_message_notice_deserialization() { - let json_data = json!({ - "body": "test", - "msgtype": "m.notice", - }); - - let content = assert_matches!( - from_json_value::(json_data), - Ok(RoomMessageEventContent { - msgtype: MessageType::Notice(content), - .. - }) => content - ); - assert_eq!(content.body, "test"); -} - #[test] fn emote_event_serialization() { let content = @@ -451,21 +382,6 @@ fn emote_event_serialization() { ); } -#[test] -fn room_message_emote_serialization() { - let message_event_content = RoomMessageEventContent::new(MessageType::Emote( - EmoteMessageEventContent::plain("> <@test:example.com> test\n\ntest reply"), - )); - - assert_eq!( - to_json_value(&message_event_content).unwrap(), - json!({ - "body": "> <@test:example.com> test\n\ntest reply", - "msgtype": "m.emote", - }) - ); -} - #[test] fn emote_event_stable_deserialization() { let json_data = json!({ @@ -524,23 +440,6 @@ fn emote_event_unstable_deserialization() { assert_eq!(message.find_html(), None); } -#[test] -fn room_message_emote_deserialization() { - let json_data = json!({ - "body": "test", - "msgtype": "m.emote", - }); - - let content = assert_matches!( - from_json_value::(json_data), - Ok(RoomMessageEventContent { - msgtype: MessageType::Emote(content), - .. - }) => content - ); - assert_eq!(content.body, "test"); -} - #[test] #[cfg(feature = "unstable-msc3554")] fn lang_serialization() { diff --git a/crates/ruma-common/tests/events/message_event.rs b/crates/ruma-common/tests/events/message_event.rs deleted file mode 100644 index cb0077f8..00000000 --- a/crates/ruma-common/tests/events/message_event.rs +++ /dev/null @@ -1,203 +0,0 @@ -use assert_matches::assert_matches; -use assign::assign; -use js_int::{uint, UInt}; -use ruma_common::{ - events::{ - call::answer::CallAnswerEventContent, - room::{ImageInfo, MediaSource, ThumbnailInfo}, - sticker::StickerEventContent, - AnyMessageLikeEvent, AnySyncMessageLikeEvent, MessageLikeEvent, - }, - mxc_uri, room_id, - serde::CanBeEmpty, - MilliSecondsSinceUnixEpoch, VoipVersionId, -}; -use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; - -#[test] -fn message_serialize_sticker() { - let content = StickerEventContent::new( - "Hello".into(), - assign!(ImageInfo::new(), { - height: UInt::new(423), - width: UInt::new(1011), - mimetype: Some("image/png".into()), - size: UInt::new(84242), - thumbnail_info: Some(Box::new(assign!(ThumbnailInfo::new(), { - width: UInt::new(800), - height: UInt::new(334), - mimetype: Some("image/png".into()), - size: UInt::new(82595), - }))), - thumbnail_source: Some(MediaSource::Plain(mxc_uri!("mxc://matrix.org/irsns989Rrsn").to_owned())), - }), - mxc_uri!("mxc://matrix.org/rnsldl8srs98IRrs").to_owned(), - ); - - let actual = to_json_value(&content).unwrap(); - - let expected = json!({ - "body": "Hello", - "info": { - "h": 423, - "mimetype": "image/png", - "size": 84242, - "thumbnail_info": { - "h": 334, - "mimetype": "image/png", - "size": 82595, - "w": 800 - }, - "thumbnail_url": "mxc://matrix.org/irsns989Rrsn", - "w": 1011 - }, - "url": "mxc://matrix.org/rnsldl8srs98IRrs", - }); - assert_eq!(actual, expected); -} - -#[test] -fn deserialize_message_call_answer_content() { - let json_data = json!({ - "answer": { - "type": "answer", - "sdp": "Hello" - }, - "call_id": "foofoo", - "version": 0 - }); - - let content = from_json_value::(json_data).unwrap(); - - assert_eq!(content.answer.sdp, "Hello"); - assert_eq!(content.call_id, "foofoo"); - assert_eq!(content.version, VoipVersionId::V0); -} - -#[test] -fn deserialize_message_call_answer() { - let json_data = json!({ - "content": { - "answer": { - "type": "answer", - "sdp": "Hello" - }, - "call_id": "foofoo", - "version": 0 - }, - "event_id": "$h29iv0s8:example.com", - "origin_server_ts": 1, - "room_id": "!roomid:room.com", - "sender": "@carl:example.com", - "type": "m.call.answer" - }); - - let message_event = assert_matches!( - from_json_value::(json_data).unwrap(), - AnyMessageLikeEvent::CallAnswer(MessageLikeEvent::Original(message_event)) => message_event - ); - assert_eq!(message_event.event_id, "$h29iv0s8:example.com"); - assert_eq!(message_event.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1))); - assert_eq!(message_event.room_id, "!roomid:room.com"); - assert_eq!(message_event.sender, "@carl:example.com"); - assert!(message_event.unsigned.is_empty()); - - let content = message_event.content; - assert_eq!(content.answer.sdp, "Hello"); - assert_eq!(content.call_id, "foofoo"); - assert_eq!(content.version, VoipVersionId::V0); -} - -#[test] -fn deserialize_message_sticker() { - let json_data = json!({ - "content": { - "body": "Hello", - "info": { - "h": 423, - "mimetype": "image/png", - "size": 84242, - "thumbnail_info": { - "h": 334, - "mimetype": "image/png", - "size": 82595, - "w": 800 - }, - "thumbnail_url": "mxc://matrix.org/irnsNRS2879", - "w": 1011 - }, - "url": "mxc://matrix.org/jxPXTKpyydzdHJkdFNZjTZrD" - }, - "event_id": "$h29iv0s8:example.com", - "origin_server_ts": 1, - "room_id": "!roomid:room.com", - "sender": "@carl:example.com", - "type": "m.sticker" - }); - - let message_event = assert_matches!( - from_json_value::(json_data), - Ok(AnyMessageLikeEvent::Sticker(MessageLikeEvent::Original(message_event))) => message_event - ); - - assert_eq!(message_event.event_id, "$h29iv0s8:example.com"); - assert_eq!(message_event.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1))); - assert_eq!(message_event.room_id, "!roomid:room.com"); - assert_eq!(message_event.sender, "@carl:example.com"); - assert!(message_event.unsigned.is_empty()); - - let content = message_event.content; - assert_eq!(content.body, "Hello"); - assert_eq!(content.info.height, Some(uint!(423))); - assert_eq!(content.info.width, Some(uint!(1011))); - assert_eq!(content.info.mimetype.as_deref(), Some("image/png")); - assert_eq!(content.info.size, Some(uint!(84242))); - assert_eq!(content.url, "mxc://matrix.org/jxPXTKpyydzdHJkdFNZjTZrD"); - - let thumbnail_url = assert_matches!( - content.info.thumbnail_source, - Some(MediaSource::Plain(thumbnail_url)) => thumbnail_url - ); - assert_eq!(thumbnail_url, "mxc://matrix.org/irnsNRS2879"); - let thumbnail_info = content.info.thumbnail_info.unwrap(); - assert_eq!(thumbnail_info.width, Some(uint!(800))); - assert_eq!(thumbnail_info.height, Some(uint!(334))); - assert_eq!(thumbnail_info.mimetype.as_deref(), Some("image/png")); - assert_eq!(thumbnail_info.size, Some(uint!(82595))); -} - -#[test] -fn deserialize_message_then_convert_to_full() { - let rid = room_id!("!roomid:room.com"); - let json_data = json!({ - "content": { - "answer": { - "type": "answer", - "sdp": "Hello" - }, - "call_id": "foofoo", - "version": 0 - }, - "event_id": "$h29iv0s8:example.com", - "origin_server_ts": 1, - "sender": "@carl:example.com", - "type": "m.call.answer" - }); - - let sync_ev: AnySyncMessageLikeEvent = from_json_value(json_data).unwrap(); - - let message_event = assert_matches!( - sync_ev.into_full_event(rid.to_owned()), - AnyMessageLikeEvent::CallAnswer(MessageLikeEvent::Original(message_event)) => message_event - ); - assert_eq!(message_event.event_id, "$h29iv0s8:example.com"); - assert_eq!(message_event.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1))); - assert_eq!(message_event.room_id, "!roomid:room.com"); - assert_eq!(message_event.sender, "@carl:example.com"); - assert!(message_event.unsigned.is_empty()); - - let content = message_event.content; - assert_eq!(content.answer.sdp, "Hello"); - assert_eq!(content.call_id, "foofoo"); - assert_eq!(content.version, VoipVersionId::V0); -} diff --git a/crates/ruma-common/tests/events/mod.rs b/crates/ruma-common/tests/events/mod.rs index 07d3db86..fd2880f1 100644 --- a/crates/ruma-common/tests/events/mod.rs +++ b/crates/ruma-common/tests/events/mod.rs @@ -13,7 +13,6 @@ mod image; mod initial_state; mod location; mod message; -mod message_event; mod pdu; mod poll; mod redacted; diff --git a/crates/ruma-common/tests/events/room_message.rs b/crates/ruma-common/tests/events/room_message.rs index 76bf41a9..afd57523 100644 --- a/crates/ruma-common/tests/events/room_message.rs +++ b/crates/ruma-common/tests/events/room_message.rs @@ -8,15 +8,18 @@ use ruma_common::{ key::verification::VerificationMethod, room::{ message::{ - AudioMessageEventContent, ForwardThread, KeyVerificationRequestEventContent, - MessageType, OriginalRoomMessageEvent, RoomMessageEventContent, - TextMessageEventContent, + AudioMessageEventContent, EmoteMessageEventContent, FileMessageEventContent, + ForwardThread, ImageMessageEventContent, KeyVerificationRequestEventContent, + LocationMessageEventContent, MessageType, OriginalRoomMessageEvent, + RoomMessageEventContent, TextMessageEventContent, VideoMessageEventContent, }, - MediaSource, + EncryptedFileInit, JsonWebKeyInit, MediaSource, }, MessageLikeUnsigned, }, - mxc_uri, room_id, user_id, MilliSecondsSinceUnixEpoch, OwnedDeviceId, + mxc_uri, room_id, + serde::Base64, + user_id, MilliSecondsSinceUnixEpoch, OwnedDeviceId, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -32,44 +35,6 @@ macro_rules! json_object { }; } -#[test] -fn serialization() { - let content = - RoomMessageEventContent::new(MessageType::Audio(AudioMessageEventContent::plain( - "test".into(), - mxc_uri!("mxc://example.org/ffed755USFFxlgbQYZGtryd").to_owned(), - None, - ))); - - assert_eq!( - to_json_value(content).unwrap(), - json!({ - "body": "test", - "msgtype": "m.audio", - "url": "mxc://example.org/ffed755USFFxlgbQYZGtryd", - }) - ); -} - -#[test] -fn content_serialization() { - let message_event_content = - RoomMessageEventContent::new(MessageType::Audio(AudioMessageEventContent::plain( - "test".into(), - mxc_uri!("mxc://example.org/ffed755USFFxlgbQYZGtryd").to_owned(), - None, - ))); - - assert_eq!( - to_json_value(&message_event_content).unwrap(), - json!({ - "body": "test", - "msgtype": "m.audio", - "url": "mxc://example.org/ffed755USFFxlgbQYZGtryd" - }) - ); -} - #[test] fn custom_msgtype_serialization() { let json_data = json_object! { @@ -91,7 +56,7 @@ fn custom_msgtype_serialization() { } #[test] -fn custom_content_deserialization() { +fn custom_msgtype_deserialization() { let json_data = json!({ "msgtype": "my_custom_msgtype", "body": "my custom message", @@ -112,7 +77,7 @@ fn custom_content_deserialization() { } #[test] -fn formatted_body_serialization() { +fn text_msgtype_formatted_body_serialization() { let message_event_content = RoomMessageEventContent::text_html("Hello, World!", "Hello, World!"); @@ -128,7 +93,7 @@ fn formatted_body_serialization() { } #[test] -fn plain_text_content_serialization() { +fn text_msgtype_plain_text_serialization() { let message_event_content = RoomMessageEventContent::text_plain("> <@test:example.com> test\n\ntest reply"); @@ -143,7 +108,7 @@ fn plain_text_content_serialization() { #[test] #[cfg(feature = "markdown")] -fn markdown_content_serialization() { +fn text_msgtype_markdown_serialization() { use ruma_common::events::room::message::TextMessageEventContent; let formatted_message = RoomMessageEventContent::new(MessageType::Text( @@ -232,7 +197,7 @@ fn markdown_options() { } #[test] -fn verification_request_deserialization() { +fn verification_request_msgtype_deserialization() { let user_id = user_id!("@example2:localhost"); let device_id: OwnedDeviceId = "XOWLHHFSWM".into(); @@ -264,7 +229,7 @@ fn verification_request_deserialization() { } #[test] -fn verification_request_serialization() { +fn verification_request_msgtype_serialization() { let user_id = user_id!("@example2:localhost").to_owned(); let device_id: OwnedDeviceId = "XOWLHHFSWM".into(); let body = "@example:localhost is requesting to verify your key, ...".to_owned(); @@ -287,31 +252,11 @@ fn verification_request_serialization() { assert_eq!(to_json_value(&content).unwrap(), json_data,); } -#[test] -fn content_deserialization() { - let json_data = json!({ - "body": "test", - "msgtype": "m.audio", - "url": "mxc://example.org/ffed755USFFxlgbQYZGtryd" - }); - - let content = from_json_value::(json_data).unwrap(); - let audio = assert_matches!( - content.msgtype, - MessageType::Audio(audio) => audio - ); - assert_eq!(audio.body, "test"); - assert_matches!(audio.info, None); - let url = assert_matches!(audio.source, MediaSource::Plain(url) => url); - assert_eq!(url, "mxc://example.org/ffed755USFFxlgbQYZGtryd"); - - assert_matches!(content.relates_to, None); -} - #[test] fn content_deserialization_failure() { let json_data = json!({ - "body": "test","msgtype": "m.location", + "body": "test", + "msgtype": "m.location", "url": "http://example.com/audio.mp3" }); assert_matches!(from_json_value::(json_data), Err(_)); @@ -529,3 +474,352 @@ fn make_replacement_with_reply() { " ); } + +#[test] +fn audio_msgtype_serialization() { + let message_event_content = + RoomMessageEventContent::new(MessageType::Audio(AudioMessageEventContent::plain( + "Upload: my_song.mp3".to_owned(), + mxc_uri!("mxc://notareal.hs/file").to_owned(), + None, + ))); + + assert_eq!( + to_json_value(&message_event_content).unwrap(), + json!({ + "body": "Upload: my_song.mp3", + "url": "mxc://notareal.hs/file", + "msgtype": "m.audio", + }) + ); +} + +#[test] +fn audio_msgtype_deserialization() { + let json_data = json!({ + "body": "Upload: my_song.mp3", + "url": "mxc://notareal.hs/file", + "msgtype": "m.audio", + }); + + let event_content = from_json_value::(json_data).unwrap(); + let content = assert_matches!(event_content.msgtype, MessageType::Audio(content) => content); + assert_eq!(content.body, "Upload: my_song.mp3"); + let url = assert_matches!(content.source, MediaSource::Plain(url) => url); + assert_eq!(url, "mxc://notareal.hs/file"); +} + +#[test] +fn file_msgtype_plain_content_serialization() { + let message_event_content = + RoomMessageEventContent::new(MessageType::File(FileMessageEventContent::plain( + "Upload: my_file.txt".to_owned(), + mxc_uri!("mxc://notareal.hs/file").to_owned(), + None, + ))); + + assert_eq!( + to_json_value(&message_event_content).unwrap(), + json!({ + "body": "Upload: my_file.txt", + "url": "mxc://notareal.hs/file", + "msgtype": "m.file", + }) + ); +} + +#[test] +fn file_msgtype_encrypted_content_serialization() { + let message_event_content = + RoomMessageEventContent::new(MessageType::File(FileMessageEventContent::encrypted( + "Upload: my_file.txt".to_owned(), + EncryptedFileInit { + url: mxc_uri!("mxc://notareal.hs/file").to_owned(), + key: JsonWebKeyInit { + kty: "oct".to_owned(), + key_ops: vec!["encrypt".to_owned(), "decrypt".to_owned()], + alg: "A256CTR".to_owned(), + k: Base64::parse("TLlG_OpX807zzQuuwv4QZGJ21_u7weemFGYJFszMn9A").unwrap(), + ext: true, + } + .into(), + iv: Base64::parse("S22dq3NAX8wAAAAAAAAAAA").unwrap(), + hashes: [( + "sha256".to_owned(), + Base64::parse("aWOHudBnDkJ9IwaR1Nd8XKoI7DOrqDTwt6xDPfVGN6Q").unwrap(), + )] + .into(), + v: "v2".to_owned(), + } + .into(), + ))); + + assert_eq!( + to_json_value(&message_event_content).unwrap(), + json!({ + "body": "Upload: my_file.txt", + "file": { + "url": "mxc://notareal.hs/file", + "key": { + "kty": "oct", + "key_ops": ["encrypt", "decrypt"], + "alg": "A256CTR", + "k": "TLlG_OpX807zzQuuwv4QZGJ21_u7weemFGYJFszMn9A", + "ext": true + }, + "iv": "S22dq3NAX8wAAAAAAAAAAA", + "hashes": { + "sha256": "aWOHudBnDkJ9IwaR1Nd8XKoI7DOrqDTwt6xDPfVGN6Q" + }, + "v": "v2", + }, + "msgtype": "m.file", + }) + ); +} + +#[test] +fn file_msgtype_plain_content_deserialization() { + let json_data = json!({ + "body": "Upload: my_file.txt", + "url": "mxc://notareal.hs/file", + "msgtype": "m.file", + }); + + let event_content = from_json_value::(json_data).unwrap(); + let content = assert_matches!(event_content.msgtype, MessageType::File(content) => content); + assert_eq!(content.body, "Upload: my_file.txt"); + let url = assert_matches!(content.source, MediaSource::Plain(url) => url); + assert_eq!(url, "mxc://notareal.hs/file"); +} + +#[test] +fn file_msgtype_encrypted_content_deserialization() { + let json_data = json!({ + "body": "Upload: my_file.txt", + "file": { + "url": "mxc://notareal.hs/file", + "key": { + "kty": "oct", + "key_ops": ["encrypt", "decrypt"], + "alg": "A256CTR", + "k": "TLlG_OpX807zzQuuwv4QZGJ21_u7weemFGYJFszMn9A", + "ext": true + }, + "iv": "S22dq3NAX8wAAAAAAAAAAA", + "hashes": { + "sha256": "aWOHudBnDkJ9IwaR1Nd8XKoI7DOrqDTwt6xDPfVGN6Q" + }, + "v": "v2", + }, + "msgtype": "m.file", + }); + + let event_content = from_json_value::(json_data).unwrap(); + let content = assert_matches!(event_content.msgtype, MessageType::File(content) => content); + assert_eq!(content.body, "Upload: my_file.txt"); + let encrypted_file = assert_matches!(content.source, MediaSource::Encrypted(f) => f); + assert_eq!(encrypted_file.url, "mxc://notareal.hs/file"); +} + +#[test] +fn image_msgtype_serialization() { + let message_event_content = + RoomMessageEventContent::new(MessageType::Image(ImageMessageEventContent::plain( + "Upload: my_image.jpg".to_owned(), + mxc_uri!("mxc://notareal.hs/file").to_owned(), + None, + ))); + + assert_eq!( + to_json_value(&message_event_content).unwrap(), + json!({ + "body": "Upload: my_image.jpg", + "url": "mxc://notareal.hs/file", + "msgtype": "m.image", + }) + ); +} + +#[test] +fn image_msgtype_deserialization() { + let json_data = json!({ + "body": "Upload: my_image.jpg", + "url": "mxc://notareal.hs/file", + "msgtype": "m.image", + }); + + let event_content = from_json_value::(json_data).unwrap(); + let content = assert_matches!(event_content.msgtype, MessageType::Image(content) => content); + assert_eq!(content.body, "Upload: my_image.jpg"); + let url = assert_matches!(content.source, MediaSource::Plain(url) => url); + assert_eq!(url, "mxc://notareal.hs/file"); +} + +#[test] +fn location_msgtype_serialization() { + let message_event_content = + RoomMessageEventContent::new(MessageType::Location(LocationMessageEventContent::new( + "Alice was at geo:51.5008,0.1247;u=35".to_owned(), + "geo:51.5008,0.1247;u=35".to_owned(), + ))); + + assert_eq!( + to_json_value(&message_event_content).unwrap(), + json!({ + "body": "Alice was at geo:51.5008,0.1247;u=35", + "geo_uri": "geo:51.5008,0.1247;u=35", + "msgtype": "m.location", + }) + ); +} + +#[test] +fn location_msgtype_deserialization() { + let json_data = json!({ + "body": "Alice was at geo:51.5008,0.1247;u=35", + "geo_uri": "geo:51.5008,0.1247;u=35", + "msgtype": "m.location", + }); + + let event_content = from_json_value::(json_data).unwrap(); + let content = assert_matches!(event_content.msgtype, MessageType::Location(c) => c); + + assert_eq!(content.body, "Alice was at geo:51.5008,0.1247;u=35"); + assert_eq!(content.geo_uri, "geo:51.5008,0.1247;u=35"); +} + +#[test] +fn text_msgtype_body_deserialization() { + let json_data = json!({ + "body": "test", + "msgtype": "m.text", + }); + + let content = assert_matches!( + from_json_value::(json_data), + Ok(RoomMessageEventContent { + msgtype: MessageType::Text(content), + .. + }) => content + ); + assert_eq!(content.body, "test"); +} + +#[test] +fn text_msgtype_formatted_body_and_body_deserialization() { + let json_data = json!({ + "body": "test", + "formatted_body": "

test

", + "format": "org.matrix.custom.html", + "msgtype": "m.text", + }); + + let content = assert_matches!( + from_json_value::(json_data), + Ok(RoomMessageEventContent { + msgtype: MessageType::Text(content), + .. + }) => content + ); + assert_eq!(content.body, "test"); + let formatted = content.formatted.unwrap(); + assert_eq!(formatted.body, "

test

"); +} + +#[test] +fn notice_msgtype_serialization() { + let message_event_content = + RoomMessageEventContent::notice_plain("> <@test:example.com> test\n\ntest reply"); + + assert_eq!( + to_json_value(&message_event_content).unwrap(), + json!({ + "body": "> <@test:example.com> test\n\ntest reply", + "msgtype": "m.notice", + }) + ); +} + +#[test] +fn notice_msgtype_deserialization() { + let json_data = json!({ + "body": "test", + "msgtype": "m.notice", + }); + + let content = assert_matches!( + from_json_value::(json_data), + Ok(RoomMessageEventContent { + msgtype: MessageType::Notice(content), + .. + }) => content + ); + assert_eq!(content.body, "test"); +} + +#[test] +fn emote_msgtype_serialization() { + let message_event_content = RoomMessageEventContent::new(MessageType::Emote( + EmoteMessageEventContent::plain("> <@test:example.com> test\n\ntest reply"), + )); + + assert_eq!( + to_json_value(&message_event_content).unwrap(), + json!({ + "body": "> <@test:example.com> test\n\ntest reply", + "msgtype": "m.emote", + }) + ); +} + +#[test] +fn emote_msgtype_deserialization() { + let json_data = json!({ + "body": "test", + "msgtype": "m.emote", + }); + + let content = assert_matches!( + from_json_value::(json_data), + Ok(RoomMessageEventContent { + msgtype: MessageType::Emote(content), + .. + }) => content + ); + assert_eq!(content.body, "test"); +} + +#[test] +fn video_msgtype_serialization() { + let message_event_content = + RoomMessageEventContent::new(MessageType::Video(VideoMessageEventContent::plain( + "Upload: my_video.mp4".to_owned(), + mxc_uri!("mxc://notareal.hs/file").to_owned(), + None, + ))); + + assert_eq!( + to_json_value(&message_event_content).unwrap(), + json!({ + "body": "Upload: my_video.mp4", + "url": "mxc://notareal.hs/file", + "msgtype": "m.video", + }) + ); +} + +#[test] +fn video_msgtype_deserialization() { + let json_data = json!({ + "body": "Upload: my_video.mp4", + "url": "mxc://notareal.hs/file", + "msgtype": "m.video", + }); + + let event_content = from_json_value::(json_data).unwrap(); + let content = assert_matches!(event_content.msgtype, MessageType::Video(content) => content); + assert_eq!(content.body, "Upload: my_video.mp4"); + let url = assert_matches!(content.source, MediaSource::Plain(url) => url); + assert_eq!(url, "mxc://notareal.hs/file"); +} diff --git a/crates/ruma-common/tests/events/sticker.rs b/crates/ruma-common/tests/events/sticker.rs index 414ca7ed..db7a60eb 100644 --- a/crates/ruma-common/tests/events/sticker.rs +++ b/crates/ruma-common/tests/events/sticker.rs @@ -1,6 +1,15 @@ +use assert_matches::assert_matches; +use assign::assign; +use js_int::{uint, UInt}; use ruma_common::{ - events::{room::ImageInfo, sticker::StickerEventContent}, + events::{ + room::{ImageInfo, MediaSource, ThumbnailInfo}, + sticker::StickerEventContent, + AnyMessageLikeEvent, MessageLikeEvent, + }, mxc_uri, + serde::CanBeEmpty, + MilliSecondsSinceUnixEpoch, }; use serde_json::{from_value as from_json_value, json, to_value as to_json_value}; @@ -22,6 +31,48 @@ fn content_serialization() { ); } +#[test] +fn event_serialization() { + let content = StickerEventContent::new( + "Hello".into(), + assign!(ImageInfo::new(), { + height: UInt::new(423), + width: UInt::new(1011), + mimetype: Some("image/png".into()), + size: UInt::new(84242), + thumbnail_info: Some(Box::new(assign!(ThumbnailInfo::new(), { + width: UInt::new(800), + height: UInt::new(334), + mimetype: Some("image/png".into()), + size: UInt::new(82595), + }))), + thumbnail_source: Some(MediaSource::Plain(mxc_uri!("mxc://matrix.org/irsns989Rrsn").to_owned())), + }), + mxc_uri!("mxc://matrix.org/rnsldl8srs98IRrs").to_owned(), + ); + + let actual = to_json_value(&content).unwrap(); + + let expected = json!({ + "body": "Hello", + "info": { + "h": 423, + "mimetype": "image/png", + "size": 84242, + "thumbnail_info": { + "h": 334, + "mimetype": "image/png", + "size": 82595, + "w": 800 + }, + "thumbnail_url": "mxc://matrix.org/irsns989Rrsn", + "w": 1011 + }, + "url": "mxc://matrix.org/rnsldl8srs98IRrs", + }); + assert_eq!(actual, expected); +} + #[test] fn content_deserialization() { let json_data = json!({ @@ -34,3 +85,61 @@ fn content_deserialization() { assert_eq!(content.body, "Upload: my_image.jpg"); assert_eq!(content.url, "mxc://notareal.hs/file"); } + +#[test] +fn event_deserialization() { + let json_data = json!({ + "content": { + "body": "Hello", + "info": { + "h": 423, + "mimetype": "image/png", + "size": 84242, + "thumbnail_info": { + "h": 334, + "mimetype": "image/png", + "size": 82595, + "w": 800 + }, + "thumbnail_url": "mxc://matrix.org/irnsNRS2879", + "w": 1011 + }, + "url": "mxc://matrix.org/jxPXTKpyydzdHJkdFNZjTZrD" + }, + "event_id": "$h29iv0s8:example.com", + "origin_server_ts": 1, + "room_id": "!roomid:room.com", + "sender": "@carl:example.com", + "type": "m.sticker" + }); + + let message_event = assert_matches!( + from_json_value::(json_data), + Ok(AnyMessageLikeEvent::Sticker(MessageLikeEvent::Original(message_event))) => message_event + ); + + assert_eq!(message_event.event_id, "$h29iv0s8:example.com"); + assert_eq!(message_event.origin_server_ts, MilliSecondsSinceUnixEpoch(uint!(1))); + assert_eq!(message_event.room_id, "!roomid:room.com"); + assert_eq!(message_event.sender, "@carl:example.com"); + assert!(message_event.unsigned.is_empty()); + + let content = message_event.content; + assert_eq!(content.body, "Hello"); + assert_eq!(content.info.height, Some(uint!(423))); + assert_eq!(content.info.width, Some(uint!(1011))); + assert_eq!(content.info.mimetype.as_deref(), Some("image/png")); + assert_eq!(content.info.size, Some(uint!(84242))); + assert_eq!(content.url, "mxc://matrix.org/jxPXTKpyydzdHJkdFNZjTZrD"); + + let thumbnail_url = assert_matches!( + content.info.thumbnail_source, + Some(MediaSource::Plain(thumbnail_url)) => thumbnail_url + ); + assert_eq!(thumbnail_url, "mxc://matrix.org/irnsNRS2879"); + let thumbnail_info = content.info.thumbnail_info.unwrap(); + assert_eq!(thumbnail_info.width, Some(uint!(800))); + assert_eq!(thumbnail_info.height, Some(uint!(334))); + assert_eq!(thumbnail_info.mimetype.as_deref(), Some("image/png")); + assert_eq!(thumbnail_info.size, Some(uint!(82595))); +} diff --git a/crates/ruma-common/tests/events/video.rs b/crates/ruma-common/tests/events/video.rs index 326d0227..1e3a1e64 100644 --- a/crates/ruma-common/tests/events/video.rs +++ b/crates/ruma-common/tests/events/video.rs @@ -12,10 +12,7 @@ use ruma_common::{ image::{ThumbnailContent, ThumbnailFileContent, ThumbnailFileContentInfo}, message::MessageContent, relation::InReplyTo, - room::{ - message::{MessageType, Relation, RoomMessageEventContent, VideoMessageEventContent}, - JsonWebKeyInit, MediaSource, - }, + room::{message::Relation, JsonWebKeyInit}, video::{VideoContent, VideoEventContent}, AnyMessageLikeEvent, MessageLikeEvent, }, @@ -298,37 +295,3 @@ fn message_event_deserialization() { assert_eq!(info.mimetype.as_deref(), Some("video/webm")); assert_eq!(info.size, Some(uint!(123_774))); } - -#[test] -fn room_message_serialization() { - let message_event_content = - RoomMessageEventContent::new(MessageType::Video(VideoMessageEventContent::plain( - "Upload: my_video.mp4".to_owned(), - mxc_uri!("mxc://notareal.hs/file").to_owned(), - None, - ))); - - assert_eq!( - to_json_value(&message_event_content).unwrap(), - json!({ - "body": "Upload: my_video.mp4", - "url": "mxc://notareal.hs/file", - "msgtype": "m.video", - }) - ); -} - -#[test] -fn room_message_deserialization() { - let json_data = json!({ - "body": "Upload: my_video.mp4", - "url": "mxc://notareal.hs/file", - "msgtype": "m.video", - }); - - let event_content = from_json_value::(json_data).unwrap(); - let content = assert_matches!(event_content.msgtype, MessageType::Video(content) => content); - assert_eq!(content.body, "Upload: my_video.mp4"); - let url = assert_matches!(content.source, MediaSource::Plain(url) => url); - assert_eq!(url, "mxc://notareal.hs/file"); -}