1import ./make-test.nix {
2 name = "dovecot";
3
4 machine = { pkgs, ... }: {
5 imports = [ common/user-account.nix ];
6 services.postfix.enable = true;
7 services.dovecot2.enable = true;
8 services.dovecot2.protocols = [ "imap" "pop3" ];
9 environment.systemPackages = let
10 sendTestMail = pkgs.writeScriptBin "send-testmail" ''
11 #!${pkgs.stdenv.shell}
12 exec sendmail -vt <<MAIL
13 From: root@localhost
14 To: alice@localhost
15 Subject: Very important!
16
17 Hello world!
18 MAIL
19 '';
20
21 testImap = pkgs.writeScriptBin "test-imap" ''
22 #!${pkgs.python3.interpreter}
23 import imaplib
24
25 with imaplib.IMAP4('localhost') as imap:
26 imap.login('alice', 'foobar')
27 imap.select()
28 status, refs = imap.search(None, 'ALL')
29 assert status == 'OK'
30 assert len(refs) == 1
31 status, msg = imap.fetch(refs[0], 'BODY[TEXT]')
32 assert status == 'OK'
33 assert msg[0][1].strip() == b'Hello world!'
34 '';
35
36 testPop = pkgs.writeScriptBin "test-pop" ''
37 #!${pkgs.python3.interpreter}
38 import poplib
39
40 pop = poplib.POP3('localhost')
41 try:
42 pop.user('alice')
43 pop.pass_('foobar')
44 assert len(pop.list()[1]) == 1
45 status, fullmail, size = pop.retr(1)
46 assert status.startswith(b'+OK ')
47 body = b"".join(fullmail[fullmail.index(b""):]).strip()
48 assert body == b'Hello world!'
49 finally:
50 pop.quit()
51 '';
52
53 in [ sendTestMail testImap testPop ];
54 };
55
56 testScript = ''
57 $machine->waitForUnit('postfix.service');
58 $machine->waitForUnit('dovecot2.service');
59 $machine->succeed('send-testmail');
60 $machine->waitUntilFails('[ "$(postqueue -p)" != "Mail queue is empty" ]');
61 $machine->succeed('test-imap');
62 $machine->succeed('test-pop');
63 '';
64}