Externalize generic event struct tests
This commit is contained in:
parent
b3f024bf13
commit
f531dce754
@ -169,8 +169,8 @@ pub use self::{
|
|||||||
event_type::EventType,
|
event_type::EventType,
|
||||||
from_raw::{FromRaw, TryFromRaw},
|
from_raw::{FromRaw, TryFromRaw},
|
||||||
json::EventJson,
|
json::EventJson,
|
||||||
message::MessageEvent,
|
message::{AnyMessageEventContent, MessageEvent},
|
||||||
state::StateEvent,
|
state::{AnyStateEventContent, StateEvent},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// Extra information about an event that is not incorporated into the event's
|
/// Extra information about an event that is not incorporated into the event's
|
||||||
|
229
src/message.rs
229
src/message.rs
@ -49,232 +49,3 @@ pub struct MessageEvent<C: MessageEventContent> {
|
|||||||
/// Additional key-value pairs not signed by the homeserver.
|
/// Additional key-value pairs not signed by the homeserver.
|
||||||
pub unsigned: UnsignedData,
|
pub unsigned: UnsignedData,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use std::{
|
|
||||||
convert::TryFrom,
|
|
||||||
time::{Duration, UNIX_EPOCH},
|
|
||||||
};
|
|
||||||
|
|
||||||
use js_int::UInt;
|
|
||||||
use matches::assert_matches;
|
|
||||||
use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId};
|
|
||||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
|
||||||
|
|
||||||
use super::{AnyMessageEventContent, MessageEvent};
|
|
||||||
use crate::{
|
|
||||||
call::{answer::AnswerEventContent, SessionDescription, SessionDescriptionType},
|
|
||||||
room::{ImageInfo, ThumbnailInfo},
|
|
||||||
sticker::StickerEventContent,
|
|
||||||
EventJson, UnsignedData,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn message_serialize_sticker() {
|
|
||||||
let aliases_event = MessageEvent {
|
|
||||||
content: AnyMessageEventContent::Sticker(StickerEventContent {
|
|
||||||
body: "Hello".into(),
|
|
||||||
info: ImageInfo {
|
|
||||||
height: UInt::new(423),
|
|
||||||
width: UInt::new(1011),
|
|
||||||
mimetype: Some("image/png".into()),
|
|
||||||
size: UInt::new(84242),
|
|
||||||
thumbnail_info: Some(Box::new(ThumbnailInfo {
|
|
||||||
width: UInt::new(800),
|
|
||||||
height: UInt::new(334),
|
|
||||||
mimetype: Some("image/png".into()),
|
|
||||||
size: UInt::new(82595),
|
|
||||||
})),
|
|
||||||
thumbnail_url: Some("mxc://matrix.org".into()),
|
|
||||||
thumbnail_file: None,
|
|
||||||
},
|
|
||||||
url: "http://www.matrix.org".into(),
|
|
||||||
}),
|
|
||||||
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
|
||||||
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
|
|
||||||
room_id: RoomId::try_from("!roomid:room.com").unwrap(),
|
|
||||||
sender: UserId::try_from("@carl:example.com").unwrap(),
|
|
||||||
unsigned: UnsignedData::default(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let actual = to_json_value(&aliases_event).unwrap();
|
|
||||||
let expected = 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",
|
|
||||||
"w": 1011
|
|
||||||
},
|
|
||||||
"url": "http://www.matrix.org"
|
|
||||||
},
|
|
||||||
"event_id": "$h29iv0s8:example.com",
|
|
||||||
"origin_server_ts": 1,
|
|
||||||
"room_id": "!roomid:room.com",
|
|
||||||
"sender": "@carl:example.com",
|
|
||||||
"type": "m.sticker",
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(actual, expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn deserialize_message_call_answer_content() {
|
|
||||||
let json_data = json!({
|
|
||||||
"answer": {
|
|
||||||
"type": "answer",
|
|
||||||
"sdp": "Hello"
|
|
||||||
},
|
|
||||||
"call_id": "foofoo",
|
|
||||||
"version": 1
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_matches!(
|
|
||||||
from_json_value::<EventJson<AnyMessageEventContent>>(json_data)
|
|
||||||
.unwrap()
|
|
||||||
.deserialize_content("m.call.answer")
|
|
||||||
.unwrap(),
|
|
||||||
AnyMessageEventContent::CallAnswer(AnswerEventContent {
|
|
||||||
answer: SessionDescription {
|
|
||||||
session_type: SessionDescriptionType::Answer,
|
|
||||||
sdp,
|
|
||||||
},
|
|
||||||
call_id,
|
|
||||||
version,
|
|
||||||
}) if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn deserialize_message_call_answer() {
|
|
||||||
let json_data = json!({
|
|
||||||
"content": {
|
|
||||||
"answer": {
|
|
||||||
"type": "answer",
|
|
||||||
"sdp": "Hello"
|
|
||||||
},
|
|
||||||
"call_id": "foofoo",
|
|
||||||
"version": 1
|
|
||||||
},
|
|
||||||
"event_id": "$h29iv0s8:example.com",
|
|
||||||
"origin_server_ts": 1,
|
|
||||||
"room_id": "!roomid:room.com",
|
|
||||||
"sender": "@carl:example.com",
|
|
||||||
"type": "m.call.answer"
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_matches!(
|
|
||||||
from_json_value::<EventJson<MessageEvent<AnyMessageEventContent>>>(json_data)
|
|
||||||
.unwrap()
|
|
||||||
.deserialize()
|
|
||||||
.unwrap(),
|
|
||||||
MessageEvent {
|
|
||||||
content: AnyMessageEventContent::CallAnswer(AnswerEventContent {
|
|
||||||
answer: SessionDescription {
|
|
||||||
session_type: SessionDescriptionType::Answer,
|
|
||||||
sdp,
|
|
||||||
},
|
|
||||||
call_id,
|
|
||||||
version,
|
|
||||||
}),
|
|
||||||
event_id,
|
|
||||||
origin_server_ts,
|
|
||||||
room_id,
|
|
||||||
sender,
|
|
||||||
unsigned,
|
|
||||||
} if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap()
|
|
||||||
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap()
|
|
||||||
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
|
|
||||||
&& room_id == RoomId::try_from("!roomid:room.com").unwrap()
|
|
||||||
&& sender == UserId::try_from("@carl:example.com").unwrap()
|
|
||||||
&& unsigned.is_empty()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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",
|
|
||||||
"w": 1011
|
|
||||||
},
|
|
||||||
"url": "http://www.matrix.org"
|
|
||||||
},
|
|
||||||
"event_id": "$h29iv0s8:example.com",
|
|
||||||
"origin_server_ts": 1,
|
|
||||||
"room_id": "!roomid:room.com",
|
|
||||||
"sender": "@carl:example.com",
|
|
||||||
"type": "m.sticker"
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_matches!(
|
|
||||||
from_json_value::<EventJson<MessageEvent<AnyMessageEventContent>>>(json_data)
|
|
||||||
.unwrap()
|
|
||||||
.deserialize()
|
|
||||||
.unwrap(),
|
|
||||||
MessageEvent {
|
|
||||||
content: AnyMessageEventContent::Sticker(StickerEventContent {
|
|
||||||
body,
|
|
||||||
info: ImageInfo {
|
|
||||||
height,
|
|
||||||
width,
|
|
||||||
mimetype: Some(mimetype),
|
|
||||||
size,
|
|
||||||
thumbnail_info: Some(thumbnail_info),
|
|
||||||
thumbnail_url: Some(thumbnail_url),
|
|
||||||
thumbnail_file: None,
|
|
||||||
},
|
|
||||||
url,
|
|
||||||
}),
|
|
||||||
event_id,
|
|
||||||
origin_server_ts,
|
|
||||||
room_id,
|
|
||||||
sender,
|
|
||||||
unsigned
|
|
||||||
} if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap()
|
|
||||||
&& body == "Hello"
|
|
||||||
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
|
|
||||||
&& room_id == RoomId::try_from("!roomid:room.com").unwrap()
|
|
||||||
&& sender == UserId::try_from("@carl:example.com").unwrap()
|
|
||||||
&& height == UInt::new(423)
|
|
||||||
&& width == UInt::new(1011)
|
|
||||||
&& mimetype == "image/png"
|
|
||||||
&& size == UInt::new(84242)
|
|
||||||
&& thumbnail_url == "mxc://matrix.org"
|
|
||||||
&& matches!(
|
|
||||||
thumbnail_info.as_ref(),
|
|
||||||
ThumbnailInfo {
|
|
||||||
width: thumb_width,
|
|
||||||
height: thumb_height,
|
|
||||||
mimetype: thumb_mimetype,
|
|
||||||
size: thumb_size,
|
|
||||||
} if *thumb_width == UInt::new(800)
|
|
||||||
&& *thumb_height == UInt::new(334)
|
|
||||||
&& *thumb_mimetype == Some("image/png".to_string())
|
|
||||||
&& *thumb_size == UInt::new(82595)
|
|
||||||
)
|
|
||||||
&& url == "http://www.matrix.org"
|
|
||||||
&& unsigned.is_empty()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
228
src/state.rs
228
src/state.rs
@ -69,231 +69,3 @@ pub struct StateEvent<C: StateEventContent> {
|
|||||||
/// Additional key-value pairs not signed by the homeserver.
|
/// Additional key-value pairs not signed by the homeserver.
|
||||||
pub unsigned: UnsignedData,
|
pub unsigned: UnsignedData,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use std::{
|
|
||||||
convert::TryFrom,
|
|
||||||
time::{Duration, UNIX_EPOCH},
|
|
||||||
};
|
|
||||||
|
|
||||||
use js_int::UInt;
|
|
||||||
use matches::assert_matches;
|
|
||||||
use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId};
|
|
||||||
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
|
||||||
|
|
||||||
use super::{AnyStateEventContent, StateEvent};
|
|
||||||
use crate::{
|
|
||||||
room::{
|
|
||||||
aliases::AliasesEventContent, avatar::AvatarEventContent, ImageInfo, ThumbnailInfo,
|
|
||||||
},
|
|
||||||
EventJson, UnsignedData,
|
|
||||||
};
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn serialize_aliases_with_prev_content() {
|
|
||||||
let aliases_event = StateEvent {
|
|
||||||
content: AnyStateEventContent::RoomAliases(AliasesEventContent {
|
|
||||||
aliases: vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()],
|
|
||||||
}),
|
|
||||||
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
|
||||||
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
|
|
||||||
prev_content: Some(AnyStateEventContent::RoomAliases(AliasesEventContent {
|
|
||||||
aliases: vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()],
|
|
||||||
})),
|
|
||||||
room_id: RoomId::try_from("!roomid:room.com").unwrap(),
|
|
||||||
sender: UserId::try_from("@carl:example.com").unwrap(),
|
|
||||||
state_key: "".to_string(),
|
|
||||||
unsigned: UnsignedData::default(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let actual = to_json_value(&aliases_event).unwrap();
|
|
||||||
let expected = json!({
|
|
||||||
"content": {
|
|
||||||
"aliases": [ "#somewhere:localhost" ]
|
|
||||||
},
|
|
||||||
"event_id": "$h29iv0s8:example.com",
|
|
||||||
"origin_server_ts": 1,
|
|
||||||
"prev_content": {
|
|
||||||
"aliases": [ "#somewhere:localhost" ]
|
|
||||||
},
|
|
||||||
"room_id": "!roomid:room.com",
|
|
||||||
"sender": "@carl:example.com",
|
|
||||||
"state_key": "",
|
|
||||||
"type": "m.room.aliases",
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(actual, expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn serialize_aliases_without_prev_content() {
|
|
||||||
let aliases_event = StateEvent {
|
|
||||||
content: AnyStateEventContent::RoomAliases(AliasesEventContent {
|
|
||||||
aliases: vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()],
|
|
||||||
}),
|
|
||||||
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
|
||||||
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
|
|
||||||
prev_content: None,
|
|
||||||
room_id: RoomId::try_from("!roomid:room.com").unwrap(),
|
|
||||||
sender: UserId::try_from("@carl:example.com").unwrap(),
|
|
||||||
state_key: "".to_string(),
|
|
||||||
unsigned: UnsignedData::default(),
|
|
||||||
};
|
|
||||||
|
|
||||||
let actual = to_json_value(&aliases_event).unwrap();
|
|
||||||
let expected = json!({
|
|
||||||
"content": {
|
|
||||||
"aliases": [ "#somewhere:localhost" ]
|
|
||||||
},
|
|
||||||
"event_id": "$h29iv0s8:example.com",
|
|
||||||
"origin_server_ts": 1,
|
|
||||||
"room_id": "!roomid:room.com",
|
|
||||||
"sender": "@carl:example.com",
|
|
||||||
"state_key": "",
|
|
||||||
"type": "m.room.aliases",
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_eq!(actual, expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn deserialize_aliases_content() {
|
|
||||||
let json_data = json!({
|
|
||||||
"aliases": [ "#somewhere:localhost" ]
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_matches!(
|
|
||||||
from_json_value::<EventJson<AnyStateEventContent>>(json_data)
|
|
||||||
.unwrap()
|
|
||||||
.deserialize_content("m.room.aliases")
|
|
||||||
.unwrap(),
|
|
||||||
AnyStateEventContent::RoomAliases(content)
|
|
||||||
if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()]
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn deserialize_aliases_with_prev_content() {
|
|
||||||
let json_data = json!({
|
|
||||||
"content": {
|
|
||||||
"aliases": [ "#somewhere:localhost" ]
|
|
||||||
},
|
|
||||||
"event_id": "$h29iv0s8:example.com",
|
|
||||||
"origin_server_ts": 1,
|
|
||||||
"prev_content": {
|
|
||||||
"aliases": [ "#inner:localhost" ]
|
|
||||||
},
|
|
||||||
"room_id": "!roomid:room.com",
|
|
||||||
"sender": "@carl:example.com",
|
|
||||||
"state_key": "",
|
|
||||||
"type": "m.room.aliases"
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_matches!(
|
|
||||||
from_json_value::<EventJson<StateEvent<AnyStateEventContent>>>(json_data)
|
|
||||||
.unwrap()
|
|
||||||
.deserialize()
|
|
||||||
.unwrap(),
|
|
||||||
StateEvent {
|
|
||||||
content: AnyStateEventContent::RoomAliases(content),
|
|
||||||
event_id,
|
|
||||||
origin_server_ts,
|
|
||||||
prev_content: Some(AnyStateEventContent::RoomAliases(prev_content)),
|
|
||||||
room_id,
|
|
||||||
sender,
|
|
||||||
state_key,
|
|
||||||
unsigned,
|
|
||||||
} if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()]
|
|
||||||
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap()
|
|
||||||
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
|
|
||||||
&& prev_content.aliases == vec![RoomAliasId::try_from("#inner:localhost").unwrap()]
|
|
||||||
&& room_id == RoomId::try_from("!roomid:room.com").unwrap()
|
|
||||||
&& sender == UserId::try_from("@carl:example.com").unwrap()
|
|
||||||
&& state_key == ""
|
|
||||||
&& unsigned.is_empty()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn deserialize_avatar_without_prev_content() {
|
|
||||||
let json_data = json!({
|
|
||||||
"content": {
|
|
||||||
"info": {
|
|
||||||
"h": 423,
|
|
||||||
"mimetype": "image/png",
|
|
||||||
"size": 84242,
|
|
||||||
"thumbnail_info": {
|
|
||||||
"h": 334,
|
|
||||||
"mimetype": "image/png",
|
|
||||||
"size": 82595,
|
|
||||||
"w": 800
|
|
||||||
},
|
|
||||||
"thumbnail_url": "mxc://matrix.org",
|
|
||||||
"w": 1011
|
|
||||||
},
|
|
||||||
"url": "http://www.matrix.org"
|
|
||||||
},
|
|
||||||
"event_id": "$h29iv0s8:example.com",
|
|
||||||
"origin_server_ts": 1,
|
|
||||||
"room_id": "!roomid:room.com",
|
|
||||||
"sender": "@carl:example.com",
|
|
||||||
"state_key": "",
|
|
||||||
"type": "m.room.avatar"
|
|
||||||
});
|
|
||||||
|
|
||||||
assert_matches!(
|
|
||||||
from_json_value::<EventJson<StateEvent<AnyStateEventContent>>>(json_data)
|
|
||||||
.unwrap()
|
|
||||||
.deserialize()
|
|
||||||
.unwrap(),
|
|
||||||
StateEvent {
|
|
||||||
content: AnyStateEventContent::RoomAvatar(AvatarEventContent {
|
|
||||||
info: Some(info),
|
|
||||||
url,
|
|
||||||
}),
|
|
||||||
event_id,
|
|
||||||
origin_server_ts,
|
|
||||||
prev_content: None,
|
|
||||||
room_id,
|
|
||||||
sender,
|
|
||||||
state_key,
|
|
||||||
unsigned
|
|
||||||
} if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap()
|
|
||||||
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
|
|
||||||
&& room_id == RoomId::try_from("!roomid:room.com").unwrap()
|
|
||||||
&& sender == UserId::try_from("@carl:example.com").unwrap()
|
|
||||||
&& state_key == ""
|
|
||||||
&& matches!(
|
|
||||||
info.as_ref(),
|
|
||||||
ImageInfo {
|
|
||||||
height,
|
|
||||||
width,
|
|
||||||
mimetype: Some(mimetype),
|
|
||||||
size,
|
|
||||||
thumbnail_info: Some(thumbnail_info),
|
|
||||||
thumbnail_url: Some(thumbnail_url),
|
|
||||||
thumbnail_file: None,
|
|
||||||
} if *height == UInt::new(423)
|
|
||||||
&& *width == UInt::new(1011)
|
|
||||||
&& *mimetype == "image/png"
|
|
||||||
&& *size == UInt::new(84242)
|
|
||||||
&& matches!(
|
|
||||||
thumbnail_info.as_ref(),
|
|
||||||
ThumbnailInfo {
|
|
||||||
width: thumb_width,
|
|
||||||
height: thumb_height,
|
|
||||||
mimetype: thumb_mimetype,
|
|
||||||
size: thumb_size,
|
|
||||||
} if *thumb_width == UInt::new(800)
|
|
||||||
&& *thumb_height == UInt::new(334)
|
|
||||||
&& *thumb_mimetype == Some("image/png".to_string())
|
|
||||||
&& *thumb_size == UInt::new(82595)
|
|
||||||
&& *thumbnail_url == "mxc://matrix.org"
|
|
||||||
)
|
|
||||||
)
|
|
||||||
&& url == "http://www.matrix.org"
|
|
||||||
&& unsigned.is_empty()
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
223
tests/message_event.rs
Normal file
223
tests/message_event.rs
Normal file
@ -0,0 +1,223 @@
|
|||||||
|
use std::{
|
||||||
|
convert::TryFrom,
|
||||||
|
time::{Duration, UNIX_EPOCH},
|
||||||
|
};
|
||||||
|
|
||||||
|
use js_int::UInt;
|
||||||
|
use matches::assert_matches;
|
||||||
|
use ruma_events::{
|
||||||
|
call::{answer::AnswerEventContent, SessionDescription, SessionDescriptionType},
|
||||||
|
room::{ImageInfo, ThumbnailInfo},
|
||||||
|
sticker::StickerEventContent,
|
||||||
|
AnyMessageEventContent, EventJson, MessageEvent, UnsignedData,
|
||||||
|
};
|
||||||
|
use ruma_identifiers::{EventId, RoomId, UserId};
|
||||||
|
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn message_serialize_sticker() {
|
||||||
|
let aliases_event = MessageEvent {
|
||||||
|
content: AnyMessageEventContent::Sticker(StickerEventContent {
|
||||||
|
body: "Hello".into(),
|
||||||
|
info: ImageInfo {
|
||||||
|
height: UInt::new(423),
|
||||||
|
width: UInt::new(1011),
|
||||||
|
mimetype: Some("image/png".into()),
|
||||||
|
size: UInt::new(84242),
|
||||||
|
thumbnail_info: Some(Box::new(ThumbnailInfo {
|
||||||
|
width: UInt::new(800),
|
||||||
|
height: UInt::new(334),
|
||||||
|
mimetype: Some("image/png".into()),
|
||||||
|
size: UInt::new(82595),
|
||||||
|
})),
|
||||||
|
thumbnail_url: Some("mxc://matrix.org".into()),
|
||||||
|
thumbnail_file: None,
|
||||||
|
},
|
||||||
|
url: "http://www.matrix.org".into(),
|
||||||
|
}),
|
||||||
|
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
||||||
|
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
|
||||||
|
room_id: RoomId::try_from("!roomid:room.com").unwrap(),
|
||||||
|
sender: UserId::try_from("@carl:example.com").unwrap(),
|
||||||
|
unsigned: UnsignedData::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let actual = to_json_value(&aliases_event).unwrap();
|
||||||
|
let expected = 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",
|
||||||
|
"w": 1011
|
||||||
|
},
|
||||||
|
"url": "http://www.matrix.org"
|
||||||
|
},
|
||||||
|
"event_id": "$h29iv0s8:example.com",
|
||||||
|
"origin_server_ts": 1,
|
||||||
|
"room_id": "!roomid:room.com",
|
||||||
|
"sender": "@carl:example.com",
|
||||||
|
"type": "m.sticker",
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deserialize_message_call_answer_content() {
|
||||||
|
let json_data = json!({
|
||||||
|
"answer": {
|
||||||
|
"type": "answer",
|
||||||
|
"sdp": "Hello"
|
||||||
|
},
|
||||||
|
"call_id": "foofoo",
|
||||||
|
"version": 1
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_matches!(
|
||||||
|
from_json_value::<EventJson<AnyMessageEventContent>>(json_data)
|
||||||
|
.unwrap()
|
||||||
|
.deserialize_content("m.call.answer")
|
||||||
|
.unwrap(),
|
||||||
|
AnyMessageEventContent::CallAnswer(AnswerEventContent {
|
||||||
|
answer: SessionDescription {
|
||||||
|
session_type: SessionDescriptionType::Answer,
|
||||||
|
sdp,
|
||||||
|
},
|
||||||
|
call_id,
|
||||||
|
version,
|
||||||
|
}) if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deserialize_message_call_answer() {
|
||||||
|
let json_data = json!({
|
||||||
|
"content": {
|
||||||
|
"answer": {
|
||||||
|
"type": "answer",
|
||||||
|
"sdp": "Hello"
|
||||||
|
},
|
||||||
|
"call_id": "foofoo",
|
||||||
|
"version": 1
|
||||||
|
},
|
||||||
|
"event_id": "$h29iv0s8:example.com",
|
||||||
|
"origin_server_ts": 1,
|
||||||
|
"room_id": "!roomid:room.com",
|
||||||
|
"sender": "@carl:example.com",
|
||||||
|
"type": "m.call.answer"
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_matches!(
|
||||||
|
from_json_value::<EventJson<MessageEvent<AnyMessageEventContent>>>(json_data)
|
||||||
|
.unwrap()
|
||||||
|
.deserialize()
|
||||||
|
.unwrap(),
|
||||||
|
MessageEvent {
|
||||||
|
content: AnyMessageEventContent::CallAnswer(AnswerEventContent {
|
||||||
|
answer: SessionDescription {
|
||||||
|
session_type: SessionDescriptionType::Answer,
|
||||||
|
sdp,
|
||||||
|
},
|
||||||
|
call_id,
|
||||||
|
version,
|
||||||
|
}),
|
||||||
|
event_id,
|
||||||
|
origin_server_ts,
|
||||||
|
room_id,
|
||||||
|
sender,
|
||||||
|
unsigned,
|
||||||
|
} if sdp == "Hello" && call_id == "foofoo" && version == UInt::new(1).unwrap()
|
||||||
|
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap()
|
||||||
|
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
|
||||||
|
&& room_id == RoomId::try_from("!roomid:room.com").unwrap()
|
||||||
|
&& sender == UserId::try_from("@carl:example.com").unwrap()
|
||||||
|
&& unsigned.is_empty()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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",
|
||||||
|
"w": 1011
|
||||||
|
},
|
||||||
|
"url": "http://www.matrix.org"
|
||||||
|
},
|
||||||
|
"event_id": "$h29iv0s8:example.com",
|
||||||
|
"origin_server_ts": 1,
|
||||||
|
"room_id": "!roomid:room.com",
|
||||||
|
"sender": "@carl:example.com",
|
||||||
|
"type": "m.sticker"
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_matches!(
|
||||||
|
from_json_value::<EventJson<MessageEvent<AnyMessageEventContent>>>(json_data)
|
||||||
|
.unwrap()
|
||||||
|
.deserialize()
|
||||||
|
.unwrap(),
|
||||||
|
MessageEvent {
|
||||||
|
content: AnyMessageEventContent::Sticker(StickerEventContent {
|
||||||
|
body,
|
||||||
|
info: ImageInfo {
|
||||||
|
height,
|
||||||
|
width,
|
||||||
|
mimetype: Some(mimetype),
|
||||||
|
size,
|
||||||
|
thumbnail_info: Some(thumbnail_info),
|
||||||
|
thumbnail_url: Some(thumbnail_url),
|
||||||
|
thumbnail_file: None,
|
||||||
|
},
|
||||||
|
url,
|
||||||
|
}),
|
||||||
|
event_id,
|
||||||
|
origin_server_ts,
|
||||||
|
room_id,
|
||||||
|
sender,
|
||||||
|
unsigned
|
||||||
|
} if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap()
|
||||||
|
&& body == "Hello"
|
||||||
|
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
|
||||||
|
&& room_id == RoomId::try_from("!roomid:room.com").unwrap()
|
||||||
|
&& sender == UserId::try_from("@carl:example.com").unwrap()
|
||||||
|
&& height == UInt::new(423)
|
||||||
|
&& width == UInt::new(1011)
|
||||||
|
&& mimetype == "image/png"
|
||||||
|
&& size == UInt::new(84242)
|
||||||
|
&& thumbnail_url == "mxc://matrix.org"
|
||||||
|
&& matches!(
|
||||||
|
thumbnail_info.as_ref(),
|
||||||
|
ThumbnailInfo {
|
||||||
|
width: thumb_width,
|
||||||
|
height: thumb_height,
|
||||||
|
mimetype: thumb_mimetype,
|
||||||
|
size: thumb_size,
|
||||||
|
} if *thumb_width == UInt::new(800)
|
||||||
|
&& *thumb_height == UInt::new(334)
|
||||||
|
&& *thumb_mimetype == Some("image/png".to_string())
|
||||||
|
&& *thumb_size == UInt::new(82595)
|
||||||
|
)
|
||||||
|
&& url == "http://www.matrix.org"
|
||||||
|
&& unsigned.is_empty()
|
||||||
|
);
|
||||||
|
}
|
220
tests/state_event.rs
Normal file
220
tests/state_event.rs
Normal file
@ -0,0 +1,220 @@
|
|||||||
|
use std::{
|
||||||
|
convert::TryFrom,
|
||||||
|
time::{Duration, UNIX_EPOCH},
|
||||||
|
};
|
||||||
|
|
||||||
|
use js_int::UInt;
|
||||||
|
use matches::assert_matches;
|
||||||
|
use ruma_events::{
|
||||||
|
room::{aliases::AliasesEventContent, avatar::AvatarEventContent, ImageInfo, ThumbnailInfo},
|
||||||
|
AnyStateEventContent, EventJson, StateEvent, UnsignedData,
|
||||||
|
};
|
||||||
|
use ruma_identifiers::{EventId, RoomAliasId, RoomId, UserId};
|
||||||
|
use serde_json::{from_value as from_json_value, json, to_value as to_json_value};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialize_aliases_with_prev_content() {
|
||||||
|
let aliases_event = StateEvent {
|
||||||
|
content: AnyStateEventContent::RoomAliases(AliasesEventContent {
|
||||||
|
aliases: vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()],
|
||||||
|
}),
|
||||||
|
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
||||||
|
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
|
||||||
|
prev_content: Some(AnyStateEventContent::RoomAliases(AliasesEventContent {
|
||||||
|
aliases: vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()],
|
||||||
|
})),
|
||||||
|
room_id: RoomId::try_from("!roomid:room.com").unwrap(),
|
||||||
|
sender: UserId::try_from("@carl:example.com").unwrap(),
|
||||||
|
state_key: "".to_string(),
|
||||||
|
unsigned: UnsignedData::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let actual = to_json_value(&aliases_event).unwrap();
|
||||||
|
let expected = json!({
|
||||||
|
"content": {
|
||||||
|
"aliases": [ "#somewhere:localhost" ]
|
||||||
|
},
|
||||||
|
"event_id": "$h29iv0s8:example.com",
|
||||||
|
"origin_server_ts": 1,
|
||||||
|
"prev_content": {
|
||||||
|
"aliases": [ "#somewhere:localhost" ]
|
||||||
|
},
|
||||||
|
"room_id": "!roomid:room.com",
|
||||||
|
"sender": "@carl:example.com",
|
||||||
|
"state_key": "",
|
||||||
|
"type": "m.room.aliases",
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn serialize_aliases_without_prev_content() {
|
||||||
|
let aliases_event = StateEvent {
|
||||||
|
content: AnyStateEventContent::RoomAliases(AliasesEventContent {
|
||||||
|
aliases: vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()],
|
||||||
|
}),
|
||||||
|
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
|
||||||
|
origin_server_ts: UNIX_EPOCH + Duration::from_millis(1),
|
||||||
|
prev_content: None,
|
||||||
|
room_id: RoomId::try_from("!roomid:room.com").unwrap(),
|
||||||
|
sender: UserId::try_from("@carl:example.com").unwrap(),
|
||||||
|
state_key: "".to_string(),
|
||||||
|
unsigned: UnsignedData::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
let actual = to_json_value(&aliases_event).unwrap();
|
||||||
|
let expected = json!({
|
||||||
|
"content": {
|
||||||
|
"aliases": [ "#somewhere:localhost" ]
|
||||||
|
},
|
||||||
|
"event_id": "$h29iv0s8:example.com",
|
||||||
|
"origin_server_ts": 1,
|
||||||
|
"room_id": "!roomid:room.com",
|
||||||
|
"sender": "@carl:example.com",
|
||||||
|
"state_key": "",
|
||||||
|
"type": "m.room.aliases",
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_eq!(actual, expected);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deserialize_aliases_content() {
|
||||||
|
let json_data = json!({
|
||||||
|
"aliases": [ "#somewhere:localhost" ]
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_matches!(
|
||||||
|
from_json_value::<EventJson<AnyStateEventContent>>(json_data)
|
||||||
|
.unwrap()
|
||||||
|
.deserialize_content("m.room.aliases")
|
||||||
|
.unwrap(),
|
||||||
|
AnyStateEventContent::RoomAliases(content)
|
||||||
|
if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deserialize_aliases_with_prev_content() {
|
||||||
|
let json_data = json!({
|
||||||
|
"content": {
|
||||||
|
"aliases": [ "#somewhere:localhost" ]
|
||||||
|
},
|
||||||
|
"event_id": "$h29iv0s8:example.com",
|
||||||
|
"origin_server_ts": 1,
|
||||||
|
"prev_content": {
|
||||||
|
"aliases": [ "#inner:localhost" ]
|
||||||
|
},
|
||||||
|
"room_id": "!roomid:room.com",
|
||||||
|
"sender": "@carl:example.com",
|
||||||
|
"state_key": "",
|
||||||
|
"type": "m.room.aliases"
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_matches!(
|
||||||
|
from_json_value::<EventJson<StateEvent<AnyStateEventContent>>>(json_data)
|
||||||
|
.unwrap()
|
||||||
|
.deserialize()
|
||||||
|
.unwrap(),
|
||||||
|
StateEvent {
|
||||||
|
content: AnyStateEventContent::RoomAliases(content),
|
||||||
|
event_id,
|
||||||
|
origin_server_ts,
|
||||||
|
prev_content: Some(AnyStateEventContent::RoomAliases(prev_content)),
|
||||||
|
room_id,
|
||||||
|
sender,
|
||||||
|
state_key,
|
||||||
|
unsigned,
|
||||||
|
} if content.aliases == vec![RoomAliasId::try_from("#somewhere:localhost").unwrap()]
|
||||||
|
&& event_id == EventId::try_from("$h29iv0s8:example.com").unwrap()
|
||||||
|
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
|
||||||
|
&& prev_content.aliases == vec![RoomAliasId::try_from("#inner:localhost").unwrap()]
|
||||||
|
&& room_id == RoomId::try_from("!roomid:room.com").unwrap()
|
||||||
|
&& sender == UserId::try_from("@carl:example.com").unwrap()
|
||||||
|
&& state_key == ""
|
||||||
|
&& unsigned.is_empty()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn deserialize_avatar_without_prev_content() {
|
||||||
|
let json_data = json!({
|
||||||
|
"content": {
|
||||||
|
"info": {
|
||||||
|
"h": 423,
|
||||||
|
"mimetype": "image/png",
|
||||||
|
"size": 84242,
|
||||||
|
"thumbnail_info": {
|
||||||
|
"h": 334,
|
||||||
|
"mimetype": "image/png",
|
||||||
|
"size": 82595,
|
||||||
|
"w": 800
|
||||||
|
},
|
||||||
|
"thumbnail_url": "mxc://matrix.org",
|
||||||
|
"w": 1011
|
||||||
|
},
|
||||||
|
"url": "http://www.matrix.org"
|
||||||
|
},
|
||||||
|
"event_id": "$h29iv0s8:example.com",
|
||||||
|
"origin_server_ts": 1,
|
||||||
|
"room_id": "!roomid:room.com",
|
||||||
|
"sender": "@carl:example.com",
|
||||||
|
"state_key": "",
|
||||||
|
"type": "m.room.avatar"
|
||||||
|
});
|
||||||
|
|
||||||
|
assert_matches!(
|
||||||
|
from_json_value::<EventJson<StateEvent<AnyStateEventContent>>>(json_data)
|
||||||
|
.unwrap()
|
||||||
|
.deserialize()
|
||||||
|
.unwrap(),
|
||||||
|
StateEvent {
|
||||||
|
content: AnyStateEventContent::RoomAvatar(AvatarEventContent {
|
||||||
|
info: Some(info),
|
||||||
|
url,
|
||||||
|
}),
|
||||||
|
event_id,
|
||||||
|
origin_server_ts,
|
||||||
|
prev_content: None,
|
||||||
|
room_id,
|
||||||
|
sender,
|
||||||
|
state_key,
|
||||||
|
unsigned
|
||||||
|
} if event_id == EventId::try_from("$h29iv0s8:example.com").unwrap()
|
||||||
|
&& origin_server_ts == UNIX_EPOCH + Duration::from_millis(1)
|
||||||
|
&& room_id == RoomId::try_from("!roomid:room.com").unwrap()
|
||||||
|
&& sender == UserId::try_from("@carl:example.com").unwrap()
|
||||||
|
&& state_key == ""
|
||||||
|
&& matches!(
|
||||||
|
info.as_ref(),
|
||||||
|
ImageInfo {
|
||||||
|
height,
|
||||||
|
width,
|
||||||
|
mimetype: Some(mimetype),
|
||||||
|
size,
|
||||||
|
thumbnail_info: Some(thumbnail_info),
|
||||||
|
thumbnail_url: Some(thumbnail_url),
|
||||||
|
thumbnail_file: None,
|
||||||
|
} if *height == UInt::new(423)
|
||||||
|
&& *width == UInt::new(1011)
|
||||||
|
&& *mimetype == "image/png"
|
||||||
|
&& *size == UInt::new(84242)
|
||||||
|
&& matches!(
|
||||||
|
thumbnail_info.as_ref(),
|
||||||
|
ThumbnailInfo {
|
||||||
|
width: thumb_width,
|
||||||
|
height: thumb_height,
|
||||||
|
mimetype: thumb_mimetype,
|
||||||
|
size: thumb_size,
|
||||||
|
} if *thumb_width == UInt::new(800)
|
||||||
|
&& *thumb_height == UInt::new(334)
|
||||||
|
&& *thumb_mimetype == Some("image/png".to_string())
|
||||||
|
&& *thumb_size == UInt::new(82595)
|
||||||
|
&& *thumbnail_url == "mxc://matrix.org"
|
||||||
|
)
|
||||||
|
)
|
||||||
|
&& url == "http://www.matrix.org"
|
||||||
|
&& unsigned.is_empty()
|
||||||
|
);
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user