Skip to content

Commit 30eb12e

Browse files
dragonfly1033poljar
authored andcommitted
test(sdk): change test_login_username_refresh_token to use MatrixMockServer
1 parent 900697b commit 30eb12e

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3252,8 +3252,20 @@ impl<'a> MockEndpoint<'a, LoginEndpoint> {
32523252
pub fn ok(self) -> MatrixMock<'a> {
32533253
self.respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN))
32543254
}
3255+
3256+
/// Returns a successful response with a given message.
3257+
pub fn ok_with(self, response: ResponseTemplate) -> MatrixMock<'a> {
3258+
self.respond_with(response)
3259+
}
3260+
3261+
/// Ensures that the body of the request is a superset of the provided
3262+
/// `body` parameter.
3263+
pub fn body_matches_partial_json(self, body: Value) -> Self {
3264+
Self { mock: self.mock.and(body_partial_json(body)), ..self }
3265+
}
32553266
}
32563267

3268+
32573269
/// A prebuilt mock for `GET /devices` requests.
32583270
pub struct DevicesEndpoint;
32593271

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

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use matrix_sdk::{
1111
executor::spawn,
1212
store::RoomLoadSettings,
1313
test_utils::{
14-
client::mock_session_meta, logged_in_client_with_server, no_retry_test_client_with_server,
15-
test_client_builder_with_server,
14+
client::mock_session_meta, logged_in_client_with_server, mocks::MatrixMockServer,
15+
no_retry_test_client_with_server, test_client_builder_with_server,
1616
},
1717
HttpError, RefreshTokenError, SessionChange, SessionTokens,
1818
};
@@ -43,27 +43,24 @@ fn session() -> MatrixSession {
4343

4444
#[async_test]
4545
async fn test_login_username_refresh_token() {
46-
let (client, server) = no_retry_test_client_with_server().await;
47-
48-
Mock::given(method("POST"))
49-
.and(path("/_matrix/client/r0/login"))
50-
.and(body_partial_json(json!({
51-
"refresh_token": true,
52-
})))
53-
.respond_with(
54-
ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN_WITH_REFRESH_TOKEN),
55-
)
56-
.mount(&server)
46+
let server = MatrixMockServer::new().await;
47+
server
48+
.mock_login()
49+
.body_matches_partial_json(json!({"refresh_token": true}))
50+
.ok_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN_WITH_REFRESH_TOKEN))
51+
.mount()
5752
.await;
5853

54+
let client = server.client_builder().unlogged().build().await;
55+
5956
let res = client
6057
.matrix_auth()
6158
.login_username("example", "wordpass")
6259
.request_refresh_token()
6360
.send()
6461
.await
6562
.unwrap();
66-
63+
6764
assert!(client.is_active(), "Client should be active");
6865
res.refresh_token.unwrap();
6966
}

0 commit comments

Comments
 (0)