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