Skip to content

Commit

Permalink
add a test for it
Browse files Browse the repository at this point in the history
  • Loading branch information
nplasterer committed Jan 6, 2025
1 parent a11ea43 commit a8a0847
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 12 deletions.
76 changes: 65 additions & 11 deletions xmtp_mls/src/storage/encrypted_store/conversation_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ impl DbConnection {
include_sync_groups,
include_duplicate_dms,
} = args.as_ref();
let mut query = conversation_list

let mut query = conversation_list_dsl::conversation_list
.select(conversation_list::all_columns())
.filter(conversation_list_dsl::conversation_type.ne(ConversationType::Sync))
.into_boxed();

if !include_duplicate_dms {
// Group by dm_id and grab the latest group (conversation stitching)
query = query.filter(sql::<diesel::sql_types::Bool>(
"id IN (
SELECT id
Expand Down Expand Up @@ -115,10 +115,9 @@ impl DbConnection {
if *consent_state == ConsentState::Unknown {
let query = query
.left_join(
consent_dsl::consent_records.on(sql::<diesel::sql_types::Text>(
"lower(hex(conversation_list_dsl::id))",
)
.eq(consent_dsl::entity)),
consent_dsl::consent_records
.on(sql::<diesel::sql_types::Text>("lower(hex(conversation_list.id))")
.eq(consent_dsl::entity)),
)
.filter(
consent_dsl::state
Expand All @@ -133,7 +132,7 @@ impl DbConnection {
let query = query
.inner_join(
consent_dsl::consent_records
.on(sql::<diesel::sql_types::Text>("lower(hex(conversation_list_dsl::id))")
.on(sql::<diesel::sql_types::Text>("lower(hex(conversation_list.id))")
.eq(consent_dsl::entity)),
)
.filter(consent_dsl::state.eq(*consent_state))
Expand All @@ -146,8 +145,6 @@ impl DbConnection {
self.raw_query(|conn| query.load::<ConversationListItem>(conn))?
};

// Were sync groups explicitly asked for? Was the include_sync_groups flag set to true?
// Then query for those separately
if matches!(conversation_type, Some(ConversationType::Sync)) || *include_sync_groups {
let query = conversation_list_dsl::conversation_list
.filter(conversation_list_dsl::conversation_type.eq(ConversationType::Sync));
Expand All @@ -157,12 +154,14 @@ impl DbConnection {

Ok(conversations)
}

}

#[cfg(test)]
pub(crate) mod tests {
use crate::storage::group::tests::{generate_group, generate_group_with_created_at};
use crate::storage::group::GroupQueryArgs;
use crate::storage::consent_record::{ConsentState, ConsentType};
use crate::storage::group::tests::{generate_consent_record, generate_dm, generate_group, generate_group_with_created_at};
use crate::storage::group::{GroupMembershipState, GroupQueryArgs};
use crate::storage::tests::with_connection;
use crate::Store;
use wasm_bindgen_test::wasm_bindgen_test;
Expand Down Expand Up @@ -294,4 +293,59 @@ pub(crate) mod tests {
})
.await
}

#[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
#[cfg_attr(not(target_arch = "wasm32"), tokio::test)]
async fn test_find_groups_by_consent_state() {
with_connection(|conn| {
let test_group_1 = generate_group(Some(GroupMembershipState::Allowed));
test_group_1.store(conn).unwrap();
let test_group_2 = generate_group(Some(GroupMembershipState::Allowed));
test_group_2.store(conn).unwrap();
let test_group_3 = generate_dm(Some(GroupMembershipState::Allowed));
test_group_3.store(conn).unwrap();
let test_group_4 = generate_dm(Some(GroupMembershipState::Allowed));
test_group_4.store(conn).unwrap();

let test_group_1_consent = generate_consent_record(
ConsentType::ConversationId,
ConsentState::Allowed,
hex::encode(test_group_1.id.clone()),
);
test_group_1_consent.store(conn).unwrap();
let test_group_2_consent = generate_consent_record(
ConsentType::ConversationId,
ConsentState::Denied,
hex::encode(test_group_2.id.clone()),
);
test_group_2_consent.store(conn).unwrap();
let test_group_3_consent = generate_consent_record(
ConsentType::ConversationId,
ConsentState::Allowed,
hex::encode(test_group_3.id.clone()),
);
test_group_3_consent.store(conn).unwrap();

let all_results = conn.fetch_conversation_list(GroupQueryArgs::default()).unwrap();
assert_eq!(all_results.len(), 4);

let allowed_results = conn
.fetch_conversation_list(GroupQueryArgs::default().consent_state(ConsentState::Allowed))
.unwrap();
assert_eq!(allowed_results.len(), 2);

let denied_results = conn
.fetch_conversation_list(GroupQueryArgs::default().consent_state(ConsentState::Denied))
.unwrap();
assert_eq!(denied_results.len(), 1);
assert_eq!(denied_results[0].id, test_group_2.id);

let unknown_results = conn
.fetch_conversation_list(GroupQueryArgs::default().consent_state(ConsentState::Unknown))
.unwrap();
assert_eq!(unknown_results.len(), 1);
assert_eq!(unknown_results[0].id, test_group_4.id);
})
.await
}
}
2 changes: 1 addition & 1 deletion xmtp_mls/src/storage/encrypted_store/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ pub(crate) mod tests {
}

/// Generate a test consent
fn generate_consent_record(
pub fn generate_consent_record(
entity_type: ConsentType,
state: ConsentState,
entity: String,
Expand Down

0 comments on commit a8a0847

Please sign in to comment.