base: Test the invited room creation.

master
Damir Jelić 2020-05-14 10:57:21 +02:00
parent fff3abf985
commit 77a8f1b1bc
2 changed files with 115 additions and 22 deletions

View File

@ -1432,7 +1432,10 @@ impl BaseClient {
#[cfg(test)]
mod test {
use crate::identifiers::{RoomId, UserId};
use crate::{events::collections::all::RoomEvent, BaseClient, Session};
use crate::{
events::{collections::all::RoomEvent, stripped::AnyStrippedStateEvent},
BaseClient, Session,
};
use matrix_sdk_test::{async_test, EventBuilder, EventsFile};
use serde_json::json;
use std::convert::TryFrom;
@ -1453,6 +1456,21 @@ mod test {
RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap()
}
fn member_event() -> serde_json::Value {
json!({
"content": {
"displayname": "example",
"membership": "join"
},
"event_id": "$151800140517rfvjc:localhost",
"membership": "join",
"origin_server_ts": 0,
"sender": "@example:localhost",
"state_key": "@example:localhost",
"type": "m.room.member",
})
}
#[async_test]
async fn test_joined_room_creation() {
let mut sync_response = EventBuilder::default()
@ -1461,12 +1479,35 @@ mod test {
let client = get_client();
let room_id = get_room_id();
let room = client.get_joined_room(&room_id).await;
assert!(room.is_none());
client
.receive_sync_response(&mut sync_response)
.await
.unwrap();
let room = client.get_left_room(&room_id).await;
assert!(room.is_none());
let room = client.get_joined_room(&room_id).await;
assert!(room.is_some());
let mut sync_response = EventBuilder::default()
.add_custom_left_event(&room_id, member_event(), RoomEvent::RoomMember)
.build_sync_response();
sync_response.next_batch = "Hello".to_owned();
client
.receive_sync_response(&mut sync_response)
.await
.unwrap();
let room = client.get_joined_room(&room_id).await;
assert!(room.is_none());
let room = client.get_left_room(&room_id).await;
assert!(room.is_some());
}
@ -1474,26 +1515,14 @@ mod test {
async fn test_left_room_creation() {
let room_id = RoomId::try_from("!left_room:localhost").unwrap();
let mut sync_response = EventBuilder::default()
.add_custom_left_event(
&room_id,
json!({
"content": {
"displayname": "example",
"membership": "join"
},
"event_id": "$151800140517rfvjc:localhost",
"membership": "join",
"origin_server_ts": 0,
"sender": "@example:localhost",
"state_key": "@example:localhost",
"type": "m.room.member",
}),
RoomEvent::RoomMember,
)
.add_custom_left_event(&room_id, member_event(), RoomEvent::RoomMember)
.build_sync_response();
let client = get_client();
let room = client.get_left_room(&room_id).await;
assert!(room.is_none());
client
.receive_sync_response(&mut sync_response)
.await
@ -1501,5 +1530,60 @@ mod test {
let room = client.get_left_room(&room_id).await;
assert!(room.is_some());
let mut sync_response = EventBuilder::default()
.add_custom_joined_event(&room_id, member_event(), RoomEvent::RoomMember)
.build_sync_response();
sync_response.next_batch = "Hello".to_owned();
client
.receive_sync_response(&mut sync_response)
.await
.unwrap();
let room = client.get_left_room(&room_id).await;
assert!(room.is_none());
let room = client.get_joined_room(&room_id).await;
assert!(room.is_some());
}
#[async_test]
async fn test_invited_room_creation() {
let room_id = RoomId::try_from("!invited_room:localhost").unwrap();
let mut sync_response = EventBuilder::default()
.add_custom_invited_event(&room_id, member_event(), AnyStrippedStateEvent::RoomMember)
.build_sync_response();
let client = get_client();
let room = client.get_invited_room(&room_id).await;
assert!(room.is_none());
client
.receive_sync_response(&mut sync_response)
.await
.unwrap();
let room = client.get_invited_room(&room_id).await;
assert!(room.is_some());
let mut sync_response = EventBuilder::default()
.add_custom_joined_event(&room_id, member_event(), RoomEvent::RoomMember)
.build_sync_response();
sync_response.next_batch = "Hello".to_owned();
client
.receive_sync_response(&mut sync_response)
.await
.unwrap();
let room = client.get_invited_room(&room_id).await;
assert!(room.is_none());
let room = client.get_joined_room(&room_id).await;
assert!(room.is_some());
}
}

View File

@ -127,6 +127,7 @@ impl EventBuilder {
pub fn add_custom_joined_event<Ev: TryFromRaw>(
mut self,
room_id: &RoomId,
event: serde_json::Value,
variant: fn(Ev) -> RoomEvent,
) -> Self {
@ -134,10 +135,7 @@ impl EventBuilder {
.unwrap()
.deserialize()
.unwrap();
self.add_joined_event(
&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap(),
variant(event),
);
self.add_joined_event(room_id, variant(event));
self
}
@ -290,6 +288,17 @@ impl EventBuilder {
left_rooms.insert(room_id, room);
}
let mut invited_rooms: HashMap<RoomId, serde_json::Value> = HashMap::new();
for (room_id, events) in self.invited_room_events.drain() {
let room = serde_json::json!({
"invite_state": {
"events": events,
},
});
invited_rooms.insert(room_id, room);
}
let body = serde_json::json! {
{
"device_one_time_keys_count": {},
@ -299,7 +308,7 @@ impl EventBuilder {
"left": []
},
"rooms": {
"invite": {},
"invite": invited_rooms,
"join": joined_rooms,
"leave": left_rooms,
},