Skip to content

Commit f7265c3

Browse files
BillCarsonFrpoljar
authored andcommitted
cleanup: Reuse existing server.mock_sync instead of custom function
1 parent 4468c36 commit f7265c3

File tree

2 files changed

+28
-95
lines changed

2 files changed

+28
-95
lines changed

crates/matrix-sdk/tests/integration/encryption/verification.rs

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ use matrix_sdk::{
55
test_utils::{logged_in_client_with_server, mocks::MatrixMockServer},
66
Client,
77
};
8-
use matrix_sdk_test::{async_test, SyncResponseBuilder};
8+
use matrix_sdk_test::async_test;
99
use ruma::{owned_device_id, owned_user_id, user_id};
1010
use serde_json::json;
1111
use wiremock::{
1212
matchers::{body_json, method, path},
1313
Mock, ResponseTemplate,
1414
};
1515

16-
use crate::mock_sync_scoped;
17-
1816
async fn bootstrap_cross_signing(client: &Client) {
1917
client.encryption().bootstrap_cross_signing(None).await.unwrap();
2018

@@ -70,18 +68,12 @@ async fn test_own_verification() {
7068
assert!(user_identity.is_verified());
7169

7270
// Force a keys query to pick up the cross-signing state
73-
let mut sync_response_builder = SyncResponseBuilder::new();
74-
sync_response_builder.add_change_device(&user_id);
75-
76-
{
77-
let _scope = mock_sync_scoped(
78-
server.server(),
79-
sync_response_builder.build_json_sync_response(),
80-
None,
81-
)
71+
server
72+
.mock_sync()
73+
.ok_and_run(&alice, |builder| {
74+
builder.add_change_device(&user_id);
75+
})
8276
.await;
83-
alice.sync_once(Default::default()).await.unwrap();
84-
}
8577

8678
// The device should now be cross-signed
8779
assert_eq!(
@@ -115,18 +107,12 @@ async fn test_reset_cross_signing_resets_verification() {
115107
assert_eq!(alice.encryption().verification_state().get(), VerificationState::Unverified);
116108

117109
// Force a keys query to pick up the cross-signing state
118-
let mut sync_response_builder = SyncResponseBuilder::new();
119-
sync_response_builder.add_change_device(&user_id);
120-
121-
{
122-
let _scope = mock_sync_scoped(
123-
server.server(),
124-
sync_response_builder.build_json_sync_response(),
125-
None,
126-
)
110+
server
111+
.mock_sync()
112+
.ok_and_run(&alice, |builder| {
113+
builder.add_change_device(&user_id);
114+
})
127115
.await;
128-
alice.sync_once(Default::default()).await.unwrap();
129-
}
130116

131117
// The device should now be cross-signed
132118
assert_eq!(
@@ -141,15 +127,12 @@ async fn test_reset_cross_signing_resets_verification() {
141127
// Have Alice bootstrap cross-signing again, this time on her second device.
142128
bootstrap_cross_signing(&alice2).await;
143129

144-
{
145-
let _scope = mock_sync_scoped(
146-
server.server(),
147-
sync_response_builder.build_json_sync_response(),
148-
None,
149-
)
130+
server
131+
.mock_sync()
132+
.ok_and_run(&alice, |builder| {
133+
builder.add_change_device(&user_id);
134+
})
150135
.await;
151-
alice.sync_once(Default::default()).await.unwrap();
152-
}
153136

154137
// The device shouldn't be cross-signed anymore.
155138
assert_eq!(alice.encryption().verification_state().get(), VerificationState::Unverified);
@@ -191,17 +174,8 @@ async fn test_unchecked_mutual_verification() {
191174
bootstrap_cross_signing(&bob).await;
192175

193176
// Have Alice and Bob upload their signed device keys.
194-
{
195-
let mut sync_response_builder = SyncResponseBuilder::new();
196-
let response_body = sync_response_builder.build_json_sync_response();
197-
let _scope = mock_sync_scoped(server.server(), response_body, None).await;
198-
199-
alice
200-
.sync_once(Default::default())
201-
.await
202-
.expect("We should be able to sync with Alice so we upload the device keys");
203-
bob.sync_once(Default::default()).await.unwrap();
204-
}
177+
server.mock_sync().ok_and_run(&alice, |_builder| {}).await;
178+
server.mock_sync().ok_and_run(&bob, |_builder| {}).await;
205179

206180
// Have Alice track Bob, so she queries his keys later.
207181
{
@@ -212,20 +186,7 @@ async fn test_unchecked_mutual_verification() {
212186

213187
// Run a sync so we do send outgoing requests, including the /keys/query for
214188
// getting bob's identity.
215-
let mut sync_response_builder = SyncResponseBuilder::new();
216-
217-
{
218-
let _scope = mock_sync_scoped(
219-
server.server(),
220-
sync_response_builder.build_json_sync_response(),
221-
None,
222-
)
223-
.await;
224-
alice
225-
.sync_once(Default::default())
226-
.await
227-
.expect("We should be able to sync so we get theinitial set of devices");
228-
}
189+
server.mock_sync().ok_and_run(&alice, |_builder| {}).await;
229190

230191
// From the point of view of Alice, Bob now has a device.
231192
let alice_bob_device = alice
@@ -248,20 +209,14 @@ async fn test_unchecked_mutual_verification() {
248209

249210
alice_bob_ident.verify().await.unwrap();
250211

251-
{
252-
// Notify Alice's devices that some identify changed, so it does another
253-
// /keys/query request.
254-
let _scope = mock_sync_scoped(
255-
server.server(),
256-
sync_response_builder.add_change_device(&bob_user_id).build_json_sync_response(),
257-
None,
258-
)
212+
// Notify Alice's devices that some identify changed, so it does another
213+
// /keys/query request.
214+
server
215+
.mock_sync()
216+
.ok_and_run(&alice, |builder| {
217+
builder.add_change_device(&bob_user_id);
218+
})
259219
.await;
260-
alice
261-
.sync_once(Default::default())
262-
.await
263-
.expect("We should be able to sync to get notified about the changed device");
264-
}
265220

266221
let alice_bob_ident = alice
267222
.encryption()

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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
use matrix_sdk::test_utils::logged_in_client_with_server;
44
use serde::Serialize;
55
use wiremock::{
6-
matchers::{header, method, path, path_regex, query_param, query_param_is_missing},
7-
Mock, MockGuard, MockServer, ResponseTemplate,
6+
matchers::{header, method, path, query_param, query_param_is_missing},
7+
Mock, MockServer, ResponseTemplate,
88
};
99

1010
mod account;
@@ -43,25 +43,3 @@ async fn mock_sync(server: &MockServer, response_body: impl Serialize, since: Op
4343
.mount(server)
4444
.await;
4545
}
46-
47-
/// Mount a Mock on the given server to handle the `GET /sync` endpoint with
48-
/// an optional `since` param that returns a 200 status code with the given
49-
/// response body.
50-
async fn mock_sync_scoped(
51-
server: &MockServer,
52-
response_body: impl Serialize,
53-
since: Option<String>,
54-
) -> MockGuard {
55-
let mut builder = Mock::given(method("GET")).and(path_regex(r"/_matrix/client/.*/sync"));
56-
57-
if let Some(since) = since {
58-
builder = builder.and(query_param("since", since));
59-
} else {
60-
builder = builder.and(query_param_is_missing("since"));
61-
}
62-
63-
builder
64-
.respond_with(ResponseTemplate::new(200).set_body_json(response_body))
65-
.mount_as_scoped(server)
66-
.await
67-
}

0 commit comments

Comments
 (0)