1# Rudimentary test checking that the Stalwart email server can:
2# - receive some message through SMTP submission, then
3# - serve this message through IMAP.
4
5let
6 certs = import ./common/acme/server/snakeoil-certs.nix;
7 domain = certs.domain;
8
9in import ./make-test-python.nix ({ lib, ... }: {
10 name = "stalwart-mail";
11
12 nodes.main = { pkgs, ... }: {
13 security.pki.certificateFiles = [ certs.ca.cert ];
14
15 services.stalwart-mail = {
16 enable = true;
17 settings = {
18 server.hostname = domain;
19
20 certificate."snakeoil" = {
21 cert = "file://${certs.${domain}.cert}";
22 private-key = "file://${certs.${domain}.key}";
23 };
24
25 server.tls = {
26 certificate = "snakeoil";
27 enable = true;
28 implicit = false;
29 };
30
31 server.listener = {
32 "smtp-submission" = {
33 bind = [ "[::]:587" ];
34 protocol = "smtp";
35 };
36
37 "imap" = {
38 bind = [ "[::]:143" ];
39 protocol = "imap";
40 };
41 };
42
43 session.auth.mechanisms = [ "PLAIN" ];
44 session.auth.directory = "in-memory";
45 jmap.directory = "in-memory"; # shared with imap
46
47 session.rcpt.directory = "in-memory";
48 queue.outbound.next-hop = [ "local" ];
49
50 directory."in-memory" = {
51 type = "memory";
52 users = [
53 {
54 name = "alice";
55 secret = "foobar";
56 email = [ "alice@${domain}" ];
57 }
58 {
59 name = "bob";
60 secret = "foobar";
61 email = [ "bob@${domain}" ];
62 }
63 ];
64 };
65 };
66 };
67
68 environment.systemPackages = [
69 (pkgs.writers.writePython3Bin "test-smtp-submission" { } ''
70 from smtplib import SMTP
71
72 with SMTP('localhost', 587) as smtp:
73 smtp.starttls()
74 smtp.login('alice', 'foobar')
75 smtp.sendmail(
76 'alice@${domain}',
77 'bob@${domain}',
78 """
79 From: alice@${domain}
80 To: bob@${domain}
81 Subject: Some test message
82
83 This is a test message.
84 """.strip()
85 )
86 '')
87
88 (pkgs.writers.writePython3Bin "test-imap-read" { } ''
89 from imaplib import IMAP4
90
91 with IMAP4('localhost') as imap:
92 imap.starttls()
93 imap.login('bob', 'foobar')
94 imap.select('"All Mail"')
95 status, [ref] = imap.search(None, 'ALL')
96 assert status == 'OK'
97 [msgId] = ref.split()
98 status, msg = imap.fetch(msgId, 'BODY[TEXT]')
99 assert status == 'OK'
100 assert msg[0][1].strip() == b'This is a test message.'
101 '')
102 ];
103 };
104
105 testScript = /* python */ ''
106 main.wait_for_unit("stalwart-mail.service")
107 main.wait_for_open_port(587)
108 main.wait_for_open_port(143)
109
110 main.succeed("test-smtp-submission")
111 main.succeed("test-imap-read")
112 '';
113
114 meta = {
115 maintainers = with lib.maintainers; [ happysalada pacien ];
116 };
117})