mirror of
https://github.com/chatmail/core.git
synced 2026-05-12 11:26:29 +03:00
test: Split test_sync_alter_chat() into smaller tests
This commit is contained in:
207
src/chat.rs
207
src/chat.rs
@@ -6880,96 +6880,113 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn test_sync_alter_chat() -> Result<()> {
|
async fn test_sync_blocked() -> Result<()> {
|
||||||
let alices = [
|
let alice0 = &TestContext::new_alice().await;
|
||||||
TestContext::new_alice().await,
|
let alice1 = &TestContext::new_alice().await;
|
||||||
TestContext::new_alice().await,
|
for a in [alice0, alice1] {
|
||||||
];
|
|
||||||
for a in &alices {
|
|
||||||
a.set_config_bool(Config::SyncMsgs, true).await?;
|
a.set_config_bool(Config::SyncMsgs, true).await?;
|
||||||
}
|
}
|
||||||
let bob = TestContext::new_bob().await;
|
let bob = TestContext::new_bob().await;
|
||||||
|
|
||||||
let ba_chat = bob.create_chat(&alices[0]).await;
|
let ba_chat = bob.create_chat(alice0).await;
|
||||||
let sent_msg = bob.send_text(ba_chat.id, "hi").await;
|
let sent_msg = bob.send_text(ba_chat.id, "hi").await;
|
||||||
let a0b_chat_id = alices[0].recv_msg(&sent_msg).await.chat_id;
|
let a0b_chat_id = alice0.recv_msg(&sent_msg).await.chat_id;
|
||||||
alices[1].recv_msg(&sent_msg).await;
|
alice1.recv_msg(&sent_msg).await;
|
||||||
let ab_contact_ids = [
|
let a0b_contact_id = alice0.add_or_lookup_contact(&bob).await.id;
|
||||||
alices[0].add_or_lookup_contact(&bob).await.id,
|
|
||||||
alices[1].add_or_lookup_contact(&bob).await.id,
|
|
||||||
];
|
|
||||||
|
|
||||||
assert_eq!(alices[1].get_chat(&bob).await.blocked, Blocked::Request);
|
assert_eq!(alice1.get_chat(&bob).await.blocked, Blocked::Request);
|
||||||
a0b_chat_id.accept(&alices[0]).await?;
|
a0b_chat_id.accept(alice0).await?;
|
||||||
sync(&alices[0], &alices[1]).await;
|
sync(alice0, alice1).await;
|
||||||
assert_eq!(alices[1].get_chat(&bob).await.blocked, Blocked::Not);
|
assert_eq!(alice1.get_chat(&bob).await.blocked, Blocked::Not);
|
||||||
a0b_chat_id.block(&alices[0]).await?;
|
a0b_chat_id.block(alice0).await?;
|
||||||
sync(&alices[0], &alices[1]).await;
|
sync(alice0, alice1).await;
|
||||||
assert_eq!(alices[1].get_chat(&bob).await.blocked, Blocked::Yes);
|
assert_eq!(alice1.get_chat(&bob).await.blocked, Blocked::Yes);
|
||||||
a0b_chat_id.unblock(&alices[0]).await?;
|
a0b_chat_id.unblock(alice0).await?;
|
||||||
sync(&alices[0], &alices[1]).await;
|
sync(alice0, alice1).await;
|
||||||
assert_eq!(alices[1].get_chat(&bob).await.blocked, Blocked::Not);
|
assert_eq!(alice1.get_chat(&bob).await.blocked, Blocked::Not);
|
||||||
|
|
||||||
// Unblocking a 1:1 chat doesn't unblock the contact currently.
|
// Unblocking a 1:1 chat doesn't unblock the contact currently.
|
||||||
Contact::unblock(&alices[0], ab_contact_ids[0]).await?;
|
Contact::unblock(alice0, a0b_contact_id).await?;
|
||||||
|
|
||||||
assert!(!alices[1].add_or_lookup_contact(&bob).await.is_blocked());
|
assert!(!alice1.add_or_lookup_contact(&bob).await.is_blocked());
|
||||||
Contact::block(&alices[0], ab_contact_ids[0]).await?;
|
Contact::block(alice0, a0b_contact_id).await?;
|
||||||
sync(&alices[0], &alices[1]).await;
|
sync(alice0, alice1).await;
|
||||||
assert!(alices[1].add_or_lookup_contact(&bob).await.is_blocked());
|
assert!(alice1.add_or_lookup_contact(&bob).await.is_blocked());
|
||||||
Contact::unblock(&alices[0], ab_contact_ids[0]).await?;
|
Contact::unblock(alice0, a0b_contact_id).await?;
|
||||||
sync(&alices[0], &alices[1]).await;
|
sync(alice0, alice1).await;
|
||||||
assert!(!alices[1].add_or_lookup_contact(&bob).await.is_blocked());
|
assert!(!alice1.add_or_lookup_contact(&bob).await.is_blocked());
|
||||||
|
|
||||||
// Test accepting and blocking groups. This way we test:
|
// Test accepting and blocking groups. This way we test:
|
||||||
// - Group chats synchronisation.
|
// - Group chats synchronisation.
|
||||||
// - That blocking a group deletes it on other devices.
|
// - That blocking a group deletes it on other devices.
|
||||||
let fiona = TestContext::new_fiona().await;
|
let fiona = TestContext::new_fiona().await;
|
||||||
let fiona_grp_chat_id = fiona
|
let fiona_grp_chat_id = fiona
|
||||||
.create_group_with_members(ProtectionStatus::Unprotected, "grp", &[&alices[0]])
|
.create_group_with_members(ProtectionStatus::Unprotected, "grp", &[alice0])
|
||||||
.await;
|
.await;
|
||||||
let sent_msg = fiona.send_text(fiona_grp_chat_id, "hi").await;
|
let sent_msg = fiona.send_text(fiona_grp_chat_id, "hi").await;
|
||||||
let a0_grp_chat_id = alices[0].recv_msg(&sent_msg).await.chat_id;
|
let a0_grp_chat_id = alice0.recv_msg(&sent_msg).await.chat_id;
|
||||||
let a1_grp_chat_id = alices[1].recv_msg(&sent_msg).await.chat_id;
|
let a1_grp_chat_id = alice1.recv_msg(&sent_msg).await.chat_id;
|
||||||
let a1_grp_chat = Chat::load_from_db(&alices[1], a1_grp_chat_id).await?;
|
let a1_grp_chat = Chat::load_from_db(alice1, a1_grp_chat_id).await?;
|
||||||
assert_eq!(a1_grp_chat.blocked, Blocked::Request);
|
assert_eq!(a1_grp_chat.blocked, Blocked::Request);
|
||||||
a0_grp_chat_id.accept(&alices[0]).await?;
|
a0_grp_chat_id.accept(alice0).await?;
|
||||||
sync(&alices[0], &alices[1]).await;
|
sync(alice0, alice1).await;
|
||||||
let a1_grp_chat = Chat::load_from_db(&alices[1], a1_grp_chat_id).await?;
|
let a1_grp_chat = Chat::load_from_db(alice1, a1_grp_chat_id).await?;
|
||||||
assert_eq!(a1_grp_chat.blocked, Blocked::Not);
|
assert_eq!(a1_grp_chat.blocked, Blocked::Not);
|
||||||
a0_grp_chat_id.block(&alices[0]).await?;
|
a0_grp_chat_id.block(alice0).await?;
|
||||||
sync(&alices[0], &alices[1]).await;
|
sync(alice0, alice1).await;
|
||||||
assert!(Chat::load_from_db(&alices[1], a1_grp_chat_id)
|
assert!(Chat::load_from_db(alice1, a1_grp_chat_id).await.is_err());
|
||||||
.await
|
|
||||||
.is_err());
|
|
||||||
assert!(
|
assert!(
|
||||||
!alices[1]
|
!alice1
|
||||||
.sql
|
.sql
|
||||||
.exists("SELECT COUNT(*) FROM chats WHERE id=?", (a1_grp_chat_id,))
|
.exists("SELECT COUNT(*) FROM chats WHERE id=?", (a1_grp_chat_id,))
|
||||||
.await?
|
.await?
|
||||||
);
|
);
|
||||||
|
|
||||||
// Test syncing of chat visibility on a self-chat. This way we test:
|
Ok(())
|
||||||
// - Self-chat synchronisation.
|
}
|
||||||
// - That sync messages don't unarchive the self-chat.
|
|
||||||
let a0self_chat_id = alices[0].get_self_chat().await.id;
|
/// Tests syncing of chat visibility on a self-chat. This way we test:
|
||||||
|
/// - Self-chat synchronisation.
|
||||||
|
/// - That sync messages don't unarchive the self-chat.
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn test_sync_visibility() -> Result<()> {
|
||||||
|
let alice0 = &TestContext::new_alice().await;
|
||||||
|
let alice1 = &TestContext::new_alice().await;
|
||||||
|
for a in [alice0, alice1] {
|
||||||
|
a.set_config_bool(Config::SyncMsgs, true).await?;
|
||||||
|
}
|
||||||
|
let a0self_chat_id = alice0.get_self_chat().await.id;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
alices[1].get_self_chat().await.get_visibility(),
|
alice1.get_self_chat().await.get_visibility(),
|
||||||
ChatVisibility::Normal
|
ChatVisibility::Normal
|
||||||
);
|
);
|
||||||
let mut visibilities =
|
let mut visibilities =
|
||||||
ChatVisibility::iter().chain(std::iter::once(ChatVisibility::Normal));
|
ChatVisibility::iter().chain(std::iter::once(ChatVisibility::Normal));
|
||||||
visibilities.next();
|
visibilities.next();
|
||||||
for v in visibilities {
|
for v in visibilities {
|
||||||
a0self_chat_id.set_visibility(&alices[0], v).await?;
|
a0self_chat_id.set_visibility(alice0, v).await?;
|
||||||
sync(&alices[0], &alices[1]).await;
|
sync(alice0, alice1).await;
|
||||||
for a in &alices {
|
for a in [alice0, alice1] {
|
||||||
assert_eq!(a.get_self_chat().await.get_visibility(), v);
|
assert_eq!(a.get_self_chat().await.get_visibility(), v);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn test_sync_muted() -> Result<()> {
|
||||||
|
let alice0 = &TestContext::new_alice().await;
|
||||||
|
let alice1 = &TestContext::new_alice().await;
|
||||||
|
for a in [alice0, alice1] {
|
||||||
|
a.set_config_bool(Config::SyncMsgs, true).await?;
|
||||||
|
}
|
||||||
|
let bob = TestContext::new_bob().await;
|
||||||
|
let a0b_chat_id = alice0.create_chat(&bob).await.id;
|
||||||
|
alice1.create_chat(&bob).await;
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
alices[1].get_chat(&bob).await.mute_duration,
|
alice1.get_chat(&bob).await.mute_duration,
|
||||||
MuteDuration::NotMuted
|
MuteDuration::NotMuted
|
||||||
);
|
);
|
||||||
let mute_durations = [
|
let mute_durations = [
|
||||||
@@ -6978,8 +6995,8 @@ mod tests {
|
|||||||
MuteDuration::NotMuted,
|
MuteDuration::NotMuted,
|
||||||
];
|
];
|
||||||
for m in mute_durations {
|
for m in mute_durations {
|
||||||
set_muted(&alices[0], a0b_chat_id, m).await?;
|
set_muted(alice0, a0b_chat_id, m).await?;
|
||||||
sync(&alices[0], &alices[1]).await;
|
sync(alice0, alice1).await;
|
||||||
let m = match m {
|
let m = match m {
|
||||||
MuteDuration::Until(time) => MuteDuration::Until(
|
MuteDuration::Until(time) => MuteDuration::Until(
|
||||||
SystemTime::UNIX_EPOCH
|
SystemTime::UNIX_EPOCH
|
||||||
@@ -6989,42 +7006,76 @@ mod tests {
|
|||||||
),
|
),
|
||||||
_ => m,
|
_ => m,
|
||||||
};
|
};
|
||||||
assert_eq!(alices[1].get_chat(&bob).await.mute_duration, m);
|
assert_eq!(alice1.get_chat(&bob).await.mute_duration, m);
|
||||||
}
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
let a0_broadcast_id = create_broadcast_list(&alices[0]).await?;
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
sync(&alices[0], &alices[1]).await;
|
async fn test_sync_broadcast() -> Result<()> {
|
||||||
let a0_broadcast_chat = Chat::load_from_db(&alices[0], a0_broadcast_id).await?;
|
let alice0 = &TestContext::new_alice().await;
|
||||||
set_chat_name(&alices[0], a0_broadcast_id, "Broadcast list 42").await?;
|
let alice1 = &TestContext::new_alice().await;
|
||||||
sync(&alices[0], &alices[1]).await;
|
for a in [alice0, alice1] {
|
||||||
let a1_broadcast_id = get_chat_id_by_grpid(&alices[1], &a0_broadcast_chat.grpid)
|
a.set_config_bool(Config::SyncMsgs, true).await?;
|
||||||
|
}
|
||||||
|
let bob = TestContext::new_bob().await;
|
||||||
|
let a0b_contact_id = alice0.add_or_lookup_contact(&bob).await.id;
|
||||||
|
|
||||||
|
let a0_broadcast_id = create_broadcast_list(alice0).await?;
|
||||||
|
sync(alice0, alice1).await;
|
||||||
|
let a0_broadcast_chat = Chat::load_from_db(alice0, a0_broadcast_id).await?;
|
||||||
|
let a1_broadcast_id = get_chat_id_by_grpid(alice1, &a0_broadcast_chat.grpid)
|
||||||
.await?
|
.await?
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.0;
|
.0;
|
||||||
let a1_broadcast_chat = Chat::load_from_db(&alices[1], a1_broadcast_id).await?;
|
let a1_broadcast_chat = Chat::load_from_db(alice1, a1_broadcast_id).await?;
|
||||||
assert_eq!(a1_broadcast_chat.get_type(), Chattype::Broadcast);
|
assert_eq!(a1_broadcast_chat.get_type(), Chattype::Broadcast);
|
||||||
assert_eq!(a1_broadcast_chat.get_name(), "Broadcast list 42");
|
assert_eq!(a1_broadcast_chat.get_name(), a0_broadcast_chat.get_name());
|
||||||
assert!(get_chat_contacts(&alices[1], a1_broadcast_id)
|
assert!(get_chat_contacts(alice1, a1_broadcast_id).await?.is_empty());
|
||||||
.await?
|
add_contact_to_chat(alice0, a0_broadcast_id, a0b_contact_id).await?;
|
||||||
.is_empty());
|
sync(alice0, alice1).await;
|
||||||
add_contact_to_chat(&alices[0], a0_broadcast_id, ab_contact_ids[0]).await?;
|
let a1b_contact_id = Contact::lookup_id_by_addr(
|
||||||
sync(&alices[0], &alices[1]).await;
|
alice1,
|
||||||
|
&bob.get_config(Config::Addr).await?.unwrap(),
|
||||||
|
Origin::Hidden,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
.unwrap();
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
get_chat_contacts(&alices[1], a1_broadcast_id).await?,
|
get_chat_contacts(alice1, a1_broadcast_id).await?,
|
||||||
vec![ab_contact_ids[1]]
|
vec![a1b_contact_id]
|
||||||
);
|
);
|
||||||
let sent_msg = alices[1].send_text(a1_broadcast_id, "hi").await;
|
let sent_msg = alice1.send_text(a1_broadcast_id, "hi").await;
|
||||||
let msg = bob.recv_msg(&sent_msg).await;
|
let msg = bob.recv_msg(&sent_msg).await;
|
||||||
let chat = Chat::load_from_db(&bob, msg.chat_id).await?;
|
let chat = Chat::load_from_db(&bob, msg.chat_id).await?;
|
||||||
assert_eq!(chat.get_type(), Chattype::Mailinglist);
|
assert_eq!(chat.get_type(), Chattype::Mailinglist);
|
||||||
let msg = alices[0].recv_msg(&sent_msg).await;
|
let msg = alice0.recv_msg(&sent_msg).await;
|
||||||
assert_eq!(msg.chat_id, a0_broadcast_id);
|
assert_eq!(msg.chat_id, a0_broadcast_id);
|
||||||
remove_contact_from_chat(&alices[0], a0_broadcast_id, ab_contact_ids[0]).await?;
|
remove_contact_from_chat(alice0, a0_broadcast_id, a0b_contact_id).await?;
|
||||||
sync(&alices[0], &alices[1]).await;
|
sync(alice0, alice1).await;
|
||||||
assert!(get_chat_contacts(&alices[1], a1_broadcast_id)
|
assert!(get_chat_contacts(alice1, a1_broadcast_id).await?.is_empty());
|
||||||
.await?
|
Ok(())
|
||||||
.is_empty());
|
}
|
||||||
|
|
||||||
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
|
async fn test_sync_name() -> Result<()> {
|
||||||
|
let alice0 = &TestContext::new_alice().await;
|
||||||
|
let alice1 = &TestContext::new_alice().await;
|
||||||
|
for a in [alice0, alice1] {
|
||||||
|
a.set_config_bool(Config::SyncMsgs, true).await?;
|
||||||
|
}
|
||||||
|
let a0_broadcast_id = create_broadcast_list(alice0).await?;
|
||||||
|
sync(alice0, alice1).await;
|
||||||
|
let a0_broadcast_chat = Chat::load_from_db(alice0, a0_broadcast_id).await?;
|
||||||
|
set_chat_name(alice0, a0_broadcast_id, "Broadcast list 42").await?;
|
||||||
|
sync(alice0, alice1).await;
|
||||||
|
let a1_broadcast_id = get_chat_id_by_grpid(alice1, &a0_broadcast_chat.grpid)
|
||||||
|
.await?
|
||||||
|
.unwrap()
|
||||||
|
.0;
|
||||||
|
let a1_broadcast_chat = Chat::load_from_db(alice1, a1_broadcast_id).await?;
|
||||||
|
assert_eq!(a1_broadcast_chat.get_type(), Chattype::Broadcast);
|
||||||
|
assert_eq!(a1_broadcast_chat.get_name(), "Broadcast list 42");
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user