Skip to content

Commit

Permalink
task(tests): have the test clients use Matrix v1.12
Browse files Browse the repository at this point in the history
  • Loading branch information
bnjbvr committed Nov 12, 2024
1 parent 73a3c91 commit e038c32
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 70 deletions.
16 changes: 8 additions & 8 deletions crates/matrix-sdk/src/test_utils/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 }
}
Expand All @@ -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 }
}

Expand All @@ -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 }
}
Expand All @@ -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 }
}
Expand Down Expand Up @@ -495,7 +495,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 }
}
Expand Down Expand Up @@ -564,7 +564,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 }
Expand Down
115 changes: 53 additions & 62 deletions crates/matrix-sdk/tests/integration/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ 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,
},
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,
Expand All @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit e038c32

Please sign in to comment.