|
1 | 1 | #!/usr/bin/env python3 |
2 | 2 |
|
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 | | - |
10 | 3 | """ |
11 | 4 | 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 |
17 | 5 | ~~~~~~~~~~~~~~~~~~~~~ |
18 | 6 | Source: https://github.com/RuedigerVoigt/bote |
19 | | -(c) 2020-2021 Rüdiger Voigt |
| 7 | +(c) 2020-2025 Rüdiger Voigt and contributors |
20 | 8 | Released under the Apache License 2.0 |
21 | 9 | """ |
22 | 10 | import logging |
@@ -296,6 +284,44 @@ def test_send_mail(mocker): |
296 | 284 | assert mock_ssl.call_count == 1 |
297 | 285 |
|
298 | 286 |
|
| 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 | + |
299 | 325 | def test_send_mail_to_admin(mocker): |
300 | 326 | # False, but 'valid' settings |
301 | 327 | mail_settings = { |
@@ -466,4 +492,3 @@ def test_send_mail_GENERIC(caplog): |
466 | 492 | with pytest.raises(Exception): |
467 | 493 | mailer.send_mail('random subject', 'random content') |
468 | 494 | assert "Problem sending mail" in caplog.text |
469 | | - |
|
0 commit comments