test(sdk): Add test for Client::logout()

Signed-off-by: Kévin Commaille <zecakeh@tedomum.fr>
This commit is contained in:
Kévin Commaille
2025-04-04 12:51:21 +02:00
committed by Damir Jelić
parent 43431b88da
commit fc93690d1f
2 changed files with 56 additions and 3 deletions
@@ -995,6 +995,12 @@ impl MatrixMockServer {
Mock::given(method("POST")).and(path_regex(r"^/_matrix/client/v3/rooms/.*/leave"));
self.mock_endpoint(mock, RoomLeaveEndpoint).expect_default_access_token()
}
/// Create a prebuilt mock for the endpoint use to log out a session.
pub fn mock_logout(&self) -> MockEndpoint<'_, LogoutEndpoint> {
let mock = Mock::given(method("POST")).and(path("/_matrix/client/v3/logout"));
self.mock_endpoint(mock, LogoutEndpoint).expect_default_access_token()
}
}
/// Parameter to [`MatrixMockServer::sync_room`].
@@ -2554,3 +2560,13 @@ impl<'a> MockEndpoint<'a, RoomLeaveEndpoint> {
})))
}
}
/// A prebuilt mock for `POST /logout` request.
pub struct LogoutEndpoint;
impl<'a> MockEndpoint<'a, LogoutEndpoint> {
/// Returns a successful empty response.
pub fn ok(self) -> MatrixMock<'a> {
self.respond_with(ResponseTemplate::new(200).set_body_json(json!({})))
}
}
+40 -3
View File
@@ -4,11 +4,14 @@ use assert_matches2::{assert_let, assert_matches};
use eyeball_im::VectorDiff;
use futures_util::FutureExt;
use matrix_sdk::{
authentication::oauth::{error::OAuthTokenRevocationError, OAuthError},
config::{RequestConfig, StoreConfig, SyncSettings},
store::RoomLoadSettings,
sync::RoomUpdate,
test_utils::{client::mock_matrix_session, no_retry_test_client_with_server},
Client, MemoryStore, StateChanges, StateStore,
test_utils::{
client::mock_matrix_session, mocks::MatrixMockServer, no_retry_test_client_with_server,
},
Client, Error, MemoryStore, StateChanges, StateStore,
};
use matrix_sdk_base::{sync::RoomUpdates, RoomState};
use matrix_sdk_test::{
@@ -567,7 +570,7 @@ async fn test_marking_room_as_dm_fails_if_undeserializable() {
let result = client.account().mark_as_dm(&DEFAULT_TEST_ROOM_ID, &users).await;
assert_matches!(result, Err(matrix_sdk::Error::SerdeJson(_)));
assert_matches!(result, Err(Error::SerdeJson(_)));
server.verify().await;
}
@@ -1395,3 +1398,37 @@ async fn test_restore_room() {
assert!(room.is_favourite());
assert!(!room.pinned_event_ids().unwrap().is_empty());
}
#[async_test]
async fn test_logout() {
let server = MatrixMockServer::new().await;
// Test unauthenticated client.
let unlogged_client = server.client_builder().unlogged().build().await;
let res = unlogged_client.logout().await;
assert_matches!(res, Err(Error::AuthenticationRequired));
// Test MatrixAuth.
server.mock_logout().ok().mock_once().named("matrix_logout").mount().await;
let matrix_auth_client = server.client_builder().build().await;
matrix_auth_client.logout().await.unwrap();
// Test OAuth.
server
.oauth()
.mock_server_metadata()
.ok()
.mock_once()
.named("oauth_server_metadata")
.mount()
.await;
let oauth_client = server.client_builder().logged_in_with_oauth().build().await;
let res = oauth_client.logout().await;
// This returns an error because it requires a HTTPS server URI, or to be able
// to call `OAuth::insecure_rewrite_https_to_http()`, but at least we are
// testing the OAuth branch inside `Client::logout()`.
assert_matches!(res, Err(Error::OAuth(OAuthError::Logout(OAuthTokenRevocationError::Url(_)))));
}