mirror of
https://github.com/chatmail/core.git
synced 2026-04-17 21:46:35 +03:00
add tests
This commit is contained in:
@@ -681,7 +681,10 @@ fn extract_address_from_receive_header<'a>(header: &'a str, start: &str) -> Opti
|
||||
let header_len = header.len();
|
||||
header.find(start).and_then(|mut begin| {
|
||||
begin += start.len();
|
||||
let end = header.get(begin..)?.find(' ').unwrap_or(header_len);
|
||||
let end = header
|
||||
.get(begin..)?
|
||||
.find(|c| c == ' ' || c == '\n')
|
||||
.unwrap_or(header_len);
|
||||
header.get(begin..begin + end)
|
||||
})
|
||||
}
|
||||
@@ -695,11 +698,11 @@ pub(crate) fn parse_receive_header(header: &str) -> String {
|
||||
};
|
||||
|
||||
if let Some(from) = extract_address_from_receive_header(header, "from ") {
|
||||
hop_info.push_str(&format!("From: {}\n", from));
|
||||
hop_info.push_str(&format!("From: {}\n", from.trim()));
|
||||
}
|
||||
|
||||
if let Some(by) = extract_address_from_receive_header(header, "by ") {
|
||||
hop_info.push_str(&format!("By: {}\n", by));
|
||||
hop_info.push_str(&format!("By: {}\n", by.trim()));
|
||||
}
|
||||
hop_info
|
||||
}
|
||||
@@ -709,11 +712,13 @@ pub(crate) fn parse_receive_headers(headers: &Headers) -> String {
|
||||
let headers = headers
|
||||
.get_all_headers("Received")
|
||||
.iter()
|
||||
.rev()
|
||||
.filter_map(|header_map_item| from_utf8(header_map_item.get_value_raw()).ok())
|
||||
.map(|header_value| parse_receive_header(header_value))
|
||||
.enumerate()
|
||||
.map(|(i, header_value)| (i + 1).to_string() + ". " + &parse_receive_header(header_value))
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
headers.iter().map(|a| a.to_string()).join("\n\n")
|
||||
headers.iter().map(|a| a.to_string()).join("\n")
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -722,7 +727,62 @@ mod tests {
|
||||
|
||||
use super::*;
|
||||
|
||||
use crate::test_utils::TestContext;
|
||||
use crate::{
|
||||
chat::ChatItem, config::Config, dc_receive_imf::dc_receive_imf, message::get_msg_info,
|
||||
test_utils::TestContext,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn test_parse_receive_headers() {
|
||||
let raw = include_bytes!("../test-data/message/mail_with_cc.txt");
|
||||
let mail = mailparse::parse_mail(&raw[..]).unwrap();
|
||||
let hop_info = parse_receive_headers(&mail.get_headers());
|
||||
let expected = concat!(
|
||||
"1. Hop:\n",
|
||||
"Date: Sat, 14 Sep 2019 19:00:22 +0200\n",
|
||||
"From: localhost\n",
|
||||
"By: hq5.merlinux.eu\n",
|
||||
"\n",
|
||||
"2. Hop:\n",
|
||||
"Date: Sat, 14 Sep 2019 19:00:25 +0200\n",
|
||||
"From: hq5.merlinux.eu\n",
|
||||
"By: hq5.merlinux.eu\n",
|
||||
);
|
||||
assert_eq!(&hop_info, expected)
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
async fn test_parse_receive_headers_integration() -> anyhow::Result<()> {
|
||||
let t = TestContext::new_alice().await;
|
||||
t.set_config(Config::ShowEmails, Some("2")).await?;
|
||||
let raw = include_bytes!("../test-data/message/mail_with_cc.txt");
|
||||
dc_receive_imf(&t, raw, "INBOX", 1, false).await.unwrap();
|
||||
let g = t.get_last_msg().await;
|
||||
|
||||
let expected = r"State: Fresh
|
||||
|
||||
hi
|
||||
|
||||
Message-ID: 2dfdbde7@example.org
|
||||
Last seen as: INBOX/1
|
||||
1. Hop:
|
||||
Date: Sat, 14 Sep 2019 19:00:22 +0200
|
||||
From: localhost
|
||||
By: hq5.merlinux.eu
|
||||
|
||||
2. Hop:
|
||||
Date: Sat, 14 Sep 2019 19:00:25 +0200
|
||||
From: hq5.merlinux.eu
|
||||
By: hq5.merlinux.eu
|
||||
";
|
||||
let result = get_msg_info(&t, g.id).await.unwrap();
|
||||
// little hack to ignore the first row of a parsed email because it contains a
|
||||
// send time that depends and the test runtime which makes it impossible to
|
||||
// compare with a static string
|
||||
let capped_result = &result[result.find("State").unwrap()..];
|
||||
assert_eq!(expected, capped_result);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_rust_ftoa() {
|
||||
|
||||
@@ -1260,7 +1260,7 @@ pub async fn get_msg_info(context: &Context, msg_id: MsgId) -> Result<String> {
|
||||
}
|
||||
if let Some(ref server_folder) = msg.server_folder {
|
||||
if !server_folder.is_empty() {
|
||||
ret += &format!("\nLast seen as: {}/{}", server_folder, msg.server_uid);
|
||||
ret += &format!("\nLast seen as: {}/{}\n", server_folder, msg.server_uid);
|
||||
}
|
||||
}
|
||||
let hop_info: Option<String> = context
|
||||
|
||||
@@ -472,7 +472,8 @@ paramsv![]
|
||||
// move requests to "Archived Chats",
|
||||
// this way, the app looks familiar after the contact request upgrade.
|
||||
info!(context, "[migration] v78");
|
||||
sql.execute_migration("UPDATE chats SET archived=1 WHERE blocked=2;", 78).await?;
|
||||
sql.execute_migration("UPDATE chats SET archived=1 WHERE blocked=2;", 78)
|
||||
.await?;
|
||||
}
|
||||
if dbversion < 79 {
|
||||
info!(context, "[migration] v79");
|
||||
|
||||
Reference in New Issue
Block a user