about summary refs log tree commit diff stats
path: root/tests/by-name/em/email-ip/test.nix
blob: 688cd8f7b1b756f5d322b24436ed5358dbfbd8a0 (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
{
  nixos-lib,
  pkgsUnstable,
  nixpkgs-unstable,
  vhackPackages,
  pkgs,
  extraModules,
  nixLib,
  ...
}: let
  domain = "mail.server.test";

  scripts = {
    checkEmailEmpty = pkgs.writeShellScript "assert-empty-emails" ''
      set -xe

      # fetchmail returns EXIT_CODE 1 when no new mail
      fetchmail --nosslcertck --verbose >&2 || [ "$?" -eq 1 ] || {
        echo "Expected exit code 1." >&2
        exit 1
      }
    '';
  };

  mkUser = user: {nodes, ...}: let
    domainIp = nodes.server.networking.primaryIPAddress;
  in {
    environment.systemPackages = with pkgs; [
      fetchmail
      msmtp
      procmail
    ];

    users.users."${user}" = {isNormalUser = true;};

    systemd.tmpfiles.rules = [
      "d /home/${user}/mail         0700 ${user} users - -"
      "L /home/${user}/.fetchmailrc -    -       -     - /etc/homeSetup/.fetchmailrc"
      "L /home/${user}/.procmailrc  -    -       -     - /etc/homeSetup/.procmailrc"
      "L /home/${user}/.msmtprc     -    -       -     - /etc/homeSetup/.msmtprc"
    ];

    environment.etc = {
      "homeSetup/.fetchmailrc" = {
        text = ''
          poll "${domainIp}" protocol IMAP
            username "${user}"
            password "${user}-password"
            ssl
            mda procmail;
        '';
        mode = "0600";
        inherit user;
      };
      "homeSetup/.procmailrc" = {
        text = ''
          DEFAULT=$HOME/mail
        '';
        mode = "0600";
        inherit user;
      };
      "homeSetup/.msmtprc" = {
        text = ''
          account        ${user}
          host           ${domainIp}
          port           465
          from           ${user}@${domain}
          user           ${user}
          password       ${user}-password
          auth           on
          tls            on
          tls_starttls   off
        '';
        mode = "0600";
        inherit user;
      };
    };
  };
in
  nixos-lib.runTest {
    hostPkgs = pkgs; # the Nixpkgs package set used outside the VMs

    name = "email";

    node = {
      specialArgs = {inherit pkgsUnstable vhackPackages nixpkgs-unstable nixLib;};

      # Use the nixpkgs as constructed by the `nixpkgs.*` options
      pkgs = null;
    };

    nodes = {
      server = {config, ...}: {
        imports =
          extraModules
          ++ [
            ../../../../modules
          ];

        vhack = {
          nginx = {
            enable = true;
            selfsign = true;
          };

          stalwart-mail = {
            enable = true;
            fqdn = domain;
            admin = "mailto:admin@${domain}";
            security = null;
            openFirewall = true;
            principals = [
              {
                class = "individual";
                name = "alice";
                secret = "alice-password";
                email = ["alice@${domain}"];
              }
              {
                class = "individual";
                name = "bob";
                secret = "bob-password";
                email = ["bob@${domain}"];
              }
            ];
          };
        };
      };

      alice = mkUser "alice";
      bob = mkUser "bob";
    };

    testScript = {...}:
    /*
    python
    */
    ''
      start_all()

      server.wait_for_unit("stalwart-mail.service")
      server.wait_for_open_port(993) # imap
      server.wait_for_open_port(465) # smtp

      with subtest("Both start without mail"):
        alice.succeed("sudo -u alice ${scripts.checkEmailEmpty}")
        bob.succeed("sudo -u bob ${scripts.checkEmailEmpty}")

      with subtest("Alice can send an email to bob"):
        alice.succeed("sudo -u alice ${pkgs.writeShellScript "alice-send" ''
        set -xe

        cat << EOF | msmtp --debug --account alice --tls-certcheck=off bob@${domain} >&2
        Hi Bob!

        This is an email.
        It contains a subject and a body.

        ALICE
        EOF
      ''}")
        bob.succeed("sudo -u bob ${pkgs.writeShellScript "bob-receive" ''
        set -xe

        fetchmail --nosslcertck --verbose >&2 || {
          echo New Mail did not arrive
          exit 1
        }
      ''}")

      server.copy_from_vm("/var/lib/", "server")
      bob.copy_from_vm("/home/bob/mail", "bob")
    '';
  }