1import ./make-test-python.nix (
2 { lib, ... }:
3 {
4 name = "listmonk";
5 meta.maintainers = with lib.maintainers; [ raitobezarius ];
6
7 nodes.machine =
8 { pkgs, ... }:
9 {
10 services.mailhog.enable = true;
11 services.listmonk = {
12 enable = true;
13 settings = {
14 admin_username = "listmonk";
15 admin_password = "hunter2";
16 };
17 database = {
18 createLocally = true;
19 # https://github.com/knadh/listmonk/blob/174a48f252a146d7e69dab42724e3329dbe25ebe/internal/messenger/email/email.go#L18-L27
20 settings.smtp = [
21 {
22 enabled = true;
23 host = "localhost";
24 port = 1025;
25 tls_type = "none";
26 }
27 ];
28 };
29 };
30 };
31
32 testScript = ''
33 import json
34
35 start_all()
36
37 basic_auth = "listmonk:hunter2"
38 def generate_listmonk_request(type, url, data=None):
39 if data is None: data = {}
40 json_data = json.dumps(data)
41 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}\'''
42
43 machine.wait_for_unit("mailhog.service")
44 machine.wait_for_unit("postgresql.target")
45 machine.wait_for_unit("listmonk.service")
46 machine.wait_for_open_port(1025)
47 machine.wait_for_open_port(8025)
48 machine.wait_for_open_port(9000)
49 machine.succeed("[[ -f /var/lib/listmonk/.db_settings_initialized ]]")
50
51 assert json.loads(machine.succeed(generate_listmonk_request("GET", 'health')))['data'], 'Health endpoint returned unexpected value'
52
53 # A sample subscriber is guaranteed to exist at install-time
54 # A sample transactional template is guaranteed to exist at install-time
55 subscribers = json.loads(machine.succeed(generate_listmonk_request('GET', "subscribers")))['data']['results']
56 templates = json.loads(machine.succeed(generate_listmonk_request('GET', "templates")))['data']
57 tx_template = templates[2]
58
59 # Test transactional endpoint
60 print(machine.succeed(
61 generate_listmonk_request('POST', 'tx', data={'subscriber_id': subscribers[0]['id'], 'template_id': tx_template['id']})
62 ))
63
64 assert 'Welcome Anon Doe' in machine.succeed(
65 "curl --fail http://localhost:8025/api/v2/messages"
66 ), "Failed to find Welcome John Doe inside the messages API endpoint"
67
68 # Test campaign endpoint
69 # Based on https://github.com/knadh/listmonk/blob/174a48f252a146d7e69dab42724e3329dbe25ebe/cmd/campaigns.go#L549 as docs do not exist.
70 campaign_data = json.loads(machine.succeed(
71 generate_listmonk_request('POST', 'campaigns/1/test', data={'template_id': templates[0]['id'], 'subscribers': ['john@example.com'], 'name': 'Test', 'subject': 'NixOS is great', 'lists': [1], 'messenger': 'email'})
72 ))
73
74 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
75
76 messages = json.loads(machine.succeed(
77 "curl --fail http://localhost:8025/api/v2/messages"
78 ))
79
80 assert messages['total'] == 2
81 '';
82 }
83)