diff --git a/testing/matrix-sdk-test/Cargo.toml b/testing/matrix-sdk-test/Cargo.toml index d87a394cf..080cea820 100644 --- a/testing/matrix-sdk-test/Cargo.toml +++ b/testing/matrix-sdk-test/Cargo.toml @@ -22,8 +22,9 @@ insta = { workspace = true } matrix-sdk-common = { path = "../../crates/matrix-sdk-common" } matrix-sdk-test-macros = { version = "0.7.0", path = "../matrix-sdk-test-macros" } once_cell = { workspace = true } -# Enabling the unstable feature for polls support. -ruma = { workspace = true, features = ["rand", "unstable-msc3381"] } +# Enable the unstable feature for polls support. +# "client-api-s" enables need the "server" feature of ruma-client-api, which is needed to serialize Response objects to JSON. +ruma = { workspace = true, features = ["client-api-s", "rand", "unstable-msc3381"] } serde = { workspace = true } serde_json = { workspace = true } diff --git a/testing/matrix-sdk-test/src/lib.rs b/testing/matrix-sdk-test/src/lib.rs index 5a3c5d721..8b80e4ad7 100644 --- a/testing/matrix-sdk-test/src/lib.rs +++ b/testing/matrix-sdk-test/src/lib.rs @@ -2,7 +2,9 @@ use http::Response; pub use matrix_sdk_test_macros::async_test; use once_cell::sync::Lazy; use ruma::{ - api::{client::sync::sync_events::v3::Response as SyncResponse, IncomingResponse}, + api::{ + client::sync::sync_events::v3::Response as SyncResponse, IncomingResponse, OutgoingResponse, + }, room_id, user_id, RoomId, UserId, }; use serde_json::Value as JsonValue; @@ -169,3 +171,13 @@ pub fn ruma_response_from_json( Response::builder().status(200).body(json_bytes).expect("Failed to build HTTP response"); ResponseType::try_from_http_response(http_response).expect("Can't parse the response json") } + +/// Serialise a typed Ruma [`OutgoingResponse`] object to JSON. +pub fn ruma_response_to_json( + response: ResponseType, +) -> serde_json::Value { + let http_response: Response> = + response.try_into_http_response().expect("Failed to build HTTP response"); + let json_bytes = http_response.into_body(); + serde_json::from_slice(&json_bytes).expect("Can't parse the response JSON") +} diff --git a/testing/matrix-sdk-test/src/test_json/keys_query_sets.rs b/testing/matrix-sdk-test/src/test_json/keys_query_sets.rs index 34282b745..c3d75bd47 100644 --- a/testing/matrix-sdk-test/src/test_json/keys_query_sets.rs +++ b/testing/matrix-sdk-test/src/test_json/keys_query_sets.rs @@ -7,7 +7,7 @@ use serde_json::{json, Value}; use super::keys_query::{keys_query, master_keys, KeysQueryUser}; use crate::{ - ruma_response_from_json, + ruma_response_from_json, ruma_response_to_json, test_json::keys_query::{device_keys_payload, self_signing_keys}, }; @@ -99,10 +99,14 @@ impl KeyDistributionTestData { } }); + let response: KeyQueryResponse = ruma_response_from_json(&data); with_settings!({sort_maps => true}, { - assert_json_snapshot!("KeyDistributionTestData::me_keys_query_response", data); + assert_json_snapshot!( + "KeyDistributionTestData::me_keys_query_response", + ruma_response_to_json(response.clone()), + ); }); - ruma_response_from_json(&data) + response } /// Dan has cross-signing setup, one device is cross signed `JHPUERYQUW`, @@ -204,11 +208,14 @@ impl KeyDistributionTestData { } }); + let response: KeyQueryResponse = ruma_response_from_json(&data); with_settings!({sort_maps => true}, { - assert_json_snapshot!("KeyDistributionTestData::dan_keys_query_response", data); + assert_json_snapshot!( + "KeyDistributionTestData::dan_keys_query_response", + ruma_response_to_json(response.clone()), + ); }); - - ruma_response_from_json(&data) + response } /// Same as `dan_keys_query_response` but `FRGNMZVOKA` was removed. @@ -291,14 +298,14 @@ impl KeyDistributionTestData { } }); + let response: KeyQueryResponse = ruma_response_from_json(&data); with_settings!({sort_maps => true}, { assert_json_snapshot!( "KeyDistributionTestData::dan_keys_query_response_device_loggedout", - data + ruma_response_to_json(response.clone()), ); }); - - ruma_response_from_json(&data) + response } /// Dave is a user that has not enabled cross-signing @@ -646,10 +653,14 @@ impl VerificationViolationTestData { } }); + let response: KeyQueryResponse = ruma_response_from_json(&data); with_settings!({sort_maps => true}, { - assert_json_snapshot!("VerificationViolationTestData::own_keys_query_response_1", data); + assert_json_snapshot!( + "VerificationViolationTestData::own_keys_query_response_1", + ruma_response_to_json(response.clone()), + ); }); - ruma_response_from_json(&data) + response } /// A second `/keys/query` response for Alice, containing a *different* set diff --git a/testing/matrix-sdk-test/src/test_json/snapshots/matrix_sdk_test__test_json__keys_query_sets__KeyDistributionTestData::dan_keys_query_response.snap b/testing/matrix-sdk-test/src/test_json/snapshots/matrix_sdk_test__test_json__keys_query_sets__KeyDistributionTestData::dan_keys_query_response.snap index 83fe4ed78..cbba19a00 100644 --- a/testing/matrix-sdk-test/src/test_json/snapshots/matrix_sdk_test__test_json__keys_query_sets__KeyDistributionTestData::dan_keys_query_response.snap +++ b/testing/matrix-sdk-test/src/test_json/snapshots/matrix_sdk_test__test_json__keys_query_sets__KeyDistributionTestData::dan_keys_query_response.snap @@ -1,6 +1,6 @@ --- source: testing/matrix-sdk-test/src/test_json/keys_query_sets.rs -expression: data +expression: ruma_response_to_json(response.clone()) --- { "device_keys": { @@ -42,7 +42,6 @@ expression: data } } }, - "failures": {}, "master_keys": { "@dan:localhost": { "keys": { diff --git a/testing/matrix-sdk-test/src/test_json/snapshots/matrix_sdk_test__test_json__keys_query_sets__KeyDistributionTestData::dan_keys_query_response_device_loggedout.snap b/testing/matrix-sdk-test/src/test_json/snapshots/matrix_sdk_test__test_json__keys_query_sets__KeyDistributionTestData::dan_keys_query_response_device_loggedout.snap index 67e66315a..e2a383892 100644 --- a/testing/matrix-sdk-test/src/test_json/snapshots/matrix_sdk_test__test_json__keys_query_sets__KeyDistributionTestData::dan_keys_query_response_device_loggedout.snap +++ b/testing/matrix-sdk-test/src/test_json/snapshots/matrix_sdk_test__test_json__keys_query_sets__KeyDistributionTestData::dan_keys_query_response_device_loggedout.snap @@ -1,6 +1,6 @@ --- source: testing/matrix-sdk-test/src/test_json/keys_query_sets.rs -expression: data +expression: ruma_response_to_json(response.clone()) --- { "device_keys": { @@ -25,7 +25,6 @@ expression: data } } }, - "failures": {}, "master_keys": { "@dan:localhost": { "keys": {