| 
									
										
										
										
											2019-11-24 01:36:08 +01:00
										 |  |  | import ./make-test-python.nix { | 
					
						
							| 
									
										
										
										
											2017-09-17 03:11:01 +02:00
										 |  |  |   name = "dovecot"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   machine = { pkgs, ... }: { | 
					
						
							|  |  |  |     imports = [ common/user-account.nix ]; | 
					
						
							|  |  |  |     services.postfix.enable = true; | 
					
						
							| 
									
										
										
										
											2021-01-04 18:01:09 +01:00
										 |  |  |     services.dovecot2 = { | 
					
						
							|  |  |  |       enable = true; | 
					
						
							|  |  |  |       protocols = [ "imap" "pop3" ]; | 
					
						
							|  |  |  |       modules = [ pkgs.dovecot_pigeonhole ]; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2017-09-17 03:11:01 +02:00
										 |  |  |     environment.systemPackages = let | 
					
						
							|  |  |  |       sendTestMail = pkgs.writeScriptBin "send-testmail" ''
 | 
					
						
							| 
									
										
										
										
											2020-04-07 07:25:48 +01:00
										 |  |  |         #!${pkgs.runtimeShell} | 
					
						
							| 
									
										
										
										
											2017-09-17 03:11:01 +02:00
										 |  |  |         exec sendmail -vt <<MAIL | 
					
						
							|  |  |  |         From: root@localhost | 
					
						
							|  |  |  |         To: alice@localhost | 
					
						
							|  |  |  |         Subject: Very important! | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         Hello world! | 
					
						
							|  |  |  |         MAIL | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 01:35:14 +02:00
										 |  |  |       sendTestMailViaDeliveryAgent = pkgs.writeScriptBin "send-lda" ''
 | 
					
						
							| 
									
										
										
										
											2020-04-07 07:25:48 +01:00
										 |  |  |         #!${pkgs.runtimeShell} | 
					
						
							| 
									
										
										
										
											2018-04-12 01:35:14 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         exec ${pkgs.dovecot}/libexec/dovecot/deliver -d bob <<MAIL | 
					
						
							|  |  |  |         From: root@localhost | 
					
						
							|  |  |  |         To: bob@localhost | 
					
						
							|  |  |  |         Subject: Something else... | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         I'm running short of ideas! | 
					
						
							|  |  |  |         MAIL | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-17 03:11:01 +02:00
										 |  |  |       testImap = pkgs.writeScriptBin "test-imap" ''
 | 
					
						
							|  |  |  |         #!${pkgs.python3.interpreter} | 
					
						
							|  |  |  |         import imaplib | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         with imaplib.IMAP4('localhost') as imap: | 
					
						
							|  |  |  |           imap.login('alice', 'foobar') | 
					
						
							|  |  |  |           imap.select() | 
					
						
							|  |  |  |           status, refs = imap.search(None, 'ALL') | 
					
						
							|  |  |  |           assert status == 'OK' | 
					
						
							|  |  |  |           assert len(refs) == 1 | 
					
						
							|  |  |  |           status, msg = imap.fetch(refs[0], 'BODY[TEXT]') | 
					
						
							|  |  |  |           assert status == 'OK' | 
					
						
							|  |  |  |           assert msg[0][1].strip() == b'Hello world!' | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       testPop = pkgs.writeScriptBin "test-pop" ''
 | 
					
						
							|  |  |  |         #!${pkgs.python3.interpreter} | 
					
						
							|  |  |  |         import poplib | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         pop = poplib.POP3('localhost') | 
					
						
							|  |  |  |         try: | 
					
						
							| 
									
										
										
										
											2018-04-12 01:35:14 +02:00
										 |  |  |           pop.user('bob') | 
					
						
							| 
									
										
										
										
											2017-09-17 03:11:01 +02:00
										 |  |  |           pop.pass_('foobar') | 
					
						
							|  |  |  |           assert len(pop.list()[1]) == 1 | 
					
						
							|  |  |  |           status, fullmail, size = pop.retr(1) | 
					
						
							|  |  |  |           assert status.startswith(b'+OK ') | 
					
						
							|  |  |  |           body = b"".join(fullmail[fullmail.index(b""):]).strip() | 
					
						
							| 
									
										
										
										
											2018-04-12 01:35:14 +02:00
										 |  |  |           assert body == b"I'm running short of ideas!" | 
					
						
							| 
									
										
										
										
											2017-09-17 03:11:01 +02:00
										 |  |  |         finally: | 
					
						
							|  |  |  |           pop.quit() | 
					
						
							|  |  |  |       '';
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-04-12 01:35:14 +02:00
										 |  |  |     in [ sendTestMail sendTestMailViaDeliveryAgent testImap testPop ]; | 
					
						
							| 
									
										
										
										
											2017-09-17 03:11:01 +02:00
										 |  |  |   }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   testScript = ''
 | 
					
						
							| 
									
										
										
										
											2019-11-24 01:36:08 +01:00
										 |  |  |     machine.wait_for_unit("postfix.service") | 
					
						
							|  |  |  |     machine.wait_for_unit("dovecot2.service") | 
					
						
							|  |  |  |     machine.succeed("send-testmail") | 
					
						
							|  |  |  |     machine.succeed("send-lda") | 
					
						
							|  |  |  |     machine.wait_until_fails('[ "$(postqueue -p)" != "Mail queue is empty" ]') | 
					
						
							|  |  |  |     machine.succeed("test-imap") | 
					
						
							|  |  |  |     machine.succeed("test-pop") | 
					
						
							| 
									
										
										
										
											2017-09-17 03:11:01 +02:00
										 |  |  |   '';
 | 
					
						
							|  |  |  | } |