Skip to content

Commit

Permalink
test: Update tests to use new UTD TimelineEventKind variant
Browse files Browse the repository at this point in the history
Make the tests behave the same way as the network code, by returning UTDs
as `TimelineEventKind::UnableToDecrypt` instead of `TimelineEventKind::PlainText`.
  • Loading branch information
richvdh committed Oct 16, 2024
1 parent 871bf4b commit d00c1a2
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 23 deletions.
11 changes: 9 additions & 2 deletions crates/matrix-sdk-base/src/sliding_sync/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,10 @@ mod tests {
};

use assert_matches::assert_matches;
use matrix_sdk_common::{deserialized_responses::SyncTimelineEvent, ring_buffer::RingBuffer};
use matrix_sdk_common::{
deserialized_responses::{SyncTimelineEvent, UnableToDecryptInfo, UnableToDecryptReason},
ring_buffer::RingBuffer,
};
use matrix_sdk_test::async_test;
use ruma::{
api::client::sync::sync_events::UnreadNotificationsCount,
Expand Down Expand Up @@ -2494,7 +2497,7 @@ mod tests {
}

fn make_encrypted_event(id: &str) -> SyncTimelineEvent {
SyncTimelineEvent::new(
SyncTimelineEvent::new_utd_event(
Raw::from_json_string(
json!({
"type": "m.room.encrypted",
Expand All @@ -2512,6 +2515,10 @@ mod tests {
.to_string(),
)
.unwrap(),
UnableToDecryptInfo {
session_id: Some("".to_owned()),
reason: UnableToDecryptReason::MissingMegolmSession,
},
)
}

Expand Down
55 changes: 36 additions & 19 deletions crates/matrix-sdk-ui/src/timeline/tests/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,13 @@ use matrix_sdk::{
crypto::{decrypt_room_key_export, types::events::UtdCause, OlmMachine},
test_utils::test_client_builder,
};
use matrix_sdk_base::deserialized_responses::{SyncTimelineEvent, UnableToDecryptReason};
use matrix_sdk_test::{async_test, BOB};
use ruma::{
assign,
events::{
room::encrypted::{
EncryptedEventScheme, MegolmV1AesSha2ContentInit, Relation, Replacement,
RoomEncryptedEventContent,
},
AnySyncTimelineEvent,
events::room::encrypted::{
EncryptedEventScheme, MegolmV1AesSha2ContentInit, Relation, Replacement,
RoomEncryptedEventContent,
},
room_id,
serde::Raw,
Expand Down Expand Up @@ -107,7 +105,8 @@ async fn test_retry_message_decryption() {
),
None,
))
.sender(&BOB),
.sender(&BOB)
.into_utd_sync_timeline_event(),
)
.await;

Expand Down Expand Up @@ -217,7 +216,11 @@ async fn test_retry_edit_decryption() {
.into(),
);
timeline
.handle_live_event(f.event(RoomEncryptedEventContent::new(encrypted, None)).sender(&BOB))
.handle_live_event(
f.event(RoomEncryptedEventContent::new(encrypted, None))
.sender(&BOB)
.into_utd_sync_timeline_event(),
)
.await;

let event_id =
Expand All @@ -244,7 +247,8 @@ async fn test_retry_edit_decryption() {
f.event(assign!(RoomEncryptedEventContent::new(encrypted, None), {
relates_to: Some(Relation::Replacement(Replacement::new(event_id))),
}))
.sender(&BOB),
.sender(&BOB)
.into_utd_sync_timeline_event(),
)
.await;

Expand Down Expand Up @@ -323,7 +327,8 @@ async fn test_retry_edit_and_more() {
mBZdKIaqDTUBFvcvbn2gQaWtUipQdJQRKyv2h0AWveVkv75lp5hRb7jolCi08oMX8cM+V3Zzyi7\
mlPAzZjDz0PaRbQwfbMTTHkcL7TZybBi4vLX4f5ZR2Iiysc7gw",
))
.sender(&BOB),
.sender(&BOB)
.into_utd_sync_timeline_event(),
)
.await;

Expand All @@ -339,7 +344,9 @@ async fn test_retry_edit_and_more() {
);
timeline
.handle_live_event(
f.event(assign!(msg2, { relates_to: Some(Relation::Replacement(Replacement::new(event_id))) })).sender(&BOB),
f.event(assign!(msg2, { relates_to: Some(Relation::Replacement(Replacement::new(event_id))) }))
.sender(&BOB)
.into_utd_sync_timeline_event(),
)
.await;

Expand All @@ -351,7 +358,8 @@ async fn test_retry_edit_and_more() {
2r/fEvAW/9QB+N6fX4g9729bt5ftXRqa5QI7NA351RNUveRHxVvx+2x0WJArQjYGRk7tMS2rUto\
IYt2ZY17nE1UJjN7M87STnCF9c9qy4aGNqIpeVIht6XbtgD7gQ",
))
.sender(&BOB),
.sender(&BOB)
.into_utd_sync_timeline_event(),
)
.await;

Expand Down Expand Up @@ -424,7 +432,8 @@ async fn test_retry_message_decryption_highlighted() {
),
None,
))
.sender(&BOB),
.sender(&BOB)
.into_utd_sync_timeline_event(),
)
.await;

Expand Down Expand Up @@ -475,7 +484,7 @@ async fn test_utd_cause_for_nonmember_event_is_found() {
let mut stream = timeline.subscribe().await;

// When we add an event with "membership: leave"
timeline.handle_live_event(raw_event_with_unsigned(json!({ "membership": "leave" }))).await;
timeline.handle_live_event(utd_event_with_unsigned(json!({ "membership": "leave" }))).await;

// Then its UTD cause is membership
let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
Expand All @@ -495,7 +504,7 @@ async fn test_utd_cause_for_nonmember_event_is_found_unstable_prefix() {

// When we add an event with "io.element.msc4115.membership: leave"
timeline
.handle_live_event(raw_event_with_unsigned(
.handle_live_event(utd_event_with_unsigned(
json!({ "io.element.msc4115.membership": "leave" }),
))
.await;
Expand All @@ -517,7 +526,7 @@ async fn test_utd_cause_for_member_event_is_unknown() {
let mut stream = timeline.subscribe().await;

// When we add an event with "membership: join"
timeline.handle_live_event(raw_event_with_unsigned(json!({ "membership": "join" }))).await;
timeline.handle_live_event(utd_event_with_unsigned(json!({ "membership": "join" }))).await;

// Then its UTD cause is membership
let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
Expand All @@ -536,7 +545,7 @@ async fn test_utd_cause_for_missing_membership_is_unknown() {
let mut stream = timeline.subscribe().await;

// When we add an event with no membership in unsigned
timeline.handle_live_event(raw_event_with_unsigned(json!({}))).await;
timeline.handle_live_event(utd_event_with_unsigned(json!({}))).await;

// Then its UTD cause is membership
let item = assert_next_matches!(stream, VectorDiff::PushBack { value } => value);
Expand All @@ -548,8 +557,8 @@ async fn test_utd_cause_for_missing_membership_is_unknown() {
assert_eq!(*cause, UtdCause::Unknown);
}

fn raw_event_with_unsigned(unsigned: serde_json::Value) -> Raw<AnySyncTimelineEvent> {
Raw::from_json(
fn utd_event_with_unsigned(unsigned: serde_json::Value) -> SyncTimelineEvent {
let raw = Raw::from_json(
to_raw_value(&json!({
"event_id": "$myevent",
"sender": "@u:s",
Expand All @@ -566,5 +575,13 @@ fn raw_event_with_unsigned(unsigned: serde_json::Value) -> Raw<AnySyncTimelineEv

}))
.unwrap(),
);

SyncTimelineEvent::new_utd_event(
raw,
matrix_sdk::deserialized_responses::UnableToDecryptInfo {
session_id: Some("SESSION_ID".into()),
reason: UnableToDecryptReason::MissingMegolmSession,
},
)
}
3 changes: 2 additions & 1 deletion crates/matrix-sdk-ui/src/timeline/tests/read_receipts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,8 @@ async fn test_read_receipts_updates_on_message_decryption() {
),
None,
))
.sender(&BOB),
.sender(&BOB)
.into_utd_sync_timeline_event(),
)
.await;

Expand Down
3 changes: 2 additions & 1 deletion crates/matrix-sdk-ui/src/timeline/tests/shields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ async fn test_utd_shield() {
),
None,
))
.sender(&ALICE),
.sender(&ALICE)
.into_utd_sync_timeline_event(),
)
.await;

Expand Down
20 changes: 20 additions & 0 deletions crates/matrix-sdk/src/test_utils/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

use std::sync::atomic::{AtomicU64, Ordering::SeqCst};

use as_variant::as_variant;
use matrix_sdk_base::deserialized_responses::{SyncTimelineEvent, TimelineEvent};
use matrix_sdk_common::deserialized_responses::UnableToDecryptReason;
use ruma::{
events::{
message::TextContentBlock,
Expand All @@ -31,6 +33,7 @@ use ruma::{
reaction::ReactionEventContent,
relation::{Annotation, InReplyTo, Replacement, Thread},
room::{
encrypted::{EncryptedEventScheme, RoomEncryptedEventContent},
message::{Relation, RoomMessageEventContent, RoomMessageEventContentWithoutRelation},
redaction::RoomRedactionEventContent,
},
Expand Down Expand Up @@ -185,6 +188,23 @@ where
}
}

impl EventBuilder<RoomEncryptedEventContent> {
/// Turn this event into a SyncTimelineEvent representing a decryption
/// failure
pub fn into_utd_sync_timeline_event(self) -> SyncTimelineEvent {
let session_id = as_variant!(&self.content.scheme, EncryptedEventScheme::MegolmV1AesSha2)
.map(|content| content.session_id.clone());

SyncTimelineEvent::new_utd_event(
self.into(),
crate::deserialized_responses::UnableToDecryptInfo {
session_id,
reason: UnableToDecryptReason::MissingMegolmSession,
},
)
}
}

impl EventBuilder<RoomMessageEventContent> {
/// Adds a reply relation to the current event.
pub fn reply_to(mut self, event_id: &EventId) -> Self {
Expand Down

0 comments on commit d00c1a2

Please sign in to comment.