Skip to content

Commit 92fe72f

Browse files
committed
test: add a mocks mod in matrix-sdk-test to reuse across different integration tests
1 parent 01e2db1 commit 92fe72f

File tree

7 files changed

+56
-32
lines changed

7 files changed

+56
-32
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/matrix-sdk-ui/tests/integration/timeline/mod.rs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ use matrix_sdk::{
2323
test_utils::{events::EventFactory, logged_in_client_with_server},
2424
};
2525
use matrix_sdk_test::{
26-
async_test, sync_timeline_event, JoinedRoomBuilder, RoomAccountDataTestEvent, StateTestEvent,
27-
SyncResponseBuilder,
26+
async_test, mocks::mock_redaction, sync_timeline_event, JoinedRoomBuilder,
27+
RoomAccountDataTestEvent, StateTestEvent, SyncResponseBuilder,
2828
};
2929
use matrix_sdk_ui::timeline::{EventSendState, RoomExt, TimelineItemContent, VirtualTimelineItem};
3030
use ruma::{
@@ -262,14 +262,7 @@ async fn test_redact_message() {
262262
assert!(day_divider.is_day_divider());
263263

264264
// Redacting a remote event works.
265-
Mock::given(method("PUT"))
266-
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/redact/.*?/.*?"))
267-
.and(header("authorization", "Bearer 1234"))
268-
.respond_with(ResponseTemplate::new(200).set_body_json(json!({
269-
"event_id": "$42"
270-
})))
271-
.mount(&server)
272-
.await;
265+
mock_redaction(event_id!("$42")).mount(&server).await;
273266

274267
let event_id = first.as_event().unwrap();
275268

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ use matrix_sdk::{
1111
};
1212
use matrix_sdk_base::RoomState;
1313
use matrix_sdk_test::{
14-
async_test, test_json, test_json::sync::CUSTOM_ROOM_POWER_LEVELS, EphemeralTestEvent,
15-
GlobalAccountDataTestEvent, JoinedRoomBuilder, SyncResponseBuilder, DEFAULT_TEST_ROOM_ID,
14+
async_test,
15+
mocks::mock_redaction,
16+
test_json::{self, sync::CUSTOM_ROOM_POWER_LEVELS},
17+
EphemeralTestEvent, GlobalAccountDataTestEvent, JoinedRoomBuilder, SyncResponseBuilder,
18+
DEFAULT_TEST_ROOM_ID,
1619
};
1720
use ruma::{
1821
api::client::{membership::Invite3pidInit, receipt::create_receipt::v3::ReceiptType},
@@ -340,22 +343,17 @@ async fn test_room_message_send() {
340343
async fn test_room_redact() {
341344
let (client, server) = synced_client().await;
342345

343-
Mock::given(method("PUT"))
344-
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/redact/.*?/.*?"))
345-
.and(header("authorization", "Bearer 1234"))
346-
.respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EVENT_ID))
347-
.mount(&server)
348-
.await;
346+
let event_id = event_id!("$h29iv0s8:example.com");
347+
mock_redaction(event_id).mount(&server).await;
349348

350349
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap();
351350

352-
let event_id = event_id!("$xxxxxxxx:example.com");
353-
354351
let txn_id = TransactionId::new();
355352
let reason = Some("Indecent material");
356-
let response = room.redact(event_id, reason, Some(txn_id)).await.unwrap();
353+
let response =
354+
room.redact(event_id!("$xxxxxxxx:example.com"), reason, Some(txn_id)).await.unwrap();
357355

358-
assert_eq!(event_id!("$h29iv0s8:example.com"), response.event_id)
356+
assert_eq!(response.event_id, event_id);
359357
}
360358

361359
#[cfg(not(target_arch = "wasm32"))]
@@ -373,8 +371,7 @@ async fn test_fetch_members_deduplication() {
373371
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/members"))
374372
.and(header("authorization", "Bearer 1234"))
375373
.respond_with(ResponseTemplate::new(200).set_body_json(response_body))
376-
// Expect that we're only going to send the request out once.
377-
.expect(1..=1)
374+
.expect(1)
378375
.mount(&server)
379376
.await;
380377

crates/matrix-sdk/tests/integration/send_queue.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ use matrix_sdk::{
1616
},
1717
Client, MemoryStore,
1818
};
19-
use matrix_sdk_test::{async_test, InvitedRoomBuilder, JoinedRoomBuilder, LeftRoomBuilder};
19+
use matrix_sdk_test::{
20+
async_test, mocks::mock_redaction, InvitedRoomBuilder, JoinedRoomBuilder, LeftRoomBuilder,
21+
};
2022
use ruma::{
2123
api::MatrixVersion,
2224
event_id,
@@ -735,13 +737,7 @@ async fn test_cancellation() {
735737
.await;
736738

737739
// The redact of txn1 will happen because we asked for it previously.
738-
Mock::given(method("PUT"))
739-
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/redact/.*?/.*?"))
740-
.and(header("authorization", "Bearer 1234"))
741-
.respond_with(ResponseTemplate::new(200).set_body_json(json!({"event_id": "$1"})))
742-
.expect(1)
743-
.mount(&server)
744-
.await;
740+
mock_redaction(event_id!("$1")).expect(1).mount(&server).await;
745741

746742
let handle1 = q.send(RoomMessageEventContent::text_plain("msg1").into()).await.unwrap();
747743
let handle2 = q.send(RoomMessageEventContent::text_plain("msg2").into()).await.unwrap();

testing/matrix-sdk-test/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ serde_json = { workspace = true }
2727
ctor = "0.2.0"
2828
tokio = { workspace = true, features = ["rt", "macros"] }
2929
tracing-subscriber = { workspace = true, features = ["env-filter"] }
30+
wiremock = { workspace = true }
3031

3132
[target.'cfg(target_arch = "wasm32")'.dependencies]
3233
getrandom = { version = "0.2.6", default-features = false, features = ["js"] }

testing/matrix-sdk-test/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ pub mod __macro_support {
110110
}
111111

112112
mod event_builder;
113+
114+
#[cfg(not(target_arch = "wasm32"))]
115+
pub mod mocks;
116+
113117
pub mod notification_settings;
114118
mod sync_builder;
115119
pub mod test_json;

testing/matrix-sdk-test/src/mocks.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2024 The Matrix.org Foundation C.I.C.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! Mocks useful to reuse across different testing contexts.
16+
17+
use ruma::EventId;
18+
use serde_json::json;
19+
use wiremock::{
20+
matchers::{header, method, path_regex},
21+
Mock, ResponseTemplate,
22+
};
23+
24+
/// Mount a mock for a redaction endpoint, that will always work and return a
25+
/// 200 response.
26+
pub fn mock_redaction(event_id: &EventId) -> Mock {
27+
Mock::given(method("PUT"))
28+
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/redact/.*?/.*?"))
29+
.and(header("authorization", "Bearer 1234"))
30+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({ "event_id": event_id })))
31+
.named("redact")
32+
}

0 commit comments

Comments
 (0)