Skip to content

Commit

Permalink
Add port offset to let SMTP servers start for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sharat87 committed Dec 4, 2023
1 parent 0f05f19 commit 79ce79f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 19 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ jobs:
run: task test-frontend

- name: Test Backend
env:
PORT_OFFSET: 10000
run: task test-backend

- name: Build frontend
Expand Down
25 changes: 13 additions & 12 deletions backend/app/handlers/smtp_servers.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,43 +179,44 @@ def make_tls_context():


def init(app: web.Application):
port_offset = int(os.getenv("PORT_OFFSET", 0))
app.on_shutdown.append(on_shutdown)

# echo $'From: from@lo.co\nTo:to@lo.co\nSubject: Hey\nDate: '"$(date +'%a, %d %b %Y %H:%m:%S')"$'\n\nTest stuff' | curl smtp://localhost:7025 --mail-from from@lo.co --mail-rcpt to@lo.co --upload-file -
serve_smtp(7025)
serve_smtp(7025 + port_offset)

# echo $'From: from@lo.co\nTo:to@lo.co\nSubject: Hey\nDate: '"$(date +'%a, %d %b %Y %H:%m:%S')"$'\n\nTest stuff' | curl smtp://localhost:7026 --mail-from from@lo.co --mail-rcpt to@lo.co -u little:non-secret --upload-file -
serve_smtp(7026, auth_style=AuthStyle.require_any)
serve_smtp(7026 + port_offset, auth_style=AuthStyle.require_any)

# echo $'From: from@lo.co\nTo:to@lo.co\nSubject: Hey\nDate: '"$(date +'%a, %d %b %Y %H:%m:%S')"$'\n\nTest stuff' | curl smtp://localhost:7027 --mail-from from@lo.co --mail-rcpt to@lo.co -u little:non-secret --upload-file -
serve_smtp(7027, auth_style=AuthStyle.require_plain)
serve_smtp(7027 + port_offset, auth_style=AuthStyle.require_plain)

# echo $'From: from@lo.co\nTo:to@lo.co\nSubject: Hey\nDate: '"$(date +'%a, %d %b %Y %H:%m:%S')"$'\n\nTest stuff' | curl smtp://localhost:7028 --mail-from from@lo.co --mail-rcpt to@lo.co -u little:non-secret --upload-file -
serve_smtp(7028, auth_style=AuthStyle.require_login)
serve_smtp(7028 + port_offset, auth_style=AuthStyle.require_login)

# Using `tls_context` vs `ssl_context` will determine how TLS will be done in the SMTP server. Yes, it's confusing.

if tls_cert:
# TLS using STARTTLS command
# echo $'From: from@lo.co\nTo:to@lo.co\nSubject: Hey\nDate: '"$(date +'%a, %d %b %Y %H:%m:%S')"$'\n\nTest stuff' | curl --ssl-reqd smtp://localhost:7028 --mail-from from@lo.co --mail-rcpt to@lo.co --upload-file -
serve_smtp(7587, tls_style=TLSStyle.starttls)
serve_smtp(7588, tls_style=TLSStyle.starttls, auth_style=AuthStyle.require_any)
serve_smtp(7587 + port_offset, tls_style=TLSStyle.starttls)
serve_smtp(7588 + port_offset, tls_style=TLSStyle.starttls, auth_style=AuthStyle.require_any)
serve_smtp(
7589, tls_style=TLSStyle.starttls, auth_style=AuthStyle.require_plain
7589 + port_offset, tls_style=TLSStyle.starttls, auth_style=AuthStyle.require_plain
)
serve_smtp(
7590, tls_style=TLSStyle.starttls, auth_style=AuthStyle.require_login
7590 + port_offset, tls_style=TLSStyle.starttls, auth_style=AuthStyle.require_login
)

# TLS using implicit TLS
# echo $'From: from@lo.co\nTo:to@lo.co\nSubject: Hey\nDate: '"$(date +'%a, %d %b %Y %H:%m:%S')"$'\n\nTest stuff' | curl smtps://localhost:7029 --mail-from from@lo.co --mail-rcpt to@lo.co --upload-file -
serve_smtp(7465, tls_style=TLSStyle.implicit_tls)
serve_smtp(7465 + port_offset, tls_style=TLSStyle.implicit_tls)
serve_smtp(
7466, tls_style=TLSStyle.implicit_tls, auth_style=AuthStyle.require_any
7466 + port_offset, tls_style=TLSStyle.implicit_tls, auth_style=AuthStyle.require_any
)
serve_smtp(
7467, tls_style=TLSStyle.implicit_tls, auth_style=AuthStyle.require_plain
7467 + port_offset, tls_style=TLSStyle.implicit_tls, auth_style=AuthStyle.require_plain
)
serve_smtp(
7468, tls_style=TLSStyle.implicit_tls, auth_style=AuthStyle.require_login
7468 + port_offset, tls_style=TLSStyle.implicit_tls, auth_style=AuthStyle.require_login
)
17 changes: 10 additions & 7 deletions backend/tests/test_smtp_servers.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
from email.message import EmailMessage

import aiohttp
import aiosmtplib
import pytest

PORT_OFFSET = int(os.getenv("PORT_OFFSET", 0))


def make_message(to="to@l.co", subject="Test mail plain body") -> EmailMessage:
message = EmailMessage()
Expand All @@ -24,7 +27,7 @@ async def test_websocket_cleanup(client_session: aiohttp.ClientSession) -> None:
await aiosmtplib.send(
make_message(to="accept@l.co", subject="accepted"),
hostname="localhost",
port=7025,
port=7025 + PORT_OFFSET,
)

msg = await ws.receive_json()
Expand All @@ -40,20 +43,20 @@ async def test_auth_none_tls_none(client_session: aiohttp.ClientSession) -> None
await aiosmtplib.send(
make_message(to="ignore@l.co", subject="ignored"),
hostname="localhost",
port=7025,
port=7025 + PORT_OFFSET,
)

await aiosmtplib.send(
make_message(to="accept@l.co", subject="accepted"),
hostname="localhost",
port=7025,
port=7025 + PORT_OFFSET,
)

with pytest.raises(aiosmtplib.SMTPException):
await aiosmtplib.send(
make_message(to="accept@l.co", subject="accepted"),
hostname="localhost",
port=7025,
port=7025 + PORT_OFFSET,
username="one",
password="two",
)
Expand All @@ -71,7 +74,7 @@ async def test_auth_any_tls_none(client_session: aiohttp.ClientSession) -> None:
await aiosmtplib.send(
make_message(to="ignore@l.co", subject="ignored"),
hostname="localhost",
port=7026,
port=7026 + PORT_OFFSET,
username="little",
password="non-secret",
use_tls=False,
Expand All @@ -81,7 +84,7 @@ async def test_auth_any_tls_none(client_session: aiohttp.ClientSession) -> None:
await aiosmtplib.send(
make_message(to="accept@l.co", subject="accepted"),
hostname="localhost",
port=7026,
port=7026 + PORT_OFFSET,
username="little",
password="non-secret",
use_tls=False,
Expand All @@ -92,7 +95,7 @@ async def test_auth_any_tls_none(client_session: aiohttp.ClientSession) -> None:
await aiosmtplib.send(
make_message(to="accept@l.co", subject="should error out"),
hostname="localhost",
port=7026,
port=7026 + PORT_OFFSET,
)

msg = await ws.receive_json()
Expand Down

0 comments on commit 79ce79f

Please sign in to comment.