tests: add support for the global account PushRules event in the event factory

This commit is contained in:
Benjamin Bouvier
2025-08-21 12:44:42 +02:00
parent 9a3ceb8be6
commit e6b1ffba99
8 changed files with 64 additions and 247 deletions

View File

@@ -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::<Raw<AnyGlobalAccountDataEvent>>(pushrules_json.clone())?;
let pushrules_raw: Raw<AnyGlobalAccountDataEvent> =
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();

View File

@@ -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);

View File

@@ -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();

View File

@@ -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<PushRulesEventContent> {
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.

View File

@@ -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,
};

View File

@@ -149,14 +149,12 @@ impl From<PresenceTestEvent> for Raw<PresenceEvent> {
/// Test events that can be added to the global account data.
pub enum GlobalAccountDataTestEvent {
PushRules,
Custom(JsonValue),
}
impl From<GlobalAccountDataTestEvent> for JsonValue {
fn from(val: GlobalAccountDataTestEvent) -> Self {
match val {
GlobalAccountDataTestEvent::PushRules => test_json::sync_events::PUSH_RULES.to_owned(),
GlobalAccountDataTestEvent::Custom(json) => json,
}
}

View File

@@ -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,
};

View File

@@ -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<JsonValue> = Lazy::new(|| {
json!({
"content": {
@@ -392,218 +390,6 @@ pub static PRESENCE: Lazy<JsonValue> = Lazy::new(|| {
})
});
pub static PUSH_RULES: Lazy<JsonValue> = 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<JsonValue> = Lazy::new(|| {
json!({
"content": {},