diff --git a/crates/matrix-sdk/Cargo.toml b/crates/matrix-sdk/Cargo.toml index ed129ae40..b8421c629 100644 --- a/crates/matrix-sdk/Cargo.toml +++ b/crates/matrix-sdk/Cargo.toml @@ -142,7 +142,6 @@ dirs = "4.0.0" futures = { version = "0.3.21", default-features = false, features = ["executor"] } matches = "0.1.9" matrix-sdk-test = { version = "0.5.0", path = "../matrix-sdk-test" } -mockito = "0.31.0" once_cell = "1.10.0" serde_json = "1.0.79" tempfile = "3.3.0" @@ -154,6 +153,7 @@ wasm-bindgen-test = "0.3.30" [target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies] tokio = { version = "1.17.0", default-features = false, features = ["rt-multi-thread", "macros"] } +wiremock = "0.5.13" [[example]] name = "cross_signing_bootstrap" diff --git a/crates/matrix-sdk/src/client/builder.rs b/crates/matrix-sdk/src/client/builder.rs index 1bb0373a1..77e5c639f 100644 --- a/crates/matrix-sdk/src/client/builder.rs +++ b/crates/matrix-sdk/src/client/builder.rs @@ -353,8 +353,8 @@ fn homeserver_from_name(server_name: &ServerName) -> String { #[cfg(not(test))] return format!("https://{}", server_name); - // Mockito only knows how to test http endpoints: - // https://github.com/lipanski/mockito/issues/127 + // Wiremock only knows how to test http endpoints: + // https://github.com/LukeMathWalker/wiremock-rs/issues/58 #[cfg(test)] return format!("http://{}", server_name); } diff --git a/crates/matrix-sdk/src/client/mod.rs b/crates/matrix-sdk/src/client/mod.rs index 23b8ed2cc..57d312382 100644 --- a/crates/matrix-sdk/src/client/mod.rs +++ b/crates/matrix-sdk/src/client/mod.rs @@ -2201,7 +2201,7 @@ impl Client { } } -// mockito (the http mocking library) is not supported for wasm32 +// The http mocking library is not supported for wasm32 #[cfg(all(test, not(target_arch = "wasm32")))] pub(crate) mod tests { use std::time::Duration; @@ -2210,33 +2210,36 @@ pub(crate) mod tests { #[cfg(target_arch = "wasm32")] wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); - use mockito::{mock, Matcher}; use ruma::{api::MatrixVersion, device_id, room_id, user_id, UserId}; use url::Url; + use wiremock::{ + matchers::{header, method, path}, + Mock, MockServer, ResponseTemplate, + }; use super::{Client, ClientBuilder, Session}; use crate::config::{RequestConfig, SyncSettings}; - fn test_client_builder() -> ClientBuilder { - let homeserver = Url::parse(&mockito::server_url()).unwrap(); + fn test_client_builder(homeserver_url: Option) -> ClientBuilder { + let homeserver = homeserver_url.as_deref().unwrap_or("http://localhost:1234"); Client::builder().homeserver_url(homeserver).server_versions([MatrixVersion::V1_0]) } - async fn no_retry_test_client() -> Client { - test_client_builder() + async fn no_retry_test_client(homeserver_url: Option) -> Client { + test_client_builder(homeserver_url) .request_config(RequestConfig::new().disable_retry()) .build() .await .unwrap() } - pub(crate) async fn logged_in_client() -> Client { + pub(crate) async fn logged_in_client(homeserver_url: Option) -> Client { let session = Session { access_token: "1234".to_owned(), user_id: user_id!("@example:localhost").to_owned(), device_id: device_id!("DEVICEID").to_owned(), }; - let client = no_retry_test_client().await; + let client = no_retry_test_client(homeserver_url).await; client.restore_login(session).await.unwrap(); client @@ -2244,13 +2247,15 @@ pub(crate) mod tests { #[async_test] async fn account_data() { - let client = logged_in_client().await; + let server = MockServer::start().await; + let client = logged_in_client(Some(server.uri())).await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(test_json::SYNC.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/client/r0/sync".to_owned())) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::SYNC)) + .mount(&server) + .await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); let _response = client.sync_once(sync_settings).await.unwrap(); @@ -2262,21 +2267,25 @@ pub(crate) mod tests { #[async_test] async fn successful_discovery() { - let server_url = mockito::server_url(); + let server = MockServer::start().await; + let server_url = server.uri(); let domain = server_url.strip_prefix("http://").unwrap(); let alice = UserId::parse("@alice:".to_owned() + domain).unwrap(); - let _m_well_known = mock("GET", "/.well-known/matrix/client") - .with_status(200) - .with_body( + Mock::given(method("GET")) + .and(path("/.well-known/matrix/client")) + .respond_with(ResponseTemplate::new(200).set_body_raw( test_json::WELL_KNOWN.to_string().replace("HOMESERVER_URL", server_url.as_ref()), - ) - .create(); + "application/json", + )) + .mount(&server) + .await; - let _m_versions = mock("GET", "/_matrix/client/versions") - .with_status(200) - .with_body(test_json::VERSIONS.to_string()) - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/client/versions")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::VERSIONS)) + .mount(&server) + .await; let client = Client::builder().user_id(&alice).build().await.unwrap(); assert_eq!(client.homeserver().await, Url::parse(server_url.as_ref()).unwrap()); @@ -2284,11 +2293,16 @@ pub(crate) mod tests { #[async_test] async fn discovery_broken_server() { - let server_url = mockito::server_url(); + let server = MockServer::start().await; + let server_url = server.uri(); let domain = server_url.strip_prefix("http://").unwrap(); let alice = UserId::parse("@alice:".to_owned() + domain).unwrap(); - let _m = mock("GET", "/.well-known/matrix/client").with_status(404).create(); + Mock::given(method("GET")) + .and(path("/.well-known/matrix/client")) + .respond_with(ResponseTemplate::new(404)) + .mount(&server) + .await; assert!( Client::builder().user_id(&alice).build().await.is_err(), @@ -2298,7 +2312,8 @@ pub(crate) mod tests { #[async_test] async fn room_creation() { - let client = logged_in_client().await; + let server = MockServer::start().await; + let client = logged_in_client(Some(server.uri())).await; let response = EventBuilder::default() .add_state_event(EventsJson::Member) @@ -2308,7 +2323,7 @@ pub(crate) mod tests { client.inner.base_client.receive_sync_response(response).await.unwrap(); let room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost"); - assert_eq!(client.homeserver().await, Url::parse(&mockito::server_url()).unwrap()); + assert_eq!(client.homeserver().await, Url::parse(&server.uri()).unwrap()); let room = client.get_joined_room(room_id); assert!(room.is_some()); @@ -2316,7 +2331,8 @@ pub(crate) mod tests { #[async_test] async fn retry_limit_http_requests() { - let client = test_client_builder() + let server = MockServer::start().await; + let client = test_client_builder(Some(server.uri())) .request_config(RequestConfig::new().retry_limit(3)) .build() .await @@ -2324,20 +2340,22 @@ pub(crate) mod tests { assert!(client.inner.http_client.request_config.retry_limit.unwrap() == 3); - let m = mock("POST", "/_matrix/client/r0/login").with_status(501).expect(3).create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(501)) + .expect(3) + .mount(&server) + .await; - if client.login_username("example", "wordpass").send().await.is_err() { - m.assert(); - } else { - panic!("this request should return an `Err` variant") - } + client.login_username("example", "wordpass").send().await.unwrap_err(); } #[async_test] async fn retry_timeout_http_requests() { // Keep this timeout small so that the test doesn't take long let retry_timeout = Duration::from_secs(5); - let client = test_client_builder() + let server = MockServer::start().await; + let client = test_client_builder(Some(server.uri())) .request_config(RequestConfig::new().retry_timeout(retry_timeout)) .build() .await @@ -2345,40 +2363,43 @@ pub(crate) mod tests { assert!(client.inner.http_client.request_config.retry_timeout.unwrap() == retry_timeout); - let m = - mock("POST", "/_matrix/client/r0/login").with_status(501).expect_at_least(2).create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(501)) + .expect(2..) + .mount(&server) + .await; - if client.login_username("example", "wordpass").send().await.is_err() { - m.assert(); - } else { - panic!("this request should return an `Err` variant") - } + client.login_username("example", "wordpass").send().await.unwrap_err(); } #[async_test] async fn short_retry_initial_http_requests() { - let client = test_client_builder().build().await.unwrap(); + let server = MockServer::start().await; + let client = test_client_builder(Some(server.uri())).build().await.unwrap(); - let m = - mock("POST", "/_matrix/client/r0/login").with_status(501).expect_at_least(3).create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(501)) + .expect(3..) + .mount(&server) + .await; - if client.login_username("example", "wordpass").send().await.is_err() { - m.assert(); - } else { - panic!("this request should return an `Err` variant") - } + client.login_username("example", "wordpass").send().await.unwrap_err(); } #[async_test] async fn no_retry_http_requests() { - let client = logged_in_client().await; + let server = MockServer::start().await; + let client = logged_in_client(Some(server.uri())).await; - let m = mock("GET", "/_matrix/client/r0/devices").with_status(501).create(); + Mock::given(method("GET")) + .and(path("/_matrix/client/r0/devices")) + .respond_with(ResponseTemplate::new(501)) + .expect(1) + .mount(&server) + .await; - if client.devices().await.is_err() { - m.assert(); - } else { - panic!("this request should return an `Err` variant") - } + client.devices().await.unwrap_err(); } } diff --git a/crates/matrix-sdk/src/encryption/mod.rs b/crates/matrix-sdk/src/encryption/mod.rs index 8d1cdf110..a76ec8d19 100644 --- a/crates/matrix-sdk/src/encryption/mod.rs +++ b/crates/matrix-sdk/src/encryption/mod.rs @@ -873,35 +873,34 @@ impl Encryption { #[cfg(all(test, not(target_arch = "wasm32")))] mod tests { use matrix_sdk_test::{async_test, EventBuilder, EventsJson}; - use mockito::{mock, Matcher}; use ruma::{ event_id, events::reaction::{ReactionEventContent, Relation}, room_id, }; use serde_json::json; + use wiremock::{ + matchers::{method, path_regex}, + Mock, MockServer, ResponseTemplate, + }; use crate::client::tests::logged_in_client; #[async_test] async fn test_reaction_sending() { - let client = logged_in_client().await; + let server = MockServer::start().await; + let client = logged_in_client(Some(server.uri())).await; let event_id = event_id!("$2:example.org"); let room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost"); - let _m = mock( - "PUT", - Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/send/m%2Ereaction/.*".to_owned()), - ) - .with_status(200) - .with_body( - json!({ + Mock::given(method("PUT")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/m%2Ereaction/.*".to_owned())) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "event_id": event_id, - }) - .to_string(), - ) - .create(); + }))) + .mount(&server) + .await; let response = EventBuilder::default() .add_state_event(EventsJson::Member) diff --git a/crates/matrix-sdk/src/event_handler.rs b/crates/matrix-sdk/src/event_handler.rs index 444a18bbd..49718a089 100644 --- a/crates/matrix-sdk/src/event_handler.rs +++ b/crates/matrix-sdk/src/event_handler.rs @@ -576,7 +576,7 @@ mod tests { async fn event_handler() -> crate::Result<()> { use std::sync::atomic::{AtomicU8, Ordering::SeqCst}; - let client = crate::client::tests::logged_in_client().await; + let client = crate::client::tests::logged_in_client(None).await; let member_count = Arc::new(AtomicU8::new(0)); let typing_count = Arc::new(AtomicU8::new(0)); diff --git a/crates/matrix-sdk/tests/integration/client.rs b/crates/matrix-sdk/tests/integration/client.rs index c7c3a7d52..f82e9ea52 100644 --- a/crates/matrix-sdk/tests/integration/client.rs +++ b/crates/matrix-sdk/tests/integration/client.rs @@ -1,6 +1,3 @@ -// mockito (the http mocking library) is not supported for wasm32 -#![cfg(not(target_arch = "wasm32"))] - use std::{collections::BTreeMap, str::FromStr, time::Duration}; use matrix_sdk::{ @@ -9,7 +6,6 @@ use matrix_sdk::{ Error, HttpError, RumaApiError, }; use matrix_sdk_test::{async_test, test_json}; -use mockito::{mock, Matcher}; use ruma::{ api::{ client::{ @@ -32,12 +28,16 @@ use ruma::{ }; use serde_json::json; use url::Url; +use wiremock::{ + matchers::{header, method, path, path_regex}, + Mock, ResponseTemplate, +}; -use crate::{logged_in_client, no_retry_test_client}; +use crate::{logged_in_client, mock_sync, no_retry_test_client}; #[async_test] async fn set_homeserver() { - let client = no_retry_test_client().await; + let (client, _) = no_retry_test_client().await; let homeserver = Url::from_str("http://example.com/").unwrap(); client.set_homeserver(homeserver.clone()).await; @@ -46,13 +46,14 @@ async fn set_homeserver() { #[async_test] async fn login() { - let homeserver = Url::from_str(&mockito::server_url()).unwrap(); - let client = no_retry_test_client().await; + let (client, server) = no_retry_test_client().await; + let homeserver = Url::from_str(&server.uri()).unwrap(); - let _m_types = mock("GET", "/_matrix/client/r0/login") - .with_status(200) - .with_body(test_json::LOGIN_TYPES.to_string()) - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN_TYPES)) + .mount(&server) + .await; let can_password = client .get_login_types() @@ -63,10 +64,11 @@ async fn login() { .any(|flow| matches!(flow, LoginType::Password(_))); assert!(can_password); - let _m_login = mock("POST", "/_matrix/client/r0/login") - .with_status(200) - .with_body(test_json::LOGIN.to_string()) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN)) + .mount(&server) + .await; client.login_username("example", "wordpass").send().await.unwrap(); @@ -78,12 +80,13 @@ async fn login() { #[async_test] async fn login_with_discovery() { - let client = no_retry_test_client().await; + let (client, server) = no_retry_test_client().await; - let _m_login = mock("POST", "/_matrix/client/r0/login") - .with_status(200) - .with_body(test_json::LOGIN_WITH_DISCOVERY.to_string()) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN_WITH_DISCOVERY)) + .mount(&server) + .await; client.login_username("example", "wordpass").send().await.unwrap(); @@ -95,31 +98,33 @@ async fn login_with_discovery() { #[async_test] async fn login_no_discovery() { - let client = no_retry_test_client().await; + let (client, server) = no_retry_test_client().await; - let _m_login = mock("POST", "/_matrix/client/r0/login") - .with_status(200) - .with_body(test_json::LOGIN.to_string()) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN)) + .mount(&server) + .await; client.login_username("example", "wordpass").send().await.unwrap(); let logged_in = client.logged_in(); assert!(logged_in, "Client should be logged in"); - assert_eq!(client.homeserver().await, Url::parse(&mockito::server_url()).unwrap()); + assert_eq!(client.homeserver().await, Url::parse(&server.uri()).unwrap()); } #[async_test] #[cfg(feature = "sso-login")] async fn login_with_sso() { - let _m_login = mock("POST", "/_matrix/client/r0/login") - .with_status(200) - .with_body(test_json::LOGIN.to_string()) - .create(); + let (client, server) = no_retry_test_client().await; + + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN)) + .mount(&server) + .await; - let _homeserver = Url::from_str(&mockito::server_url()).unwrap(); - let client = no_retry_test_client().await; let idp = ruma::api::client::session::get_login_types::v3::IdentityProvider::new( "some-id".to_owned(), "idp-name".to_owned(), @@ -149,12 +154,13 @@ async fn login_with_sso() { #[async_test] async fn login_with_sso_token() { - let client = no_retry_test_client().await; + let (client, server) = no_retry_test_client().await; - let _m = mock("GET", "/_matrix/client/r0/login") - .with_status(200) - .with_body(test_json::LOGIN_TYPES.to_string()) - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN_TYPES)) + .mount(&server) + .await; let can_sso = client .get_login_types() @@ -168,10 +174,11 @@ async fn login_with_sso_token() { let sso_url = client.get_sso_login_url("http://127.0.0.1:3030", None).await; assert!(sso_url.is_ok()); - let _m = mock("POST", "/_matrix/client/r0/login") - .with_status(200) - .with_body(test_json::LOGIN.to_string()) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN)) + .mount(&server) + .await; client.login_token("averysmalltoken").send().await.unwrap(); @@ -181,12 +188,13 @@ async fn login_with_sso_token() { #[async_test] async fn login_error() { - let client = no_retry_test_client().await; + let (client, server) = no_retry_test_client().await; - let _m = mock("POST", "/_matrix/client/r0/login") - .with_status(403) - .with_body(test_json::LOGIN_RESPONSE_ERR.to_string()) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/login")) + .respond_with(ResponseTemplate::new(403).set_body_json(&*test_json::LOGIN_RESPONSE_ERR)) + .mount(&server) + .await; if let Err(err) = client.login_username("example", "wordpass").send().await { if let Error::Http(HttpError::Api(FromHttpResponseError::Server(ServerError::Known( @@ -209,12 +217,15 @@ async fn login_error() { #[async_test] async fn register_error() { - let client = no_retry_test_client().await; + let (client, server) = no_retry_test_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/register\?.*$".to_owned())) - .with_status(403) - .with_body(test_json::REGISTRATION_RESPONSE_ERR.to_string()) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/register")) + .respond_with( + ResponseTemplate::new(403).set_body_json(&*test_json::REGISTRATION_RESPONSE_ERR), + ) + .mount(&server) + .await; let user = assign!(RegistrationRequest::new(), { username: Some("user"), @@ -246,13 +257,9 @@ async fn register_error() { #[async_test] async fn sync() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(test_json::SYNC.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -265,44 +272,55 @@ async fn sync() { #[async_test] async fn devices() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("GET", "/_matrix/client/r0/devices") - .with_status(200) - .with_body(test_json::DEVICES.to_string()) - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/client/r0/devices")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::DEVICES)) + .mount(&server) + .await; assert!(client.devices().await.is_ok()); } #[async_test] async fn delete_devices() { - let client = no_retry_test_client().await; + let (client, server) = no_retry_test_client().await; - let _m = mock("POST", "/_matrix/client/r0/delete_devices") - .with_status(401) - .with_body( - json!({ - "flows": [ - { - "stages": [ - "m.login.password" - ] - } - ], - "params": {}, - "session": "vBslorikviAjxzYBASOBGfPp" - }) - .to_string(), - ) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/delete_devices")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(401).set_body_json(json!({ + "flows": [ + { + "stages": [ + "m.login.password" + ] + } + ], + "params": {}, + "session": "vBslorikviAjxzYBASOBGfPp" + }))) + .up_to_n_times(1) + .mount(&server) + .await; - let _m = mock("POST", "/_matrix/client/r0/delete_devices") - .with_status(401) - // empty response - // TODO rename that response type. - .with_body(test_json::LOGOUT.to_string()) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/delete_devices")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(401).set_body_json(json!({ + "flows": [ + { + "stages": [ + "m.login.password" + ] + } + ], + "params": {}, + "session": "vBslorikviAjxzYBASOBGfPp" + }))) + .mount(&server) + .await; let devices = &[device_id!("DEVICEID").to_owned()]; @@ -333,12 +351,13 @@ async fn delete_devices() { #[async_test] async fn resolve_room_alias() { - let client = no_retry_test_client().await; + let (client, server) = no_retry_test_client().await; - let _m = mock("GET", "/_matrix/client/r0/directory/room/%23alias%3Aexample%2Eorg") - .with_status(200) - .with_body(test_json::GET_ALIAS.to_string()) - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/client/r0/directory/room/%23alias%3Aexample%2Eorg")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::GET_ALIAS)) + .mount(&server) + .await; let alias = ruma::room_alias_id!("#alias:example.org"); assert!(client.resolve_room_alias(alias).await.is_ok()); @@ -347,14 +366,9 @@ async fn resolve_room_alias() { #[async_test] async fn join_leave_room() { let room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost"); + let (client, server) = logged_in_client().await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(test_json::SYNC.to_string()) - .create(); - - let client = logged_in_client().await; - let session = client.session().unwrap().clone(); + mock_sync(&server, &*test_json::SYNC, None).await; let room = client.get_joined_room(room_id); assert!(room.is_none()); @@ -367,38 +381,28 @@ async fn join_leave_room() { let room = client.get_joined_room(room_id); assert!(room.is_some()); - // test store reloads with correct room state from the state store - let joined_client = no_retry_test_client().await; - joined_client.restore_login(session).await.unwrap(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, &*test_json::LEAVE_SYNC_EVENT, Some(sync_token.clone())).await; - // joined room reloaded from state store - joined_client.sync_once(SyncSettings::default()).await.unwrap(); - let room = joined_client.get_joined_room(room_id); - assert!(room.is_some()); + client.sync_once(SyncSettings::default().token(sync_token)).await.unwrap(); - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(test_json::LEAVE_SYNC_EVENT.to_string()) - .create(); - - joined_client.sync_once(SyncSettings::default()).await.unwrap(); - - let room = joined_client.get_joined_room(room_id); + let room = client.get_joined_room(room_id); assert!(room.is_none()); - let room = joined_client.get_left_room(room_id); + let room = client.get_left_room(room_id); assert!(room.is_some()); } #[async_test] async fn join_room_by_id() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/join".to_owned())) - .with_status(200) - .with_body(test_json::ROOM_ID.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/join")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::ROOM_ID)) + .mount(&server) + .await; let room_id = room_id!("!testroom:example.org"); @@ -412,13 +416,14 @@ async fn join_room_by_id() { #[async_test] async fn join_room_by_id_or_alias() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/join/".to_owned())) - .with_status(200) - .with_body(test_json::ROOM_ID.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path_regex(r"^/_matrix/client/r0/join/")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::ROOM_ID)) + .mount(&server) + .await; let room_id = room_id!("!testroom:example.org").into(); @@ -436,12 +441,13 @@ async fn join_room_by_id_or_alias() { #[async_test] async fn room_search_all() { - let client = no_retry_test_client().await; + let (client, server) = no_retry_test_client().await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/publicRooms".to_owned())) - .with_status(200) - .with_body(test_json::PUBLIC_ROOMS.to_string()) - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/client/r0/publicRooms")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::PUBLIC_ROOMS)) + .mount(&server) + .await; let get_public_rooms::v3::Response { chunk, .. } = client.public_rooms(Some(10), None, None).await.unwrap(); @@ -450,13 +456,14 @@ async fn room_search_all() { #[async_test] async fn room_search_filtered() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/publicRooms".to_owned())) - .with_status(200) - .with_body(test_json::PUBLIC_ROOMS.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/client/r0/publicRooms")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::PUBLIC_ROOMS)) + .mount(&server) + .await; let generic_search_term = Some("cheese"); let filter = assign!(Filter::new(), { generic_search_term }); @@ -469,13 +476,9 @@ async fn room_search_filtered() { #[async_test] async fn invited_rooms() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::INVITE_SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::INVITE_SYNC, None).await; let _response = client.sync_once(SyncSettings::default()).await.unwrap(); @@ -488,13 +491,9 @@ async fn invited_rooms() { #[async_test] async fn left_rooms() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::LEAVE_SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::LEAVE_SYNC, None).await; let _response = client.sync_once(SyncSettings::default()).await.unwrap(); @@ -507,31 +506,28 @@ async fn left_rooms() { #[async_test] async fn get_media_content() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; let request = MediaRequest { source: MediaSource::Plain(mxc_uri!("mxc://localhost/textfile").to_owned()), format: MediaFormat::File, }; - let m = mock( - "GET", - Matcher::Regex(r"^/_matrix/media/r0/download/localhost/textfile\?.*$".to_owned()), - ) - .with_status(200) - .with_body("Some very interesting text.") - .expect(2) - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/media/r0/download/localhost/textfile")) + .respond_with(ResponseTemplate::new(200).set_body_string("Some very interesting text.")) + .expect(2) + .mount(&server) + .await; assert!(client.get_media_content(&request, true).await.is_ok()); assert!(client.get_media_content(&request, true).await.is_ok()); assert!(client.get_media_content(&request, false).await.is_ok()); - m.assert(); } #[async_test] async fn get_media_file() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; let event_content = ImageMessageEventContent::plain( "filename.jpg".into(), @@ -544,25 +540,26 @@ async fn get_media_file() { }))), ); - let m = mock( - "GET", - Matcher::Regex(r"^/_matrix/media/r0/download/example%2Eorg/image\?.*$".to_owned()), - ) - .with_status(200) - .with_body("binaryjpegdata") - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/media/r0/download/example%2Eorg/image")) + .respond_with(ResponseTemplate::new(200).set_body_raw("binaryjpegdata", "image/jpeg")) + .expect(1) + .named("get_file") + .mount(&server) + .await; assert!(client.get_file(event_content.clone(), true).await.is_ok()); assert!(client.get_file(event_content.clone(), true).await.is_ok()); - m.assert(); - let m = mock( - "GET", - Matcher::Regex(r"^/_matrix/media/r0/thumbnail/example%2Eorg/image\?.*$".to_owned()), - ) - .with_status(200) - .with_body("smallerbinaryjpegdata") - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/media/r0/thumbnail/example%2Eorg/image")) + .respond_with( + ResponseTemplate::new(200).set_body_raw("smallerbinaryjpegdata", "image/jpeg"), + ) + .expect(1) + .named("get_thumbnail") + .mount(&server) + .await; assert!(client .get_thumbnail( @@ -572,18 +569,18 @@ async fn get_media_file() { ) .await .is_ok()); - m.assert(); } #[async_test] async fn whoami() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("GET", "/_matrix/client/r0/account/whoami") - .with_status(200) - .with_body(test_json::WHOAMI.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("GET")) + .and(path("/_matrix/client/r0/account/whoami")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::WHOAMI)) + .mount(&server) + .await; let user_id = user_id!("@joe:example.org"); diff --git a/crates/matrix-sdk/tests/integration/main.rs b/crates/matrix-sdk/tests/integration/main.rs index a2c8ad143..9ddca69e3 100644 --- a/crates/matrix-sdk/tests/integration/main.rs +++ b/crates/matrix-sdk/tests/integration/main.rs @@ -1,31 +1,59 @@ +// The http mocking library is not supported for wasm32 +#![cfg(not(target_arch = "wasm32"))] + use matrix_sdk::{config::RequestConfig, Client, ClientBuilder, Session}; use ruma::{api::MatrixVersion, device_id, user_id}; -use url::Url; +use serde::Serialize; +use wiremock::{ + matchers::{header, method, path, query_param, query_param_is_missing}, + Mock, MockServer, ResponseTemplate, +}; mod client; mod room; -fn test_client_builder() -> ClientBuilder { - let homeserver = Url::parse(&mockito::server_url()).unwrap(); - Client::builder().homeserver_url(homeserver).server_versions([MatrixVersion::V1_0]) +async fn test_client_builder() -> (ClientBuilder, MockServer) { + let server = MockServer::start().await; + let builder = + Client::builder().homeserver_url(server.uri()).server_versions([MatrixVersion::V1_0]); + (builder, server) } -async fn no_retry_test_client() -> Client { - test_client_builder() - .request_config(RequestConfig::new().disable_retry()) - .build() - .await - .unwrap() +async fn no_retry_test_client() -> (Client, MockServer) { + let (builder, server) = test_client_builder().await; + let client = + builder.request_config(RequestConfig::new().disable_retry()).build().await.unwrap(); + (client, server) } -async fn logged_in_client() -> Client { +async fn logged_in_client() -> (Client, MockServer) { let session = Session { access_token: "1234".to_owned(), user_id: user_id!("@example:localhost").to_owned(), device_id: device_id!("DEVICEID").to_owned(), }; - let client = no_retry_test_client().await; + let (client, server) = no_retry_test_client().await; client.restore_login(session).await.unwrap(); - client + (client, server) +} + +/// Mount a Mock on the given server to handle the `GET /sync` endpoint with +/// an optional `since` param that returns a 200 status code with the given +/// response body. +async fn mock_sync(server: &MockServer, response_body: impl Serialize, since: Option) { + let mut builder = Mock::given(method("GET")) + .and(path("/_matrix/client/r0/sync")) + .and(header("authorization", "Bearer 1234")); + + if let Some(since) = since { + builder = builder.and(query_param("since", since)); + } else { + builder = builder.and(query_param_is_missing("since")); + } + + builder + .respond_with(ResponseTemplate::new(200).set_body_json(response_body)) + .mount(server) + .await; } diff --git a/crates/matrix-sdk/tests/integration/room/common.rs b/crates/matrix-sdk/tests/integration/room/common.rs index 910e4064d..c9c9bd177 100644 --- a/crates/matrix-sdk/tests/integration/room/common.rs +++ b/crates/matrix-sdk/tests/integration/room/common.rs @@ -1,35 +1,32 @@ use std::time::Duration; -use matrix_sdk::{ - config::{RequestConfig, SyncSettings}, - DisplayName, RoomMember, Session, -}; +use matrix_sdk::{config::SyncSettings, DisplayName, RoomMember}; use matrix_sdk_test::{async_test, test_json}; -use mockito::{mock, Matcher}; use ruma::{ - device_id, event_id, + event_id, events::{AnySyncStateEvent, StateEventType}, - room_id, user_id, + room_id, }; use serde_json::{json, Value as JsonValue}; +use wiremock::{ + matchers::{header, method, path_regex}, + Mock, ResponseTemplate, +}; -use crate::{logged_in_client, test_client_builder}; +use crate::{logged_in_client, mock_sync}; #[async_test] async fn user_presence() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/members".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::MEMBERS.to_string()) - .create(); + Mock::given(method("GET")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/members")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::MEMBERS)) + .mount(&server) + .await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -44,13 +41,9 @@ async fn user_presence() { #[async_test] async fn calculate_room_names_from_summary() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::DEFAULT_SYNC_SUMMARY.to_string()) - .create(); + mock_sync(&server, &*test_json::DEFAULT_SYNC_SUMMARY, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); let _response = client.sync_once(sync_settings).await.unwrap(); @@ -61,14 +54,9 @@ async fn calculate_room_names_from_summary() { #[async_test] async fn room_names() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .expect_at_least(1) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -79,14 +67,10 @@ async fn room_names() { assert_eq!(DisplayName::Aliased("tutorial".to_owned()), room.display_name().await.unwrap()); - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::INVITE_SYNC.to_string()) - .expect_at_least(1) - .create(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, &*test_json::INVITE_SYNC, Some(sync_token.clone())).await; - let _response = client.sync_once(SyncSettings::new()).await.unwrap(); + let _response = client.sync_once(SyncSettings::new().token(sync_token)).await.unwrap(); assert_eq!(client.rooms().len(), 1); let invited_room = client.get_invited_room(room_id!("!696r7674:example.com")).unwrap(); @@ -101,11 +85,7 @@ async fn room_names() { async fn test_state_event_getting() { let room_id = room_id!("!SVkFJHzfwvuaIEawgC:localhost"); - let session = Session { - access_token: "1234".to_owned(), - user_id: user_id!("@example:localhost").to_owned(), - device_id: device_id!("DEVICEID").to_owned(), - }; + let (client, server) = logged_in_client().await; let sync = json!({ "next_batch": "1234", @@ -162,17 +142,7 @@ async fn test_state_event_getting() { } }); - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(sync.to_string()) - .create(); - - let client = test_client_builder() - .request_config(RequestConfig::new().retry_limit(3)) - .build() - .await - .unwrap(); - client.restore_login(session.clone()).await.unwrap(); + mock_sync(&server, sync, None).await; let room = client.get_joined_room(room_id); assert!(room.is_none()); @@ -206,80 +176,61 @@ async fn test_state_event_getting() { #[allow(dead_code)] #[cfg(feature = "experimental-timeline")] async fn room_timeline_with_remove() { - let client = logged_in_client().await; + use futures_util::StreamExt; + use matrix_sdk::deserialized_responses::SyncRoomEvent; + use wiremock::matchers::query_param; + + let (client, server) = logged_in_client().await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); - let sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(test_json::SYNC.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let _ = client.sync_once(sync_settings).await.unwrap(); - sync.assert(); - drop(sync); + let room = client.get_joined_room(room_id!("!SVkFJHzfwvuaIEawgC:localhost")).unwrap(); let (forward_stream, backward_stream) = room.timeline().await.unwrap(); // these two syncs lead to the store removing its existing timeline // and replace them with new ones - let sync_2 = mock( - "GET", - Matcher::Regex( - r"^/_matrix/client/r0/sync\?.*since=s526_47314_0_7_1_1_1_11444_1.*".to_owned(), - ), - ) - .with_status(200) - .with_body(test_json::MORE_SYNC.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + mock_sync(&server, &*test_json::MORE_SYNC, Some("s526_47314_0_7_1_1_1_11444_1".to_owned())) + .await; + mock_sync(&server, &*test_json::MORE_SYNC_2, Some("s526_47314_0_7_1_1_1_11444_2".to_owned())) + .await; - let sync_3 = mock( - "GET", - Matcher::Regex( - r"^/_matrix/client/r0/sync\?.*since=s526_47314_0_7_1_1_1_11444_2.*".to_owned(), - ), - ) - .with_status(200) - .with_body(test_json::MORE_SYNC_2.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("GET")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/messages$")) + .and(header("authorization", "Bearer 1234")) + .and(query_param("from", "t392-516_47314_0_7_1_1_1_11444_1")) + .respond_with( + ResponseTemplate::new(200).set_body_json(&*test_json::SYNC_ROOM_MESSAGES_BATCH_1), + ) + .expect(1) + .named("messages_batch_1") + .mount(&server) + .await; - let mocked_messages = mock( - "GET", - Matcher::Regex( - r"^/_matrix/client/r0/rooms/.*/messages.*from=t392-516_47314_0_7_1_1_1_11444_1.*" - .to_owned(), - ), - ) - .with_status(200) - .with_body(test_json::SYNC_ROOM_MESSAGES_BATCH_1.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - - let mocked_messages_2 = mock( - "GET", - Matcher::Regex( - r"^/_matrix/client/r0/rooms/.*/messages.*from=t47409-4357353_219380_26003_2269.*" - .to_owned(), - ), - ) - .with_status(200) - .with_body(test_json::SYNC_ROOM_MESSAGES_BATCH_2.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("GET")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/messages$")) + .and(header("authorization", "Bearer 1234")) + .and(query_param("from", "t47409-4357353_219380_26003_2269")) + .respond_with( + ResponseTemplate::new(200).set_body_json(&*test_json::SYNC_ROOM_MESSAGES_BATCH_2), + ) + .expect(1) + .named("messages_batch_2") + .mount(&server) + .await; assert_eq!(client.sync_token().await, Some("s526_47314_0_7_1_1_1_11444_1".to_owned())); let sync_settings = SyncSettings::new() .timeout(Duration::from_millis(3000)) .token("s526_47314_0_7_1_1_1_11444_1"); let _ = client.sync_once(sync_settings).await.unwrap(); - sync_2.assert(); + let sync_settings = SyncSettings::new() .timeout(Duration::from_millis(3000)) .token("s526_47314_0_7_1_1_1_11444_2"); let _ = client.sync_once(sync_settings).await.unwrap(); - sync_3.assert(); let expected_forward_events = vec![ "$152037280074GZeOm:localhost", @@ -296,8 +247,6 @@ async fn room_timeline_with_remove() { "$098237280074GZeOm2:localhost", ]; - use futures_util::StreamExt; - use matrix_sdk::deserialized_responses::SyncRoomEvent; let forward_events = forward_stream.take(expected_forward_events.len()).collect::>().await; @@ -323,70 +272,55 @@ async fn room_timeline_with_remove() { for (r, e) in backward_events.into_iter().zip(expected_backwards_events.iter()) { assert_eq!(&r.unwrap().event_id().unwrap().as_str(), e); } - - mocked_messages.assert(); - mocked_messages_2.assert(); } #[async_test] #[cfg(feature = "experimental-timeline")] async fn room_timeline() { - let client = logged_in_client().await; + use futures_util::StreamExt; + use matrix_sdk::deserialized_responses::SyncRoomEvent; + use wiremock::matchers::query_param; + + let (client, server) = logged_in_client().await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); - let sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(test_json::MORE_SYNC.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + mock_sync(&server, &*test_json::MORE_SYNC, None).await; let _ = client.sync_once(sync_settings).await.unwrap(); - sync.assert(); - drop(sync); + let room = client.get_joined_room(room_id!("!SVkFJHzfwvuaIEawgC:localhost")).unwrap(); let (forward_stream, backward_stream) = room.timeline().await.unwrap(); - let sync_2 = mock( - "GET", - Matcher::Regex( - r"^/_matrix/client/r0/sync\?.*since=s526_47314_0_7_1_1_1_11444_2.*".to_owned(), - ), - ) - .with_status(200) - .with_body(test_json::MORE_SYNC_2.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + let sync_token = client.sync_token().await.unwrap(); + assert_eq!(sync_token, "s526_47314_0_7_1_1_1_11444_2"); + mock_sync(&server, &*test_json::MORE_SYNC_2, Some(sync_token.clone())).await; - let mocked_messages = mock( - "GET", - Matcher::Regex( - r"^/_matrix/client/r0/rooms/.*/messages.*from=t392-516_47314_0_7_1_1_1_11444_1.*" - .to_owned(), - ), - ) - .with_status(200) - .with_body(test_json::SYNC_ROOM_MESSAGES_BATCH_1.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("GET")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/messages$")) + .and(header("authorization", "Bearer 1234")) + .and(query_param("from", "t392-516_47314_0_7_1_1_1_11444_1")) + .respond_with( + ResponseTemplate::new(200).set_body_json(&*test_json::SYNC_ROOM_MESSAGES_BATCH_1), + ) + .expect(1) + .named("messages_batch_1") + .mount(&server) + .await; - let mocked_messages_2 = mock( - "GET", - Matcher::Regex( - r"^/_matrix/client/r0/rooms/.*/messages.*from=t47409-4357353_219380_26003_2269.*" - .to_owned(), - ), - ) - .with_status(200) - .with_body(test_json::SYNC_ROOM_MESSAGES_BATCH_2.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("GET")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/messages$")) + .and(header("authorization", "Bearer 1234")) + .and(query_param("from", "t47409-4357353_219380_26003_2269")) + .respond_with( + ResponseTemplate::new(200).set_body_json(&*test_json::SYNC_ROOM_MESSAGES_BATCH_2), + ) + .expect(1) + .named("messages_batch_2") + .mount(&server) + .await; - assert_eq!(client.sync_token().await, Some("s526_47314_0_7_1_1_1_11444_2".to_owned())); - let sync_settings = SyncSettings::new() - .timeout(Duration::from_millis(3000)) - .token("s526_47314_0_7_1_1_1_11444_2"); + let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)).token(sync_token); let _ = client.sync_once(sync_settings).await.unwrap(); - sync_2.assert(); let expected_forward_events = vec![ "$152037280074GZeOm2:localhost", @@ -397,8 +331,6 @@ async fn room_timeline() { "$098237280074GZeOm2:localhost", ]; - use futures_util::StreamExt; - use matrix_sdk::deserialized_responses::SyncRoomEvent; let forward_events = forward_stream.take(expected_forward_events.len()).collect::>().await; @@ -434,9 +366,6 @@ async fn room_timeline() { for (r, e) in backward_events.into_iter().zip(expected_backwards_events.iter()) { assert_eq!(&r.unwrap().event_id().unwrap().as_str(), e); } - - mocked_messages.assert(); - mocked_messages_2.assert(); } #[async_test] @@ -507,8 +436,7 @@ async fn room_permalink() { events } - let client = logged_in_client().await; - let sync_settings = SyncSettings::new(); + let (client, server) = logged_in_client().await; // Without elligible server let mut sync_index = 1; @@ -538,12 +466,8 @@ async fn room_permalink() { }), ], ); - let _sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(res.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - client.sync_once(sync_settings.clone()).await.unwrap(); + mock_sync(&server, res, None).await; + client.sync_once(SyncSettings::new()).await.unwrap(); let room = client.get_room(room_id!("!test_room:127.0.0.1")).unwrap(); assert_eq!( @@ -574,12 +498,9 @@ async fn room_permalink() { "type": "m.room.member", })], ); - let _sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(res.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - client.sync_once(sync_settings.clone()).await.unwrap(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, res, Some(sync_token.clone())).await; + client.sync_once(SyncSettings::new().token(sync_token)).await.unwrap(); assert_eq!( room.matrix_to_permalink().await.unwrap().to_string(), @@ -593,12 +514,9 @@ async fn room_permalink() { // With two elligible servers sync_index += 1; let res = sync_response(sync_index, &room_member_events(15, "notarealhs")); - let _sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(res.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - client.sync_once(sync_settings.clone()).await.unwrap(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, res, Some(sync_token.clone())).await; + client.sync_once(SyncSettings::new().token(sync_token)).await.unwrap(); assert_eq!( room.matrix_to_permalink().await.unwrap().to_string(), @@ -612,12 +530,9 @@ async fn room_permalink() { // With three elligible servers sync_index += 1; let res = sync_response(sync_index, &room_member_events(5, "mymatrix")); - let _sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(res.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - client.sync_once(sync_settings.clone()).await.unwrap(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, res, Some(sync_token.clone())).await; + client.sync_once(SyncSettings::new().token(sync_token)).await.unwrap(); assert_eq!( room.matrix_to_permalink().await.unwrap().to_string(), @@ -631,12 +546,9 @@ async fn room_permalink() { // With four elligible servers sync_index += 1; let res = sync_response(sync_index, &room_member_events(10, "yourmatrix")); - let _sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(res.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - client.sync_once(sync_settings.clone()).await.unwrap(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, res, Some(sync_token.clone())).await; + client.sync_once(SyncSettings::new().token(sync_token)).await.unwrap(); assert_eq!( room.matrix_to_permalink().await.unwrap().to_string(), @@ -664,12 +576,9 @@ async fn room_permalink() { "type": "m.room.power_levels", })], ); - let _sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(res.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - client.sync_once(sync_settings.clone()).await.unwrap(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, res, Some(sync_token.clone())).await; + client.sync_once(SyncSettings::new().token(sync_token)).await.unwrap(); assert_eq!( room.matrix_to_permalink().await.unwrap().to_string(), @@ -698,12 +607,9 @@ async fn room_permalink() { "type": "m.room.power_levels", })], ); - let _sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(res.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - client.sync_once(sync_settings.clone()).await.unwrap(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, res, Some(sync_token.clone())).await; + client.sync_once(SyncSettings::new().token(sync_token)).await.unwrap(); assert_eq!( room.matrix_to_permalink().await.unwrap().to_string(), @@ -731,12 +637,9 @@ async fn room_permalink() { "type": "m.room.server_acl", })], ); - let _sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(res.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - client.sync_once(sync_settings.clone()).await.unwrap(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, res, Some(sync_token.clone())).await; + client.sync_once(SyncSettings::new().token(sync_token)).await.unwrap(); assert_eq!( room.matrix_to_permalink().await.unwrap().to_string(), @@ -762,12 +665,9 @@ async fn room_permalink() { "type": "m.room.canonical_alias", })], ); - let _sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(res.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - client.sync_once(sync_settings.clone()).await.unwrap(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, res, Some(sync_token.clone())).await; + client.sync_once(SyncSettings::new().token(sync_token)).await.unwrap(); assert_eq!( room.matrix_to_permalink().await.unwrap().to_string(), @@ -791,12 +691,9 @@ async fn room_permalink() { "type": "m.room.canonical_alias", })], ); - let _sync = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .with_body(res.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); - client.sync_once(sync_settings).await.unwrap(); + let sync_token = client.sync_token().await.unwrap(); + mock_sync(&server, res, Some(sync_token.clone())).await; + client.sync_once(SyncSettings::new().token(sync_token)).await.unwrap(); assert_eq!( room.matrix_to_permalink().await.unwrap().to_string(), diff --git a/crates/matrix-sdk/tests/integration/room/joined.rs b/crates/matrix-sdk/tests/integration/room/joined.rs index 63c2431ea..e255b1837 100644 --- a/crates/matrix-sdk/tests/integration/room/joined.rs +++ b/crates/matrix-sdk/tests/integration/room/joined.rs @@ -8,31 +8,31 @@ use matrix_sdk::{ config::SyncSettings, }; use matrix_sdk_test::{async_test, test_json}; -use mockito::{mock, Matcher}; use ruma::{ api::client::membership::Invite3pidInit, assign, event_id, events::room::message::RoomMessageEventContent, mxc_uri, room_id, thirdparty, uint, user_id, TransactionId, }; use serde_json::json; +use wiremock::{ + matchers::{body_partial_json, header, method, path, path_regex}, + Mock, ResponseTemplate, +}; -use crate::logged_in_client; +use crate::{logged_in_client, mock_sync}; #[async_test] async fn invite_user_by_id() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/invite".to_owned())) - .with_status(200) - .with_body(test_json::LOGOUT.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/invite$")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGOUT)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -46,20 +46,16 @@ async fn invite_user_by_id() { #[async_test] async fn invite_user_by_3pid() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/invite".to_owned())) - .with_status(200) - // empty JSON object - .with_body(test_json::LOGOUT.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/invite$")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGOUT)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -82,20 +78,16 @@ async fn invite_user_by_3pid() { #[async_test] async fn leave_room() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/leave".to_owned())) - .with_status(200) - // this is an empty JSON object - .with_body(test_json::LOGOUT.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/leave$")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGOUT)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -108,20 +100,16 @@ async fn leave_room() { #[async_test] async fn ban_user() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/ban".to_owned())) - .with_status(200) - // this is an empty JSON object - .with_body(test_json::LOGOUT.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/ban$")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGOUT)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -135,20 +123,16 @@ async fn ban_user() { #[async_test] async fn kick_user() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/kick".to_owned())) - .with_status(200) - // this is an empty JSON object - .with_body(test_json::LOGOUT.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/kick$")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGOUT)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -162,20 +146,16 @@ async fn kick_user() { #[async_test] async fn read_receipt() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/receipt".to_owned())) - .with_status(200) - // this is an empty JSON object - .with_body(test_json::LOGOUT.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/receipt")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGOUT)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -189,20 +169,16 @@ async fn read_receipt() { #[async_test] async fn read_marker() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/read_markers".to_owned())) - .with_status(200) - // this is an empty JSON object - .with_body(test_json::LOGOUT.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/read_markers$")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGOUT)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -216,20 +192,16 @@ async fn read_marker() { #[async_test] async fn typing_notice() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("PUT", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/typing".to_owned())) - .with_status(200) - // this is an empty JSON object - .with_body(test_json::LOGOUT.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("PUT")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/typing")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGOUT)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -244,19 +216,16 @@ async fn typing_notice() { async fn room_state_event_send() { use ruma::events::room::member::{MembershipState, RoomMemberEventContent}; - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("PUT", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/state/.*".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::EVENT_ID.to_string()) - .create(); + Mock::given(method("PUT")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/.*")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EVENT_ID)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -276,19 +245,16 @@ async fn room_state_event_send() { #[async_test] async fn room_message_send() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("PUT", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/send/".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::EVENT_ID.to_string()) - .create(); + Mock::given(method("PUT")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EVENT_ID)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -305,35 +271,31 @@ async fn room_message_send() { #[async_test] async fn room_attachment_send() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("PUT", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/send/".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .match_body(Matcher::PartialJson(json!({ + Mock::given(method("PUT")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*")) + .and(header("authorization", "Bearer 1234")) + .and(body_partial_json(json!({ "info": { - "mimetype": "image/jpeg" + "mimetype": "image/jpeg", } }))) - .with_body(test_json::EVENT_ID.to_string()) - .create(); + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EVENT_ID)) + .mount(&server) + .await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/media/r0/upload".to_owned())) - .with_status(200) - .match_header("content-type", "image/jpeg") - .with_body( - json!({ - "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" - }) - .to_string(), - ) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/media/r0/upload")) + .and(header("authorization", "Bearer 1234")) + .and(header("content-type", "image/jpeg")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" + }))) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -353,37 +315,33 @@ async fn room_attachment_send() { #[async_test] async fn room_attachment_send_info() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("PUT", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/send/".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .match_body(Matcher::PartialJson(json!({ + Mock::given(method("PUT")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*")) + .and(header("authorization", "Bearer 1234")) + .and(body_partial_json(json!({ "info": { "mimetype": "image/jpeg", "h": 600, "w": 800, } }))) - .with_body(test_json::EVENT_ID.to_string()) - .create(); + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EVENT_ID)) + .mount(&server) + .await; - let upload_mock = mock("POST", Matcher::Regex(r"^/_matrix/media/r0/upload".to_owned())) - .with_status(200) - .match_header("content-type", "image/jpeg") - .with_body( - json!({ - "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" - }) - .to_string(), - ) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/media/r0/upload")) + .and(header("authorization", "Bearer 1234")) + .and(header("content-type", "image/jpeg")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" + }))) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -403,43 +361,38 @@ async fn room_attachment_send_info() { let response = room.send_attachment("image", &mime::IMAGE_JPEG, &mut media, config).await.unwrap(); - upload_mock.assert(); assert_eq!(event_id!("$h29iv0s8:example.com"), response.event_id) } #[async_test] async fn room_attachment_send_wrong_info() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("PUT", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/send/".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .match_body(Matcher::PartialJson(json!({ + Mock::given(method("PUT")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*")) + .and(header("authorization", "Bearer 1234")) + .and(body_partial_json(json!({ "info": { "mimetype": "image/jpeg", "h": 600, "w": 800, } }))) - .with_body(test_json::EVENT_ID.to_string()) - .create(); + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EVENT_ID)) + .mount(&server) + .await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/media/r0/upload".to_owned())) - .with_status(200) - .match_header("content-type", "image/jpeg") - .with_body( - json!({ - "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" - }) - .to_string(), - ) - .create(); + Mock::given(method("POST")) + .and(path("/_matrix/media/r0/upload")) + .and(header("authorization", "Bearer 1234")) + .and(header("content-type", "image/jpeg")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" + }))) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -464,12 +417,12 @@ async fn room_attachment_send_wrong_info() { #[async_test] async fn room_attachment_send_info_thumbnail() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("PUT", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/send/".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .match_body(Matcher::PartialJson(json!({ + Mock::given(method("PUT")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*")) + .and(header("authorization", "Bearer 1234")) + .and(body_partial_json(json!({ "info": { "mimetype": "image/jpeg", "h": 600, @@ -483,26 +436,22 @@ async fn room_attachment_send_info_thumbnail() { "thumbnail_url": "mxc://example.com/AQwafuaFswefuhsfAFAgsw", } }))) - .with_body(test_json::EVENT_ID.to_string()) - .create(); + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EVENT_ID)) + .mount(&server) + .await; - let upload_mock = mock("POST", Matcher::Regex(r"^/_matrix/media/r0/upload".to_owned())) - .with_status(200) - .match_header("content-type", "image/jpeg") - .with_body( - json!({ - "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" - }) - .to_string(), - ) + Mock::given(method("POST")) + .and(path("/_matrix/media/r0/upload")) + .and(header("authorization", "Bearer 1234")) + .and(header("content-type", "image/jpeg")) + .respond_with(ResponseTemplate::new(200).set_body_json(json!({ + "content_uri": "mxc://example.com/AQwafuaFswefuhsfAFAgsw" + }))) .expect(2) - .create(); + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); @@ -533,25 +482,21 @@ async fn room_attachment_send_info_thumbnail() { let response = room.send_attachment("image", &mime::IMAGE_JPEG, &mut media, config).await.unwrap(); - upload_mock.assert(); assert_eq!(event_id!("$h29iv0s8:example.com"), response.event_id) } #[async_test] async fn room_redact() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("PUT", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/redact/.*?/.*?".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::EVENT_ID.to_string()) - .create(); + Mock::given(method("PUT")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/redact/.*?/.*?")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EVENT_ID)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); diff --git a/crates/matrix-sdk/tests/integration/room/left.rs b/crates/matrix-sdk/tests/integration/room/left.rs index 9bc48d2f0..435f4a1f6 100644 --- a/crates/matrix-sdk/tests/integration/room/left.rs +++ b/crates/matrix-sdk/tests/integration/room/left.rs @@ -2,27 +2,26 @@ use std::time::Duration; use matrix_sdk::config::SyncSettings; use matrix_sdk_test::{async_test, test_json}; -use mockito::{mock, Matcher}; use ruma::room_id; +use wiremock::{ + matchers::{header, method, path_regex}, + Mock, ResponseTemplate, +}; -use crate::logged_in_client; +use crate::{logged_in_client, mock_sync}; #[async_test] async fn forget_room() { - let client = logged_in_client().await; + let (client, server) = logged_in_client().await; - let _m = mock("POST", Matcher::Regex(r"^/_matrix/client/r0/rooms/.*/forget".to_owned())) - .with_status(200) - // this is an empty JSON object - .with_body(test_json::LOGOUT.to_string()) - .match_header("authorization", "Bearer 1234") - .create(); + Mock::given(method("POST")) + .and(path_regex(r"^/_matrix/client/r0/rooms/.*/forget$")) + .and(header("authorization", "Bearer 1234")) + .respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGOUT)) + .mount(&server) + .await; - let _m = mock("GET", Matcher::Regex(r"^/_matrix/client/r0/sync\?.*$".to_owned())) - .with_status(200) - .match_header("authorization", "Bearer 1234") - .with_body(test_json::LEAVE_SYNC.to_string()) - .create(); + mock_sync(&server, &*test_json::LEAVE_SYNC, None).await; let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000));