Skip to content

Commit 26f6080

Browse files
committed
Correct SMTP sending, avoid fragile doctest
1 parent 07f9243 commit 26f6080

File tree

7 files changed

+47
-24
lines changed

7 files changed

+47
-24
lines changed

.github/workflows/main.yml

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,19 @@ jobs:
2323
python-version: ['3.9', '3.10', '3.11', '3.12']
2424

2525
steps:
26-
- uses: actions/checkout@v2
26+
- name: Checkout repository code
27+
uses: actions/checkout@master
28+
- name: Update openssl
29+
run: |
30+
sudo apt-get update
31+
sudo apt-get -y install openssl
2732
- name: Set up Python ${{ matrix.python-version }}
28-
uses: actions/setup-python@v2
33+
uses: actions/setup-python@master
2934
with:
3035
python-version: ${{ matrix.python-version }}
3136
- name: Install dependencies
3237
run: |
33-
python3 -m pip install --upgrade pip
34-
python3 -m pip install -r requirements.txt
35-
python3 -m pip install -r requirements-serial.txt
36-
python3 -m pip install -r requirements-gui.txt
37-
python3 -m pip install -r requirements-wallet.txt
38-
python3 -m pip install -r requirements-tests.txt
38+
python3 -m pip install .[all,tests]
3939
- name: Lint with flake8
4040
run: |
4141
make analyze

GNUmakefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ else
6464
endif
6565

6666
# To see all pytest output, uncomment --capture=no, ...
67-
PYTESTOPTS = --capture=no --log-cli-level=WARNING
67+
PYTESTOPTS = --capture=no --log-cli-level=WARNING # --doctest-modules
6868

6969
PY3TEST = $(PY3) -m pytest $(PYTESTOPTS)
7070

@@ -211,7 +211,7 @@ $(VENV):
211211
@echo; echo "*** Building $@ VirtualEnv..."
212212
@rm -rf $@ && $(PY3) -m venv $(VENV_OPTS) $@ \
213213
&& source $@/bin/activate \
214-
&& make install
214+
&& make install install-tests
215215

216216

217217
wheel: deps $(WHEEL)

pytest.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
[pytest]
22
testpaths = slip39
3-
addopts = -vv --doctest-modules --ignore-glob=**/__main__.py --ignore-glob=**/main.py --ignore-glob=**/ethereum.py --cov=slip39 --cov-config=.coveragerc
3+
addopts = -v --ignore-glob=**/__main__.py --ignore-glob=**/main.py --ignore-glob=**/ethereum.py --cov=slip39 --cov-config=.coveragerc

setup.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
# Make python-slip39[all] install all extra (non-tests) requirements, excluding duplicates
2929
extras_require['all'] = list( set( sum( extras_require.values(), [] )))
3030

31+
# Since setuptools is retiring tests_require, add it as another option (but not included in 'all')
32+
extras_require['tests'] = tests_require
33+
options_require.append( 'tests' )
34+
3135
Executable = None
3236
if sys.platform == 'win32':
3337
# We use cx_Freeze for executable/installer packaging on Windows, only, for now.

slip39/communications.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ def send_message(
254254
for a in utils.getaddresses( addr_fields )
255255
]
256256

257+
log.info( "Message headers:\n" + tabulate( [[k, len(v), v] for k,v in msg.items()], headers=["Description", "Length", "Value"], tablefmt='orgtbl' ))
258+
257259
# Now that we have a to_addrs, construct a mapping of (mx, ...) --> [addr, ...]. For each
258260
# to_addrs, lookup its destination's mx records; we'll append all to_addrs w/ the same mx's
259261
# (sorted by priority).
@@ -286,12 +288,15 @@ def send_message(
286288
if verifycert is None:
287289
verifycert = False
288290

289-
try:
291+
# Obtain message, ensuring \r\n line termination
292+
if sys.version_info[0:2] >= (3,0):
290293
# Python 3 libraries expect bytes.
291-
msg_data = msg.as_bytes()
292-
except Exception:
294+
msg_data = b'\r\n'.join( msg.as_bytes().split( b'\n' ))
295+
else:
293296
# Python 2 libraries expect strings.
294-
msg_data = msg.as_string()
297+
msg_data = '\r\n'.join( msg.as_string().split( '\n' ))
298+
299+
log.info( "Message body:\n" + msg_data.decode('UTF-8'))
295300

296301
smtp_kwds = dict()
297302
if usessl:

slip39/communications_test.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,22 @@
22
import os
33
import re
44
import sys
5+
import pytest
56

67
from io import StringIO
78
from pathlib import Path
89
from subprocess import Popen, PIPE
910
from email import message_from_string
1011

11-
import dkim
1212

1313
from aiosmtpd.controller import Controller
1414

15-
from .communications import dkim_message, send_message, matchaddr, AutoResponder
15+
try:
16+
import dkim
17+
from .communications import dkim_message, send_message, matchaddr, AutoResponder
18+
except ImportError:
19+
dkim = None
20+
1621
from .defaults import SMTP_TO, SMTP_FROM
1722

1823
log = logging.getLogger( __package__ )
@@ -61,6 +66,8 @@
6166
""" )
6267

6368

69+
@pytest.mark.skipif( not dkim,
70+
reason="DKIM support unavailable; install w/ [invoice] option" )
6471
def test_communications_matchaddr():
6572
assert matchaddr( "abc+def@xyz", mailbox="abc", domain="xyz" ) == ("abc", "def", "xyz")
6673
assert matchaddr( "abc+def@xyz", domain="xYz" ) == ("abc", "def", "xyz")
@@ -73,6 +80,8 @@ def test_communications_matchaddr():
7380
assert matchaddr( "abc+def@xyz", mailbox="xxx" ) is None
7481

7582

83+
@pytest.mark.skipif( not dkim,
84+
reason="DKIM support unavailable; install w/ [invoice] option" )
7685
def test_communications_dkim():
7786
log.info( f"Using DKIM: {dkim_selector}: {dkim_key}" )
7887
if dkim_key:
@@ -135,6 +144,8 @@ def test_communications_dkim():
135144
pass
136145

137146

147+
@pytest.mark.skipif( not dkim,
148+
reason="DKIM support unavailable; install w/ [invoice] option" )
138149
def test_communications_autoresponder( monkeypatch ):
139150
"""The Postfix-compatible auto-responder takes an email.Message from stdin, and auto-forwards it
140151
(via a relay; normally the same Postfix installation that it is running within).
@@ -183,8 +194,8 @@ async def handle_DATA(self, server, session, envelope):
183194
controller = Controller( handler, hostname='localhost', port=11111 )
184195
controller.start()
185196

186-
# Send the email.Message directly our SMTP daemon, w/ RCTP TO: [email protected] (taken
187-
# from the To: header)
197+
# Send the email.Message directly to our SMTP daemon, w/ RCTP TO: [email protected]
198+
# (taken from the To: header)
188199
send_message(
189200
msg,
190201
relay = controller.hostname,

slip39/invoice/artifact_test.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,14 @@ def test_tabulate( tmp_path ):
497497

498498

499499
# Generate a sequence of Invoices w/ unique accounts
500-
with open( '/usr/share/dict/words', 'r' ) as words_f:
501-
words = list(
502-
w.strip() for w in words_f.readlines()
503-
)
504-
500+
words = list(
501+
(
502+
"Lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua "
503+
" Ut enim ad minim veniam quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat"
504+
" Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur"
505+
" Excepteur sint occaecat cupidatat non proident sunt in culpa qui officia deserunt mollit anim id est laborum"
506+
).split( " " )
507+
)
505508

506509
line_currencies = [
507510
"Bitcoin",

0 commit comments

Comments
 (0)