From 65fe3c8b5b17bd056dda2f726673cbc4bc5acb69 Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Thu, 22 Feb 2024 16:30:30 +0100 Subject: [PATCH] test: commonize the `logged_in_base_client` methods in sdk-base --- crates/matrix-sdk-base/src/client.rs | 23 +++------ crates/matrix-sdk-base/src/lib.rs | 2 + crates/matrix-sdk-base/src/sliding_sync.rs | 60 +++++++++------------- crates/matrix-sdk-base/src/test_utils.rs | 34 ++++++++++++ 4 files changed, 68 insertions(+), 51 deletions(-) create mode 100644 crates/matrix-sdk-base/src/test_utils.rs diff --git a/crates/matrix-sdk-base/src/client.rs b/crates/matrix-sdk-base/src/client.rs index 85b60bf61..5caca6225 100644 --- a/crates/matrix-sdk-base/src/client.rs +++ b/crates/matrix-sdk-base/src/client.rs @@ -1472,14 +1472,17 @@ mod tests { use serde_json::json; use super::BaseClient; - use crate::{store::StateStoreExt, DisplayName, RoomState, SessionMeta}; + use crate::{ + store::StateStoreExt, test_utils::logged_in_base_client, DisplayName, RoomState, + SessionMeta, + }; #[async_test] async fn test_invite_after_leaving() { let user_id = user_id!("@alice:example.org"); let room_id = room_id!("!test:example.org"); - let client = logged_in_base_client(user_id).await; + let client = logged_in_base_client(Some(user_id)).await; let mut ev_builder = SyncResponseBuilder::new(); @@ -1523,7 +1526,7 @@ mod tests { let user_id = user_id!("@alice:example.org"); let room_id = room_id!("!ithpyNKDtmhneaTQja:example.org"); - let client = logged_in_base_client(user_id).await; + let client = logged_in_base_client(Some(user_id)).await; let response = api::sync::sync_events::v3::Response::try_from_http_response(response_from_file(&json!({ "next_batch": "asdkl;fjasdkl;fj;asdkl;f", @@ -1612,7 +1615,7 @@ mod tests { // Given a room let user_id = user_id!("@u:u.to"); let room_id = room_id!("!r:u.to"); - let client = logged_in_base_client(user_id).await; + let client = logged_in_base_client(Some(user_id)).await; let room = process_room_join_test_helper(&client, room_id, "$1", user_id).await; // Sanity: it has no latest_encrypted_events or latest_event @@ -1634,18 +1637,6 @@ mod tests { // events. In the meantime, there are tests for the most difficult logic // inside Room. --andyb - async fn logged_in_base_client(user_id: &UserId) -> BaseClient { - let client = BaseClient::new(); - client - .set_session_meta(SessionMeta { - user_id: user_id.to_owned(), - device_id: "FOOBAR".into(), - }) - .await - .expect("set_session_meta failed!"); - client - } - #[cfg(feature = "e2e-encryption")] async fn process_room_join_test_helper( client: &BaseClient, diff --git a/crates/matrix-sdk-base/src/lib.rs b/crates/matrix-sdk-base/src/lib.rs index a5ba2324a..e382c7823 100644 --- a/crates/matrix-sdk-base/src/lib.rs +++ b/crates/matrix-sdk-base/src/lib.rs @@ -38,6 +38,8 @@ mod sliding_sync; pub mod store; pub mod sync; +#[cfg(any(test, feature = "testing"))] +mod test_utils; mod utils; #[cfg(feature = "uniffi")] diff --git a/crates/matrix-sdk-base/src/sliding_sync.rs b/crates/matrix-sdk-base/src/sliding_sync.rs index 930466381..ea4ee5b2e 100644 --- a/crates/matrix-sdk-base/src/sliding_sync.rs +++ b/crates/matrix-sdk-base/src/sliding_sync.rs @@ -728,7 +728,7 @@ mod tests { use matrix_sdk_test::async_test; use ruma::{ api::client::sync::sync_events::{v4, UnreadNotificationsCount}, - assign, device_id, event_id, + assign, event_id, events::{ direct::DirectEventContent, room::{ @@ -747,11 +747,13 @@ mod tests { use serde_json::json; use super::cache_latest_events; - use crate::{store::MemoryStore, BaseClient, Room, RoomState, SessionMeta}; + use crate::{ + store::MemoryStore, test_utils::logged_in_base_client, BaseClient, Room, RoomState, + }; #[async_test] async fn test_notification_count_set() { - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let mut response = v4::Response::new("42".to_owned()); let room_id = room_id!("!room:example.org"); @@ -781,7 +783,7 @@ mod tests { #[async_test] async fn can_process_empty_sliding_sync_response() { - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let empty_response = v4::Response::new("5".to_owned()); client.process_sliding_sync(&empty_response, &()).await.expect("Failed to process sync"); } @@ -789,7 +791,7 @@ mod tests { #[async_test] async fn room_with_unspecified_state_is_added_to_client_and_joined_list() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); // When I send sliding sync response containing a room (with identifiable data @@ -815,7 +817,7 @@ mod tests { #[async_test] async fn room_name_is_found_when_processing_sliding_sync_response() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); // When I send sliding sync response containing a room with a name @@ -839,7 +841,7 @@ mod tests { #[async_test] async fn invited_room_name_is_found_when_processing_sliding_sync_response() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let user_id = user_id!("@w:e.uk"); @@ -865,7 +867,7 @@ mod tests { #[async_test] async fn left_a_room_from_required_state_event() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let user_id = user_id!("@u:e.uk"); @@ -895,7 +897,7 @@ mod tests { #[async_test] async fn left_a_room_from_timeline_state_event() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let user_id = user_id!("@u:e.uk"); @@ -921,7 +923,7 @@ mod tests { // See https://github.com/matrix-org/matrix-rust-sdk/issues/1834 // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let user_id = user_id!("@u:e.uk"); @@ -958,7 +960,7 @@ mod tests { let user_b_id = user_id!("@b:e.uk"); // Given we have a DM with B, who is joined - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; create_dm(&client, room_id, user_a_id, user_b_id, MembershipState::Join).await; // (Sanity: B is a direct target, and is in Join state) @@ -983,7 +985,7 @@ mod tests { let user_b_id = user_id!("@b:e.uk"); // Given I have invited B to a DM - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; create_dm(&client, room_id, user_a_id, user_b_id, MembershipState::Invite).await; // (Sanity: B is a direct target, and is in Invite state) @@ -1007,7 +1009,7 @@ mod tests { let user_b_id = user_id!("@b:bar.org"); // Given we have a DM with B, who is joined - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; create_dm(&client, room_id, user_a_id, user_b_id, MembershipState::Join).await; // (Sanity: A is in Join state) @@ -1031,7 +1033,7 @@ mod tests { let user_b_id = user_id!("@b:bar.org"); // Given we have a DM with B, who is joined - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; create_dm(&client, room_id, user_a_id, user_b_id, MembershipState::Invite).await; // (Sanity: A is in Join state) @@ -1051,7 +1053,7 @@ mod tests { #[async_test] async fn avatar_is_found_when_processing_sliding_sync_response() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); // When I send sliding sync response containing a room with an avatar @@ -1075,7 +1077,7 @@ mod tests { #[async_test] async fn avatar_can_be_unset_when_processing_sliding_sync_response() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); // Set the avatar. @@ -1131,7 +1133,7 @@ mod tests { #[async_test] async fn avatar_is_found_from_required_state_when_processing_sliding_sync_response() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let user_id = user_id!("@u:e.uk"); @@ -1151,7 +1153,7 @@ mod tests { #[async_test] async fn invitation_room_is_added_to_client_and_invite_list() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let user_id = user_id!("@u:e.uk"); @@ -1175,7 +1177,7 @@ mod tests { #[async_test] async fn avatar_is_found_in_invitation_room_when_processing_sliding_sync_response() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let user_id = user_id!("@u:e.uk"); @@ -1196,7 +1198,7 @@ mod tests { #[async_test] async fn canonical_alias_is_found_in_invitation_room_when_processing_sliding_sync_response() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let user_id = user_id!("@u:e.uk"); let room_alias_id = room_alias_id!("#myroom:e.uk"); @@ -1215,7 +1217,7 @@ mod tests { #[async_test] async fn display_name_from_sliding_sync_overrides_alias() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let user_id = user_id!("@u:e.uk"); let room_alias_id = room_alias_id!("#myroom:e.uk"); @@ -1238,7 +1240,7 @@ mod tests { #[async_test] async fn last_event_from_sliding_sync_is_cached() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let event_a = json!({ "sender":"@alice:example.com", @@ -1272,7 +1274,7 @@ mod tests { #[async_test] async fn cached_latest_event_can_be_redacted() { // Given a logged-in client - let client = logged_in_client().await; + let client = logged_in_base_client(None).await; let room_id = room_id!("!r:e.uk"); let event_a = json!({ "sender": "@alice:example.com", @@ -1726,18 +1728,6 @@ mod tests { .push(make_global_account_data_event(DirectEventContent(direct_content))); } - async fn logged_in_client() -> BaseClient { - let client = BaseClient::new(); - client - .set_session_meta(SessionMeta { - user_id: user_id!("@u:e.uk").to_owned(), - device_id: device_id!("XYZ").to_owned(), - }) - .await - .expect("Failed to set session meta"); - client - } - async fn response_with_room(room_id: &RoomId, room: v4::SlidingSyncRoom) -> v4::Response { let mut response = v4::Response::new("5".to_owned()); response.rooms.insert(room_id.to_owned(), room); diff --git a/crates/matrix-sdk-base/src/test_utils.rs b/crates/matrix-sdk-base/src/test_utils.rs new file mode 100644 index 000000000..f59f976b7 --- /dev/null +++ b/crates/matrix-sdk-base/src/test_utils.rs @@ -0,0 +1,34 @@ +// Copyright 2024 The Matrix.org Foundation C.I.C. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +//! Testing utilities - DO NOT USE IN PRODUCTION. + +#![allow(dead_code)] + +use ruma::{owned_user_id, UserId}; + +use crate::{BaseClient, SessionMeta}; + +/// Create a [`BaseClient`] with the given user id, if provided, or an hardcoded +/// one otherwise. +pub(crate) async fn logged_in_base_client(user_id: Option<&UserId>) -> BaseClient { + let client = BaseClient::new(); + let user_id = + user_id.map(|user_id| user_id.to_owned()).unwrap_or_else(|| owned_user_id!("@u:e.uk")); + client + .set_session_meta(SessionMeta { user_id: user_id.to_owned(), device_id: "FOOBAR".into() }) + .await + .expect("set_session_meta failed!"); + client +}