Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds test for group members function for non group creator client #794

Merged
merged 4 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions xmtp_mls/src/groups/members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct GroupMember {
pub permission_level: PermissionLevel,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PermissionLevel {
Member,
Admin,
Expand Down Expand Up @@ -49,28 +49,25 @@ impl MlsGroup {
let members = association_state_map
.into_iter()
.map(|association_state| {
let is_admin = self
.is_admin(association_state.inbox_id().to_string())
.unwrap();
let is_super_admin = self
.is_super_admin(association_state.inbox_id().to_string())
.unwrap();
let inbox_id_str = association_state.inbox_id().to_string();
let is_admin = self.is_admin(inbox_id_str.clone())?;
neekolas marked this conversation as resolved.
Show resolved Hide resolved
let is_super_admin = self.is_super_admin(inbox_id_str.clone())?;
let permission_level = if is_super_admin {
PermissionLevel::SuperAdmin
} else if is_admin && !is_super_admin {
} else if is_admin {
PermissionLevel::Admin
} else {
PermissionLevel::Member
};

GroupMember {
inbox_id: association_state.inbox_id().to_string(),
Ok(GroupMember {
inbox_id: inbox_id_str,
account_addresses: association_state.account_addresses(),
installation_ids: association_state.installation_ids(),
permission_level,
}
})
})
.collect::<Vec<GroupMember>>();
.collect::<Result<Vec<GroupMember>, GroupError>>()?;

Ok(members)
}
Expand Down
56 changes: 53 additions & 3 deletions xmtp_mls/src/groups/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -925,9 +925,11 @@ mod tests {
builder::ClientBuilder,
codecs::{group_updated::GroupUpdatedCodec, ContentCodec},
groups::{
build_group_membership_extension, group_membership::GroupMembership,
group_mutable_metadata::MetadataField, members::PermissionLevel, PreconfiguredPolicies,
UpdateAdminListType,
build_group_membership_extension,
group_membership::GroupMembership,
group_mutable_metadata::MetadataField,
members::{GroupMember, PermissionLevel},
PreconfiguredPolicies, UpdateAdminListType,
},
storage::{
group_intent::IntentState,
Expand Down Expand Up @@ -1078,6 +1080,54 @@ mod tests {
assert_eq!(message.decrypted_message_bytes, bo_message);
}

// Test members function from non group creator
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
async fn test_members_func_from_non_creator() {
let amal = ClientBuilder::new_test_client(&generate_local_wallet()).await;
let bola = ClientBuilder::new_test_client(&generate_local_wallet()).await;

let amal_group = amal.create_group(None).unwrap();
amal_group
.add_members_by_inbox_id(&amal, vec![bola.inbox_id()])
.await
.unwrap();

// Get bola's version of the same group
let bola_groups = bola.sync_welcomes().await.unwrap();
let bola_group = bola_groups.first().unwrap();

// Call sync for both
amal_group.sync(&amal).await.unwrap();
bola_group.sync(&bola).await.unwrap();

// Verify bola can see the group name
let bola_group_name = bola_group.group_name().unwrap();
assert_eq!(bola_group_name, "New Group");

// Check if both clients can see the members correctly
let amal_members: Vec<GroupMember> = amal_group.members().unwrap();
let bola_members: Vec<GroupMember> = bola_group.members().unwrap();

assert_eq!(amal_members.len(), 2);
assert_eq!(bola_members.len(), 2);

for member in &amal_members {
if member.inbox_id == amal.inbox_id() {
assert_eq!(
member.permission_level,
PermissionLevel::SuperAdmin,
"Amal should be a super admin"
);
} else if member.inbox_id == bola.inbox_id() {
assert_eq!(
member.permission_level,
PermissionLevel::Member,
"Bola should be a member"
);
}
}
}

// Amal and Bola will both try and add Charlie from the same epoch.
// The group should resolve to a consistent state
#[tokio::test(flavor = "multi_thread", worker_threads = 1)]
Expand Down
Loading