From 9e738f45ef18b18d7545a8a2fdfd96fcb5a19c89 Mon Sep 17 00:00:00 2001 From: Devin R Date: Mon, 22 Jun 2020 15:40:51 -0400 Subject: [PATCH] crypto/base: Finish moving to using static json values for test data --- matrix_sdk/src/client.rs | 54 +++++++------- matrix_sdk_crypto/Cargo.toml | 1 + matrix_sdk_crypto/src/machine.rs | 20 +++--- matrix_sdk_test/src/lib.rs | 2 +- matrix_sdk_test/src/test_json/events.rs | 93 +++++++++++++++++++++++++ matrix_sdk_test/src/test_json/mod.rs | 7 +- matrix_sdk_test/src/test_json/sync.rs | 69 +++++++++++++++++- 7 files changed, 203 insertions(+), 43 deletions(-) diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index ff47ef86..ff8f6c37 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -1404,7 +1404,7 @@ mod test { use crate::RegistrationBuilder; use matrix_sdk_base::JsonStore; - use matrix_sdk_test::{EventBuilder, EventsJson}; + use matrix_sdk_test::{test_json, EventBuilder, EventsJson}; use mockito::{mock, Matcher}; use tempfile::tempdir; @@ -1430,7 +1430,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/sync.json") + .with_body(test_json::SYNC.to_string()) .create(); let dir = tempdir().unwrap(); @@ -1468,7 +1468,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/leave_event_sync.json") + .with_body(test_json::LEAVE_SYNC_EVENT.to_string()) .create(); joined_client.sync(SyncSettings::default()).await.unwrap(); @@ -1495,7 +1495,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/sync.json") + .with_body(test_json::SYNC.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1548,7 +1548,7 @@ mod test { let _m = mock("POST", "/_matrix/client/r0/login") .with_status(403) - .with_body_from_file("../test_data/login_response_error.json") + .with_body(test_json::LOGIN_RESPONSE_ERR.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1588,6 +1588,7 @@ mod test { let _m = mock("POST", "/_matrix/client/r0/register") .with_status(403) + .with_body(test_json::REGISTRATION_RESPONSE_ERR.to_string()) .with_body_from_file("../test_data/registration_response_error.json") .create(); @@ -1635,7 +1636,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/join".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/room_id.json") + .with_body(test_json::ROOM_ID.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1664,7 +1665,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/join/".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/room_id.json") + .with_body(test_json::ROOM_ID.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1700,7 +1701,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/invite".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/logout_response.json") + .with_body(test_json::LOGOUT.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1727,7 +1728,8 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/invite".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/logout_response.json") + // empty JSON object + .with_body(test_json::LOGOUT.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1765,7 +1767,7 @@ mod test { ) .with_status(200) // this is an empty JSON object - .with_body_from_file("../test_data/logout_response.json") + .with_body(test_json::LOGOUT.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1801,7 +1803,7 @@ mod test { ) .with_status(200) // this is an empty JSON object - .with_body_from_file("../test_data/logout_response.json") + .with_body(test_json::LOGOUT.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1836,7 +1838,7 @@ mod test { ) .with_status(200) // this is an empty JSON object - .with_body_from_file("../test_data/logout_response.json") + .with_body(test_json::LOGOUT.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1871,7 +1873,7 @@ mod test { ) .with_status(200) // this is an empty JSON object - .with_body_from_file("../test_data/logout_response.json") + .with_body(test_json::LOGOUT.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1907,7 +1909,7 @@ mod test { ) .with_status(200) // this is an empty JSON object - .with_body_from_file("../test_data/logout_response.json") + .with_body(test_json::LOGOUT.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1943,7 +1945,7 @@ mod test { ) .with_status(200) // this is an empty JSON object - .with_body_from_file("../test_data/logout_response.json") + .with_body(test_json::LOGOUT.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -1978,7 +1980,7 @@ mod test { ) .with_status(200) // this is an empty JSON object - .with_body_from_file("../test_data/logout_response.json") + .with_body(test_json::LOGOUT.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -2021,7 +2023,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/send/".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/event_id.json") + .with_body(test_json::EVENT_ID.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -2060,7 +2062,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/sync.json") + .with_body(test_json::SYNC.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -2101,7 +2103,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/sync_with_summary.json") + .with_body(test_json::DEFAULT_SYNC_SUMMARY.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -2137,7 +2139,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/invite_sync.json") + .with_body(test_json::INVITE_SYNC.to_string()) .create(); let _response = client.sync(SyncSettings::default()).await.unwrap(); @@ -2171,7 +2173,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/leave_sync.json") + .with_body(test_json::LEAVE_SYNC.to_string()) .create(); let _response = client.sync(SyncSettings::default()).await.unwrap(); @@ -2201,12 +2203,12 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/sync.json") + .with_body(test_json::SYNC.to_string()) .create(); let _m = mock("POST", "/_matrix/client/r0/login") .with_status(200) - .with_body_from_file("../test_data/login_response.json") + .with_body(test_json::LOGIN.to_string()) .create(); let dir = tempdir().unwrap(); @@ -2247,7 +2249,7 @@ mod test { let _m = mock("POST", "/_matrix/client/r0/login") .with_status(200) - .with_body_from_file("../test_data/login_response.json") + .with_body(test_json::LOGIN.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -2276,7 +2278,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/sync.json") + .with_body(test_json::SYNC.to_string()) .create(); let client = Client::new(homeserver).unwrap(); @@ -2306,7 +2308,7 @@ mod test { Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_string()), ) .with_status(200) - .with_body_from_file("../test_data/sync.json") + .with_body(test_json::SYNC.to_string()) .create(); let client = Client::new(homeserver).unwrap(); diff --git a/matrix_sdk_crypto/Cargo.toml b/matrix_sdk_crypto/Cargo.toml index 868f7372..0ef831e3 100644 --- a/matrix_sdk_crypto/Cargo.toml +++ b/matrix_sdk_crypto/Cargo.toml @@ -50,3 +50,4 @@ ruma-identifiers = { version = "0.16.1", features = ["rand"] } serde_json = "1.0.53" tempfile = "3.1.0" http = "0.2.1" +matrix-sdk-test = { version = "0.1.0", path = "../matrix_sdk_test" } diff --git a/matrix_sdk_crypto/src/machine.rs b/matrix_sdk_crypto/src/machine.rs index b1ed1088..f4deca84 100644 --- a/matrix_sdk_crypto/src/machine.rs +++ b/matrix_sdk_crypto/src/machine.rs @@ -1587,8 +1587,6 @@ mod test { use matrix_sdk_common::js_int::UInt; use std::collections::BTreeMap; use std::convert::TryFrom; - use std::fs::File; - use std::io::prelude::*; use std::sync::atomic::AtomicU64; use std::time::SystemTime; @@ -1611,6 +1609,7 @@ mod test { EventJson, EventType, UnsignedData, }; use matrix_sdk_common::identifiers::{DeviceId, EventId, RoomId, UserId}; + use matrix_sdk_test::test_json; fn alice_id() -> UserId { UserId::try_from("@alice:example.org").unwrap() @@ -1624,23 +1623,20 @@ mod test { UserId::try_from(USER_ID).unwrap() } - fn response_from_file(path: &str) -> Response> { - let mut file = File::open(path) - .unwrap_or_else(|_| panic!(format!("No such data file found {}", path))); - let mut contents = Vec::new(); - file.read_to_end(&mut contents) - .unwrap_or_else(|_| panic!(format!("Can't read data file {}", path))); - - Response::builder().status(200).body(contents).unwrap() + fn response_from_file(json: &serde_json::Value) -> Response> { + Response::builder() + .status(200) + .body(json.to_string().as_bytes().to_vec()) + .unwrap() } fn keys_upload_response() -> keys::upload_keys::Response { - let data = response_from_file("../test_data/keys_upload.json"); + let data = response_from_file(&test_json::KEYS_UPLOAD); keys::upload_keys::Response::try_from(data).expect("Can't parse the keys upload response") } fn keys_query_response() -> keys::get_keys::Response { - let data = response_from_file("../test_data/keys_query.json"); + let data = response_from_file(&test_json::KEYS_QUERY); keys::get_keys::Response::try_from(data).expect("Can't parse the keys upload response") } diff --git a/matrix_sdk_test/src/lib.rs b/matrix_sdk_test/src/lib.rs index 0dc0456d..4a567acb 100644 --- a/matrix_sdk_test/src/lib.rs +++ b/matrix_sdk_test/src/lib.rs @@ -344,7 +344,7 @@ pub fn sync_response(kind: SyncResponseFile) -> SyncResponse { let data: &JsonValue = match kind { SyncResponseFile::All => &test_json::MORE_SYNC, SyncResponseFile::Default => &test_json::SYNC, - SyncResponseFile::DefaultWithSummary => &test_json::DEFAULT_SYNC, + SyncResponseFile::DefaultWithSummary => &test_json::DEFAULT_SYNC_SUMMARY, SyncResponseFile::Invite => &test_json::INVITE_SYNC, SyncResponseFile::Leave => &test_json::LEAVE_SYNC, }; diff --git a/matrix_sdk_test/src/test_json/events.rs b/matrix_sdk_test/src/test_json/events.rs index 46fb19c8..75e7770a 100644 --- a/matrix_sdk_test/src/test_json/events.rs +++ b/matrix_sdk_test/src/test_json/events.rs @@ -95,6 +95,71 @@ lazy_static! { }); } +lazy_static! { + pub static ref KEYS_QUERY: JsonValue = json!({ + "device_keys": { + "@alice:example.org": { + "JLAFKJWSCS": { + "algorithms": [ + "m.olm.v1.curve25519-aes-sha2", + "m.megolm.v1.aes-sha2" + ], + "device_id": "JLAFKJWSCS", + "user_id": "@alice:example.org", + "keys": { + "curve25519:JLAFKJWSCS": "wjLpTLRqbqBzLs63aYaEv2Boi6cFEbbM/sSRQ2oAKk4", + "ed25519:JLAFKJWSCS": "nE6W2fCblxDcOFmeEtCHNl8/l8bXcu7GKyAswA4r3mM" + }, + "signatures": { + "@alice:example.org": { + "ed25519:JLAFKJWSCS": "m53Wkbh2HXkc3vFApZvCrfXcX3AI51GsDHustMhKwlv3TuOJMj4wistcOTM8q2+e/Ro7rWFUb9ZfnNbwptSUBA" + } + }, + "unsigned": { + "device_display_name": "Alice's mobile phone" + } + } + } + }, + "failures": {} + }); +} + +lazy_static! { + pub static ref KEYS_UPLOAD: JsonValue = json!({ + "one_time_key_counts": { + "curve25519": 10, + "signed_curve25519": 20 + } + }); +} + +lazy_static! { + pub static ref LOGIN: JsonValue = json!({ + "access_token": "abc123", + "device_id": "GHTYAJCE", + "home_server": "matrix.org", + "user_id": "@cheeky_monkey:matrix.org" + }); +} + +lazy_static! { + pub static ref LOGIN_RESPONSE_ERR: JsonValue = json!({ + "errcode": "M_FORBIDDEN", + "error": "Invalid password" + }); +} + +lazy_static! { + pub static ref LOGOUT: JsonValue = json!({}); +} + +lazy_static! { + pub static ref EVENT_ID: JsonValue = json!({ + "event_id": "$h29iv0s8:example.com" + }); +} + lazy_static! { pub static ref MEMBER: JsonValue = json!({ "content": { @@ -262,6 +327,28 @@ lazy_static! { }); } +lazy_static! { + pub static ref REGISTRATION_RESPONSE_ERR: JsonValue = json!({ + "errcode": "M_FORBIDDEN", + "error": "Invalid password", + "completed": ["example.type.foo"], + "flows": [ + { + "stages": ["example.type.foo", "example.type.bar"] + }, + { + "stages": ["example.type.foo", "example.type.baz"] + } + ], + "params": { + "example.type.baz": { + "example_key": "foobar" + } + }, + "session": "xxxxxx" + }); +} + lazy_static! { pub static ref REACTION: JsonValue = json!({ "content": { @@ -376,6 +463,12 @@ lazy_static! { }); } +lazy_static! { + pub static ref ROOM_ID: JsonValue = json!({ + "room_id": "!testroom:example.org" + }); +} + lazy_static! { pub static ref TAG: JsonValue = json!({ "content": { diff --git a/matrix_sdk_test/src/test_json/mod.rs b/matrix_sdk_test/src/test_json/mod.rs index f48bc642..7a2863c6 100644 --- a/matrix_sdk_test/src/test_json/mod.rs +++ b/matrix_sdk_test/src/test_json/mod.rs @@ -8,7 +8,8 @@ pub mod events; pub mod sync; pub use events::{ - ALIAS, ALIASES, MEMBER, MESSAGE_EDIT, MESSAGE_TEXT, NAME, POWER_LEVELS, PRESENCE, REACTION, - TYPING, + ALIAS, ALIASES, EVENT_ID, KEYS_QUERY, KEYS_UPLOAD, LOGIN, LOGIN_RESPONSE_ERR, LOGOUT, MEMBER, + MESSAGE_EDIT, MESSAGE_TEXT, NAME, POWER_LEVELS, PRESENCE, REACTION, REGISTRATION_RESPONSE_ERR, + ROOM_ID, TYPING, }; -pub use sync::{DEFAULT_SYNC, INVITE_SYNC, LEAVE_SYNC, MORE_SYNC, SYNC}; +pub use sync::{DEFAULT_SYNC_SUMMARY, INVITE_SYNC, LEAVE_SYNC, LEAVE_SYNC_EVENT, MORE_SYNC, SYNC}; diff --git a/matrix_sdk_test/src/test_json/sync.rs b/matrix_sdk_test/src/test_json/sync.rs index d235ec8b..52752bd2 100644 --- a/matrix_sdk_test/src/test_json/sync.rs +++ b/matrix_sdk_test/src/test_json/sync.rs @@ -282,7 +282,7 @@ lazy_static! { } lazy_static! { - pub static ref DEFAULT_SYNC: JsonValue = json!({ + pub static ref DEFAULT_SYNC_SUMMARY: JsonValue = json!({ "device_one_time_keys_count": {}, "next_batch": "s526_47314_0_7_1_1_1_11444_1", "device_lists": { @@ -1032,3 +1032,70 @@ lazy_static! { } }); } + +lazy_static! { + pub static ref LEAVE_SYNC_EVENT: JsonValue = json!({ + "account_data": { + "events": [] + }, + "to_device": { + "events": [] + }, + "device_lists": { + "changed": [], + "left": [] + }, + "presence": { + "events": [] + }, + "rooms": { + "join": {}, + "invite": {}, + "leave": { + "!SVkFJHzfwvuaIEawgC:localhost": { + "timeline": { + "events": [ + { + "content": { + "membership": "leave" + }, + "origin_server_ts": 158957809, + "sender": "@example:localhost", + "state_key": "@example:localhost", + "type": "m.room.member", + "unsigned": { + "replaces_state": "$blahblah", + "prev_content": { + "avatar_url": null, + "displayname": "me", + "membership": "invite" + }, + "prev_sender": "@2example:localhost", + "age": 1757 + }, + "event_id": "$lQQ116Y-XqcjpSUGpuz36rNntUvOSpTjuaIvmtQ2AwA" + } + ], + "prev_batch": "tokenTOKEN", + "limited": false + }, + "state": { + "events": [] + }, + "account_data": { + "events": [] + } + } + } + }, + "groups": { + "join": {}, + "invite": {}, + "leave": {} + }, + "device_one_time_keys_count": { + "signed_curve25519": 50 + }, + "next_batch": "s1380317562_757269739_1655566_503953763_334052043_1209862_55290918_65705002_101146" + }); +}