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 17, 2024
1 parent 995ff1e commit d838ed5
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 14 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
39 changes: 29 additions & 10 deletions crates/matrix-sdk-ui/src/timeline/tests/encryption.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use matrix_sdk::{
crypto::{decrypt_room_key_export, types::events::UtdCause, OlmMachine},
test_utils::test_client_builder,
};
use matrix_sdk_base::deserialized_responses::SyncTimelineEvent;
use matrix_sdk_base::deserialized_responses::{SyncTimelineEvent, UnableToDecryptReason};
use matrix_sdk_test::{async_test, BOB};
use ruma::{
assign,
Expand Down Expand Up @@ -105,7 +105,8 @@ async fn test_retry_message_decryption() {
),
None,
))
.sender(&BOB),
.sender(&BOB)
.into_utd_sync_timeline_event(),
)
.await;

Expand Down Expand Up @@ -215,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 @@ -242,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 @@ -321,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 @@ -337,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 @@ -349,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 @@ -422,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 @@ -547,7 +558,7 @@ async fn test_utd_cause_for_missing_membership_is_unknown() {
}

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

}))
.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 d838ed5

Please sign in to comment.