From 30eb12ed2dfd21033a9aa5c981af4d0f34fcacd7 Mon Sep 17 00:00:00 2001 From: dragonfly1033 <=> Date: Wed, 9 Jul 2025 11:58:46 +0100 Subject: [PATCH] test(sdk): change test_login_username_refresh_token to use MatrixMockServer --- crates/matrix-sdk/src/test_utils/mocks/mod.rs | 12 +++++++++ .../tests/integration/refresh_token.rs | 25 ++++++++----------- 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/crates/matrix-sdk/src/test_utils/mocks/mod.rs b/crates/matrix-sdk/src/test_utils/mocks/mod.rs index bb7549b36..440341890 100644 --- a/crates/matrix-sdk/src/test_utils/mocks/mod.rs +++ b/crates/matrix-sdk/src/test_utils/mocks/mod.rs @@ -3252,8 +3252,20 @@ impl<'a> MockEndpoint<'a, LoginEndpoint> { pub fn ok(self) -> MatrixMock<'a> { self.respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN)) } + + /// Returns a successful response with a given message. + pub fn ok_with(self, response: ResponseTemplate) -> MatrixMock<'a> { + self.respond_with(response) + } + + /// Ensures that the body of the request is a superset of the provided + /// `body` parameter. + pub fn body_matches_partial_json(self, body: Value) -> Self { + Self { mock: self.mock.and(body_partial_json(body)), ..self } + } } + /// A prebuilt mock for `GET /devices` requests. pub struct DevicesEndpoint; diff --git a/crates/matrix-sdk/tests/integration/refresh_token.rs b/crates/matrix-sdk/tests/integration/refresh_token.rs index 30afba5e7..f25f90346 100644 --- a/crates/matrix-sdk/tests/integration/refresh_token.rs +++ b/crates/matrix-sdk/tests/integration/refresh_token.rs @@ -11,8 +11,8 @@ use matrix_sdk::{ executor::spawn, store::RoomLoadSettings, test_utils::{ - client::mock_session_meta, logged_in_client_with_server, no_retry_test_client_with_server, - test_client_builder_with_server, + client::mock_session_meta, logged_in_client_with_server, mocks::MatrixMockServer, + no_retry_test_client_with_server, test_client_builder_with_server, }, HttpError, RefreshTokenError, SessionChange, SessionTokens, }; @@ -43,19 +43,16 @@ fn session() -> MatrixSession { #[async_test] async fn test_login_username_refresh_token() { - let (client, server) = no_retry_test_client_with_server().await; - - Mock::given(method("POST")) - .and(path("/_matrix/client/r0/login")) - .and(body_partial_json(json!({ - "refresh_token": true, - }))) - .respond_with( - ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN_WITH_REFRESH_TOKEN), - ) - .mount(&server) + let server = MatrixMockServer::new().await; + server + .mock_login() + .body_matches_partial_json(json!({"refresh_token": true})) + .ok_with(ResponseTemplate::new(200).set_body_json(&*test_json::LOGIN_WITH_REFRESH_TOKEN)) + .mount() .await; + let client = server.client_builder().unlogged().build().await; + let res = client .matrix_auth() .login_username("example", "wordpass") @@ -63,7 +60,7 @@ async fn test_login_username_refresh_token() { .send() .await .unwrap(); - + assert!(client.is_active(), "Client should be active"); res.refresh_token.unwrap(); }