Skip to content

Commit 965a59d

Browse files
committed
task(tests): create the client with MatrixMockServer::make_client() instead of embedding one into the struct
1 parent f032d16 commit 965a59d

File tree

4 files changed

+88
-73
lines changed

4 files changed

+88
-73
lines changed

crates/matrix-sdk/src/test_utils/mocks.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,8 @@ use wiremock::{
3434
use super::logged_in_client;
3535
use crate::{Client, Room};
3636

37-
/// A `wiremock` server along with a client connected to it, with useful methods
38-
/// to help mocking Matrix client-server API endpoints easily.
39-
///
40-
/// This is a pair of a [`MockServer`] and a [`Client]` (one can retrieve them
41-
/// respectively with [`Self::server()`] and [`Self::client()`]).
37+
/// A `wiremock` [`MockServer`] along with useful methods to help mocking Matrix
38+
/// client-server API endpoints easily.
4239
///
4340
/// It implements mock endpoints, limiting the shared code as much as possible,
4441
/// so the mocks are still flexible to use as scoped/unscoped mounts, named, and
@@ -63,7 +60,6 @@ use crate::{Client, Room};
6360
/// mostly defers its implementations to [`wiremock::Mock`] under the hood.
6461
pub struct MatrixMockServer {
6562
server: MockServer,
66-
client: Client,
6763

6864
/// Make the sync response builder stateful, to keep in memory the batch
6965
/// token and avoid the client ignoring subsequent responses after the first
@@ -75,19 +71,19 @@ impl MatrixMockServer {
7571
/// Create a new `wiremock` server specialized for Matrix usage.
7672
pub async fn new() -> Self {
7773
let server = MockServer::start().await;
78-
let client = logged_in_client(Some(server.uri().to_string())).await;
79-
Self { client, server, sync_response_builder: Default::default() }
74+
Self { server, sync_response_builder: Default::default() }
8075
}
8176

8277
/// Creates a new [`MatrixMockServer`] when both parts have been already
8378
/// created.
84-
pub fn from_parts(server: MockServer, client: Client) -> Self {
85-
Self { client, server, sync_response_builder: Default::default() }
79+
pub fn from_server(server: MockServer) -> Self {
80+
Self { server, sync_response_builder: Default::default() }
8681
}
8782

88-
/// Return the underlying client.
89-
pub fn client(&self) -> Client {
90-
self.client.clone()
83+
/// Creates a new [`Client`] configured to use this server, preconfigured
84+
/// with a session expected by the server endpoints.
85+
pub async fn make_client(&self) -> Client {
86+
logged_in_client(Some(self.server.uri().to_string())).await
9187
}
9288

9389
/// Return the underlying server.
@@ -98,11 +94,16 @@ impl MatrixMockServer {
9894
/// Overrides the sync/ endpoint with knowledge that the given
9995
/// invited/joined/knocked/left room exists, runs a sync and returns the
10096
/// given room.
101-
pub async fn sync_room(&self, room_id: &RoomId, room_data: impl Into<AnyRoomBuilder>) -> Room {
97+
pub async fn sync_room(
98+
&self,
99+
client: &Client,
100+
room_id: &RoomId,
101+
room_data: impl Into<AnyRoomBuilder>,
102+
) -> Room {
102103
let any_room = room_data.into();
103104

104105
self.mock_sync()
105-
.ok_and_run(move |builder| match any_room {
106+
.ok_and_run(client, move |builder| match any_room {
106107
AnyRoomBuilder::Invited(invited) => {
107108
builder.add_invited_room(invited);
108109
}
@@ -118,13 +119,13 @@ impl MatrixMockServer {
118119
})
119120
.await;
120121

121-
self.client.get_room(room_id).expect("look at me, the room is known now")
122+
client.get_room(room_id).expect("look at me, the room is known now")
122123
}
123124

124125
/// Overrides the sync/ endpoint with knowledge that the given room exists
125126
/// in the joined state, runs a sync and returns the given room.
126-
pub async fn sync_joined_room(&self, room_id: &RoomId) -> Room {
127-
self.sync_room(room_id, JoinedRoomBuilder::new(room_id)).await
127+
pub async fn sync_joined_room(&self, client: &Client, room_id: &RoomId) -> Room {
128+
self.sync_room(client, room_id, JoinedRoomBuilder::new(room_id)).await
128129
}
129130

130131
/// Verify that the previous mocks expected number of requests match
@@ -144,7 +145,6 @@ impl MatrixMockServer {
144145
.and(header("authorization", "Bearer 1234"));
145146
MockSync {
146147
mock,
147-
client: self.client.clone(),
148148
server: &self.server,
149149
sync_response_builder: self.sync_response_builder.clone(),
150150
}
@@ -360,15 +360,14 @@ pub struct MockSync<'a> {
360360
mock: MockBuilder,
361361
server: &'a MockServer,
362362
sync_response_builder: Arc<Mutex<SyncResponseBuilder>>,
363-
client: Client,
364363
}
365364

366365
impl<'a> MockSync<'a> {
367366
/// Temporarily mocks the sync with the given endpoint and runs a client
368367
/// sync with it.
369368
///
370369
/// After calling this function, the sync endpoint isn't mocked anymore.
371-
pub async fn ok_and_run<F: FnOnce(&mut SyncResponseBuilder)>(self, func: F) {
370+
pub async fn ok_and_run<F: FnOnce(&mut SyncResponseBuilder)>(self, client: &Client, func: F) {
372371
let json_response = {
373372
let mut builder = self.sync_response_builder.lock().unwrap();
374373
func(&mut builder);
@@ -381,7 +380,7 @@ impl<'a> MockSync<'a> {
381380
.mount_as_scoped(self.server)
382381
.await;
383382

384-
let _response = self.client.sync_once(Default::default()).await.unwrap();
383+
let _response = client.sync_once(Default::default()).await.unwrap();
385384
}
386385
}
387386

crates/matrix-sdk/tests/integration/room/attachment/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ async fn test_room_attachment_send() {
4040
.mount()
4141
.await;
4242

43-
let room = mock.sync_joined_room(&DEFAULT_TEST_ROOM_ID).await;
43+
let client = mock.make_client().await;
44+
let room = mock.sync_joined_room(&client, &DEFAULT_TEST_ROOM_ID).await;
4445
mock.mock_room_state_encryption().plain().mount().await;
4546

4647
let response = room
@@ -81,7 +82,8 @@ async fn test_room_attachment_send_info() {
8182
.mount()
8283
.await;
8384

84-
let room = mock.sync_joined_room(&DEFAULT_TEST_ROOM_ID).await;
85+
let client = mock.make_client().await;
86+
let room = mock.sync_joined_room(&client, &DEFAULT_TEST_ROOM_ID).await;
8587
mock.mock_room_state_encryption().plain().mount().await;
8688

8789
let config = AttachmentConfig::new()
@@ -130,7 +132,8 @@ async fn test_room_attachment_send_wrong_info() {
130132
.mount()
131133
.await;
132134

133-
let room = mock.sync_joined_room(&DEFAULT_TEST_ROOM_ID).await;
135+
let client = mock.make_client().await;
136+
let room = mock.sync_joined_room(&client, &DEFAULT_TEST_ROOM_ID).await;
134137
mock.mock_room_state_encryption().plain().mount().await;
135138

136139
// Here, using `AttachmentInfo::Video`…
@@ -188,7 +191,8 @@ async fn test_room_attachment_send_info_thumbnail() {
188191
// Second request: return the media MXC.
189192
mock.mock_upload().expect_mime_type("image/jpeg").ok(&media_mxc).mock_once().mount().await;
190193

191-
let room = mock.sync_joined_room(&DEFAULT_TEST_ROOM_ID).await;
194+
let client = mock.make_client().await;
195+
let room = mock.sync_joined_room(&client, &DEFAULT_TEST_ROOM_ID).await;
192196
mock.mock_room_state_encryption().plain().mount().await;
193197

194198
// Preconditions: nothing is found in the cache.
@@ -199,7 +203,6 @@ async fn test_room_attachment_send_info_thumbnail() {
199203
format: MediaFormat::Thumbnail(MediaThumbnailSettings::new(uint!(480), uint!(360))),
200204
};
201205

202-
let client = mock.client();
203206
let _ = client.media().get_media_content(&media_request, true).await.unwrap_err();
204207
let _ = client.media().get_media_content(&thumbnail_request, true).await.unwrap_err();
205208

@@ -283,7 +286,8 @@ async fn test_room_attachment_send_mentions() {
283286
.mount()
284287
.await;
285288

286-
let room = mock.sync_joined_room(&DEFAULT_TEST_ROOM_ID).await;
289+
let client = mock.make_client().await;
290+
let room = mock.sync_joined_room(&client, &DEFAULT_TEST_ROOM_ID).await;
287291
mock.mock_room_state_encryption().plain().mount().await;
288292

289293
let response = room

crates/matrix-sdk/tests/integration/room/joined.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -721,10 +721,11 @@ async fn test_make_reply_event_doesnt_require_event_cache() {
721721
// /event query to get details on an event.
722722

723723
let mock = MatrixMockServer::new().await;
724-
let user_id = mock.client().user_id().unwrap().to_owned();
724+
let client = mock.make_client().await;
725+
let user_id = client.user_id().unwrap().to_owned();
725726

726727
let room_id = room_id!("!galette:saucisse.bzh");
727-
let room = mock.sync_joined_room(room_id).await;
728+
let room = mock.sync_joined_room(&client, room_id).await;
728729

729730
let event_id = event_id!("$1");
730731
let f = EventFactory::new();
@@ -744,12 +745,13 @@ async fn test_make_reply_event_doesnt_require_event_cache() {
744745
#[async_test]
745746
async fn test_enable_encryption_doesnt_stay_unencrypted() {
746747
let mock = MatrixMockServer::new().await;
748+
let client = mock.make_client().await;
747749

748750
mock.mock_room_state_encryption().plain().mount().await;
749751
mock.mock_set_room_state_encryption().ok(event_id!("$1")).mount().await;
750752

751753
let room_id = room_id!("!a:b.c");
752-
let room = mock.sync_joined_room(room_id).await;
754+
let room = mock.sync_joined_room(&client, room_id).await;
753755

754756
assert!(!room.is_encrypted().await.unwrap());
755757

0 commit comments

Comments
 (0)