summaryrefslogtreecommitdiffstats
path: root/tests/test_deliver.rs
blob: ac8066479f27535c5ff1902ca128abe536f6b948 (plain)
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
// Copyright 2019 Alexandros Frantzis
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//
// SPDX-License-Identifier: MPL-2.0

use mda::Email;
use tempfile;
use std::fs;
use std::os::unix::fs as unix_fs;

#[test]
fn creates_maildir_dir_structure() {
    let tmpdir = tempfile::tempdir().unwrap();

    let email = Email::from_vec(Vec::new()).unwrap();
    email.deliver_to_maildir(tmpdir.path()).unwrap();

    let entries: Vec<_> = fs::read_dir(tmpdir.path()).unwrap().collect();

    let dir_named = |x,e: &fs::DirEntry| e.path() == tmpdir.path().join(x) &&
                                         e.metadata().unwrap().is_dir();

    assert_eq!(entries.len(), 3);
    assert_eq!(entries.iter().filter(|e| dir_named("new", e.as_ref().unwrap())).count(), 1);
    assert_eq!(entries.iter().filter(|e| dir_named("tmp", e.as_ref().unwrap())).count(), 1);
    assert_eq!(entries.iter().filter(|e| dir_named("cur", e.as_ref().unwrap())).count(), 1);
}

#[test]
fn delivers_to_maildir_new() {
    let tmpdir = tempfile::tempdir().unwrap();
    let data = [1, 3, 5, 7, 11];

    let email = Email::from_vec(data.to_vec()).unwrap();
    email.deliver_to_maildir(tmpdir.path()).unwrap();

    let new_entries: Vec<_> = fs::read_dir(tmpdir.path().join("new")).unwrap().collect();
    let tmp_entries: Vec<_> = fs::read_dir(tmpdir.path().join("tmp")).unwrap().collect();
    let cur_entries: Vec<_> = fs::read_dir(tmpdir.path().join("cur")).unwrap().collect();

    assert_eq!(new_entries.len(), 1);

    let file_contents = fs::read(new_entries[0].as_ref().unwrap().path()).unwrap();
    assert_eq!(file_contents, &data);

    assert_eq!(tmp_entries.len(), 0);
    assert_eq!(cur_entries.len(), 0);
}

#[test]
fn keeps_old_maildir_data() {
    let tmpdir = tempfile::tempdir().unwrap();

    let data1 = [1, 3, 5, 7, 11];
    let email1 = Email::from_vec(data1.to_vec()).unwrap();
    let path1 = email1.deliver_to_maildir(tmpdir.path()).unwrap();

    let data2 = [2, 4, 6, 8, 12];
    let email2 = Email::from_vec(data2.to_vec()).unwrap();
    let path2 = email2.deliver_to_maildir(tmpdir.path()).unwrap();

    let new_entries: Vec<_> = fs::read_dir(tmpdir.path().join("new")).unwrap().collect();

    assert_eq!(new_entries.len(), 2);
    assert_eq!(new_entries.iter().filter(|e| e.as_ref().unwrap().path() == path1).count(), 1);
    assert_eq!(new_entries.iter().filter(|e| e.as_ref().unwrap().path() == path2).count(), 1);

    assert_eq!(fs::read(path1).unwrap(), &data1);
    assert_eq!(fs::read(path2).unwrap(), &data2);
}

#[test]
fn deals_with_soft_link_path() {
    let tmpdir = tempfile::tempdir().unwrap();
    let subdir = tmpdir.path().join("subdir");
    let symlink = tmpdir.path().join("symlink");

    fs::create_dir(&subdir).unwrap();
    unix_fs::symlink(&subdir, &symlink).unwrap();

    let email = Email::from_vec(Vec::new()).unwrap();
    email.deliver_to_maildir(&symlink).unwrap();
}