From 003c36c98a68a137a95bf105341bae4c0a301683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tim=20M=C3=B6hlmann?= Date: Wed, 31 Oct 2018 17:47:05 +0200 Subject: [PATCH 01/11] Fix imap login when no webmail selected --- core/admin/mailu/internal/views/dovecot.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/core/admin/mailu/internal/views/dovecot.py b/core/admin/mailu/internal/views/dovecot.py index 036140f0..e6e4c10f 100644 --- a/core/admin/mailu/internal/views/dovecot.py +++ b/core/admin/mailu/internal/views/dovecot.py @@ -3,7 +3,7 @@ from mailu.internal import internal import flask import socket - +import os @internal.route("/dovecot/passdb/") def dovecot_passdb_dict(user_email): @@ -13,7 +13,8 @@ def dovecot_passdb_dict(user_email): app.config.get("POD_ADDRESS_RANGE") or socket.gethostbyname(app.config["HOST_FRONT"]) ) - allow_nets.append(socket.gethostbyname(app.config["HOST_WEBMAIL"])) + if os.environ["WEBMAIL"] != "none": + allow_nets.append(socket.gethostbyname(app.config["HOST_WEBMAIL"])) print(allow_nets) return flask.jsonify({ "password": None, From 4e9dc0c3c93fbc65b2704b50b9f92dab46ceac86 Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Thu, 1 Nov 2018 11:40:54 +0200 Subject: [PATCH 02/11] Implemented sending/reading email(local tests) --- tests/certs/cert.pem | 29 ++++++++++++++ tests/certs/key.pem | 52 +++++++++++++++++++++++++ tests/compose/core/email_test.py | 55 +++++++++++++++++++++++++++ tests/compose/core/mailu.env | 2 +- tests/compose/fetchmail/email_test.py | 55 +++++++++++++++++++++++++++ tests/compose/fetchmail/mailu.env | 2 +- tests/compose/filters/email_test.py | 55 +++++++++++++++++++++++++++ tests/compose/filters/mailu.env | 2 +- tests/compose/rainloop/email_test.py | 55 +++++++++++++++++++++++++++ tests/compose/rainloop/mailu.env | 2 +- tests/compose/roundcube/email_test.py | 55 +++++++++++++++++++++++++++ tests/compose/roundcube/mailu.env | 2 +- tests/compose/test.py | 14 ++++++- tests/compose/webdav/email_test.py | 55 +++++++++++++++++++++++++++ tests/compose/webdav/mailu.env | 2 +- 15 files changed, 430 insertions(+), 7 deletions(-) create mode 100644 tests/certs/cert.pem create mode 100644 tests/certs/key.pem create mode 100755 tests/compose/core/email_test.py create mode 100755 tests/compose/fetchmail/email_test.py create mode 100755 tests/compose/filters/email_test.py create mode 100755 tests/compose/rainloop/email_test.py create mode 100755 tests/compose/roundcube/email_test.py create mode 100755 tests/compose/webdav/email_test.py diff --git a/tests/certs/cert.pem b/tests/certs/cert.pem new file mode 100644 index 00000000..d6dc928f --- /dev/null +++ b/tests/certs/cert.pem @@ -0,0 +1,29 @@ +-----BEGIN CERTIFICATE----- +MIIE/jCCAuagAwIBAgIJAKVnyadXS7SuMA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNV +BAMMCWxvY2FsaG9zdDAeFw0xODEwMzExMDE1MzFaFw0yODEwMjgxMDE1MzFaMBQx +EjAQBgNVBAMMCWxvY2FsaG9zdDCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoC +ggIBAOQ2ZDqR+YvW5FKykBXz/Ec+jSb0Lv7GYQkT5t+TB1NXuR+QH1LfNWFmXOo7 +YXcPVXlmcuLDuUldrctdS59fx8dnFu5gRRUqJwZuEQICypsX0rTDtsV6xqZB8c8y +2+BztP9OHfPpZdnU1IBx2fDbjpdKUaoAMFMFvyTaEcIyp6aGAhejvJCwc3D8fIJI +NhWA2O11sZQHUs7/MHzpu/IHpgutgk8EsNOUNLwB3+9p3IlOlTT6GilIXOYeTzoD +hiI6B5BQqXHsRrkao3v0YL6Ekun4hOx3MYx09AZtmuyrlq1mkNueKS5JwKDrXXbq +Ta0oyJ18UTZFRwVqApcuR4CA8vuhI9PsoDCvBQH1rW6FyiM4bhybatFJAYjQAODe +gwh2p6JWux5C1gaBUubOrKO7o5ePI6s0MmK8ZxrL4PpBYt3B33ztFfjWmVbCTSvP +GuQ2Ux73OY2NNxx2aNt4Th0IxrvMdsG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ZX9Uw== +-----END CERTIFICATE----- diff --git a/tests/certs/key.pem b/tests/certs/key.pem new file mode 100644 index 00000000..938633fd --- /dev/null +++ b/tests/certs/key.pem @@ -0,0 +1,52 @@ +-----BEGIN PRIVATE KEY----- +MIIJQwIBADANBgkqhkiG9w0BAQEFAASCCS0wggkpAgEAAoICAQDkNmQ6kfmL1uRS +spAV8/xHPo0m9C7+xmEJE+bfkwdTV7kfkB9S3zVhZlzqO2F3D1V5ZnLiw7lJXa3L +XUufX8fHZxbuYEUVKicGbhECAsqbF9K0w7bFesamQfHPMtvgc7T/Th3z6WXZ1NSA +cdnw246XSlGqADBTBb8k2hHCMqemhgIXo7yQsHNw/HyCSDYVgNjtdbGUB1LO/zB8 +6bvyB6YLrYJPBLDTlDS8Ad/vadyJTpU0+hopSFzmHk86A4YiOgeQUKlx7Ea5GqN7 +9GC+hJLp+ITsdzGMdPQGbZrsq5atZpDbnikuScCg61126k2tKMidfFE2RUcFagKX +LkeAgPL7oSPT7KAwrwUB9a1uhcojOG4cm2rRSQGI0ADg3oMIdqeiVrseQtYGgVLm +zqyju6OXjyOrNDJivGcay+D6QWLdwd987RX41plWwk0rzxrkNlMe9zmNjTccdmjb +eE4dCMa7zHbBi62bHZmtq1muXkyUwLqm0oyP1m9c4ztKYqcKMZOn9QjhS6KaNsYC +Y+viihtZyi3zs0BXsXKXav5qbrpXFXCwzycyUy196rILQ4gaZtaMzUU/jO11Gr4P +GhzX5+kvk3A2+YaftcmeqG2av1ZQOmrZNH7j83/p3UNH6mDdY5QCM2V7fw6XPjm4 +0tPVhg1UF6GxnrHcAOgR9eDlfk8ThwIDAQABAoICACoHsnHvDIyqqSZp6IuCggYF +CS4Rbs5RbvGjDrRCeejpkRi1DG/Q2B32IkqpYQvycQWIzsPg1DEk5as8pX7Wvw6E +d/6zEEYTm1hd0RgTt4jU3GOaYAEC2a8pGgXVEhXGeaFDm9SeObnirrhxP3hSl3JZ +p6ytmDjSKB/7YaXoemP67ku4RjRHqxs2BS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azbhi/3ghLmDGVJ +p5OrIPQUwcp1bFOciX22fAaZwoa63ng3K+WZjSqqma05AiOc59MhDLAu6a0rKKO1 +W3079UVfBB4hkfN2721fqyj+r/0z+R0= +-----END PRIVATE KEY----- diff --git a/tests/compose/core/email_test.py b/tests/compose/core/email_test.py new file mode 100755 index 00000000..c16ce9d8 --- /dev/null +++ b/tests/compose/core/email_test.py @@ -0,0 +1,55 @@ +import string +import random +import smtplib +import imaplib +import time + +def secret(length=16): + charset = string.ascii_uppercase + string.digits + return ''.join( + random.SystemRandom().choice(charset) + for _ in range(length) + ) + +#Generating secret message +secret_message = secret(16) + +#Login to smt server and sending email with secret message +def send_email(msg): + print("Sending email ...") + server = smtplib.SMTP('localhost') + server.set_debuglevel(1) + server.connect('localhost', 587) + server.ehlo() + server.starttls() + server.ehlo() + server.login("admin@mailu.io", "password") + + server.sendmail("admin@mailu.io", "user@mailu.io", msg) + server.quit() + + print("email sent with message " + msg) + +#Login to imap server, read latest email and check for secret message +def read_email(): + print("Receiving email ...") + server = imaplib.IMAP4_SSL('localhost') + server.login('user@mailu.io', 'password') + + stat, count = server.select('inbox') + stat, data = server.fetch(count[0], '(UID BODY[TEXT])') + + print("email received with message " + str(data[0][1])) + + if secret_message in str(data[0][1]): + print("Success!") + else: + print("Failed! Something went wrong") + server.close() + server.logout() + + +send_email(secret_message) +print("Sleeping for 1m") +time.sleep(60) +read_email() diff --git a/tests/compose/core/mailu.env b/tests/compose/core/mailu.env index dabd523f..9a744e35 100644 --- a/tests/compose/core/mailu.env +++ b/tests/compose/core/mailu.env @@ -29,7 +29,7 @@ SECRET_KEY=HGZCYGVI6FVG31HS DOMAIN=mailu.io # Hostnames for this server, separated with comas -HOSTNAMES=mail.mailu.io +HOSTNAMES=localhost # Postmaster local part (will append the main mail domain) POSTMASTER=admin diff --git a/tests/compose/fetchmail/email_test.py b/tests/compose/fetchmail/email_test.py new file mode 100755 index 00000000..c16ce9d8 --- /dev/null +++ b/tests/compose/fetchmail/email_test.py @@ -0,0 +1,55 @@ +import string +import random +import smtplib +import imaplib +import time + +def secret(length=16): + charset = string.ascii_uppercase + string.digits + return ''.join( + random.SystemRandom().choice(charset) + for _ in range(length) + ) + +#Generating secret message +secret_message = secret(16) + +#Login to smt server and sending email with secret message +def send_email(msg): + print("Sending email ...") + server = smtplib.SMTP('localhost') + server.set_debuglevel(1) + server.connect('localhost', 587) + server.ehlo() + server.starttls() + server.ehlo() + server.login("admin@mailu.io", "password") + + server.sendmail("admin@mailu.io", "user@mailu.io", msg) + server.quit() + + print("email sent with message " + msg) + +#Login to imap server, read latest email and check for secret message +def read_email(): + print("Receiving email ...") + server = imaplib.IMAP4_SSL('localhost') + server.login('user@mailu.io', 'password') + + stat, count = server.select('inbox') + stat, data = server.fetch(count[0], '(UID BODY[TEXT])') + + print("email received with message " + str(data[0][1])) + + if secret_message in str(data[0][1]): + print("Success!") + else: + print("Failed! Something went wrong") + server.close() + server.logout() + + +send_email(secret_message) +print("Sleeping for 1m") +time.sleep(60) +read_email() diff --git a/tests/compose/fetchmail/mailu.env b/tests/compose/fetchmail/mailu.env index ef78508b..a987c853 100644 --- a/tests/compose/fetchmail/mailu.env +++ b/tests/compose/fetchmail/mailu.env @@ -29,7 +29,7 @@ SECRET_KEY=JS48Q9KE3B6T97E6 DOMAIN=mailu.io # Hostnames for this server, separated with comas -HOSTNAMES=mail.mailu.io +HOSTNAMES=localhost # Postmaster local part (will append the main mail domain) POSTMASTER=admin diff --git a/tests/compose/filters/email_test.py b/tests/compose/filters/email_test.py new file mode 100755 index 00000000..c16ce9d8 --- /dev/null +++ b/tests/compose/filters/email_test.py @@ -0,0 +1,55 @@ +import string +import random +import smtplib +import imaplib +import time + +def secret(length=16): + charset = string.ascii_uppercase + string.digits + return ''.join( + random.SystemRandom().choice(charset) + for _ in range(length) + ) + +#Generating secret message +secret_message = secret(16) + +#Login to smt server and sending email with secret message +def send_email(msg): + print("Sending email ...") + server = smtplib.SMTP('localhost') + server.set_debuglevel(1) + server.connect('localhost', 587) + server.ehlo() + server.starttls() + server.ehlo() + server.login("admin@mailu.io", "password") + + server.sendmail("admin@mailu.io", "user@mailu.io", msg) + server.quit() + + print("email sent with message " + msg) + +#Login to imap server, read latest email and check for secret message +def read_email(): + print("Receiving email ...") + server = imaplib.IMAP4_SSL('localhost') + server.login('user@mailu.io', 'password') + + stat, count = server.select('inbox') + stat, data = server.fetch(count[0], '(UID BODY[TEXT])') + + print("email received with message " + str(data[0][1])) + + if secret_message in str(data[0][1]): + print("Success!") + else: + print("Failed! Something went wrong") + server.close() + server.logout() + + +send_email(secret_message) +print("Sleeping for 1m") +time.sleep(60) +read_email() diff --git a/tests/compose/filters/mailu.env b/tests/compose/filters/mailu.env index ab97e191..8609a287 100644 --- a/tests/compose/filters/mailu.env +++ b/tests/compose/filters/mailu.env @@ -29,7 +29,7 @@ SECRET_KEY=11H6XURLGE7GW3U1 DOMAIN=mailu.io # Hostnames for this server, separated with comas -HOSTNAMES=mail.mailu.io +HOSTNAMES=localhost # Postmaster local part (will append the main mail domain) POSTMASTER=admin diff --git a/tests/compose/rainloop/email_test.py b/tests/compose/rainloop/email_test.py new file mode 100755 index 00000000..c16ce9d8 --- /dev/null +++ b/tests/compose/rainloop/email_test.py @@ -0,0 +1,55 @@ +import string +import random +import smtplib +import imaplib +import time + +def secret(length=16): + charset = string.ascii_uppercase + string.digits + return ''.join( + random.SystemRandom().choice(charset) + for _ in range(length) + ) + +#Generating secret message +secret_message = secret(16) + +#Login to smt server and sending email with secret message +def send_email(msg): + print("Sending email ...") + server = smtplib.SMTP('localhost') + server.set_debuglevel(1) + server.connect('localhost', 587) + server.ehlo() + server.starttls() + server.ehlo() + server.login("admin@mailu.io", "password") + + server.sendmail("admin@mailu.io", "user@mailu.io", msg) + server.quit() + + print("email sent with message " + msg) + +#Login to imap server, read latest email and check for secret message +def read_email(): + print("Receiving email ...") + server = imaplib.IMAP4_SSL('localhost') + server.login('user@mailu.io', 'password') + + stat, count = server.select('inbox') + stat, data = server.fetch(count[0], '(UID BODY[TEXT])') + + print("email received with message " + str(data[0][1])) + + if secret_message in str(data[0][1]): + print("Success!") + else: + print("Failed! Something went wrong") + server.close() + server.logout() + + +send_email(secret_message) +print("Sleeping for 1m") +time.sleep(60) +read_email() diff --git a/tests/compose/rainloop/mailu.env b/tests/compose/rainloop/mailu.env index 747dac58..678ea048 100644 --- a/tests/compose/rainloop/mailu.env +++ b/tests/compose/rainloop/mailu.env @@ -29,7 +29,7 @@ SECRET_KEY=V5J4SHRYVW9PZIQU DOMAIN=mailu.io # Hostnames for this server, separated with comas -HOSTNAMES=mail.mailu.io +HOSTNAMES=localhost # Postmaster local part (will append the main mail domain) POSTMASTER=admin diff --git a/tests/compose/roundcube/email_test.py b/tests/compose/roundcube/email_test.py new file mode 100755 index 00000000..c16ce9d8 --- /dev/null +++ b/tests/compose/roundcube/email_test.py @@ -0,0 +1,55 @@ +import string +import random +import smtplib +import imaplib +import time + +def secret(length=16): + charset = string.ascii_uppercase + string.digits + return ''.join( + random.SystemRandom().choice(charset) + for _ in range(length) + ) + +#Generating secret message +secret_message = secret(16) + +#Login to smt server and sending email with secret message +def send_email(msg): + print("Sending email ...") + server = smtplib.SMTP('localhost') + server.set_debuglevel(1) + server.connect('localhost', 587) + server.ehlo() + server.starttls() + server.ehlo() + server.login("admin@mailu.io", "password") + + server.sendmail("admin@mailu.io", "user@mailu.io", msg) + server.quit() + + print("email sent with message " + msg) + +#Login to imap server, read latest email and check for secret message +def read_email(): + print("Receiving email ...") + server = imaplib.IMAP4_SSL('localhost') + server.login('user@mailu.io', 'password') + + stat, count = server.select('inbox') + stat, data = server.fetch(count[0], '(UID BODY[TEXT])') + + print("email received with message " + str(data[0][1])) + + if secret_message in str(data[0][1]): + print("Success!") + else: + print("Failed! Something went wrong") + server.close() + server.logout() + + +send_email(secret_message) +print("Sleeping for 1m") +time.sleep(60) +read_email() diff --git a/tests/compose/roundcube/mailu.env b/tests/compose/roundcube/mailu.env index e87d1f61..b8a8b266 100644 --- a/tests/compose/roundcube/mailu.env +++ b/tests/compose/roundcube/mailu.env @@ -29,7 +29,7 @@ SECRET_KEY=PGGO2JRQ59QV3DW7 DOMAIN=mailu.io # Hostnames for this server, separated with comas -HOSTNAMES=mail.mailu.io +HOSTNAMES=localhost # Postmaster local part (will append the main mail domain) POSTMASTER=admin diff --git a/tests/compose/test.py b/tests/compose/test.py index 819c7222..d3b2d109 100755 --- a/tests/compose/test.py +++ b/tests/compose/test.py @@ -74,8 +74,19 @@ def hooks(): os.system("python3 " + test_path + test_file) elif test_file.endswith(".sh"): os.system("./" + test_path + test_file) - + +#Create admin and user +def create_users(): + print("Creating admin account...") + os.system("docker-compose -p $DOCKER_ORG exec admin python manage.py admin admin mailu.io password") + print("Admin account created") + print("Creating user account...") + os.system("docker-compose -p $DOCKER_ORG exec admin python manage.py user --hash_scheme='SHA512-CRYPT' user mailu.io 'password'") + print("User account created") + # Start up containers +os.system("mkdir -p /mailu && cp -r tests/certs /mailu") +os.system("chmod 600 /mailu/certs/* ") os.system("docker-compose -f " + compose_file + " -p ${DOCKER_ORG:-mailu} up -d ") print() sleep() @@ -84,6 +95,7 @@ os.system("docker ps -a") print() health_checks() print() +create_users() hooks() print() stop(0) diff --git a/tests/compose/webdav/email_test.py b/tests/compose/webdav/email_test.py new file mode 100755 index 00000000..c16ce9d8 --- /dev/null +++ b/tests/compose/webdav/email_test.py @@ -0,0 +1,55 @@ +import string +import random +import smtplib +import imaplib +import time + +def secret(length=16): + charset = string.ascii_uppercase + string.digits + return ''.join( + random.SystemRandom().choice(charset) + for _ in range(length) + ) + +#Generating secret message +secret_message = secret(16) + +#Login to smt server and sending email with secret message +def send_email(msg): + print("Sending email ...") + server = smtplib.SMTP('localhost') + server.set_debuglevel(1) + server.connect('localhost', 587) + server.ehlo() + server.starttls() + server.ehlo() + server.login("admin@mailu.io", "password") + + server.sendmail("admin@mailu.io", "user@mailu.io", msg) + server.quit() + + print("email sent with message " + msg) + +#Login to imap server, read latest email and check for secret message +def read_email(): + print("Receiving email ...") + server = imaplib.IMAP4_SSL('localhost') + server.login('user@mailu.io', 'password') + + stat, count = server.select('inbox') + stat, data = server.fetch(count[0], '(UID BODY[TEXT])') + + print("email received with message " + str(data[0][1])) + + if secret_message in str(data[0][1]): + print("Success!") + else: + print("Failed! Something went wrong") + server.close() + server.logout() + + +send_email(secret_message) +print("Sleeping for 1m") +time.sleep(60) +read_email() diff --git a/tests/compose/webdav/mailu.env b/tests/compose/webdav/mailu.env index ac7b434c..21dd3981 100644 --- a/tests/compose/webdav/mailu.env +++ b/tests/compose/webdav/mailu.env @@ -29,7 +29,7 @@ SECRET_KEY=XVDDSWOAGVF5J9QJ DOMAIN=mailu.io # Hostnames for this server, separated with comas -HOSTNAMES=mail.mailu.io +HOSTNAMES=localhost # Postmaster local part (will append the main mail domain) POSTMASTER=admin From c3bc7988c90d25d65d1eb8d2142a58686bacab40 Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Fri, 2 Nov 2018 14:27:46 +0200 Subject: [PATCH 03/11] Implemented email test for travis --- .travis.yml | 4 ++ tests/compose/{core => }/email_test.py | 0 tests/compose/fetchmail/email_test.py | 55 -------------------------- tests/compose/filters/email_test.py | 55 -------------------------- tests/compose/rainloop/email_test.py | 55 -------------------------- tests/compose/roundcube/email_test.py | 55 -------------------------- tests/compose/test.py | 15 ++----- tests/compose/webdav/email_test.py | 55 -------------------------- 8 files changed, 7 insertions(+), 287 deletions(-) rename tests/compose/{core => }/email_test.py (100%) delete mode 100755 tests/compose/fetchmail/email_test.py delete mode 100755 tests/compose/filters/email_test.py delete mode 100755 tests/compose/rainloop/email_test.py delete mode 100755 tests/compose/roundcube/email_test.py delete mode 100755 tests/compose/webdav/email_test.py diff --git a/.travis.yml b/.travis.yml index 54b3b3c7..ffb6af43 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,6 +18,10 @@ install: before_script: - docker-compose -v - docker-compose -f tests/build.yml build + - docker-compose up -d admin + - docker-compose exec admin python manage.py admin admin mailu.io password + - docker-compose exec admin python manage.py user --hash_scheme='SHA512-CRYPT' user mailu.io 'password' + - docker-compose down script: # test.py, test name and timeout between start and tests. diff --git a/tests/compose/core/email_test.py b/tests/compose/email_test.py similarity index 100% rename from tests/compose/core/email_test.py rename to tests/compose/email_test.py diff --git a/tests/compose/fetchmail/email_test.py b/tests/compose/fetchmail/email_test.py deleted file mode 100755 index c16ce9d8..00000000 --- a/tests/compose/fetchmail/email_test.py +++ /dev/null @@ -1,55 +0,0 @@ -import string -import random -import smtplib -import imaplib -import time - -def secret(length=16): - charset = string.ascii_uppercase + string.digits - return ''.join( - random.SystemRandom().choice(charset) - for _ in range(length) - ) - -#Generating secret message -secret_message = secret(16) - -#Login to smt server and sending email with secret message -def send_email(msg): - print("Sending email ...") - server = smtplib.SMTP('localhost') - server.set_debuglevel(1) - server.connect('localhost', 587) - server.ehlo() - server.starttls() - server.ehlo() - server.login("admin@mailu.io", "password") - - server.sendmail("admin@mailu.io", "user@mailu.io", msg) - server.quit() - - print("email sent with message " + msg) - -#Login to imap server, read latest email and check for secret message -def read_email(): - print("Receiving email ...") - server = imaplib.IMAP4_SSL('localhost') - server.login('user@mailu.io', 'password') - - stat, count = server.select('inbox') - stat, data = server.fetch(count[0], '(UID BODY[TEXT])') - - print("email received with message " + str(data[0][1])) - - if secret_message in str(data[0][1]): - print("Success!") - else: - print("Failed! Something went wrong") - server.close() - server.logout() - - -send_email(secret_message) -print("Sleeping for 1m") -time.sleep(60) -read_email() diff --git a/tests/compose/filters/email_test.py b/tests/compose/filters/email_test.py deleted file mode 100755 index c16ce9d8..00000000 --- a/tests/compose/filters/email_test.py +++ /dev/null @@ -1,55 +0,0 @@ -import string -import random -import smtplib -import imaplib -import time - -def secret(length=16): - charset = string.ascii_uppercase + string.digits - return ''.join( - random.SystemRandom().choice(charset) - for _ in range(length) - ) - -#Generating secret message -secret_message = secret(16) - -#Login to smt server and sending email with secret message -def send_email(msg): - print("Sending email ...") - server = smtplib.SMTP('localhost') - server.set_debuglevel(1) - server.connect('localhost', 587) - server.ehlo() - server.starttls() - server.ehlo() - server.login("admin@mailu.io", "password") - - server.sendmail("admin@mailu.io", "user@mailu.io", msg) - server.quit() - - print("email sent with message " + msg) - -#Login to imap server, read latest email and check for secret message -def read_email(): - print("Receiving email ...") - server = imaplib.IMAP4_SSL('localhost') - server.login('user@mailu.io', 'password') - - stat, count = server.select('inbox') - stat, data = server.fetch(count[0], '(UID BODY[TEXT])') - - print("email received with message " + str(data[0][1])) - - if secret_message in str(data[0][1]): - print("Success!") - else: - print("Failed! Something went wrong") - server.close() - server.logout() - - -send_email(secret_message) -print("Sleeping for 1m") -time.sleep(60) -read_email() diff --git a/tests/compose/rainloop/email_test.py b/tests/compose/rainloop/email_test.py deleted file mode 100755 index c16ce9d8..00000000 --- a/tests/compose/rainloop/email_test.py +++ /dev/null @@ -1,55 +0,0 @@ -import string -import random -import smtplib -import imaplib -import time - -def secret(length=16): - charset = string.ascii_uppercase + string.digits - return ''.join( - random.SystemRandom().choice(charset) - for _ in range(length) - ) - -#Generating secret message -secret_message = secret(16) - -#Login to smt server and sending email with secret message -def send_email(msg): - print("Sending email ...") - server = smtplib.SMTP('localhost') - server.set_debuglevel(1) - server.connect('localhost', 587) - server.ehlo() - server.starttls() - server.ehlo() - server.login("admin@mailu.io", "password") - - server.sendmail("admin@mailu.io", "user@mailu.io", msg) - server.quit() - - print("email sent with message " + msg) - -#Login to imap server, read latest email and check for secret message -def read_email(): - print("Receiving email ...") - server = imaplib.IMAP4_SSL('localhost') - server.login('user@mailu.io', 'password') - - stat, count = server.select('inbox') - stat, data = server.fetch(count[0], '(UID BODY[TEXT])') - - print("email received with message " + str(data[0][1])) - - if secret_message in str(data[0][1]): - print("Success!") - else: - print("Failed! Something went wrong") - server.close() - server.logout() - - -send_email(secret_message) -print("Sleeping for 1m") -time.sleep(60) -read_email() diff --git a/tests/compose/roundcube/email_test.py b/tests/compose/roundcube/email_test.py deleted file mode 100755 index c16ce9d8..00000000 --- a/tests/compose/roundcube/email_test.py +++ /dev/null @@ -1,55 +0,0 @@ -import string -import random -import smtplib -import imaplib -import time - -def secret(length=16): - charset = string.ascii_uppercase + string.digits - return ''.join( - random.SystemRandom().choice(charset) - for _ in range(length) - ) - -#Generating secret message -secret_message = secret(16) - -#Login to smt server and sending email with secret message -def send_email(msg): - print("Sending email ...") - server = smtplib.SMTP('localhost') - server.set_debuglevel(1) - server.connect('localhost', 587) - server.ehlo() - server.starttls() - server.ehlo() - server.login("admin@mailu.io", "password") - - server.sendmail("admin@mailu.io", "user@mailu.io", msg) - server.quit() - - print("email sent with message " + msg) - -#Login to imap server, read latest email and check for secret message -def read_email(): - print("Receiving email ...") - server = imaplib.IMAP4_SSL('localhost') - server.login('user@mailu.io', 'password') - - stat, count = server.select('inbox') - stat, data = server.fetch(count[0], '(UID BODY[TEXT])') - - print("email received with message " + str(data[0][1])) - - if secret_message in str(data[0][1]): - print("Success!") - else: - print("Failed! Something went wrong") - server.close() - server.logout() - - -send_email(secret_message) -print("Sleeping for 1m") -time.sleep(60) -read_email() diff --git a/tests/compose/test.py b/tests/compose/test.py index d3b2d109..84281123 100755 --- a/tests/compose/test.py +++ b/tests/compose/test.py @@ -17,7 +17,7 @@ containers = [] # Stop containers def stop(exit_code): print_logs() - os.system("docker-compose -f " + compose_file + " -p ${DOCKER_ORG:-mailu} down") + os.system("docker-compose -f " + compose_file + " down") sys.exit(exit_code) # Sleep for a defined amount of time @@ -69,25 +69,17 @@ def print_logs(): #Iterating over hooks in test folder and running them def hooks(): print("Running hooks") + os.system("python3 tests/compose/email_test.py") for test_file in sorted(os.listdir(test_path)): if test_file.endswith(".py"): os.system("python3 " + test_path + test_file) elif test_file.endswith(".sh"): os.system("./" + test_path + test_file) -#Create admin and user -def create_users(): - print("Creating admin account...") - os.system("docker-compose -p $DOCKER_ORG exec admin python manage.py admin admin mailu.io password") - print("Admin account created") - print("Creating user account...") - os.system("docker-compose -p $DOCKER_ORG exec admin python manage.py user --hash_scheme='SHA512-CRYPT' user mailu.io 'password'") - print("User account created") - # Start up containers os.system("mkdir -p /mailu && cp -r tests/certs /mailu") os.system("chmod 600 /mailu/certs/* ") -os.system("docker-compose -f " + compose_file + " -p ${DOCKER_ORG:-mailu} up -d ") +os.system("docker-compose -f " + compose_file + " up -d ") print() sleep() print() @@ -95,7 +87,6 @@ os.system("docker ps -a") print() health_checks() print() -create_users() hooks() print() stop(0) diff --git a/tests/compose/webdav/email_test.py b/tests/compose/webdav/email_test.py deleted file mode 100755 index c16ce9d8..00000000 --- a/tests/compose/webdav/email_test.py +++ /dev/null @@ -1,55 +0,0 @@ -import string -import random -import smtplib -import imaplib -import time - -def secret(length=16): - charset = string.ascii_uppercase + string.digits - return ''.join( - random.SystemRandom().choice(charset) - for _ in range(length) - ) - -#Generating secret message -secret_message = secret(16) - -#Login to smt server and sending email with secret message -def send_email(msg): - print("Sending email ...") - server = smtplib.SMTP('localhost') - server.set_debuglevel(1) - server.connect('localhost', 587) - server.ehlo() - server.starttls() - server.ehlo() - server.login("admin@mailu.io", "password") - - server.sendmail("admin@mailu.io", "user@mailu.io", msg) - server.quit() - - print("email sent with message " + msg) - -#Login to imap server, read latest email and check for secret message -def read_email(): - print("Receiving email ...") - server = imaplib.IMAP4_SSL('localhost') - server.login('user@mailu.io', 'password') - - stat, count = server.select('inbox') - stat, data = server.fetch(count[0], '(UID BODY[TEXT])') - - print("email received with message " + str(data[0][1])) - - if secret_message in str(data[0][1]): - print("Success!") - else: - print("Failed! Something went wrong") - server.close() - server.logout() - - -send_email(secret_message) -print("Sleeping for 1m") -time.sleep(60) -read_email() From 88f5e6e4cff54757bbbd3bd8653d8f8cad7bee51 Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Fri, 2 Nov 2018 15:05:16 +0200 Subject: [PATCH 04/11] Moved users creation in core --- .travis.yml | 4 ---- tests/compose/core/00_create_users.sh | 3 +++ tests/compose/test.py | 21 +++++++++++---------- 3 files changed, 14 insertions(+), 14 deletions(-) create mode 100755 tests/compose/core/00_create_users.sh diff --git a/.travis.yml b/.travis.yml index ffb6af43..54b3b3c7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,10 +18,6 @@ install: before_script: - docker-compose -v - docker-compose -f tests/build.yml build - - docker-compose up -d admin - - docker-compose exec admin python manage.py admin admin mailu.io password - - docker-compose exec admin python manage.py user --hash_scheme='SHA512-CRYPT' user mailu.io 'password' - - docker-compose down script: # test.py, test name and timeout between start and tests. diff --git a/tests/compose/core/00_create_users.sh b/tests/compose/core/00_create_users.sh new file mode 100755 index 00000000..babe307d --- /dev/null +++ b/tests/compose/core/00_create_users.sh @@ -0,0 +1,3 @@ +echo "Creating users ..." +docker-compose -f tests/compose/core/docker-compose.yml exec admin python manage.py admin admin mailu.io password || exit 1 +docker-compose -f tests/compose/core/docker-compose.yml exec admin python manage.py user --hash_scheme='SHA512-CRYPT' user mailu.io 'password' || exit 1 diff --git a/tests/compose/test.py b/tests/compose/test.py index 84281123..c90f481b 100755 --- a/tests/compose/test.py +++ b/tests/compose/test.py @@ -19,19 +19,19 @@ def stop(exit_code): print_logs() os.system("docker-compose -f " + compose_file + " down") sys.exit(exit_code) - + # Sleep for a defined amount of time def sleep(): print(Fore.LIGHTMAGENTA_EX + "Sleeping for " + str(timeout) + "m" + Style.RESET_ALL) time.sleep(timeout*60) - + def health_checks(): exit_code = 0 #Iterating trough all containers dictionary for container in client.containers(all=True): #Perform "docker container inspect" on container based on container ID and save output to a dictionary container_inspect = client.inspect_container(container['Id']) #Dict - + if "Health" in container_inspect['State'].keys(): if container_inspect['State']['Health']['Status'] == "healthy": print(Fore.GREEN + "Health status for " + container_inspect['Name'].replace("/", "") + " : " + Fore.CYAN + container_inspect['State']['Health']['Status'] + Style.RESET_ALL) @@ -46,36 +46,37 @@ def health_checks(): if container_inspect['State']['Status'] != "running": print(Fore.RED + "Container " + container_inspect['Name'].replace("/", "") + " state is: " + Fore.YELLOW + container_inspect['State']['Status'] + Style.RESET_ALL) exit_code = 1 - + #Saving Id, Name and state to a new dictionary containers_dict = {} containers_dict['Name'] = container_inspect['Name'].replace("/", "") containers_dict['Id'] = container_inspect['Id'] containers_dict['State'] = container_inspect['State'] - + #Adding the generated dictionary to a list containers.append(containers_dict) - + if exit_code != 0: stop(exit_code) - + def print_logs(): print("Printing logs ...") #Iterating through docker container inspect list and print logs for container in containers: print(Fore.LIGHTMAGENTA_EX + "Printing logs for: " + Fore.GREEN + container['Name'] + Style.RESET_ALL) os.system('docker container logs ' + container['Name']) - -#Iterating over hooks in test folder and running them + +#Iterating over hooks in test folder and running them def hooks(): print("Running hooks") - os.system("python3 tests/compose/email_test.py") for test_file in sorted(os.listdir(test_path)): if test_file.endswith(".py"): os.system("python3 " + test_path + test_file) elif test_file.endswith(".sh"): os.system("./" + test_path + test_file) + os.system("python3 tests/compose/email_test.py") + # Start up containers os.system("mkdir -p /mailu && cp -r tests/certs /mailu") os.system("chmod 600 /mailu/certs/* ") From fd8ed3dfa6c901bd2a493d64042ac5a6e3c4bd39 Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Fri, 2 Nov 2018 15:17:59 +0200 Subject: [PATCH 05/11] Moved certs copy in travis --- .travis.yml | 3 ++- tests/compose/test.py | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 54b3b3c7..a0762280 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,8 @@ install: before_script: - docker-compose -v - docker-compose -f tests/build.yml build - + - mkdir -p /mailu && sudo cp -r tests/certs /mailu && chmod 600 /mailu/certs/* + script: # test.py, test name and timeout between start and tests. - python tests/compose/test.py core 1 diff --git a/tests/compose/test.py b/tests/compose/test.py index c90f481b..a44382a1 100755 --- a/tests/compose/test.py +++ b/tests/compose/test.py @@ -78,8 +78,6 @@ def hooks(): os.system("python3 tests/compose/email_test.py") # Start up containers -os.system("mkdir -p /mailu && cp -r tests/certs /mailu") -os.system("chmod 600 /mailu/certs/* ") os.system("docker-compose -f " + compose_file + " up -d ") print() sleep() From 0d6a203a9d8009e8c7348f9f7692c748bf6fdba3 Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Fri, 2 Nov 2018 15:32:36 +0200 Subject: [PATCH 06/11] Use sudo for running sh commands --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index a0762280..22e024ec 100644 --- a/.travis.yml +++ b/.travis.yml @@ -18,7 +18,7 @@ install: before_script: - docker-compose -v - docker-compose -f tests/build.yml build - - mkdir -p /mailu && sudo cp -r tests/certs /mailu && chmod 600 /mailu/certs/* + - sudo -- sh -c 'mkdir -p /mailu && cp -r tests/certs /mailu && chmod 600 /mailu/certs/*' script: # test.py, test name and timeout between start and tests. From 982e586e499a6bc30e1961882ba2c14291e71396 Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Fri, 2 Nov 2018 16:25:55 +0200 Subject: [PATCH 07/11] Replaced os.system calls with native python calls --- tests/compose/core/00_create_users.sh | 1 + tests/compose/test.py | 14 +++++++------- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/tests/compose/core/00_create_users.sh b/tests/compose/core/00_create_users.sh index babe307d..fd998a15 100755 --- a/tests/compose/core/00_create_users.sh +++ b/tests/compose/core/00_create_users.sh @@ -1,3 +1,4 @@ echo "Creating users ..." docker-compose -f tests/compose/core/docker-compose.yml exec admin python manage.py admin admin mailu.io password || exit 1 docker-compose -f tests/compose/core/docker-compose.yml exec admin python manage.py user --hash_scheme='SHA512-CRYPT' user mailu.io 'password' || exit 1 +echo "Admin and user successfully created!" \ No newline at end of file diff --git a/tests/compose/test.py b/tests/compose/test.py index a44382a1..7172aab9 100755 --- a/tests/compose/test.py +++ b/tests/compose/test.py @@ -17,7 +17,7 @@ containers = [] # Stop containers def stop(exit_code): print_logs() - os.system("docker-compose -f " + compose_file + " down") + print(os.popen("docker-compose -f " + compose_file + " down").read()) sys.exit(exit_code) # Sleep for a defined amount of time @@ -64,25 +64,25 @@ def print_logs(): #Iterating through docker container inspect list and print logs for container in containers: print(Fore.LIGHTMAGENTA_EX + "Printing logs for: " + Fore.GREEN + container['Name'] + Style.RESET_ALL) - os.system('docker container logs ' + container['Name']) + print(os.popen('docker container logs ' + container['Name']).read()) #Iterating over hooks in test folder and running them def hooks(): print("Running hooks") for test_file in sorted(os.listdir(test_path)): if test_file.endswith(".py"): - os.system("python3 " + test_path + test_file) + print(os.popen("python3 " + test_path + test_file).read()) elif test_file.endswith(".sh"): - os.system("./" + test_path + test_file) + print(os.popen("./" + test_path + test_file).read()) - os.system("python3 tests/compose/email_test.py") + print(os.popen("python3 tests/compose/email_test.py").read()) # Start up containers -os.system("docker-compose -f " + compose_file + " up -d ") +print(os.popen("docker-compose -f " + compose_file + " up -d ").read()) print() sleep() print() -os.system("docker ps -a") +print(os.popen("docker ps -a").read()) print() health_checks() print() From 9a7fc1416a29d81faac7710aaa7c796cbe33bb05 Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Sat, 3 Nov 2018 19:15:05 +0200 Subject: [PATCH 08/11] Replaces os.popen with subprocess.check_output --- tests/compose/test.py | 15 ++++++++------- tests/{compose => }/email_test.py | 4 +++- 2 files changed, 11 insertions(+), 8 deletions(-) rename tests/{compose => }/email_test.py (94%) diff --git a/tests/compose/test.py b/tests/compose/test.py index 7172aab9..b04bad00 100755 --- a/tests/compose/test.py +++ b/tests/compose/test.py @@ -3,6 +3,7 @@ import os import time import docker from colorama import Fore, Style +import subprocess # Declare variables for service name and sleep time test_name=sys.argv[1] @@ -17,7 +18,7 @@ containers = [] # Stop containers def stop(exit_code): print_logs() - print(os.popen("docker-compose -f " + compose_file + " down").read()) + print(subprocess.check_output("docker-compose -f " + compose_file + " down", shell=True)) sys.exit(exit_code) # Sleep for a defined amount of time @@ -64,25 +65,25 @@ def print_logs(): #Iterating through docker container inspect list and print logs for container in containers: print(Fore.LIGHTMAGENTA_EX + "Printing logs for: " + Fore.GREEN + container['Name'] + Style.RESET_ALL) - print(os.popen('docker container logs ' + container['Name']).read()) + print(subprocess.check_output('docker container logs ' + container['Name'], shell=True)) #Iterating over hooks in test folder and running them def hooks(): print("Running hooks") for test_file in sorted(os.listdir(test_path)): if test_file.endswith(".py"): - print(os.popen("python3 " + test_path + test_file).read()) + print(subprocess.check_output("python3 " + test_path + test_file, shell=True)) elif test_file.endswith(".sh"): - print(os.popen("./" + test_path + test_file).read()) + print(subprocess.check_output("./" + test_path + test_file, shell=True)) - print(os.popen("python3 tests/compose/email_test.py").read()) + print(subprocess.check_output("python3 tests/email_test.py", shell=True)) # Start up containers -print(os.popen("docker-compose -f " + compose_file + " up -d ").read()) +print(subprocess.check_output("docker-compose -f " + compose_file + " up -d", shell=True)) print() sleep() print() -print(os.popen("docker ps -a").read()) +print(subprocess.check_output("docker ps -a", shell=True)) print() health_checks() print() diff --git a/tests/compose/email_test.py b/tests/email_test.py similarity index 94% rename from tests/compose/email_test.py rename to tests/email_test.py index c16ce9d8..c56c1f57 100755 --- a/tests/compose/email_test.py +++ b/tests/email_test.py @@ -3,6 +3,7 @@ import random import smtplib import imaplib import time +import sys def secret(length=16): charset = string.ascii_uppercase + string.digits @@ -44,7 +45,8 @@ def read_email(): if secret_message in str(data[0][1]): print("Success!") else: - print("Failed! Something went wrong") + print("Failed! Something went wrong") + sys.exit(1) server.close() server.logout() From fca3dc4e7005250a3a49028e2131b439663e0829 Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Sat, 3 Nov 2018 19:34:47 +0200 Subject: [PATCH 09/11] Flushing stream before calling subprocess --- tests/compose/test.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/tests/compose/test.py b/tests/compose/test.py index b04bad00..576e128d 100755 --- a/tests/compose/test.py +++ b/tests/compose/test.py @@ -18,6 +18,7 @@ containers = [] # Stop containers def stop(exit_code): print_logs() + sys.stdout.flush() print(subprocess.check_output("docker-compose -f " + compose_file + " down", shell=True)) sys.exit(exit_code) @@ -65,6 +66,7 @@ def print_logs(): #Iterating through docker container inspect list and print logs for container in containers: print(Fore.LIGHTMAGENTA_EX + "Printing logs for: " + Fore.GREEN + container['Name'] + Style.RESET_ALL) + sys.stdout.flush() print(subprocess.check_output('docker container logs ' + container['Name'], shell=True)) #Iterating over hooks in test folder and running them @@ -72,17 +74,22 @@ def hooks(): print("Running hooks") for test_file in sorted(os.listdir(test_path)): if test_file.endswith(".py"): + sys.stdout.flush() print(subprocess.check_output("python3 " + test_path + test_file, shell=True)) elif test_file.endswith(".sh"): + sys.stdout.flush() print(subprocess.check_output("./" + test_path + test_file, shell=True)) - + + sys.stdout.flush() print(subprocess.check_output("python3 tests/email_test.py", shell=True)) # Start up containers +sys.stdout.flush() print(subprocess.check_output("docker-compose -f " + compose_file + " up -d", shell=True)) print() sleep() print() +sys.stdout.flush() print(subprocess.check_output("docker ps -a", shell=True)) print() health_checks() From 4032e7128d2093cf12a0557cca25503ed1b19d9b Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Mon, 5 Nov 2018 12:34:52 +0200 Subject: [PATCH 10/11] Calling email test as hook with msg as arg --- tests/compose/core/01_email_test.sh | 1 + tests/compose/filters/01_email_test.sh | 1 + tests/compose/test.py | 29 +++++++++++++------------- tests/email_test.py | 18 ++++------------ 4 files changed, 21 insertions(+), 28 deletions(-) create mode 100755 tests/compose/core/01_email_test.sh create mode 100755 tests/compose/filters/01_email_test.sh diff --git a/tests/compose/core/01_email_test.sh b/tests/compose/core/01_email_test.sh new file mode 100755 index 00000000..97dd6e4b --- /dev/null +++ b/tests/compose/core/01_email_test.sh @@ -0,0 +1 @@ +python3 tests/email_test.py message-core \ No newline at end of file diff --git a/tests/compose/filters/01_email_test.sh b/tests/compose/filters/01_email_test.sh new file mode 100755 index 00000000..64fa3fba --- /dev/null +++ b/tests/compose/filters/01_email_test.sh @@ -0,0 +1 @@ +python3 tests/email_test.py message-filters \ No newline at end of file diff --git a/tests/compose/test.py b/tests/compose/test.py index 576e128d..690855d4 100755 --- a/tests/compose/test.py +++ b/tests/compose/test.py @@ -19,7 +19,7 @@ containers = [] def stop(exit_code): print_logs() sys.stdout.flush() - print(subprocess.check_output("docker-compose -f " + compose_file + " down", shell=True)) + print(subprocess.check_output("docker-compose -f " + compose_file + " down", shell=True).decode()) sys.exit(exit_code) # Sleep for a defined amount of time @@ -67,30 +67,31 @@ def print_logs(): for container in containers: print(Fore.LIGHTMAGENTA_EX + "Printing logs for: " + Fore.GREEN + container['Name'] + Style.RESET_ALL) sys.stdout.flush() - print(subprocess.check_output('docker container logs ' + container['Name'], shell=True)) + print(subprocess.check_output('docker container logs ' + container['Name'], shell=True).decode()) #Iterating over hooks in test folder and running them def hooks(): - print("Running hooks") + print(Fore.LIGHTMAGENTA_EX + "Running hooks" + Style.RESET_ALL) for test_file in sorted(os.listdir(test_path)): - if test_file.endswith(".py"): - sys.stdout.flush() - print(subprocess.check_output("python3 " + test_path + test_file, shell=True)) - elif test_file.endswith(".sh"): - sys.stdout.flush() - print(subprocess.check_output("./" + test_path + test_file, shell=True)) - - sys.stdout.flush() - print(subprocess.check_output("python3 tests/email_test.py", shell=True)) + try: + if test_file.endswith(".py"): + sys.stdout.flush() + print(subprocess.check_output("python3 " + test_path + test_file, shell=True).decode()) + elif test_file.endswith(".sh"): + sys.stdout.flush() + print(subprocess.check_output("./" + test_path + test_file, shell=True).decode()) + except subprocess.CalledProcessError as e: + sys.stderr.write("[ERROR]: output = %s, error code = %s\n" % (e.output.decode(), e.returncode)) + stop(1) # Start up containers sys.stdout.flush() -print(subprocess.check_output("docker-compose -f " + compose_file + " up -d", shell=True)) +print(subprocess.check_output("docker-compose -f " + compose_file + " up -d", shell=True).decode()) print() sleep() print() sys.stdout.flush() -print(subprocess.check_output("docker ps -a", shell=True)) +print(subprocess.check_output("docker ps -a", shell=True).decode()) print() health_checks() print() diff --git a/tests/email_test.py b/tests/email_test.py index c56c1f57..7148365e 100755 --- a/tests/email_test.py +++ b/tests/email_test.py @@ -1,19 +1,9 @@ -import string -import random import smtplib import imaplib import time import sys -def secret(length=16): - charset = string.ascii_uppercase + string.digits - return ''.join( - random.SystemRandom().choice(charset) - for _ in range(length) - ) - -#Generating secret message -secret_message = secret(16) +email_msg = sys.argv[1] #Login to smt server and sending email with secret message def send_email(msg): @@ -42,16 +32,16 @@ def read_email(): print("email received with message " + str(data[0][1])) - if secret_message in str(data[0][1]): + if email_msg in str(data[0][1]): print("Success!") else: - print("Failed! Something went wrong") + print("Failed receiving email with message %s" % email_msg) sys.exit(1) server.close() server.logout() -send_email(secret_message) +send_email(email_msg) print("Sleeping for 1m") time.sleep(60) read_email() From 76d9fc3865efb010c0dfc33f796df4af981bed8f Mon Sep 17 00:00:00 2001 From: Ionut Filip Date: Mon, 5 Nov 2018 17:28:40 +0200 Subject: [PATCH 11/11] Rewrite of email test script and added eicar virus file --- tests/compose/filters/01_email_test.sh | 7 ++- tests/compose/filters/eicar.com | 1 + tests/email_test.py | 86 +++++++++++++++----------- 3 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 tests/compose/filters/eicar.com diff --git a/tests/compose/filters/01_email_test.sh b/tests/compose/filters/01_email_test.sh index 64fa3fba..5af395c4 100755 --- a/tests/compose/filters/01_email_test.sh +++ b/tests/compose/filters/01_email_test.sh @@ -1 +1,6 @@ -python3 tests/email_test.py message-filters \ No newline at end of file +python3 tests/email_test.py message-virus "tests/compose/filters/eicar.com" +if [ $? -eq 99 ]; then + exit 0 +else + exit 1 +fi \ No newline at end of file diff --git a/tests/compose/filters/eicar.com b/tests/compose/filters/eicar.com new file mode 100644 index 00000000..704cac85 --- /dev/null +++ b/tests/compose/filters/eicar.com @@ -0,0 +1 @@ +X5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H* diff --git a/tests/email_test.py b/tests/email_test.py index 7148365e..853b76b5 100755 --- a/tests/email_test.py +++ b/tests/email_test.py @@ -2,46 +2,58 @@ import smtplib import imaplib import time import sys +from email.mime.multipart import MIMEMultipart +from email.mime.text import MIMEText +import ntpath +from email.mime.base import MIMEBase +from email import encoders -email_msg = sys.argv[1] +msg = MIMEMultipart() +msg['From'] = "admin@mailu.io" +msg['To'] = "user@mailu.io" +msg['Subject'] = "File Test" +msg.attach(MIMEText(sys.argv[1], 'plain')) -#Login to smt server and sending email with secret message -def send_email(msg): - print("Sending email ...") - server = smtplib.SMTP('localhost') - server.set_debuglevel(1) - server.connect('localhost', 587) - server.ehlo() - server.starttls() - server.ehlo() - server.login("admin@mailu.io", "password") - - server.sendmail("admin@mailu.io", "user@mailu.io", msg) - server.quit() +if len(sys.argv) == 3: + part = MIMEBase('application', 'octet-stream') + part.set_payload((open(sys.argv[2], "rb")).read()) + encoders.encode_base64(part) + part.add_header('Content-Disposition', "attachment; filename=%s" % ntpath.basename(sys.argv[2])) + msg.attach(part) - print("email sent with message " + msg) +try: + smtp_server = smtplib.SMTP('localhost') + smtp_server.set_debuglevel(1) + smtp_server.connect('localhost', 587) + smtp_server.ehlo() + smtp_server.starttls() + smtp_server.ehlo() + smtp_server.login("admin@mailu.io", "password") + + smtp_server.sendmail("admin@mailu.io", "user@mailu.io", msg.as_string()) + smtp_server.quit() +except: + sys.exit(25) -#Login to imap server, read latest email and check for secret message -def read_email(): - print("Receiving email ...") - server = imaplib.IMAP4_SSL('localhost') - server.login('user@mailu.io', 'password') - - stat, count = server.select('inbox') - stat, data = server.fetch(count[0], '(UID BODY[TEXT])') - - print("email received with message " + str(data[0][1])) - - if email_msg in str(data[0][1]): - print("Success!") - else: - print("Failed receiving email with message %s" % email_msg) - sys.exit(1) - server.close() - server.logout() +time.sleep(30) +try: + imap_server = imaplib.IMAP4_SSL('localhost') + imap_server.login('user@mailu.io', 'password') +except: + sys.exit(110) + +stat, count = imap_server.select('inbox') +try: + stat, data = imap_server.fetch(count[0], '(UID BODY[TEXT])') +except : + sys.exit(99) + +if sys.argv[1] in str(data[0][1]): + print("Success sending and receiving email!") +else: + print("Failed receiving email with message %s" % sys.argv[1]) + sys.exit(99) -send_email(email_msg) -print("Sleeping for 1m") -time.sleep(60) -read_email() +imap_server.close() +imap_server.logout()