2022-08-30 11:15:41 +00:00
import ./make-test-python.nix ( { lib , . . . }: {
name = " l i s t m o n k " ;
meta . maintainers = with lib . maintainers ; [ raitobezarius ] ;
nodes . machine = { pkgs , . . . }: {
services . mailhog . enable = true ;
services . listmonk = {
enable = true ;
settings = {
admin_username = " l i s t m o n k " ;
admin_password = " h u n t e r 2 " ;
} ;
database = {
createLocally = true ;
# https://github.com/knadh/listmonk/blob/174a48f252a146d7e69dab42724e3329dbe25ebe/internal/messenger/email/email.go#L18-L27
settings . smtp = [ {
enabled = true ;
host = " l o c a l h o s t " ;
port = 1025 ;
tls_type = " n o n e " ;
} ] ;
} ;
} ;
} ;
testScript = ''
import json
start_all ( )
basic_auth = " l i s t m o n k : h u n t e r 2 "
def generate_listmonk_request ( type , url , data = None ) :
if data is None : data = { }
json_data = json . dumps ( data )
return f'curl - u " { b a s i c _ a u t h } " - X { type } " h t t p : / / l o c a l h o s t : 9 0 0 0 / a p i / { u r l } " - H " C o n t e n t - T y p e : a p p l i c a t i o n / j s o n ; c h a r s e t = u t f - 8 " - - data-raw \ ' { json_data } \ '' '
machine . wait_for_unit ( " m a i l h o g . s e r v i c e " )
machine . wait_for_unit ( " p o s t g r e s q l . s e r v i c e " )
machine . wait_for_unit ( " l i s t m o n k . s e r v i c e " )
machine . wait_for_open_port ( 1025 )
machine . wait_for_open_port ( 8025 )
machine . wait_for_open_port ( 9000 )
machine . succeed ( " [ [ - f / v a r / l i b / l i s t m o n k / . d b _ s e t t i n g s _ i n i t i a l i z e d ] ] " )
2023-08-22 22:25:47 +00:00
assert json . loads ( machine . succeed ( generate_listmonk_request ( " G E T " , ' health' ) ) ) [ ' data' ] , ' Health endpoint returned unexpected value'
# A sample subscriber is guaranteed to exist at install-time
# A sample transactional template is guaranteed to exist at install-time
subscribers = json . loads ( machine . succeed ( generate_listmonk_request ( ' GET' , " s u b s c r i b e r s " ) ) ) [ ' data' ] [ ' results' ]
templates = json . loads ( machine . succeed ( generate_listmonk_request ( ' GET' , " t e m p l a t e s " ) ) ) [ ' data' ]
tx_template = templates [ 2 ]
2022-08-30 11:15:41 +00:00
# Test transactional endpoint
2023-08-22 22:25:47 +00:00
print ( machine . succeed (
generate_listmonk_request ( ' POST' , ' tx' , data = { ' subscriber_id' : subscribers [ 0 ] [ ' id' ] , ' template_id' : tx_template [ ' id' ] } )
) )
assert ' Welcome Anon Doe' in machine . succeed (
2022-08-30 11:15:41 +00:00
" c u r l - - f a i l h t t p : / / l o c a l h o s t : 8 0 2 5 / a p i / v 2 / m e s s a g e s "
2023-08-22 22:25:47 +00:00
) , " F a i l e d t o f i n d W e l c o m e J o h n D o e i n s i d e t h e m e s s a g e s A P I e n d p o i n t "
2022-08-30 11:15:41 +00:00
# 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 (
2023-08-22 22:25:47 +00:00
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' } )
2022-08-30 11:15:41 +00:00
) )
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 (
" c u r l - - f a i l h t t p : / / l o c a l h o s t : 8 0 2 5 / a p i / v 2 / m e s s a g e s "
) )
assert messages [ ' total' ] == 2
'' ;
} )