Skip to content

Commit

Permalink
Require the port field of SMTPSender to be non-negative
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Mar 8, 2021
1 parent 1473738 commit af74a95
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
v0.2.0 (in development)
-----------------------
- Require the `port` field of `SMTPSender` to be non-negative

v0.1.0 (2021-03-06)
-------------------
Expand Down
4 changes: 2 additions & 2 deletions src/outgoing/senders/smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import smtplib
import sys
from typing import Any, Dict, Optional
from pydantic import PrivateAttr, validator
from pydantic import Field, PrivateAttr, validator
from ..config import NetrcConfig
from ..util import OpenClosable

Expand All @@ -16,7 +16,7 @@

class SMTPSender(NetrcConfig, OpenClosable):
ssl: Literal[False, True, "starttls"] = False
port: int = 0
port: int = Field(0, ge=0)
_client: Optional[smtplib.SMTP] = PrivateAttr(None)

@validator("port", always=True)
Expand Down
19 changes: 15 additions & 4 deletions test/test_senders/test_smtp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from pytest_mock import MockerFixture
from smtpdfix import SMTPDFix
from outgoing import from_dict
from outgoing.errors import InvalidConfigError
from outgoing.senders.smtp import SMTPSender

smtpdfix_headers = ["x-mailfrom", "x-peer", "x-rcptto"]
Expand Down Expand Up @@ -70,7 +71,7 @@ def test_smtp_construct_no_ssl(monkeypatch: pytest.MonkeyPatch, tmp_path: Path)
assert sender._client is None


def test_smtp_construct_ssl(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None:
def test_smtp_construct_ssl(tmp_path: Path) -> None:
(tmp_path / "secret").write_text("12345\n")
sender = from_dict(
{
Expand Down Expand Up @@ -124,9 +125,7 @@ def test_smtp_construct_starttls(


@pytest.mark.parametrize("ssl", [False, True, "starttls"])
def test_smtp_construct_explicit_port(
monkeypatch: pytest.MonkeyPatch, ssl: Union[bool, str], tmp_path: Path
) -> None:
def test_smtp_construct_explicit_port(ssl: Union[bool, str], tmp_path: Path) -> None:
sender = from_dict(
{
"method": "smtp",
Expand All @@ -148,6 +147,18 @@ def test_smtp_construct_explicit_port(
}


def test_smtp_construct_negative_port(tmp_path: Path) -> None:
with pytest.raises(InvalidConfigError):
from_dict(
{
"method": "smtp",
"host": "mx.example.com",
"port": -1,
},
configpath=str(tmp_path / "foo.txt"),
)


def test_smtp_send_no_ssl_no_auth(
mocker: MockerFixture, test_email1: EmailMessage
) -> None:
Expand Down

0 comments on commit af74a95

Please sign in to comment.