aboutsummaryrefslogtreecommitdiffstats
path: root/tests/fixtures.rs
blob: c9e04c81ef4531e8d566c3f08856b0139d2eb392 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
extern crate dirs;
extern crate tempfile;
extern crate notmuch;
extern crate gethostname;
extern crate maildir;
extern crate lettre;
extern crate lettre_email;

use std::ffi::OsStr;
use std::io::{self, Result, Write};
use std::fs::{self, File};
use std::rc::Rc;
use std::path::{Path, PathBuf};
use tempfile::{tempdir, tempdir_in, Builder, TempDir};
use std::net::ToSocketAddrs;
use std::process::Command;
use std::time::{SystemTime, UNIX_EPOCH};
use maildir::Maildir;
use lettre_email::{EmailBuilder, Header};
use lettre::SendableEmail;


pub fn timestamp_ms() -> u128 {
    let start = SystemTime::now();
    let time_since_epoch = start.duration_since(UNIX_EPOCH).unwrap();
    time_since_epoch.as_millis()
}

// A basic test interface to a valid maildir directory.
//
// This creates a valid maildir and provides a simple mechanism to
// deliver test emails to it.  It also writes a notmuch-config file
// in the top of the maildir.
pub struct MailBox {
    root_dir: TempDir,
    idcount: u32,
    maildir: Maildir
}

impl MailBox {

    // Creates a new maildir fixture. Since this is only used for tests,
    // may just panic of something is wrong
    pub fn new() -> Self {

        let root_dir = tempdir().unwrap();
        let root_path = root_dir.path().to_path_buf();

        let tmp_path = root_path.join("tmp");
        fs::create_dir(&tmp_path).unwrap();

        let cfg_fname = root_path.join("notmuch-config");
        let mut cfg_file = File::create(cfg_fname).unwrap();
        write!(cfg_file, r#"
            [database]
            path={tmppath}
            [user]
            name=Some Hacker
            primary_email=dst@example.com
            [new]
            tags=unread;inbox;
            ignore=
            [search]
            exclude_tags=deleted;spam;
            [maildir]
            synchronize_flags=true
            [crypto]
            gpg_path=gpg
        "#, tmppath=root_path.to_string_lossy()).unwrap();

        let maildir = Maildir::from(root_path.to_path_buf());
        maildir.create_dirs().unwrap();

        Self {
            root_dir,
            idcount: 0,
            maildir
        }
    }

    /// Return a new unique message ID
    // fn next_msgid(&mut self) -> String{
    //     let hostname = gethostname::gethostname();
    //     let msgid = format!("{}@{}", self.idcount, hostname.to_string_lossy());
    //     self.idcount += 1;
    //     msgid
    // }

    pub fn path(&self) -> PathBuf
    {
        self.root_dir.path().into()
    }

    pub fn hostname(&self) -> String {
        let hname = gethostname::gethostname();
        hname.to_string_lossy().into()
    }

    /// Deliver a new mail message in the mbox.
    /// This does only adds the message to maildir, does not insert it
    /// into the notmuch database.
    /// returns a tuple of (msgid, pathname).
    pub fn deliver(&self,
                   subject: Option<String>,
                   body: Option<String>,
                   to: Option<String>,
                   from: Option<String>,
                   headers: Vec<(String, String)>,
                   is_new: bool,      // Move to new dir or cur dir?
                   keywords: Option<Vec<String>>,  // List of keywords or labels
                   seen: bool,     // Seen flag (cur dir only)
                   replied: bool,  // Replied flag (cur dir only)
                   flagged: bool)  // Flagged flag (cur dir only)
        -> Result<(String, PathBuf)>
    {

        let mut builder = EmailBuilder::new()
                            .subject(subject.unwrap_or_else(|| "Test mail".to_string()));


        if let Some(val) = body {
            builder = builder.text(val);
        }

        builder = builder.to(to.unwrap_or_else(|| "to@example.com".to_string()))
                         .from(from.unwrap_or_else(|| "src@example.com".to_string()));

        for h in headers.into_iter(){
            let hdr: Header = h.into();
            builder = builder.header(hdr);
        }

        let msg:SendableEmail = builder.build().unwrap().into();

        // not sure why lettre doesn't add the host suffix itself
        let msg_id = msg.message_id().to_string() + ".lettre@localhost";
        let id = if is_new {
            self.maildir.store_new(&msg.message_to_string().unwrap().as_bytes()).unwrap()
        }else{
            let mut flags = String::from("");
            if flagged {
                flags += "F";
            }
            if replied {
                flags += "R";
            }
            if seen {
                flags += "S";
            }
            self.maildir.store_cur_with_flags(&msg.message_to_string().unwrap().as_bytes(), flags.as_str()).unwrap()
        };

        let mut msgpath = self.path();
        msgpath = if is_new {
            msgpath.join("new")
        } else {
            msgpath.join("cur")
        };

        msgpath = msgpath.join(&id);

        Ok((msg_id, msgpath))
    }
}

impl Drop for MailBox {
    fn drop(&mut self) {
    }
}


#[derive(Clone, Debug)]
pub struct NotmuchCommand {
    maildir_path: PathBuf
}

impl NotmuchCommand {

    /// Return a function which runs notmuch commands on our test maildir.
    ///
    /// This uses the notmuch-config file created by the ``maildir``
    /// fixture.
    pub fn new(maildir_path: &PathBuf) -> Self {
        Self {
            maildir_path: maildir_path.clone()
        }
    }

    /// Run a notmuch comand.
    /// 
    /// This function runs with a timeout error as many notmuch
    /// commands may block if multiple processes are trying to open
    /// the database in write-mode.  It is all too easy to
    /// accidentally do this in the unittests.
    pub fn run<I, S>(&self, args: I) -> Result<()>
    where
        I: IntoIterator<Item=S>,
        S: AsRef<OsStr>
    {
        let cfg_fname = self.maildir_path.join("notmuch-config");

        Command::new("notmuch").env("NOTMUCH_CONFIG", &cfg_fname)
                               .args(args)
                               .status()?;
        Ok(())
    }

}