about summary refs log tree commit diff
path: root/nixos/tests/listmonk.nix
blob: 91003653c09edd1ff1b82ce0c9dbe2d93c20851b (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
import ./make-test-python.nix ({ lib, ... }: {
  name = "listmonk";
  meta.maintainers = with lib.maintainers; [ raitobezarius ];

  nodes.machine = { pkgs, ... }: {
    services.mailhog.enable = true;
    services.listmonk = {
      enable = true;
      settings = {
        admin_username = "listmonk";
        admin_password = "hunter2";
      };
      database = {
        createLocally = true;
        # https://github.com/knadh/listmonk/blob/174a48f252a146d7e69dab42724e3329dbe25ebe/internal/messenger/email/email.go#L18-L27
        settings.smtp = [ {
          enabled = true;
          host = "localhost";
          port = 1025;
          tls_type = "none";
        }];
      };
    };
  };

  testScript = ''
    import json

    start_all()

    basic_auth = "listmonk:hunter2"
    def generate_listmonk_request(type, url, data=None):
       if data is None: data = {}
       json_data = json.dumps(data)
       return f'curl -u "{basic_auth}" -X {type} "http://localhost:9000/api/{url}" -H "Content-Type: application/json; charset=utf-8" --data-raw \'{json_data}\'''

    machine.wait_for_unit("mailhog.service")
    machine.wait_for_unit("postgresql.service")
    machine.wait_for_unit("listmonk.service")
    machine.wait_for_open_port(1025)
    machine.wait_for_open_port(8025)
    machine.wait_for_open_port(9000)
    machine.succeed("[[ -f /var/lib/listmonk/.db_settings_initialized ]]")

    # Test transactional endpoint
    # subscriber_id=1 is guaranteed to exist at install-time
    # template_id=2 is guaranteed to exist at install-time and is a transactional template (1 is a campaign template).
    machine.succeed(
      generate_listmonk_request('POST', 'tx', data={'subscriber_id': 1, 'template_id': 2})
    )
    assert 'Welcome John Doe' in machine.succeed(
        "curl --fail http://localhost:8025/api/v2/messages"
    )

    # Test campaign endpoint
    # Based on https://github.com/knadh/listmonk/blob/174a48f252a146d7e69dab42724e3329dbe25ebe/cmd/campaigns.go#L549 as docs do not exist.
    campaign_data = json.loads(machine.succeed(
      generate_listmonk_request('POST', 'campaigns/1/test', data={'template_id': 1, 'subscribers': ['john@example.com'], 'name': 'Test', 'subject': 'NixOS is great', 'lists': [1], 'messenger': 'email'})
    ))

    assert campaign_data['data']  # This is a boolean asserting if the test was successful or not: https://github.com/knadh/listmonk/blob/174a48f252a146d7e69dab42724e3329dbe25ebe/cmd/campaigns.go#L626

    messages = json.loads(machine.succeed(
        "curl --fail http://localhost:8025/api/v2/messages"
    ))

    assert messages['total'] == 2
  '';
})