From fc93690d1f33a6abbf9946fb081b2bbc172f3e03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Commaille?= Date: Fri, 4 Apr 2025 12:51:21 +0200 Subject: [PATCH] test(sdk): Add test for Client::logout() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Kévin Commaille --- crates/matrix-sdk/src/test_utils/mocks/mod.rs | 16 +++++++ crates/matrix-sdk/tests/integration/client.rs | 43 +++++++++++++++++-- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/crates/matrix-sdk/src/test_utils/mocks/mod.rs b/crates/matrix-sdk/src/test_utils/mocks/mod.rs index c46e29c6c..320722543 100644 --- a/crates/matrix-sdk/src/test_utils/mocks/mod.rs +++ b/crates/matrix-sdk/src/test_utils/mocks/mod.rs @@ -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!({}))) + } +} diff --git a/crates/matrix-sdk/tests/integration/client.rs b/crates/matrix-sdk/tests/integration/client.rs index aec3a3462..63d2bd00f 100644 --- a/crates/matrix-sdk/tests/integration/client.rs +++ b/crates/matrix-sdk/tests/integration/client.rs @@ -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(_))))); +}