From 7b29338ff9646755e91442ff96f9968741daf1b6 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Tue, 31 Jan 2017 19:55:51 +0200 Subject: Start mail handling scenario --- 100-mail.yarn | 51 +++++++++++++++++++++++++++++++++++++++++++++++ 900-implements.yarn | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 100-mail.yarn diff --git a/100-mail.yarn b/100-mail.yarn new file mode 100644 index 0000000..48a2f1a --- /dev/null +++ b/100-mail.yarn @@ -0,0 +1,51 @@ +# Mail handling + +`pieni.net` is what handles all my incoming mail. There are several +important addresses on a couple of domains. Let's see if they all +work. We do this by sending mail (over SMTP+TLS) and checking it +arrives (over IMAP+TLS). For each mail we use a large random number as +the subject, making each mail unique and easy to check for. + +To run these tests we need to be able to log in via IMAP. For this, +we'll need passwords, and we will get the passwords with the `pass` +utility. Each password should be stored using a name +`server-yarns/imap/username@server` where `username` is the user and +`server` the address used for IMAP. + + SCENARIO server handles incoming mail + + GIVEN server is also known as pieni.net + + GIVEN large random number R + WHEN someone sends mail to postmaster@pieni.net with subject $R + THEN mailbox of liw has a mail with subject $R + + GIVEN large random number R + WHEN someone sends mail to root@pieni.net with subject $R + THEN mailbox of liw has a mail with subject $R + + GIVEN large random number R + WHEN someone sends mail to abuse@pieni.net with subject $R + THEN mailbox of liw has a mail with subject $R + + GIVEN large random number R + WHEN someone sends mail to postmaster@pieni.net with subject $R + THEN mailbox of liw has a mail with subject $R + + GIVEN server is also known as liw.fi + + GIVEN large random number R + WHEN someone sends mail to postmaster@liw.fi with subject $R + THEN mailbox of liw has a mail with subject $R + + GIVEN large random number R + WHEN someone sends mail to abuse@liw.fi with subject $R + THEN mailbox of liw has a mail with subject $R + + GIVEN large random number R + WHEN someone sends mail to liw@liw.fi with subject $R + THEN mailbox of liw has a mail with subject $R + + WHEN someone sends mail to bugs@liw.fi with subject $R + THEN mailbox of distix has a mail with subject $R + AND mailbox of distix has a mail with subject $R diff --git a/900-implements.yarn b/900-implements.yarn index e5943f7..9a2493f 100644 --- a/900-implements.yarn +++ b/900-implements.yarn @@ -87,3 +87,60 @@ print 'argv:', repr(h.get_variable('argv')) print 'stderr:', repr(stderr) h.assertNotEqual(m, None) + +# Mail handling tests + +## Generate a random number + + IMPLEMENTS GIVEN large random number (\S+) + import yarnhelper, random + h = yarnhelper.YarnHelper() + varname = h.get_next_match() + mini = 2**32 + maxi = 2**64 + r = random.randint(mini, maxi) + h.set_variable(varname, str(r)) + + +## Send mail over SMTP+TLS + + IMPLEMENTS WHEN someone sends mail to (\S+) with subject \$(\S+) + import smtplib, sys, yarnhelper + h = yarnhelper.YarnHelper() + to_addr = h.get_next_match() + subject_key = h.get_next_match() + server_name = h.get_variable('SERVER') + from_addr = 'someone@example.com' + msg = 'From: {}\nTo: {}\nSubject: {}\n\nThis is the body\n'.format( + from_addr, to_addr, subject) + server = smtplib.SMTP(server_name) + server.set_debuglevel(True) + server.starttls() + server.sendmail(from_addr, to_addr, msg) + server.quit() + +## Check for a mail in a mailbox + + IMPLEMENTS THEN mailbox of (\S+) has a mail with subject \$(\S+) + import email, imaplib, subprocess, yarnhelper + h = yarnhelper.YarnHelper() + user = h.get_next_match() + subject = h.get_next_match() + server = h.get_variable('SERVER') + pass_name = 'server-yarns/imap/{}@{}'.format(user, server) + p = subprocess.Popen(['pass', 'show', pass_name], stdout=subprocess.PIPE) + stdout, stderr = p.communicate() + password = stdout.rstrip() + m = imaplib.IMAP4_SSL(server) + m.login(user, password) + m.select('INBOX', True) + typ, data = m.search(None, 'ALL') + got_it = False + for num in data[0].split(): + typ, data = m.fetch(num, '(RFC822)') + typ, text = data[0] + msg = email.message_from_string(text) + got_it = got_it or (msg['Subject'] == subject) + m.close() + m.logout() + h.assertEqual(got_it, True) -- cgit v1.2.1