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