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)] #[cfg(test)]
mod test { mod test {
use crate::identifiers::{RoomId, UserId}; 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 matrix_sdk_test::{async_test, EventBuilder, EventsFile};
use serde_json::json; use serde_json::json;
use std::convert::TryFrom; use std::convert::TryFrom;
@ -1453,29 +1456,7 @@ mod test {
RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap() RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap()
} }
#[async_test] fn member_event() -> serde_json::Value {
async fn test_joined_room_creation() {
let mut sync_response = EventBuilder::default()
.add_room_event(EventsFile::Member, RoomEvent::RoomMember)
.build_sync_response();
let client = get_client();
let room_id = get_room_id();
client
.receive_sync_response(&mut sync_response)
.await
.unwrap();
let room = client.get_joined_room(&room_id).await;
assert!(room.is_some());
}
#[async_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!({ json!({
"content": { "content": {
"displayname": "example", "displayname": "example",
@ -1487,13 +1468,61 @@ mod test {
"sender": "@example:localhost", "sender": "@example:localhost",
"state_key": "@example:localhost", "state_key": "@example:localhost",
"type": "m.room.member", "type": "m.room.member",
}), })
RoomEvent::RoomMember, }
)
#[async_test]
async fn test_joined_room_creation() {
let mut sync_response = EventBuilder::default()
.add_room_event(EventsFile::Member, RoomEvent::RoomMember)
.build_sync_response();
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());
}
#[async_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, member_event(), RoomEvent::RoomMember)
.build_sync_response(); .build_sync_response();
let client = get_client(); let client = get_client();
let room = client.get_left_room(&room_id).await;
assert!(room.is_none());
client client
.receive_sync_response(&mut sync_response) .receive_sync_response(&mut sync_response)
.await .await
@ -1501,5 +1530,60 @@ mod test {
let room = client.get_left_room(&room_id).await; let room = client.get_left_room(&room_id).await;
assert!(room.is_some()); 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>( pub fn add_custom_joined_event<Ev: TryFromRaw>(
mut self, mut self,
room_id: &RoomId,
event: serde_json::Value, event: serde_json::Value,
variant: fn(Ev) -> RoomEvent, variant: fn(Ev) -> RoomEvent,
) -> Self { ) -> Self {
@ -134,10 +135,7 @@ impl EventBuilder {
.unwrap() .unwrap()
.deserialize() .deserialize()
.unwrap(); .unwrap();
self.add_joined_event( self.add_joined_event(room_id, variant(event));
&RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap(),
variant(event),
);
self self
} }
@ -290,6 +288,17 @@ impl EventBuilder {
left_rooms.insert(room_id, room); 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! { let body = serde_json::json! {
{ {
"device_one_time_keys_count": {}, "device_one_time_keys_count": {},
@ -299,7 +308,7 @@ impl EventBuilder {
"left": [] "left": []
}, },
"rooms": { "rooms": {
"invite": {}, "invite": invited_rooms,
"join": joined_rooms, "join": joined_rooms,
"leave": left_rooms, "leave": left_rooms,
}, },