summaryrefslogtreecommitdiffstats
path: root/nixos/tests/nextcloud/with-mail.nix
blob: 0a193ce76076f3e772a0a48c11ab3b6166afac48 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
{
  name,
  pkgs,
  testBase,
  system,
  ...
}:
with import ../../lib/testing-python.nix { inherit system pkgs; };
runTest (
  { config, lib, ... }:
  let
    certs = import ../common/acme/server/snakeoil-certs.nix;
    domain = certs.domain;
  in
  {
    inherit name;

    meta.maintainers = lib.teams.nextcloud.members;

    imports = [ testBase ];

    nodes = {
      nextcloud =
        {
          config,
          pkgs,
          nodes,
          ...
        }:
        {
          security.pki.certificateFiles = [ certs.ca.cert ];

          networking.extraHosts = ''
            ${nodes.stalwart.networking.primaryIPAddress} ${domain}
          '';

          environment.etc."nextcloud/mail_smtppassword".text = "foobar";

          services.nextcloud = {
            config.dbtype = "sqlite";

            settings = {
              mail_from_address = "alice";
              mail_domain = domain;
              mail_smtpmode = "smtp";
              mail_smtphost = domain;
              mail_smtpport = 587;
              mail_smtpauth = true;
              mail_smtpname = "alice";
              mail_send_plaintext_only = true;
            };

            secrets.mail_smtppassword = "/etc/nextcloud/mail_smtppassword";
          };
        };

      stalwart =
        { pkgs, ... }:
        {
          imports = [ ../stalwart/stalwart-config.nix ];

          networking.firewall.allowedTCPPorts = [ 587 ];

          environment.systemPackages = [
            (pkgs.writers.writePython3Bin "test-imap-read" { } ''
              from imaplib import IMAP4

              with IMAP4('localhost') as imap:
                  imap.starttls()
                  status, [caps] = imap.login('bob', 'foobar')
                  assert status == 'OK'
                  imap.select()
                  status, [ref] = imap.search(None, 'ALL')
                  assert status == 'OK'
                  [msgId] = ref.split()
                  status, msg = imap.fetch(msgId, 'BODY[TEXT]')
                  assert status == 'OK'
                  assert (msg[0][1].strip()
                          == (b'Well done, ${config.adminuser}!\r\n\r\n'
                              b'If you received this email, the email configuration '
                              b's=\r\neems to be correct.\r\n\r\n\r\n--=20\r\n'
                              b'Nextcloud - a safe home for all your data=\r\n\r\n'
                              b'This is an automatically sent email, please do not reply.'))
            '')
          ];
        };
    };

    test-helpers.init = ''
      stalwart.wait_for_unit("multi-user.target")
      stalwart.wait_until_succeeds("nc -vzw 2 localhost 587")

      nextcloud.succeed("nc -vzw 2 ${domain} 587")
      nextcloud.succeed("curl -sS --fail-with-body -u ${config.adminuser}:${config.adminpass} -H 'OCS-APIRequest: true' -X PUT http://nextcloud/ocs/v2.php/cloud/users/${config.adminuser} -H 'Content-Type: application/json' --data-raw '{\"key\":\"email\",\"value\":\"bob@${domain}\"}'")
      nextcloud.succeed("curl -sS --fail-with-body -u ${config.adminuser}:${config.adminpass} -H 'OCS-APIRequest: true' -X POST http://nextcloud/settings/admin/mailtest")

      stalwart.succeed("test-imap-read")
    '';
  }
)