summary refs log tree commit diff
path: root/nixos/tests/alps.nix
blob: 8d7814117df1eb145c2bacb80b7584fecaf673c2 (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
let
  certs = import ./common/acme/server/snakeoil-certs.nix;
  domain = certs.domain;
in
import ./make-test-python.nix ({ pkgs, ... }: {
  name = "alps";

  nodes = {
    server = {
      imports = [ ./common/user-account.nix ];
      security.pki.certificateFiles = [
        certs.ca.cert
      ];
      networking.extraHosts = ''
        127.0.0.1 ${domain}
      '';
      networking.firewall.allowedTCPPorts = [ 25 465 993 ];
      services.postfix = {
        enable = true;
        enableSubmission = true;
        enableSubmissions = true;
        tlsTrustedAuthorities = "${certs.ca.cert}";
        sslCert = "${certs.${domain}.cert}";
        sslKey = "${certs.${domain}.key}";
      };
      services.dovecot2 = {
        enable = true;
        enableImap = true;
        sslCACert = "${certs.ca.cert}";
        sslServerCert = "${certs.${domain}.cert}";
        sslServerKey = "${certs.${domain}.key}";
      };
    };

    client = { nodes, config, ... }: {
      security.pki.certificateFiles = [
        certs.ca.cert
      ];
      networking.extraHosts = ''
        ${nodes.server.config.networking.primaryIPAddress} ${domain}
      '';
      services.alps = {
        enable = true;
        theme = "alps";
        imaps = {
          host = domain;
          port = 993;
        };
        smtps = {
          host = domain;
          port = 465;
        };
      };
      environment.systemPackages = [
        (pkgs.writers.writePython3Bin "test-alps-login" { } ''
          from urllib.request import build_opener, HTTPCookieProcessor, Request
          from urllib.parse import urlencode, urljoin
          from http.cookiejar import CookieJar

          baseurl = "http://localhost:${toString config.services.alps.port}"
          username = "alice"
          password = "${nodes.server.config.users.users.alice.password}"
          cookiejar = CookieJar()
          cookieprocessor = HTTPCookieProcessor(cookiejar)
          opener = build_opener(cookieprocessor)

          data = urlencode({"username": username, "password": password}).encode()
          req = Request(urljoin(baseurl, "login"), data=data, method="POST")
          with opener.open(req) as ret:
              # Check that the alps_session cookie is set
              print(cookiejar)
              assert any(cookie.name == "alps_session" for cookie in cookiejar)

          req = Request(baseurl)
          with opener.open(req) as ret:
              # Check that the alps_session cookie is still there...
              print(cookiejar)
              assert any(cookie.name == "alps_session" for cookie in cookiejar)
              # ...and that we have not been redirected back to the login page
              print(ret.url)
              assert ret.url == urljoin(baseurl, "mailbox/INBOX")

          req = Request(urljoin(baseurl, "logout"))
          with opener.open(req) as ret:
              # Check that the alps_session cookie is now gone
              print(cookiejar)
              assert all(cookie.name != "alps_session" for cookie in cookiejar)
        '')
      ];
    };
  };

  testScript = ''
    server.start()
    server.wait_for_unit("postfix.service")
    server.wait_for_unit("dovecot2.service")
    server.wait_for_open_port(465)
    server.wait_for_open_port(993)

    client.start()
    client.wait_for_unit("alps.service")
    client.succeed("test-alps-login")
  '';
})