aboutsummaryrefslogtreecommitdiffstats
path: root/tests/by-name/sn/snm-email-dual-server/test.nix
blob: 6f7bebb4225b02da871d7939587d1300e41aa8c4 (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
{
  lib,
  vhack,
  pkgs,
  vhackPackages,
  ...
}: let
  server1Domain = "mail.server1";
  server2Domain = "mail.server2";

  common = import ../../../common/email/functions.nix {inherit pkgs vhackPackages;};

  # That's a horrible hack. We should instead wait, until the delivery of the email is
  # confirmed.
  sleepTime = "40";
in
  vhack.runTest {
    name = "email";

    serverDomains = [
      {server1 = server1Domain;}
      {server2 = server2Domain;}
    ];

    nodes = {
      server1 = common.mkMailServer server1Domain "alice";
      server2 = common.mkMailServer server2Domain "bob";

      alice = common.mkUser "alice" server1Domain;
      bob = common.mkUser "bob" server2Domain;
    };

    dns = {
      server1 = common.mkDnsZone server1Domain;
      server2 = common.mkDnsZone server2Domain;
    };

    services = [
      {server1 = "postfix.service";}
      {server1 = "dovecot.service";}
      {server2 = "postfix.service";}
      {server2 = "dovecot.service";}

      {alice = "multi-user.target";}
      {bob = "multi-user.target";}
    ];

    ports = [
      {server1 = 993;} # imap
      {server1 = 465;} # smtp

      {server2 = 993;} # imap
      {server2 = 465;} # smtp
    ];

    testScript = {...}:
    # Python
    ''
      from time import sleep

      with subtest("Dkim keys work"):
        server1.succeed("sudo -u rspamd ${pkgs.writeShellScript "dkim-keys-check" ''
        set -xe

        stat /run/agenix/mailDkim

        ${lib.getExe pkgs.openssl} pkey -text -noout -in /run/agenix/mailDkim | grep Private-Key
        ${lib.getExe pkgs.openssl} pkey -text -noout -in /run/agenix/mailDkim
      ''}")

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


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

        echo "" | msmtp --debug --account alice bob@${server2Domain} >&2
      ''}")

        # Give the mail server some time to send the email.
        sleep(${sleepTime})

        bob.succeed("sudo -u bob ${common.scripts.expectNewSpamMail}")

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

        cat << EOF | msmtp --debug --account alice bob@${server2Domain} >&2
        Subject: Hi bob, I'm Alice!

        Good day, Bob!

        This is an email.
        It contains a subject and a body.
        I also assert utf8 support by including my last name in this very message.

        XOXO
        Alice van Dåligen.

        .
        EOF
      ''}")

        # Give the mail server some time to send the email.
        sleep(${sleepTime})

        bob.succeed("sudo -u bob ${common.scripts.expectNewSpamMail}")

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

        cat << EOF | msmtp --debug --account bob alice@${server1Domain} >&2
        Hi Alice!

        Thank you for your emails! (Although they seemed kinda weird tbh :?)

        XXX
        Bob Älšter

        .
        EOF
      ''}")

        # Give the mail server some time to send the email.
        sleep(${sleepTime})

        alice.succeed("sudo -u alice ${common.scripts.expectNewSpamMail}")
    '';
  }