From a984602d3039c7aa695556592bea1f525c3a1371 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Thu, 7 Nov 2024 16:36:49 +0100 Subject: [PATCH] task(tests): have the test clients use Matrix v1.12 --- crates/matrix-sdk/src/test_utils/mocks.rs | 16 +-- crates/matrix-sdk/tests/integration/widget.rs | 115 ++++++++---------- 2 files changed, 61 insertions(+), 70 deletions(-) diff --git a/crates/matrix-sdk/src/test_utils/mocks.rs b/crates/matrix-sdk/src/test_utils/mocks.rs index 7780fb86656..5f977e9e93c 100644 --- a/crates/matrix-sdk/src/test_utils/mocks.rs +++ b/crates/matrix-sdk/src/test_utils/mocks.rs @@ -144,7 +144,7 @@ impl MatrixMockServer { /// Mocks a sync endpoint. pub fn mock_sync(&self) -> MockEndpoint<'_, SyncEndpoint> { let mock = Mock::given(method("GET")) - .and(path("/_matrix/client/r0/sync")) + .and(path("/_matrix/client/v3/sync")) .and(header("authorization", "Bearer 1234")); MockEndpoint { mock, @@ -158,7 +158,7 @@ impl MatrixMockServer { /// Note: works with *any* room. pub fn mock_room_send(&self) -> MockEndpoint<'_, RoomSendEndpoint> { let mock = Mock::given(method("PUT")) - .and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/.*")) + .and(path_regex(r"^/_matrix/client/v3/rooms/.*/send/.*")) .and(header("authorization", "Bearer 1234")); MockEndpoint { mock, server: &self.server, endpoint: RoomSendEndpoint } } @@ -169,7 +169,7 @@ impl MatrixMockServer { pub fn mock_room_state_encryption(&self) -> MockEndpoint<'_, EncryptionStateEndpoint> { let mock = Mock::given(method("GET")) .and(header("authorization", "Bearer 1234")) - .and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.*room.*encryption.?")); + .and(path_regex(r"^/_matrix/client/v3/rooms/.*/state/m.*room.*encryption.?")); MockEndpoint { mock, server: &self.server, endpoint: EncryptionStateEndpoint } } @@ -179,14 +179,14 @@ impl MatrixMockServer { pub fn mock_set_room_state_encryption(&self) -> MockEndpoint<'_, SetEncryptionStateEndpoint> { let mock = Mock::given(method("PUT")) .and(header("authorization", "Bearer 1234")) - .and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.*room.*encryption.?")); + .and(path_regex(r"^/_matrix/client/v3/rooms/.*/state/m.*room.*encryption.?")); MockEndpoint { mock, server: &self.server, endpoint: SetEncryptionStateEndpoint } } /// Creates a prebuilt mock for the room redact endpoint. pub fn mock_room_redact(&self) -> MockEndpoint<'_, RoomRedactEndpoint> { let mock = Mock::given(method("PUT")) - .and(path_regex(r"^/_matrix/client/r0/rooms/.*/redact/.*?/.*?")) + .and(path_regex(r"^/_matrix/client/v3/rooms/.*/redact/.*?/.*?")) .and(header("authorization", "Bearer 1234")); MockEndpoint { mock, server: &self.server, endpoint: RoomRedactEndpoint } } @@ -204,7 +204,7 @@ impl MatrixMockServer { /// Create a prebuilt mock for uploading media. pub fn mock_upload(&self) -> MockEndpoint<'_, UploadEndpoint> { let mock = Mock::given(method("POST")) - .and(path("/_matrix/media/r0/upload")) + .and(path("/_matrix/media/v3/upload")) .and(header("authorization", "Bearer 1234")); MockEndpoint { mock, server: &self.server, endpoint: UploadEndpoint } } @@ -488,7 +488,7 @@ impl<'a> MockEndpoint<'a, RoomEventEndpoint> { let mock = self .mock - .and(path_regex(format!("^/_matrix/client/r0/rooms/{room_path}/event/{event_path}"))) + .and(path_regex(format!("^/_matrix/client/v3/rooms/{room_path}/event/{event_path}"))) .respond_with(ResponseTemplate::new(200).set_body_json(event.into_raw().json())); MatrixMock { server: self.server, mock } } @@ -557,7 +557,7 @@ impl MockClientBuilder { fn new(homeserver: String) -> Self { let default_builder = Client::builder() .homeserver_url(homeserver) - .server_versions([MatrixVersion::V1_0]) + .server_versions([MatrixVersion::V1_12]) .request_config(RequestConfig::new().disable_retry()); Self { builder: default_builder, logged_in: true } diff --git a/crates/matrix-sdk/tests/integration/widget.rs b/crates/matrix-sdk/tests/integration/widget.rs index e03b73a9a62..ecd00353937 100644 --- a/crates/matrix-sdk/tests/integration/widget.rs +++ b/crates/matrix-sdk/tests/integration/widget.rs @@ -18,7 +18,6 @@ use assert_matches::assert_matches; use async_trait::async_trait; use futures_util::FutureExt; use matrix_sdk::{ - config::SyncSettings, test_utils::mocks::MatrixMockServer, widget::{ Capabilities, CapabilitiesProvider, WidgetDriver, WidgetDriverHandle, WidgetSettings, @@ -26,9 +25,7 @@ use matrix_sdk::{ Client, }; use matrix_sdk_common::{executor::spawn, timeout::timeout}; -use matrix_sdk_test::{ - async_test, EventBuilder, JoinedRoomBuilder, SyncResponseBuilder, ALICE, BOB, -}; +use matrix_sdk_test::{async_test, EventBuilder, JoinedRoomBuilder, ALICE, BOB}; use once_cell::sync::Lazy; use ruma::{ event_id, @@ -50,8 +47,6 @@ use wiremock::{ Mock, ResponseTemplate, }; -use crate::mock_sync; - /// Create a JSON string from a [`json!`][serde_json::json] "literal". #[macro_export] macro_rules! json_string { @@ -246,7 +241,7 @@ async fn test_read_messages() { "start": "t392-516_47314_0_7_1_1_1_11444_1" }); Mock::given(method("GET")) - .and(path_regex(r"^/_matrix/client/r0/rooms/.*/messages$")) + .and(path_regex(r"^/_matrix/client/v3/rooms/.*/messages$")) .and(header("authorization", "Bearer 1234")) .and(query_param("limit", "2")) .respond_with(ResponseTemplate::new(200).set_body_json(response_json)) @@ -341,7 +336,7 @@ async fn test_read_messages_with_msgtype_capabilities() { "start": "t392-516_47314_0_7_1_1_1_11444_1" }); Mock::given(method("GET")) - .and(path_regex(r"^/_matrix/client/r0/rooms/.*/messages$")) + .and(path_regex(r"^/_matrix/client/v3/rooms/.*/messages$")) .and(header("authorization", "Bearer 1234")) .and(query_param("limit", "3")) .respond_with(ResponseTemplate::new(200).set_body_json(response_json)) @@ -429,58 +424,54 @@ async fn test_receive_live_events() { // No messages from the driver yet assert_matches!(recv_message(&driver_handle).now_or_never(), None); - let mut sync_builder = SyncResponseBuilder::new(); - // bump the internal batch counter, otherwise the response will be seen as - // identical to the one done in `run_test_driver` - sync_builder.build_json_sync_response(); - - let event_builder = EventBuilder::new(); - sync_builder.add_joined_room( - JoinedRoomBuilder::new(&ROOM_ID) - // text message from alice - matches filter #2 - .add_timeline_event(event_builder.make_sync_message_event( - &ALICE, - RoomMessageEventContent::text_plain("simple text message"), - )) - // emote from alice - doesn't match - .add_timeline_event(event_builder.make_sync_message_event( - &ALICE, - RoomMessageEventContent::emote_plain("emote message"), - )) - // pointless member event - matches filter #4 - .add_timeline_event(event_builder.make_sync_state_event( - user_id!("@example:localhost"), - "@example:localhost", - RoomMemberEventContent::new(MembershipState::Join), - Some(RoomMemberEventContent::new(MembershipState::Join)), - )) - // kick alice - doesn't match because the `#@example:localhost` bit - // is about the state_key, not the sender - .add_timeline_event(event_builder.make_sync_state_event( - user_id!("@example:localhost"), - ALICE.as_str(), - RoomMemberEventContent::new(MembershipState::Ban), - Some(RoomMemberEventContent::new(MembershipState::Join)), - )) - // set room tpoic - doesn't match - .add_timeline_event(event_builder.make_sync_state_event( - &BOB, - "", - RoomTopicEventContent::new("new room topic".to_owned()), - None, - )) - // set room name - matches filter #3 - .add_timeline_event(event_builder.make_sync_state_event( - &BOB, - "", - RoomNameEventContent::new("New Room Name".to_owned()), - None, - )), - ); - - mock_sync(mock_server.server(), sync_builder.build_json_sync_response(), None).await; - let _response = - client.sync_once(SyncSettings::new().timeout(Duration::from_millis(3000))).await.unwrap(); + mock_server + .mock_sync() + .ok_and_run(&client, |sync_builder| { + let event_builder = EventBuilder::new(); + sync_builder.add_joined_room( + JoinedRoomBuilder::new(&ROOM_ID) + // text message from alice - matches filter #2 + .add_timeline_event(event_builder.make_sync_message_event( + &ALICE, + RoomMessageEventContent::text_plain("simple text message"), + )) + // emote from alice - doesn't match + .add_timeline_event(event_builder.make_sync_message_event( + &ALICE, + RoomMessageEventContent::emote_plain("emote message"), + )) + // pointless member event - matches filter #4 + .add_timeline_event(event_builder.make_sync_state_event( + user_id!("@example:localhost"), + "@example:localhost", + RoomMemberEventContent::new(MembershipState::Join), + Some(RoomMemberEventContent::new(MembershipState::Join)), + )) + // kick alice - doesn't match because the `#@example:localhost` bit + // is about the state_key, not the sender + .add_timeline_event(event_builder.make_sync_state_event( + user_id!("@example:localhost"), + ALICE.as_str(), + RoomMemberEventContent::new(MembershipState::Ban), + Some(RoomMemberEventContent::new(MembershipState::Join)), + )) + // set room tpoic - doesn't match + .add_timeline_event(event_builder.make_sync_state_event( + &BOB, + "", + RoomTopicEventContent::new("new room topic".to_owned()), + None, + )) + // set room name - matches filter #3 + .add_timeline_event(event_builder.make_sync_state_event( + &BOB, + "", + RoomNameEventContent::new("New Room Name".to_owned()), + None, + )), + ); + }) + .await; let msg = recv_message(&driver_handle).await; assert_eq!(msg["api"], "toWidget"); @@ -518,7 +509,7 @@ async fn test_send_room_message() { .await; Mock::given(method("PUT")) - .and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/m.room.message/.*$")) + .and(path_regex(r"^/_matrix/client/v3/rooms/.*/send/m.room.message/.*$")) .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "event_id": "$foobar" }))) .expect(1) .mount(mock_server.server()) @@ -559,7 +550,7 @@ async fn test_send_room_name() { .await; Mock::given(method("PUT")) - .and(path_regex(r"^/_matrix/client/r0/rooms/.*/state/m.room.name/?$")) + .and(path_regex(r"^/_matrix/client/v3/rooms/.*/state/m.room.name/?$")) .respond_with(ResponseTemplate::new(200).set_body_json(json!({ "event_id": "$foobar" }))) .expect(1) .mount(mock_server.server())