From 3fa06eeb992ba7b0c1467b7389c54e1fa20c7c6e Mon Sep 17 00:00:00 2001 From: Devin R Date: Sat, 4 Jul 2020 19:08:15 -0400 Subject: [PATCH] matrix-sdk-base: Add test for MessageQueue/JsonStore interaction Ruma can't currently handle an event with the wrong event content type. When replacing the MessageEventStub's content it automatically serializes as "m.room.redaction". --- matrix_sdk_base/Cargo.toml | 2 + matrix_sdk_base/src/client.rs | 91 ++++++++++++++++++++++++- matrix_sdk_base/src/models/room.rs | 1 - matrix_sdk_base/src/state/json_store.rs | 1 - 4 files changed, 92 insertions(+), 3 deletions(-) diff --git a/matrix_sdk_base/Cargo.toml b/matrix_sdk_base/Cargo.toml index 3ad0013e..20958300 100644 --- a/matrix_sdk_base/Cargo.toml +++ b/matrix_sdk_base/Cargo.toml @@ -39,6 +39,8 @@ matrix-sdk-test = { version = "0.1.0", path = "../matrix_sdk_test" } http = "0.2.1" tracing-subscriber = "0.2.7" tempfile = "3.1.0" +mockito = "0.25.1" +url = "2.1.1" [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] tokio = { version = "0.2.21", features = ["rt-threaded", "macros"] } diff --git a/matrix_sdk_base/src/client.rs b/matrix_sdk_base/src/client.rs index 7a90c8b4..23310c89 100644 --- a/matrix_sdk_base/src/client.rs +++ b/matrix_sdk_base/src/client.rs @@ -1839,11 +1839,13 @@ impl BaseClient { #[cfg(test)] mod test { use crate::identifiers::{RoomId, UserId}; - use crate::{BaseClient, Session}; + use crate::{BaseClient, BaseClientConfig, JsonStore, Session}; + use matrix_sdk_common::events::{AnyRoomEventStub, EventJson}; use matrix_sdk_common_macros::async_trait; use matrix_sdk_test::{async_test, test_json, EventBuilder, EventsJson}; use serde_json::json; use std::convert::TryFrom; + use tempfile::tempdir; #[cfg(target_arch = "wasm32")] use wasm_bindgen_test::*; @@ -2344,6 +2346,93 @@ mod test { assert!(passed.load(Ordering::SeqCst)) } + #[cfg(feature = "messages")] + #[async_test] + async fn message_queue_redaction_event_store_deser() { + use std::ops::Deref; + + let room_id = RoomId::try_from("!SVkFJHzfwvuaIEawgC:localhost").unwrap(); + + let session = Session { + access_token: "1234".to_owned(), + user_id: UserId::try_from("@cheeky_monkey:matrix.org").unwrap(), + device_id: "DEVICEID".to_owned(), + }; + + let _m = mockito::mock( + "GET", + mockito::Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), + ) + .with_status(200) + .with_body(test_json::SYNC.to_string()) + .create(); + + let dir = tempdir().unwrap(); + // a sync response to populate our JSON store + let config = + BaseClientConfig::default().state_store(Box::new(JsonStore::open(dir.path()).unwrap())); + let client = BaseClient::new_with_config(config).unwrap(); + client.restore_login(session.clone()).await.unwrap(); + + let response = http::Response::builder() + .body(serde_json::to_vec(test_json::SYNC.deref()).unwrap()) + .unwrap(); + let mut sync = + matrix_sdk_common::api::r0::sync::sync_events::Response::try_from(response).unwrap(); + + client.receive_sync_response(&mut sync).await.unwrap(); + + let json = serde_json::json!({ + "content": { + "reason": "😀" + }, + "event_id": "$XXXX:localhost", + "origin_server_ts": 151957878, + "sender": "@example:localhost", + "type": "m.room.redaction", + "redacts": "$152037280074GZeOm:localhost" + }); + let mut event: EventJson = serde_json::from_value(json).unwrap(); + client + .receive_joined_timeline_event(&room_id, &mut event) + .await + .unwrap(); + + // check that the message has actually been redacted + for room in client.joined_rooms().read().await.values() { + let queue = &room.read().await.messages; + if let crate::events::AnyMessageEventContent::RoomRedaction(content) = + &queue.msgs[0].content + { + assert_eq!(content.reason, Some("😀".to_string())); + } else { + panic!("[pre store sync] message event in message queue should be redacted") + } + } + + // `receive_joined_timeline_event` does not save the state to the store so we must + client.store_room_state(&room_id).await.unwrap(); + + // we load state from the store only + let config = + BaseClientConfig::default().state_store(Box::new(JsonStore::open(dir.path()).unwrap())); + let client = BaseClient::new_with_config(config).unwrap(); + client.restore_login(session).await.unwrap(); + + // make sure that our redacted message event is redacted and that ser/de works + // properly + for room in client.joined_rooms().read().await.values() { + let queue = &room.read().await.messages; + if let crate::events::AnyMessageEventContent::RoomRedaction(content) = + &queue.msgs[0].content + { + assert_eq!(content.reason, Some("😀".to_string())); + } else { + panic!("[post store sync] message event in message queue should be redacted") + } + } + } + #[async_test] #[cfg(feature = "encryption")] async fn test_group_session_invalidation() { diff --git a/matrix_sdk_base/src/models/room.rs b/matrix_sdk_base/src/models/room.rs index 74c3d386..41fefce0 100644 --- a/matrix_sdk_base/src/models/room.rs +++ b/matrix_sdk_base/src/models/room.rs @@ -1212,7 +1212,6 @@ mod test { for room in client.joined_rooms().read().await.values() { let queue = &room.read().await.messages; - println!("{:?}", queue); if let crate::events::AnyMessageEventContent::RoomRedaction(content) = &queue.msgs[0].content { diff --git a/matrix_sdk_base/src/state/json_store.rs b/matrix_sdk_base/src/state/json_store.rs index f4114af9..f5074231 100644 --- a/matrix_sdk_base/src/state/json_store.rs +++ b/matrix_sdk_base/src/state/json_store.rs @@ -93,7 +93,6 @@ impl StateStore for JsonStore { } let json = async_fs::read_to_string(&file).await?; - let room = serde_json::from_str::(&json).map_err(Error::from)?; let room_id = room.room_id.clone();