From 8bbe7f14efb495035c9e6ae61292b4dbfb52d14a Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 28 Sep 2024 13:07:43 +0100 Subject: [PATCH] Start of an integration test harness --- .github/workflows/ci.yml | 2 + attachment_downloader/version_info.py | 11 +++++ requirements_tests.txt | 1 + run-integration-tests.sh | 2 + setup.sh | 1 + tests_integration/__init__.py | 0 .../attachment_downloader/__init__.py | 0 .../test_send_and_download_attachment.py | 43 +++++++++++++++++++ 8 files changed, 60 insertions(+) create mode 100644 requirements_tests.txt create mode 100755 run-integration-tests.sh create mode 100644 tests_integration/__init__.py create mode 100644 tests_integration/attachment_downloader/__init__.py create mode 100644 tests_integration/attachment_downloader/test_send_and_download_attachment.py diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2f6962d..8a172e9 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -21,3 +21,5 @@ jobs: run: ./run-pylint.sh - name: Tests run: ./run-tests.sh + - name: Integration Tests + run: ./run-integration-tests diff --git a/attachment_downloader/version_info.py b/attachment_downloader/version_info.py index 06ffae1..a04859f 100644 --- a/attachment_downloader/version_info.py +++ b/attachment_downloader/version_info.py @@ -13,6 +13,17 @@ class Version: """ Version information. """ + + @staticmethod + def commit_hash_short(): + """ + Returns the short commit hash. + """ + with subprocess.Popen(["git", "rev-parse", "--short", "HEAD"], + stdout=subprocess.PIPE, + stderr=None) as process: + return process.communicate()[0].decode('ascii').strip() + @staticmethod def generate(): """ diff --git a/requirements_tests.txt b/requirements_tests.txt new file mode 100644 index 0000000..d80d9fc --- /dev/null +++ b/requirements_tests.txt @@ -0,0 +1 @@ +requests==2.32.3 diff --git a/run-integration-tests.sh b/run-integration-tests.sh new file mode 100755 index 0000000..1853d33 --- /dev/null +++ b/run-integration-tests.sh @@ -0,0 +1,2 @@ +#!/usr/bin/env bash +./venv/bin/pytest -s tests_integration diff --git a/setup.sh b/setup.sh index dc6c37f..eabba41 100755 --- a/setup.sh +++ b/setup.sh @@ -4,3 +4,4 @@ python3 -m pip install --user virtualenv python3 -m venv venv ./venv/bin/pip install --upgrade pip ./venv/bin/pip install -r requirements.txt +./venv/bin/pip install -r requirements_tests.txt diff --git a/tests_integration/__init__.py b/tests_integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests_integration/attachment_downloader/__init__.py b/tests_integration/attachment_downloader/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests_integration/attachment_downloader/test_send_and_download_attachment.py b/tests_integration/attachment_downloader/test_send_and_download_attachment.py new file mode 100644 index 0000000..58ef7cd --- /dev/null +++ b/tests_integration/attachment_downloader/test_send_and_download_attachment.py @@ -0,0 +1,43 @@ +import os +import tempfile + +import requests + +from attachment_downloader.version_info import Version + + +def send_email_with_attachment(api_key, domain, sender, recipient, subject, text, attachment_path): + url = f"https://api.eu.mailgun.net/v3/{domain}/messages" + + with open(attachment_path, 'rb') as attachment_file: + response = requests.post( + url, + auth=("api", api_key), + files=[("attachment", (attachment_path, attachment_file.read()))], + data={ + "from": sender, + "to": recipient, + "subject": subject, + "text": text, + } + ) + return response.status_code == 200 + +class TestSendAndDownloadAttachment: + def test_send_and_download_attachment(self): + mailgun_domain = os.getenv('MAILGUN_DOMAIN') + mailgun_api_key = os.getenv('MAILGUN_API_KEY') + recipient = os.getenv('AD_INT_TEST_RECIPIENT') + version_info = Version.commit_hash_short() + + with tempfile.NamedTemporaryFile(mode='w+t',prefix=version_info,suffix='.txt') as temp_attachment: + temp_attachment.write(f"This is an example attachment from attachment-downloader {version_info}") + temp_attachment.flush() + + send_email_with_attachment(mailgun_api_key, + mailgun_domain, + f'attachment-downloader@{mailgun_domain}', + recipient, + f"Integration Test - {version_info}", + 'This email is part of an automated integration test.', + temp_attachment.name)