mirror of
https://github.com/vacp2p/de-mls.git
synced 2026-01-10 07:38:20 -05:00
* chore(msg): refactor Waku message handling and user action processing - Updated `WakuMessageToSend` struct to streamline message creation and handling across the application. - Refactored user action handling to utilize the new `WakuMessageToSend` type, enhancing clarity and consistency. - Removed the `ds_waku` module and integrated its functionality directly into the main library structure. - Improved error handling and logging in user actions and Waku message processing. - Updated tests to reflect changes in message handling and user action processing, ensuring robustness and reliability. * refactor(user): rename and restructure user action messages for clarity user action flows.
352 lines
12 KiB
Rust
352 lines
12 KiB
Rust
use de_mls::{
|
|
user::{User, UserAction},
|
|
ws_actor::{RawWsMessage, UserMessage, WsAction},
|
|
};
|
|
|
|
#[tokio::test]
|
|
async fn test_invite_users_flow() {
|
|
let group_name = "new_group".to_string();
|
|
|
|
let alice_priv_key = "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d";
|
|
let mut alice = User::new(alice_priv_key).expect("Failed to create user");
|
|
alice
|
|
.create_group(group_name.clone(), true)
|
|
.await
|
|
.expect("Failed to create group");
|
|
|
|
let bob_priv_key = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
|
|
let mut bob = User::new(bob_priv_key).expect("Failed to create user");
|
|
bob.create_group(group_name.clone(), false)
|
|
.await
|
|
.expect("Failed to create group");
|
|
|
|
let carol_priv_key = "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a";
|
|
let mut carol = User::new(carol_priv_key).expect("Failed to create user");
|
|
carol
|
|
.create_group(group_name.clone(), false)
|
|
.await
|
|
.expect("Failed to create group");
|
|
|
|
let group_announcement = alice
|
|
.prepare_admin_msg(group_name.clone())
|
|
.await
|
|
.expect("Failed to prepare admin message");
|
|
let group_announcement_message = group_announcement
|
|
.build_waku_message()
|
|
.expect("Failed to build waku message");
|
|
|
|
let bob_action = bob
|
|
.handle_waku_message(group_announcement_message.clone())
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
let bob_kp_message = match bob_action {
|
|
UserAction::SendToWaku(msg) => msg,
|
|
_ => panic!("User action is not SendToWaku"),
|
|
};
|
|
let bob_kp_waku_message = bob_kp_message
|
|
.build_waku_message()
|
|
.expect("Failed to build waku message");
|
|
|
|
let _alice_action = alice
|
|
.handle_waku_message(bob_kp_waku_message)
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
|
|
let carol_action = carol
|
|
.handle_waku_message(group_announcement_message.clone())
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
let carol_kp_message = match carol_action {
|
|
UserAction::SendToWaku(msg) => msg,
|
|
_ => panic!("User action is not SendToWaku"),
|
|
};
|
|
let carol_kp_waku_message = carol_kp_message
|
|
.build_waku_message()
|
|
.expect("Failed to build waku message");
|
|
|
|
let _alice_action = alice
|
|
.handle_waku_message(carol_kp_waku_message)
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
|
|
let users_to_invite = alice
|
|
.get_processed_income_key_packages(group_name.clone())
|
|
.await
|
|
.expect("Failed to process income key packages");
|
|
assert!(users_to_invite.len() == 2, "Expected 2 users to invite");
|
|
|
|
let res = alice
|
|
.invite_users(users_to_invite, group_name.clone())
|
|
.await
|
|
.expect("Failed to invite users");
|
|
assert!(res.len() == 2, "Expected 2 messages to send");
|
|
|
|
let welcome_message = res[1]
|
|
.build_waku_message()
|
|
.expect("Failed to build waku message");
|
|
|
|
let _bob_action = bob
|
|
.handle_waku_message(welcome_message.clone())
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
|
|
let _carol_action = carol
|
|
.handle_waku_message(welcome_message.clone())
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
|
|
let carol_group = carol
|
|
.get_group(group_name.clone())
|
|
.expect("Failed to get group");
|
|
let carol_members = carol_group.members_identity().await;
|
|
assert!(
|
|
carol_members.len() == 3,
|
|
"Wrong number of members in the group for Carol"
|
|
);
|
|
let bob_group = bob
|
|
.get_group(group_name.clone())
|
|
.expect("Failed to get group");
|
|
let bob_members = bob_group.members_identity().await;
|
|
assert!(
|
|
bob_members.len() == 3,
|
|
"Wrong number of members in the group for Bob"
|
|
);
|
|
let alice_group = alice
|
|
.get_group(group_name.clone())
|
|
.expect("Failed to get group");
|
|
let alice_members = alice_group.members_identity().await;
|
|
assert!(
|
|
alice_members.len() == 3,
|
|
"Wrong number of members in the group for Alice"
|
|
);
|
|
|
|
assert_eq!(
|
|
alice_members, bob_members,
|
|
"Alice and Bob have different members"
|
|
);
|
|
assert_eq!(
|
|
alice_members, carol_members,
|
|
"Alice and Carol have different members"
|
|
);
|
|
assert_eq!(
|
|
bob_members, carol_members,
|
|
"Bob and Carol have different members"
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn test_remove_user_flow() {
|
|
let group_name = "new_group".to_string();
|
|
|
|
let alice_priv_key = "0x59c6995e998f97a5a0044966f0945389dc9e86dae88c7a8412f4603b6b78690d";
|
|
let mut alice = User::new(alice_priv_key).expect("Failed to create user");
|
|
alice
|
|
.create_group(group_name.clone(), true)
|
|
.await
|
|
.expect("Failed to create group");
|
|
|
|
let bob_priv_key = "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80";
|
|
let mut bob = User::new(bob_priv_key).expect("Failed to create user");
|
|
bob.create_group(group_name.clone(), false)
|
|
.await
|
|
.expect("Failed to create group");
|
|
|
|
let carol_priv_key = "0x5de4111afa1a4b94908f83103eb1f1706367c2e68ca870fc3fb9a804cdab365a";
|
|
let mut carol = User::new(carol_priv_key).expect("Failed to create user");
|
|
carol
|
|
.create_group(group_name.clone(), false)
|
|
.await
|
|
.expect("Failed to create group");
|
|
|
|
let group_announcement = alice
|
|
.prepare_admin_msg(group_name.clone())
|
|
.await
|
|
.expect("Failed to prepare admin message");
|
|
let group_announcement_message = group_announcement
|
|
.build_waku_message()
|
|
.expect("Failed to build waku message");
|
|
|
|
let bob_action = bob
|
|
.handle_waku_message(group_announcement_message.clone())
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
let bob_kp_message = match bob_action {
|
|
UserAction::SendToWaku(msg) => msg,
|
|
_ => panic!("User action is not SendToWaku"),
|
|
};
|
|
let bob_kp_waku_message = bob_kp_message
|
|
.build_waku_message()
|
|
.expect("Failed to build waku message");
|
|
|
|
let _alice_action = alice
|
|
.handle_waku_message(bob_kp_waku_message)
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
|
|
let carol_action = carol
|
|
.handle_waku_message(group_announcement_message.clone())
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
let carol_kp_message = match carol_action {
|
|
UserAction::SendToWaku(msg) => msg,
|
|
_ => panic!("User action is not SendToWaku"),
|
|
};
|
|
let carol_kp_waku_message = carol_kp_message
|
|
.build_waku_message()
|
|
.expect("Failed to build waku message");
|
|
|
|
let _alice_action = alice
|
|
.handle_waku_message(carol_kp_waku_message)
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
|
|
let users_to_invite = alice
|
|
.get_processed_income_key_packages(group_name.clone())
|
|
.await
|
|
.expect("Failed to process income key packages");
|
|
assert!(users_to_invite.len() == 2, "Expected 2 users to invite");
|
|
|
|
let res = alice
|
|
.invite_users(users_to_invite, group_name.clone())
|
|
.await
|
|
.expect("Failed to invite users");
|
|
assert!(res.len() == 2, "Expected 2 messages to send");
|
|
|
|
let welcome_message = res[1]
|
|
.build_waku_message()
|
|
.expect("Failed to build waku message");
|
|
|
|
let _bob_action = bob
|
|
.handle_waku_message(welcome_message.clone())
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
|
|
let _carol_action = carol
|
|
.handle_waku_message(welcome_message.clone())
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
|
|
let carol_group = carol
|
|
.get_group(group_name.clone())
|
|
.expect("Failed to get group");
|
|
let carol_members = carol_group.members_identity().await;
|
|
assert!(
|
|
carol_members.len() == 3,
|
|
"Wrong number of members in the group for Carol"
|
|
);
|
|
let bob_group = bob
|
|
.get_group(group_name.clone())
|
|
.expect("Failed to get group");
|
|
let bob_members = bob_group.members_identity().await;
|
|
assert!(
|
|
bob_members.len() == 3,
|
|
"Wrong number of members in the group for Bob"
|
|
);
|
|
let alice_group = alice
|
|
.get_group(group_name.clone())
|
|
.expect("Failed to get group");
|
|
let alice_members = alice_group.members_identity().await;
|
|
assert!(
|
|
alice_members.len() == 3,
|
|
"Wrong number of members in the group for Alice"
|
|
);
|
|
|
|
assert_eq!(
|
|
alice_members, bob_members,
|
|
"Alice and Bob have different members"
|
|
);
|
|
assert_eq!(
|
|
alice_members, carol_members,
|
|
"Alice and Carol have different members"
|
|
);
|
|
assert_eq!(
|
|
bob_members, carol_members,
|
|
"Bob and Carol have different members"
|
|
);
|
|
|
|
let raw_msg = RawWsMessage {
|
|
message: serde_json::to_string(&UserMessage {
|
|
message: "/ban f39fd6e51aad88f6f4ce6ab8827279cfffb92266".to_string(),
|
|
group_id: group_name.clone(),
|
|
})
|
|
.expect("Failed to serialize user message"),
|
|
};
|
|
|
|
let ws_action = match serde_json::from_str(&raw_msg.message) {
|
|
Ok(UserMessage { message, group_id }) => {
|
|
let ws_action = if message.starts_with("/") {
|
|
let mut tokens = message.split_whitespace();
|
|
let ws = match tokens.next() {
|
|
Some("/ban") => {
|
|
let user_to_ban = tokens.next().expect("Failed to get user to ban");
|
|
WsAction::RemoveUser(user_to_ban.to_string(), group_id.clone())
|
|
}
|
|
_ => {
|
|
assert!(false, "Invalid user message");
|
|
WsAction::DoNothing
|
|
}
|
|
};
|
|
ws
|
|
} else {
|
|
WsAction::UserMessage(UserMessage { message, group_id })
|
|
};
|
|
ws_action
|
|
}
|
|
Err(_) => {
|
|
assert!(false, "Failed to parse user message");
|
|
WsAction::DoNothing
|
|
}
|
|
};
|
|
assert_eq!(
|
|
ws_action,
|
|
WsAction::RemoveUser(
|
|
"f39fd6e51aad88f6f4ce6ab8827279cfffb92266".to_string(),
|
|
group_name.clone()
|
|
)
|
|
);
|
|
|
|
let pmt = match ws_action {
|
|
WsAction::RemoveUser(user_to_ban, group_name) => alice
|
|
.remove_group_users(vec![user_to_ban], group_name.clone())
|
|
.await
|
|
.expect("Failed to remove user from group"),
|
|
_ => panic!("User action is not RemoveUser"),
|
|
};
|
|
|
|
let waku_commit_message = pmt
|
|
.build_waku_message()
|
|
.expect("Failed to build waku message");
|
|
|
|
let _ = carol
|
|
.handle_waku_message(waku_commit_message.clone())
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
let carol_group = carol
|
|
.get_group(group_name.clone())
|
|
.expect("Failed to get group");
|
|
let carol_members = carol_group.members_identity().await;
|
|
assert!(
|
|
carol_members.len() == 2,
|
|
"Bob is not removed from the group"
|
|
);
|
|
|
|
let bob_action = bob
|
|
.handle_waku_message(waku_commit_message.clone())
|
|
.await
|
|
.expect("Failed to process waku message");
|
|
assert_eq!(
|
|
bob_action,
|
|
UserAction::RemoveGroup(group_name.clone()),
|
|
"User action is not RemoveGroup"
|
|
);
|
|
let _ = bob
|
|
.leave_group(group_name.clone())
|
|
.await
|
|
.expect("Failed to leave group");
|
|
assert_eq!(
|
|
bob.if_group_exists(group_name.clone()),
|
|
false,
|
|
"Bob is still in the group"
|
|
);
|
|
}
|