Continue implementation of m.room.power_levels and add tests.

This commit is contained in:
Jimmy Cuadra 2019-06-21 16:02:53 -07:00
parent 090a91f78b
commit cc107106f0
3 changed files with 92 additions and 25 deletions

View File

@ -20,7 +20,7 @@ pub mod join_rules;
// pub mod message;
pub mod name;
pub mod pinned_events;
// pub mod power_levels;
pub mod power_levels;
pub mod redaction;
pub mod server_acl;
pub mod third_party_invite;

View File

@ -203,7 +203,33 @@ mod raw {
#[cfg(test)]
mod tests {
use super::NameEvent;
use std::convert::TryFrom;
use js_int::UInt;
use ruma_identifiers::{EventId, UserId};
use super::{NameEvent, NameEventContent};
#[test]
fn serialization_with_optional_fields_as_none() {
let name_event = NameEvent {
content: NameEventContent {
name: Some("The room name".to_string()),
},
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
origin_server_ts: UInt::try_from(1).unwrap(),
prev_content: None,
room_id: None,
unsigned: None,
sender: UserId::try_from("@carl:matrix.org").unwrap(),
state_key: "".to_string(),
};
let actual = serde_json::to_string(&name_event).unwrap();
let expected = r#"{"content":{"name":"The room name"},"event_id":"$h29iv0s8:example.com","origin_server_ts":1,"sender":"@carl:matrix.org","state_key":"","type":"m.room.name"}"#;
assert_eq!(actual, expected);
}
#[test]
fn absent_field_as_none() {

View File

@ -2,15 +2,12 @@
use std::{collections::HashMap, convert::TryFrom, str::FromStr};
use js_int::UInt;
use js_int::{Int, UInt};
use ruma_identifiers::{EventId, RoomId, UserId};
use serde::{ser::SerializeStruct, Deserialize, Serialize, Serializer};
use serde_json::Value;
use crate::{
Event, EventType, InvalidEvent, InvalidInput, RoomEvent, StateEvent,
};
use crate::{Event, EventType, InvalidEvent, InvalidInput, RoomEvent, StateEvent};
/// Defines the power levels (privileges) of users in the room.
#[derive(Clone, Debug, PartialEq)]
@ -26,7 +23,7 @@ pub struct PowerLevelsEvent {
pub origin_server_ts: UInt,
/// The previous content for this state key, if any.
pub prev_content: Option<NameEventContent>,
pub prev_content: Option<PowerLevelsEventContent>,
/// The unique identifier for the room associated with this event.
pub room_id: Option<RoomId>,
@ -110,9 +107,7 @@ impl FromStr for PowerLevelsEvent {
},
event_id: raw.event_id,
origin_server_ts: raw.origin_server_ts,
prev_content: raw
.prev_content
.map(|prev| PowerLevelsEventContent {
prev_content: raw.prev_content.map(|prev| PowerLevelsEventContent {
ban: prev.ban,
events: prev.events,
events_default: prev.events_default,
@ -141,7 +136,7 @@ impl<'a> TryFrom<&'a str> for PowerLevelsEvent {
}
}
impl Serialize for NameEvent {
impl Serialize for PowerLevelsEvent {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
@ -186,7 +181,11 @@ impl Serialize for NameEvent {
}
}
impl_state_event!(PowerLevelsEvent, PowerLevelsEventContent, EventType::RoomPowerLevels);
impl_state_event!(
PowerLevelsEvent,
PowerLevelsEventContent,
EventType::RoomPowerLevels
);
mod raw {
use super::*;
@ -205,7 +204,7 @@ mod raw {
pub origin_server_ts: UInt,
/// The previous content for this state key, if any.
pub prev_content: Option<NameEventContent>,
pub prev_content: Option<PowerLevelsEventContent>,
/// The unique identifier for the room associated with this event.
pub room_id: Option<RoomId>,
@ -280,3 +279,45 @@ pub struct NotificationPowerLevels {
fn default_power_level() -> Int {
Int::from(50)
}
#[cfg(test)]
mod tests {
use std::{collections::HashMap, convert::TryFrom};
use js_int::{Int, UInt};
use ruma_identifiers::{EventId, UserId};
use super::{NotificationPowerLevels, PowerLevelsEvent, PowerLevelsEventContent};
#[test]
fn serialization_with_optional_fields_as_none() {
let default = Int::try_from(50).unwrap();
let power_levels_event = PowerLevelsEvent {
content: PowerLevelsEventContent {
ban: default,
events: HashMap::new(),
events_default: default,
invite: default,
kick: default,
redact: default,
state_default: default,
users: HashMap::new(),
users_default: default,
notifications: NotificationPowerLevels { room: default },
},
event_id: EventId::try_from("$h29iv0s8:example.com").unwrap(),
origin_server_ts: UInt::try_from(1).unwrap(),
prev_content: None,
room_id: None,
unsigned: None,
sender: UserId::try_from("@carl:matrix.org").unwrap(),
state_key: "".to_string(),
};
let actual = serde_json::to_string(&power_levels_event).unwrap();
let expected = r#"{"content":{"ban":50,"events":{},"events_default":50,"invite":50,"kick":50,"redact":50,"state_default":50,"users":{},"users_default":50,"notifications":{"room":50}},"event_id":"$h29iv0s8:example.com","origin_server_ts":1,"sender":"@carl:matrix.org","state_key":"","type":"m.room.power_levels"}"#;
assert_eq!(actual, expected);
}
}