From 77a8f1b1bc7a2b875e682e1fb73a85ace664f6c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 14 May 2020 10:57:21 +0200 Subject: [PATCH] base: Test the invited room creation. --- matrix_sdk_base/src/client.rs | 118 +++++++++++++++++++++++++++++----- matrix_sdk_test/src/lib.rs | 19 ++++-- 2 files changed, 115 insertions(+), 22 deletions(-) diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index bc44b00a..0f8826c5 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -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()); } } diff --git a/matrix_sdk_test/src/lib.rs b/matrix_sdk_test/src/lib.rs index ad4bf3ec..2042bb16 100644 --- a/matrix_sdk_test/src/lib.rs +++ b/matrix_sdk_test/src/lib.rs @@ -127,6 +127,7 @@ impl EventBuilder { pub fn add_custom_joined_event( 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 = 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, },