himalaya/tests/imap_test.rs

118 lines
3.3 KiB
Rust
Raw Normal View History

2021-05-02 21:49:20 +00:00
extern crate himalaya;
2021-05-04 21:19:05 +00:00
use himalaya::{
config::model::Account, imap::model::ImapConnector, mbox::model::Mboxes, msg::model::Msgs, smtp,
};
2021-05-02 21:49:20 +00:00
fn get_account(addr: &str) -> Account {
Account {
name: None,
downloads_dir: None,
signature: None,
default_page_size: None,
default: Some(true),
email: addr.into(),
2021-05-08 11:49:13 +00:00
watch_cmds: None,
2021-05-02 21:49:20 +00:00
imap_host: String::from("localhost"),
imap_port: 3993,
imap_starttls: Some(false),
imap_insecure: Some(true),
imap_login: addr.into(),
imap_passwd_cmd: String::from("echo 'password'"),
smtp_host: String::from("localhost"),
smtp_port: 3465,
smtp_starttls: Some(false),
smtp_insecure: Some(true),
smtp_login: addr.into(),
smtp_passwd_cmd: String::from("echo 'password'"),
}
}
#[test]
fn mbox() {
let account = get_account("inbox@localhost");
let mut imap_conn = ImapConnector::new(&account).unwrap();
2021-05-04 21:19:05 +00:00
let names = imap_conn.list_mboxes().unwrap();
let mboxes: Vec<String> = Mboxes::from(&names)
2021-05-02 21:49:20 +00:00
.0
.into_iter()
.map(|mbox| mbox.name)
.collect();
assert_eq!(mboxes, vec![String::from("INBOX")]);
imap_conn.logout();
}
#[test]
fn msg() {
let account = get_account("inbox@localhost");
2021-05-03 16:42:09 +00:00
// Add messages
2021-05-02 21:49:20 +00:00
smtp::send(
&account,
&lettre::Message::builder()
2021-05-03 16:42:09 +00:00
.from("sender-a@localhost".parse().unwrap())
2021-05-02 21:49:20 +00:00
.to("inbox@localhost".parse().unwrap())
2021-05-03 16:42:09 +00:00
.subject("Subject A")
.singlepart(lettre::message::SinglePart::builder().body("Body A".as_bytes().to_vec()))
.unwrap(),
)
.unwrap();
smtp::send(
&account,
&lettre::Message::builder()
.from("\"Sender B\" <sender-b@localhost>".parse().unwrap())
.to("inbox@localhost".parse().unwrap())
.subject("Subject B")
.singlepart(lettre::message::SinglePart::builder().body("Body B".as_bytes().to_vec()))
2021-05-02 21:49:20 +00:00
.unwrap(),
)
.unwrap();
2021-05-03 16:42:09 +00:00
// Login
2021-05-02 21:49:20 +00:00
let mut imap_conn = ImapConnector::new(&account).unwrap();
2021-05-03 16:42:09 +00:00
// List messages
// TODO: check non-existance of \Seen flag
2021-05-02 21:49:20 +00:00
let msgs = imap_conn.list_msgs("INBOX", &10, &0).unwrap();
2021-05-08 11:49:13 +00:00
let msgs = if let Some(ref fetches) = msgs {
Msgs::from(fetches)
} else {
Msgs::new()
};
2021-05-03 16:42:09 +00:00
assert_eq!(msgs.0.len(), 2);
let msg_a = msgs
.0
.iter()
.find(|msg| msg.subject == "Subject A")
.unwrap();
assert_eq!(msg_a.subject, "Subject A");
assert_eq!(msg_a.sender, "sender-a@localhost");
let msg_b = msgs
.0
.iter()
.find(|msg| msg.subject == "Subject B")
.unwrap();
assert_eq!(msg_b.subject, "Subject B");
assert_eq!(msg_b.sender, "Sender B");
// TODO: search messages
// TODO: read message (+ \Seen flag)
// TODO: list message attachments
// TODO: add/set/remove flags
// Delete messages
imap_conn
.add_flags("INBOX", &msg_a.uid.to_string(), "\\Deleted")
.unwrap();
imap_conn
.add_flags("INBOX", &msg_b.uid.to_string(), "\\Deleted")
.unwrap();
imap_conn.expunge("INBOX").unwrap();
2021-05-08 11:49:13 +00:00
assert!(imap_conn.list_msgs("INBOX", &10, &0).unwrap().is_none());
2021-05-03 16:42:09 +00:00
// Logout
2021-05-02 21:49:20 +00:00
imap_conn.logout();
}