1 import ./make-test-python.nix ({ pkgs, ... }:
3 certs = import ./common/acme/server/snakeoil-certs.nix;
6 name = "rspamd-trainer";
7 meta = with pkgs.lib.maintainers; { maintainers = [ onny ]; };
10 machine = { options, config, ... }: {
12 security.pki.certificateFiles = [
16 networking.extraHosts = ''
20 services.rspamd-trainer = {
24 USERNAME = "spam@${domain}";
25 INBOXPREFIX = "INBOX/";
28 # Do not use this in production. This will make passwords
29 # world-readable in the Nix store
30 "${pkgs.writeText "secrets" ''
39 primaryDomain = domain;
40 ensureAccounts = [ "spam@${domain}" ];
42 # Do not use this in production. This will make passwords world-readable
44 "spam@${domain}".passwordFile = "${pkgs.writeText "postmaster" "test123"}";
49 certPath = "${certs.${domain}.cert}";
50 keyPath = "${certs.${domain}.key}";
53 config = builtins.replaceStrings [
54 "imap tcp://0.0.0.0:143"
55 "submission tcp://0.0.0.0:587"
57 "imap tls://0.0.0.0:993 tcp://0.0.0.0:143"
58 "submission tls://0.0.0.0:465 tcp://0.0.0.0:587"
59 ] options.services.maddy.config.default;
65 "redis.conf".text = ''
66 servers = "${config.services.redis.servers.rspamd.unixSocket}";
68 "classifier-bayes.conf".text = ''
75 services.redis.servers.rspamd = {
78 unixSocket = "/run/redis-rspamd/redis.sock";
79 user = config.services.rspamd.user;
82 environment.systemPackages = [
83 (pkgs.writers.writePython3Bin "send-testmail" { } ''
86 from email.mime.text import MIMEText
87 context = ssl.create_default_context()
88 msg = MIMEText("Hello World")
89 msg['Subject'] = 'Test'
90 msg['From'] = "spam@${domain}"
91 msg['To'] = "spam@${domain}"
92 with smtplib.SMTP_SSL(host='${domain}', port=465, context=context) as smtp:
93 smtp.login('spam@${domain}', 'test123')
95 'spam@${domain}', 'spam@${domain}', msg.as_string()
98 (pkgs.writers.writePython3Bin "create-mail-dirs" { } ''
100 with imaplib.IMAP4_SSL('${domain}') as imap:
101 imap.login('spam@${domain}', 'test123')
102 imap.create("\"INBOX/report_spam\"")
103 imap.create("\"INBOX/report_ham\"")
104 imap.create("\"INBOX/report_spam_reply\"")
106 imap.copy("1", "\"INBOX/report_ham\"")
109 (pkgs.writers.writePython3Bin "test-imap" { } ''
111 with imaplib.IMAP4_SSL('${domain}') as imap:
112 imap.login('spam@${domain}', 'test123')
113 imap.select("INBOX/learned_ham")
114 status, refs = imap.search(None, 'ALL')
115 assert status == 'OK'
116 assert len(refs) == 1
117 status, msg = imap.fetch(refs[0], 'BODY[TEXT]')
118 assert status == 'OK'
119 assert msg[0][1].strip() == b"Hello World"
130 testScript = { nodes }: ''
132 machine.wait_for_unit("maddy.service")
133 machine.wait_for_open_port(143)
134 machine.wait_for_open_port(993)
135 machine.wait_for_open_port(587)
136 machine.wait_for_open_port(465)
138 # Send test mail to spam@domain
139 machine.succeed("send-testmail")
141 # Create mail directories required for rspamd-trainer and copy mail from
142 # INBOX into INBOX/report_ham
143 machine.succeed("create-mail-dirs")
145 # Start rspamd-trainer. It should read mail from INBOX/report_ham
146 machine.wait_for_unit("rspamd.service")
147 machine.wait_for_unit("redis-rspamd.service")
148 machine.wait_for_file("/run/rspamd/rspamd.sock")
149 machine.succeed("systemctl start rspamd-trainer.service")
151 # Check if mail got processed by rspamd-trainer successfully and check for
152 # it in INBOX/learned_ham
153 machine.succeed("test-imap")