From e6b1ffba99d2ffbcff9257e996d04cea408125ef Mon Sep 17 00:00:00 2001 From: Benjamin Bouvier Date: Thu, 21 Aug 2025 12:44:42 +0200 Subject: [PATCH] tests: add support for the global account PushRules event in the event factory --- .../src/store/integration_tests.rs | 12 +- .../src/notification_settings/mod.rs | 36 ++- .../integration/room/notification_mode.rs | 35 ++- testing/matrix-sdk-test/src/event_factory.rs | 9 + .../matrix-sdk-test/src/sync_builder/mod.rs | 1 - .../src/sync_builder/test_event.rs | 2 - testing/matrix-sdk-test/src/test_json/mod.rs | 2 +- .../src/test_json/sync_events.rs | 214 ------------------ 8 files changed, 64 insertions(+), 247 deletions(-) diff --git a/crates/matrix-sdk-base/src/store/integration_tests.rs b/crates/matrix-sdk-base/src/store/integration_tests.rs index 6249ef23d..e2f17a48a 100644 --- a/crates/matrix-sdk-base/src/store/integration_tests.rs +++ b/crates/matrix-sdk-base/src/store/integration_tests.rs @@ -29,7 +29,9 @@ use ruma::{ topic::RoomTopicEventContent, }, }, - owned_event_id, owned_mxc_uri, room_id, + owned_event_id, owned_mxc_uri, + push::Ruleset, + room_id, room_version_rules::AuthorizationRules, serde::Raw, uint, user_id, @@ -107,6 +109,7 @@ pub trait StateStoreIntegrationTests { impl StateStoreIntegrationTests for DynStateStore { async fn populate(&self) -> TestResult { + let f = EventFactory::new(); let mut changes = StateChanges::default(); let user_id = user_id(); @@ -121,9 +124,8 @@ impl StateStoreIntegrationTests for DynStateStore { let presence_event = presence_raw.deserialize()?; changes.add_presence_event(presence_event, presence_raw); - let pushrules_json: &JsonValue = &test_json::PUSH_RULES; - let pushrules_raw = - serde_json::from_value::>(pushrules_json.clone())?; + let pushrules_raw: Raw = + f.push_rules(Ruleset::server_default(user_id)).into_raw(); let pushrules_event = pushrules_raw.deserialize()?; changes.account_data.insert(pushrules_event.event_type(), pushrules_raw); @@ -175,8 +177,8 @@ impl StateStoreIntegrationTests for DynStateStore { let invited_member_state_event = invited_member_state_raw.deserialize()?; changes.add_state_event(room_id, invited_member_state_event, invited_member_state_raw); - let f = EventFactory::new().room(room_id); let receipt_content = f + .room(room_id) .read_receipts() .add(event_id!("$example"), user_id, ReceiptType::Read, ReceiptThread::Unthreaded) .into_content(); diff --git a/crates/matrix-sdk/src/notification_settings/mod.rs b/crates/matrix-sdk/src/notification_settings/mod.rs index 6ba33d7f6..5dcaba8d5 100644 --- a/crates/matrix-sdk/src/notification_settings/mod.rs +++ b/crates/matrix-sdk/src/notification_settings/mod.rs @@ -584,27 +584,26 @@ mod tests { use assert_matches::assert_matches; use matrix_sdk_test::{ async_test, + event_factory::EventFactory, notification_settings::{build_ruleset, get_server_default_ruleset}, - test_json, TestResult, + TestResult, }; use ruma::{ owned_room_id, push::{ Action, AnyPushRuleRef, NewPatternedPushRule, NewPushRule, PredefinedContentRuleId, - PredefinedOverrideRuleId, PredefinedUnderrideRuleId, RuleKind, + PredefinedOverrideRuleId, PredefinedUnderrideRuleId, RuleKind, Ruleset, }, OwnedRoomId, RoomId, }; - use serde_json::json; use stream_assert::{assert_next_eq, assert_pending}; use tokio_stream::wrappers::BroadcastStream; use wiremock::{ - matchers::{header, method, path, path_regex}, + matchers::{method, path, path_regex}, Mock, MockServer, ResponseTemplate, }; use crate::{ - config::SyncSettings, error::NotificationSettingsError, notification_settings::{ IsEncrypted, IsOneToOne, NotificationSettings, RoomNotificationMode, @@ -634,29 +633,24 @@ mod tests { #[async_test] async fn test_subscribe_to_changes() -> TestResult { - let server = MockServer::start().await; - let client = logged_in_client(Some(server.uri())).await; + let server = MatrixMockServer::new().await; + let client = server.client_builder().build().await; let settings = client.notification_settings().await; - Mock::given(method("GET")) - .and(path("/_matrix/client/r0/sync")) - .and(header("authorization", "Bearer 1234")) - .respond_with(ResponseTemplate::new(200).set_body_json(json!({ - "next_batch": "1234", - "account_data": { - "events": [*test_json::PUSH_RULES] - } - }))) - .expect(1) - .mount(&server) - .await; - let subscriber = settings.subscribe_to_changes(); let mut stream = BroadcastStream::new(subscriber); assert_pending!(stream); - client.sync_once(SyncSettings::default()).await?; + server + .mock_sync() + .ok_and_run(&client, |sync_response_builder| { + let f = EventFactory::new(); + sync_response_builder.add_global_account_data( + f.push_rules(Ruleset::server_default(client.user_id().unwrap())).into_raw(), + ); + }) + .await; assert_next_eq!(stream, Ok(())); assert_pending!(stream); diff --git a/crates/matrix-sdk/tests/integration/room/notification_mode.rs b/crates/matrix-sdk/tests/integration/room/notification_mode.rs index 1e2a347a7..6dc85e418 100644 --- a/crates/matrix-sdk/tests/integration/room/notification_mode.rs +++ b/crates/matrix-sdk/tests/integration/room/notification_mode.rs @@ -4,10 +4,13 @@ use assert_matches::assert_matches; use matrix_sdk::{config::SyncSettings, notification_settings::RoomNotificationMode}; use matrix_sdk_base::RoomState; use matrix_sdk_test::{ - async_test, GlobalAccountDataTestEvent, InvitedRoomBuilder, JoinedRoomBuilder, + async_test, event_factory::EventFactory, InvitedRoomBuilder, JoinedRoomBuilder, SyncResponseBuilder, DEFAULT_TEST_ROOM_ID, }; -use ruma::room_id; +use ruma::{ + push::{Action, ConditionalPushRule, NewSimplePushRule, PatternedPushRule, Ruleset, Tweak}, + room_id, +}; use serde_json::json; use wiremock::{ matchers::{header, method, path_regex}, @@ -29,7 +32,33 @@ async fn test_get_notification_mode() { sync_builder.add_joined_room(JoinedRoomBuilder::new(&DEFAULT_TEST_ROOM_ID)); sync_builder.add_joined_room(JoinedRoomBuilder::new(room_no_rules_id)); sync_builder.add_invited_room(InvitedRoomBuilder::new(room_not_joined_id)); - sync_builder.add_global_account_data_event(GlobalAccountDataTestEvent::PushRules); + + let f = EventFactory::new(); + + let mut ruleset = Ruleset::default(); + #[allow(deprecated)] + ruleset.content.insert(PatternedPushRule::contains_user_name(client.user_id().unwrap())); + ruleset.override_ = + [ConditionalPushRule::master(), ConditionalPushRule::suppress_notices()].into(); + ruleset.room.insert( + NewSimplePushRule::new( + (*DEFAULT_TEST_ROOM_ID).into(), + vec![Action::Notify, Action::SetTweak(Tweak::Sound("default".into()))], + ) + .into(), + ); + ruleset.underride = [ + ConditionalPushRule::call(), + #[allow(deprecated)] + ConditionalPushRule::contains_display_name(), + ConditionalPushRule::room_one_to_one(), + ConditionalPushRule::invite_for_me(client.user_id().unwrap()), + ConditionalPushRule::member_event(), + ConditionalPushRule::message(), + ] + .into(); + + sync_builder.add_global_account_data(f.push_rules(ruleset).into_raw()); mock_sync(&server, sync_builder.build_json_sync_response(), None).await; let _response = client.sync_once(sync_settings.clone()).await.unwrap(); diff --git a/testing/matrix-sdk-test/src/event_factory.rs b/testing/matrix-sdk-test/src/event_factory.rs index 3739caf64..9a46d7e5c 100644 --- a/testing/matrix-sdk-test/src/event_factory.rs +++ b/testing/matrix-sdk-test/src/event_factory.rs @@ -49,6 +49,7 @@ use ruma::{ UnstablePollAnswer, UnstablePollStartContentBlock, UnstablePollStartEventContent, }, }, + push_rules::PushRulesEventContent, reaction::ReactionEventContent, receipt::{Receipt, ReceiptEventContent, ReceiptThread, ReceiptType}, relation::{Annotation, BundledThread, InReplyTo, Replacement, Thread}, @@ -74,6 +75,7 @@ use ruma::{ sticker::StickerEventContent, typing::TypingEventContent, }, + push::Ruleset, room_version_rules::AuthorizationRules, serde::Raw, server_name, @@ -1010,6 +1012,13 @@ impl EventFactory { builder } + /// Create a new `m.push_rules` global account data event. + pub fn push_rules(&self, rules: Ruleset) -> EventBuilder { + let mut builder = self.event(PushRulesEventContent::new(rules)); + builder.is_global = true; + builder + } + /// Set the next server timestamp. /// /// Timestamps will continue to increase by 1 (millisecond) from that value. diff --git a/testing/matrix-sdk-test/src/sync_builder/mod.rs b/testing/matrix-sdk-test/src/sync_builder/mod.rs index e3a5535dc..389123a50 100644 --- a/testing/matrix-sdk-test/src/sync_builder/mod.rs +++ b/testing/matrix-sdk-test/src/sync_builder/mod.rs @@ -151,7 +151,6 @@ impl SyncResponseBuilder { event: GlobalAccountDataTestEvent, ) -> &mut Self { let val = match event { - GlobalAccountDataTestEvent::PushRules => test_json::PUSH_RULES.to_owned(), GlobalAccountDataTestEvent::Custom(json) => json, }; diff --git a/testing/matrix-sdk-test/src/sync_builder/test_event.rs b/testing/matrix-sdk-test/src/sync_builder/test_event.rs index d0ada91d7..e3403d9bc 100644 --- a/testing/matrix-sdk-test/src/sync_builder/test_event.rs +++ b/testing/matrix-sdk-test/src/sync_builder/test_event.rs @@ -149,14 +149,12 @@ impl From for Raw { /// Test events that can be added to the global account data. pub enum GlobalAccountDataTestEvent { - PushRules, Custom(JsonValue), } impl From for JsonValue { fn from(val: GlobalAccountDataTestEvent) -> Self { match val { - GlobalAccountDataTestEvent::PushRules => test_json::sync_events::PUSH_RULES.to_owned(), GlobalAccountDataTestEvent::Custom(json) => json, } } diff --git a/testing/matrix-sdk-test/src/test_json/mod.rs b/testing/matrix-sdk-test/src/test_json/mod.rs index 2d9fbd011..8843cd4b4 100644 --- a/testing/matrix-sdk-test/src/test_json/mod.rs +++ b/testing/matrix-sdk-test/src/test_json/mod.rs @@ -30,7 +30,7 @@ pub use sync::{ }; pub use sync_events::{ ALIAS, ALIASES, ENCRYPTION, MEMBER, MEMBER_ADDITIONAL, MEMBER_BAN, MEMBER_INVITE, MEMBER_LEAVE, - MEMBER_NAME_CHANGE, MEMBER_STRIPPED, NAME, NAME_STRIPPED, POWER_LEVELS, PRESENCE, PUSH_RULES, + MEMBER_NAME_CHANGE, MEMBER_STRIPPED, NAME, NAME_STRIPPED, POWER_LEVELS, PRESENCE, REDACTED_INVALID, REDACTED_STATE, TAG, TOPIC, TOPIC_REDACTION, }; diff --git a/testing/matrix-sdk-test/src/test_json/sync_events.rs b/testing/matrix-sdk-test/src/test_json/sync_events.rs index dab8fe725..587cbd73d 100644 --- a/testing/matrix-sdk-test/src/test_json/sync_events.rs +++ b/testing/matrix-sdk-test/src/test_json/sync_events.rs @@ -3,8 +3,6 @@ use once_cell::sync::Lazy; use serde_json::{Value as JsonValue, json}; -use crate::DEFAULT_TEST_ROOM_ID; - pub static ALIAS: Lazy = Lazy::new(|| { json!({ "content": { @@ -392,218 +390,6 @@ pub static PRESENCE: Lazy = Lazy::new(|| { }) }); -pub static PUSH_RULES: Lazy = Lazy::new(|| { - json!({ - "content": { - "global": { - "content": [ - { - "actions": [ - "notify", - { - "set_tweak": "sound", - "value": "default" - }, - { - "set_tweak": "highlight" - } - ], - "default": true, - "enabled": true, - "pattern": "example", - "rule_id": ".m.rule.contains_user_name" - } - ], - "override": [ - { - "actions": [ - "dont_notify" - ], - "conditions": [], - "default": true, - "enabled": false, - "rule_id": ".m.rule.master" - }, - { - "actions": [ - "dont_notify" - ], - "conditions": [ - { - "key": "content.msgtype", - "kind": "event_match", - "pattern": "m.notice" - } - ], - "default": true, - "enabled": true, - "rule_id": ".m.rule.suppress_notices" - } - ], - "room": [ - { - "actions": [ - "notify", - { - "set_tweak": "sound", - "value": "default" - } - ], - "rule_id": *DEFAULT_TEST_ROOM_ID, - "default": false, - "enabled": true - } - ], - "sender": [], - "underride": [ - { - "actions": [ - "notify", - { - "set_tweak": "sound", - "value": "ring" - }, - { - "set_tweak": "highlight", - "value": false - } - ], - "conditions": [ - { - "key": "type", - "kind": "event_match", - "pattern": "m.call.invite" - } - ], - "default": true, - "enabled": true, - "rule_id": ".m.rule.call" - }, - { - "actions": [ - "notify", - { - "set_tweak": "sound", - "value": "default" - }, - { - "set_tweak": "highlight" - } - ], - "conditions": [ - { - "kind": "contains_display_name" - } - ], - "default": true, - "enabled": true, - "rule_id": ".m.rule.contains_display_name" - }, - { - "actions": [ - "notify", - { - "set_tweak": "sound", - "value": "default" - }, - { - "set_tweak": "highlight", - "value": false - } - ], - "conditions": [ - { - "is": "2", - "kind": "room_member_count" - }, - { - "key": "type", - "kind": "event_match", - "pattern": "m.room.message" - } - ], - "default": true, - "enabled": true, - "rule_id": ".m.rule.room_one_to_one" - }, - { - "actions": [ - "notify", - { - "set_tweak": "sound", - "value": "default" - }, - { - "set_tweak": "highlight", - "value": false - } - ], - "conditions": [ - { - "key": "type", - "kind": "event_match", - "pattern": "m.room.member" - }, - { - "key": "content.membership", - "kind": "event_match", - "pattern": "invite" - }, - { - "key": "state_key", - "kind": "event_match", - "pattern": "@example:localhost" - } - ], - "default": true, - "enabled": true, - "rule_id": ".m.rule.invite_for_me" - }, - { - "actions": [ - "notify", - { - "set_tweak": "highlight", - "value": false - } - ], - "conditions": [ - { - "key": "type", - "kind": "event_match", - "pattern": "m.room.member" - } - ], - "default": true, - "enabled": true, - "rule_id": ".m.rule.member_event" - }, - { - "actions": [ - "notify", - { - "set_tweak": "highlight", - "value": false - } - ], - "conditions": [ - { - "key": "type", - "kind": "event_match", - "pattern": "m.room.message" - } - ], - "default": true, - "enabled": true, - "rule_id": ".m.rule.message" - } - ] - } - }, - "type": "m.push_rules" - }) -}); - pub static REDACTED_INVALID: Lazy = Lazy::new(|| { json!({ "content": {},