generic add event

master
Devin R 2020-04-06 09:11:38 -04:00
parent 00ac6d08f9
commit 854948fc6d
3 changed files with 61 additions and 14 deletions

View File

@ -64,3 +64,4 @@ serde_json = { version = "1.0.49" }
tracing-subscriber = "0.2.3" tracing-subscriber = "0.2.3"
tempfile = "3.1.0" tempfile = "3.1.0"
mockito = "0.23.3" mockito = "0.23.3"
serde = "1.0.105"

View File

@ -9,12 +9,18 @@ mod test_it {
use std::path::Path; use std::path::Path;
use crate::identifiers::UserId; use crate::identifiers::UserId;
use crate::events::collections::all::RoomEvent; use crate::events::{
collections::all::RoomEvent,
room::{
power_levels::PowerLevelsEvent,
},
EventResult, FromRaw, TryFromRaw,
};
use crate::{AsyncClient, Session, SyncSettings}; use crate::{AsyncClient, Session, SyncSettings};
use serde_json as json; use serde_json::Value;
use mockito::{mock, Matcher}; use mockito::{mock, Matcher};
use serde::{Serialize, Deserialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};
use url::Url; use url::Url;
use std::convert::TryFrom; use std::convert::TryFrom;
@ -22,9 +28,9 @@ mod test_it {
use std::time::Duration; use std::time::Duration;
use std::panic; use std::panic;
#[derive(Default)]
pub struct ResponseBuilder { pub struct ResponseBuilder {
events: Vec<RoomEvent> events: Vec<RoomEvent>,
} }
pub struct TestRunner { pub struct TestRunner {
@ -40,34 +46,35 @@ mod test_it {
impl ResponseBuilder { impl ResponseBuilder {
/// Creates an `IncomingResponse` to hold events for a sync. /// Creates an `IncomingResponse` to hold events for a sync.
pub fn create_sync_response(&mut self) -> &mut Self { pub fn create_sync_response(mut self) -> &mut Self {
self self
} }
/// Just throw events at the client, not part of a specific response. /// Just throw events at the client, not part of a specific response.
pub fn create_event_stream(&mut self) -> &mut Self { pub fn create_event_stream(mut self) -> Self {
self self
} }
/// Add an event either to the event stream or the appropriate `IncomingResponse` field. /// Add an event either to the event stream or the appropriate `IncomingResponse` field.
pub fn add_event_from_file<S: Serialize, P: AsRef<Path>>(&mut self, path: P) -> &mut Self { pub fn add_event_from_file<Ev: TryFromRaw, P: AsRef<Path>>(mut self, path: P, variant: fn(Ev) -> RoomEvent) -> Self {
let json = fs::read_to_string(path.as_ref()).expect(&format!("file not found {:?}", path.as_ref())); let val = fs::read_to_string(path.as_ref()).expect(&format!("file not found {:?}", path.as_ref()));
let event = serde_json::from_str::<S>(&json).expect("deserialization failed"); let json = serde_json::value::Value::from_str(&val).expect(&format!("not valid json {}", val));
self.events.push(event); let event = serde_json::from_value::<ruma_events::EventResult<Ev>>(json).unwrap().into_result().unwrap();
self.add_event(variant(event));
self self
} }
fn add_event<T, U>(&mut self, content: T, event: fn(T) -> RoomEvent) { fn add_event(&mut self, event: RoomEvent) {
self.events.push(event(content)) self.events.push(event)
} }
/// Consumes `ResponseBuilder and returns a `TestRunner`. /// Consumes `ResponseBuilder and returns a `TestRunner`.
/// ///
/// The `TestRunner` streams the events to the client and enables methods to set assertions /// The `TestRunner` streams the events to the client and enables methods to set assertions
/// about the state of the client. /// about the state of the client.
pub fn build(self) -> TestRunner { pub fn build(mut self) -> TestRunner {
let mock = mock("POST", "/_matrix/client/r0/login") let mock = mock("POST", "/_matrix/client/r0/login")
.with_status(200) .with_status(200)
.with_body_from_file("tests/data/login_response.json") .with_body_from_file("tests/data/login_response.json")
@ -93,4 +100,12 @@ mod test_it {
} }
} }
#[test]
fn test() {
let mut bld = ResponseBuilder::default();
let runner = bld.add_event_from_file("./tests/data/events/power_levels.json", RoomEvent::RoomPowerLevels)
.build();
}
} }

View File

@ -0,0 +1,31 @@
{
"content": {
"ban": 50,
"events": {
"m.room.avatar": 50,
"m.room.canonical_alias": 50,
"m.room.history_visibility": 100,
"m.room.name": 50,
"m.room.power_levels": 100,
"m.room.message": 25
},
"events_default": 0,
"invite": 0,
"kick": 50,
"redact": 50,
"state_default": 50,
"users": {
"@example:localhost": 100,
"@bob:localhost": 0
},
"users_default": 0
},
"event_id": "$15139375512JaHAW:localhost",
"origin_server_ts": 1513937551359,
"sender": "@example:localhost",
"state_key": "",
"type": "m.room.power_levels",
"unsigned": {
"age": 7034220535
}
}