2021-05-02 21:49:20 +00:00
|
|
|
extern crate himalaya;
|
|
|
|
|
|
|
|
use himalaya::{config::model::Account, imap::model::ImapConnector, msg::model::Msgs, smtp};
|
|
|
|
|
|
|
|
fn get_account(addr: &str) -> Account {
|
|
|
|
Account {
|
|
|
|
name: None,
|
|
|
|
downloads_dir: None,
|
|
|
|
signature: None,
|
|
|
|
default_page_size: None,
|
|
|
|
default: Some(true),
|
|
|
|
email: addr.into(),
|
|
|
|
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();
|
|
|
|
let mboxes: Vec<String> = imap_conn
|
|
|
|
.list_mboxes()
|
|
|
|
.unwrap()
|
|
|
|
.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();
|
|
|
|
let msgs = Msgs::from(&msgs);
|
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();
|
|
|
|
assert!(match imap_conn.list_msgs("INBOX", &10, &0) {
|
|
|
|
Err(err) => err.to_string() == "The `INBOX` mailbox is empty",
|
|
|
|
Ok(_) => false,
|
|
|
|
});
|
|
|
|
|
|
|
|
// Logout
|
2021-05-02 21:49:20 +00:00
|
|
|
imap_conn.logout();
|
|
|
|
}
|