Skip to content

Commit 227ab3c

Browse files
committed
server_port on localhost
1 parent 3e0c6a7 commit 227ab3c

2 files changed

Lines changed: 45 additions & 16 deletions

File tree

bote/__main__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,12 @@ def __init__(self,
153153

154154
def __send_unencrypted(self,
155155
msg: EmailMessage) -> None:
156-
with smtplib.SMTP(self.server) as s:
157-
s.send_message(msg)
156+
if self.server_port is not None:
157+
with smtplib.SMTP(self.server, self.server_port) as s:
158+
s.send_message(msg)
159+
else:
160+
with smtplib.SMTP(self.server) as s:
161+
s.send_message(msg)
158162

159163
def __send_ssl(self,
160164
msg: EmailMessage) -> None:

tests/test_bote.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,10 @@
11
#!/usr/bin/env python3
22

3-
"""
4-
Pytest test suite for bote (relocated to tests/ for scalability).
5-
Content preserved from original root-level tests.py.
6-
"""
7-
8-
#!/usr/bin/env python3
9-
103
"""
114
Automatic Tests for bote
12-
13-
To run these tests:
14-
coverage run --source bote -m pytest tests.py
15-
To generate a report afterwards.
16-
coverage html
175
~~~~~~~~~~~~~~~~~~~~~
186
Source: https://github.com/RuedigerVoigt/bote
19-
(c) 2020-2021 Rüdiger Voigt
7+
(c) 2020-2025 Rüdiger Voigt and contributors
208
Released under the Apache License 2.0
219
"""
2210
import logging
@@ -296,6 +284,44 @@ def test_send_mail(mocker):
296284
assert mock_ssl.call_count == 1
297285

298286

287+
def test_unencrypted_uses_custom_port(mocker):
288+
# Ensure that when encryption is off and a port is set, SMTP is called with that port
289+
mail_settings = {
290+
'server': 'localhost',
291+
'server_port': 2525,
292+
'encryption': 'off',
293+
'username': None,
294+
'passphrase': None,
295+
'recipient': 'foo@example.com',
296+
'sender': 'bar@example.com',
297+
}
298+
mailer = bote.Mailer(mail_settings)
299+
mock_smtp = mocker.patch('smtplib.SMTP')
300+
mailer.send_mail('subject', 'body')
301+
# Called once with server and custom port
302+
assert mock_smtp.call_count == 1
303+
args, kwargs = mock_smtp.call_args
304+
assert args[:2] == ('localhost', 2525)
305+
306+
307+
def test_unencrypted_without_port_uses_default_call(mocker):
308+
# Without a server_port, SMTP should be called with only the server argument
309+
mail_settings = {
310+
'server': 'localhost',
311+
'encryption': 'off',
312+
'username': None,
313+
'passphrase': None,
314+
'recipient': 'foo@example.com',
315+
'sender': 'bar@example.com',
316+
}
317+
mailer = bote.Mailer(mail_settings)
318+
mock_smtp = mocker.patch('smtplib.SMTP')
319+
mailer.send_mail('subject', 'body')
320+
assert mock_smtp.call_count == 1
321+
args, kwargs = mock_smtp.call_args
322+
assert args == ('localhost',)
323+
324+
299325
def test_send_mail_to_admin(mocker):
300326
# False, but 'valid' settings
301327
mail_settings = {
@@ -466,4 +492,3 @@ def test_send_mail_GENERIC(caplog):
466492
with pytest.raises(Exception):
467493
mailer.send_mail('random subject', 'random content')
468494
assert "Problem sending mail" in caplog.text
469-

0 commit comments

Comments
 (0)