mirror of
https://github.com/chatmail/core.git
synced 2026-04-28 02:46:29 +03:00
Create and use some test helper functions
This commit is contained in:
32
src/chat.rs
32
src/chat.rs
@@ -2976,11 +2976,7 @@ mod tests {
|
||||
#[async_std::test]
|
||||
async fn test_chat_info() {
|
||||
let t = TestContext::new().await;
|
||||
let bob = Contact::create(&t.ctx, "bob", "bob@example.com")
|
||||
.await
|
||||
.unwrap();
|
||||
let chat_id = create_by_contact_id(&t.ctx, bob).await.unwrap();
|
||||
let chat = Chat::load_from_db(&t.ctx, chat_id).await.unwrap();
|
||||
let chat = t.chat_with_contact("bob", "bob@example.com").await;
|
||||
let info = chat.get_info(&t.ctx).await.unwrap();
|
||||
|
||||
// Ensure we can serialize this.
|
||||
@@ -3011,10 +3007,8 @@ mod tests {
|
||||
#[async_std::test]
|
||||
async fn test_get_draft_no_draft() {
|
||||
let t = TestContext::new().await;
|
||||
let chat_id = create_by_contact_id(&t.ctx, DC_CONTACT_ID_SELF)
|
||||
.await
|
||||
.unwrap();
|
||||
let draft = chat_id.get_draft(&t.ctx).await.unwrap();
|
||||
let chat = t.get_self_chat().await;
|
||||
let draft = chat.id.get_draft(&t.ctx).await.unwrap();
|
||||
assert!(draft.is_none());
|
||||
}
|
||||
|
||||
@@ -3040,9 +3034,7 @@ mod tests {
|
||||
#[async_std::test]
|
||||
async fn test_get_draft() {
|
||||
let t = TestContext::new().await;
|
||||
let chat_id = create_by_contact_id(&t.ctx, DC_CONTACT_ID_SELF)
|
||||
.await
|
||||
.unwrap();
|
||||
let chat_id = &t.get_self_chat().await.id;
|
||||
let mut msg = Message::new(Viewtype::Text);
|
||||
msg.set_text(Some("hello".to_string()));
|
||||
chat_id.set_draft(&t.ctx, Some(&mut msg)).await;
|
||||
@@ -3068,13 +3060,9 @@ mod tests {
|
||||
#[async_std::test]
|
||||
async fn test_self_talk() {
|
||||
let t = TestContext::new().await;
|
||||
let chat_id = create_by_contact_id(&t.ctx, DC_CONTACT_ID_SELF)
|
||||
.await
|
||||
.unwrap();
|
||||
let chat = &t.get_self_chat().await;
|
||||
assert_eq!(DC_CONTACT_ID_SELF, 1);
|
||||
assert!(!chat_id.is_special());
|
||||
let chat = Chat::load_from_db(&t.ctx, chat_id).await.unwrap();
|
||||
assert_eq!(chat.id, chat_id);
|
||||
assert!(!chat.id.is_special());
|
||||
assert!(chat.is_self_talk());
|
||||
assert!(chat.visibility == ChatVisibility::Normal);
|
||||
assert!(!chat.is_device_talk());
|
||||
@@ -3321,9 +3309,7 @@ mod tests {
|
||||
.await
|
||||
.unwrap()
|
||||
.chat_id;
|
||||
let chat_id2 = create_by_contact_id(&t.ctx, DC_CONTACT_ID_SELF)
|
||||
.await
|
||||
.unwrap();
|
||||
let chat_id2 = t.get_self_chat().await.id;
|
||||
assert!(!chat_id1.is_special());
|
||||
assert!(!chat_id2.is_special());
|
||||
assert_eq!(get_chat_cnt(&t.ctx).await, 2);
|
||||
@@ -3438,9 +3424,7 @@ mod tests {
|
||||
.unwrap()
|
||||
.chat_id;
|
||||
async_std::task::sleep(std::time::Duration::from_millis(1000)).await;
|
||||
let chat_id2 = create_by_contact_id(&t.ctx, DC_CONTACT_ID_SELF)
|
||||
.await
|
||||
.unwrap();
|
||||
let chat_id2 = t.get_self_chat().await.id;
|
||||
async_std::task::sleep(std::time::Duration::from_millis(1000)).await;
|
||||
let chat_id3 = create_group_chat(&t.ctx, ProtectionStatus::Unprotected, "foo")
|
||||
.await
|
||||
|
||||
@@ -2164,7 +2164,6 @@ mod tests {
|
||||
// create alice's account
|
||||
let t = TestContext::new_alice().await;
|
||||
|
||||
// create one-to-one with bob, archive one-to-one
|
||||
let bob_id = Contact::create(&t.ctx, "bob", "bob@example.com")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
20
src/e2ee.rs
20
src/e2ee.rs
@@ -346,7 +346,6 @@ mod tests {
|
||||
|
||||
use crate::chat;
|
||||
use crate::constants::Viewtype;
|
||||
use crate::contact::{Contact, Origin};
|
||||
use crate::message::Message;
|
||||
use crate::param::Param;
|
||||
use crate::test_utils::*;
|
||||
@@ -415,23 +414,8 @@ Sent with my Delta Chat Messenger: https://delta.chat";
|
||||
let alice = TestContext::new_alice().await;
|
||||
let bob = TestContext::new_bob().await;
|
||||
|
||||
let (contact_alice_id, _modified) = Contact::add_or_lookup(
|
||||
&bob.ctx,
|
||||
"Alice",
|
||||
"alice@example.com",
|
||||
Origin::ManuallyCreated,
|
||||
)
|
||||
.await?;
|
||||
let (contact_bob_id, _modified) = Contact::add_or_lookup(
|
||||
&alice.ctx,
|
||||
"Bob",
|
||||
"bob@example.net",
|
||||
Origin::ManuallyCreated,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let chat_alice = chat::create_by_contact_id(&alice.ctx, contact_bob_id).await?;
|
||||
let chat_bob = chat::create_by_contact_id(&bob.ctx, contact_alice_id).await?;
|
||||
let chat_alice = alice.create_chat(&bob).await.id;
|
||||
let chat_bob = bob.create_chat(&alice).await.id;
|
||||
|
||||
// Alice sends unencrypted message to Bob
|
||||
let mut msg = Message::new(Viewtype::Text);
|
||||
|
||||
@@ -461,7 +461,6 @@ pub(crate) async fn start_ephemeral_timers(context: &Context) -> sql::Result<()>
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::chat;
|
||||
use crate::contact::{Contact, Origin};
|
||||
use crate::test_utils::*;
|
||||
|
||||
#[async_std::test]
|
||||
@@ -533,23 +532,8 @@ mod tests {
|
||||
let alice = TestContext::new_alice().await;
|
||||
let bob = TestContext::new_bob().await;
|
||||
|
||||
let (contact_alice_id, _modified) = Contact::add_or_lookup(
|
||||
&bob.ctx,
|
||||
"Alice",
|
||||
"alice@example.com",
|
||||
Origin::ManuallyCreated,
|
||||
)
|
||||
.await?;
|
||||
let (contact_bob_id, _modified) = Contact::add_or_lookup(
|
||||
&alice.ctx,
|
||||
"Bob",
|
||||
"bob@example.net",
|
||||
Origin::ManuallyCreated,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let chat_alice = chat::create_by_contact_id(&alice.ctx, contact_bob_id).await?;
|
||||
let chat_bob = chat::create_by_contact_id(&bob.ctx, contact_alice_id).await?;
|
||||
let chat_alice = alice.create_chat(&bob).await.id;
|
||||
let chat_bob = bob.create_chat(&alice).await.id;
|
||||
|
||||
// Alice sends message to Bob
|
||||
let mut msg = Message::new(Viewtype::Text);
|
||||
|
||||
@@ -1883,20 +1883,15 @@ mod tests {
|
||||
let d = test::TestContext::new().await;
|
||||
let ctx = &d.ctx;
|
||||
|
||||
let contact = Contact::create(ctx, "", "dest@example.com")
|
||||
ctx.set_config(Config::ConfiguredAddr, Some("self@example.com"))
|
||||
.await
|
||||
.expect("failed to create contact");
|
||||
.unwrap();
|
||||
|
||||
let res = ctx
|
||||
.set_config(Config::ConfiguredAddr, Some("self@example.com"))
|
||||
.await;
|
||||
assert!(res.is_ok());
|
||||
|
||||
let chat = chat::create_by_contact_id(ctx, contact).await.unwrap();
|
||||
let chat = d.chat_with_contact("", "dest@example.com").await;
|
||||
|
||||
let mut msg = Message::new(Viewtype::Text);
|
||||
|
||||
let msg_id = chat::prepare_msg(ctx, chat, &mut msg).await.unwrap();
|
||||
let msg_id = chat::prepare_msg(ctx, chat.id, &mut msg).await.unwrap();
|
||||
|
||||
let _msg2 = Message::load_from_db(ctx, msg_id).await.unwrap();
|
||||
assert_eq!(_msg2.get_filemime(), None);
|
||||
@@ -1908,15 +1903,11 @@ mod tests {
|
||||
let d = test::TestContext::new().await;
|
||||
let ctx = &d.ctx;
|
||||
|
||||
let contact = Contact::create(ctx, "", "dest@example.com")
|
||||
.await
|
||||
.expect("failed to create contact");
|
||||
|
||||
let chat = chat::create_by_contact_id(ctx, contact).await.unwrap();
|
||||
let chat = d.chat_with_contact("", "dest@example.com").await;
|
||||
|
||||
let mut msg = Message::new(Viewtype::Text);
|
||||
|
||||
assert!(chat::prepare_msg(ctx, chat, &mut msg).await.is_err());
|
||||
assert!(chat::prepare_msg(ctx, chat.id, &mut msg).await.is_err());
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
@@ -2098,23 +2089,18 @@ mod tests {
|
||||
let d = test::TestContext::new().await;
|
||||
let ctx = &d.ctx;
|
||||
|
||||
let contact = Contact::create(ctx, "", "dest@example.com")
|
||||
ctx.set_config(Config::ConfiguredAddr, Some("self@example.com"))
|
||||
.await
|
||||
.expect("failed to create contact");
|
||||
.unwrap();
|
||||
|
||||
let res = ctx
|
||||
.set_config(Config::ConfiguredAddr, Some("self@example.com"))
|
||||
.await;
|
||||
assert!(res.is_ok());
|
||||
|
||||
let chat = chat::create_by_contact_id(ctx, contact).await.unwrap();
|
||||
let chat = d.chat_with_contact("", "dest@example.com").await;
|
||||
|
||||
let mut msg = Message::new(Viewtype::Text);
|
||||
msg.set_text(Some("Quoted message".to_string()));
|
||||
|
||||
// Prepare message for sending, so it gets a Message-Id.
|
||||
assert!(msg.rfc724_mid.is_empty());
|
||||
let msg_id = chat::prepare_msg(ctx, chat, &mut msg).await.unwrap();
|
||||
let msg_id = chat::prepare_msg(ctx, chat.id, &mut msg).await.unwrap();
|
||||
let msg = Message::load_from_db(ctx, msg_id).await.unwrap();
|
||||
assert!(!msg.rfc724_mid.is_empty());
|
||||
|
||||
|
||||
@@ -2,16 +2,13 @@
|
||||
//!
|
||||
//! This module is only compiled for test runs.
|
||||
|
||||
use std::str::FromStr;
|
||||
use std::time::{Duration, Instant};
|
||||
use std::{ops::Deref, str::FromStr};
|
||||
|
||||
use async_std::path::PathBuf;
|
||||
use async_std::sync::RwLock;
|
||||
use tempfile::{tempdir, TempDir};
|
||||
|
||||
use crate::chat;
|
||||
use crate::chat::{ChatId, ChatItem};
|
||||
use crate::config::Config;
|
||||
use crate::context::Context;
|
||||
use crate::dc_receive_imf::dc_receive_imf;
|
||||
use crate::dc_tools::EmailAddress;
|
||||
@@ -20,6 +17,12 @@ use crate::key::{self, DcKey};
|
||||
use crate::message::{update_msg_state, Message, MessageState, MsgId};
|
||||
use crate::mimeparser::MimeMessage;
|
||||
use crate::param::{Param, Params};
|
||||
use crate::{chat, contact::Contact};
|
||||
use crate::{
|
||||
chat::{Chat, ChatId, ChatItem},
|
||||
contact::Origin,
|
||||
};
|
||||
use crate::{config::Config, constants::DC_CONTACT_ID_SELF};
|
||||
|
||||
/// A Context and temporary directory.
|
||||
///
|
||||
@@ -199,6 +202,47 @@ impl TestContext {
|
||||
};
|
||||
Message::load_from_db(&self.ctx, *msg_id).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn create_chat(&self, other: &TestContext) -> Chat {
|
||||
let (contact_id, _modified) = Contact::add_or_lookup(
|
||||
self,
|
||||
other
|
||||
.ctx
|
||||
.get_config(Config::Displayname)
|
||||
.await
|
||||
.unwrap_or_default(),
|
||||
other.ctx.get_config(Config::ConfiguredAddr).await.unwrap(),
|
||||
Origin::ManuallyCreated,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let chat_id = chat::create_by_contact_id(self, contact_id).await.unwrap();
|
||||
Chat::load_from_db(self, chat_id).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn chat_with_contact(&self, name: &str, addr: &str) -> Chat {
|
||||
let contact = Contact::create(self, name, addr)
|
||||
.await
|
||||
.expect("failed to create contact");
|
||||
let chat_id = chat::create_by_contact_id(self, contact).await.unwrap();
|
||||
Chat::load_from_db(self, chat_id).await.unwrap()
|
||||
}
|
||||
|
||||
pub async fn get_self_chat(&self) -> Chat {
|
||||
let chat_id = chat::create_by_contact_id(self, DC_CONTACT_ID_SELF)
|
||||
.await
|
||||
.unwrap();
|
||||
Chat::load_from_db(self, chat_id).await.unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for TestContext {
|
||||
type Target = Context;
|
||||
|
||||
fn deref(&self) -> &Context {
|
||||
&self.ctx
|
||||
}
|
||||
}
|
||||
|
||||
/// A raw message as it was scheduled to be sent.
|
||||
|
||||
Reference in New Issue
Block a user