2020-05-17 16:42:44 +02:00
|
|
|
import pytest
|
2021-01-20 16:47:00 +01:00
|
|
|
|
|
|
|
from fittrackee.emails.utils_email import (
|
|
|
|
InvalidEmailUrlScheme,
|
|
|
|
parse_email_url,
|
|
|
|
)
|
2020-05-17 16:42:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestEmailUrlParser:
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_raises_error_if_url_scheme_is_invalid(self) -> None:
|
2020-05-17 16:42:44 +02:00
|
|
|
url = 'stmp://username:password@localhost:587'
|
|
|
|
with pytest.raises(InvalidEmailUrlScheme):
|
|
|
|
parse_email_url(url)
|
|
|
|
|
2022-01-01 11:27:16 +01:00
|
|
|
@staticmethod
|
|
|
|
def assert_parsed_email(url: str) -> None:
|
2022-01-01 11:04:08 +01:00
|
|
|
parsed_email = parse_email_url(url)
|
|
|
|
assert parsed_email['username'] is None
|
|
|
|
assert parsed_email['password'] is None
|
|
|
|
assert parsed_email['host'] == 'localhost'
|
|
|
|
assert parsed_email['port'] == 25
|
|
|
|
assert parsed_email['use_tls'] is False
|
|
|
|
assert parsed_email['use_ssl'] is False
|
|
|
|
|
2022-01-01 11:27:16 +01:00
|
|
|
def test_it_parses_email_url_without_port(self) -> None:
|
|
|
|
url = 'smtp://localhost'
|
|
|
|
self.assert_parsed_email(url)
|
|
|
|
|
|
|
|
def test_it_parses_email_url_without_authentication(self) -> None:
|
|
|
|
url = 'smtp://localhost:25'
|
|
|
|
self.assert_parsed_email(url)
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_parses_email_url(self) -> None:
|
2020-05-17 16:42:44 +02:00
|
|
|
url = 'smtp://test@example.com:12345678@localhost:25'
|
|
|
|
parsed_email = parse_email_url(url)
|
|
|
|
assert parsed_email['username'] == 'test@example.com'
|
|
|
|
assert parsed_email['password'] == '12345678'
|
|
|
|
assert parsed_email['host'] == 'localhost'
|
|
|
|
assert parsed_email['port'] == 25
|
|
|
|
assert parsed_email['use_tls'] is False
|
|
|
|
assert parsed_email['use_ssl'] is False
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_parses_email_url_with_tls(self) -> None:
|
2020-05-17 16:42:44 +02:00
|
|
|
url = 'smtp://test@example.com:12345678@localhost:587?tls=True'
|
|
|
|
parsed_email = parse_email_url(url)
|
|
|
|
assert parsed_email['username'] == 'test@example.com'
|
|
|
|
assert parsed_email['password'] == '12345678'
|
|
|
|
assert parsed_email['host'] == 'localhost'
|
|
|
|
assert parsed_email['port'] == 587
|
|
|
|
assert parsed_email['use_tls'] is True
|
|
|
|
assert parsed_email['use_ssl'] is False
|
|
|
|
|
2021-01-02 19:28:03 +01:00
|
|
|
def test_it_parses_email_url_with_ssl(self) -> None:
|
2020-05-17 16:42:44 +02:00
|
|
|
url = 'smtp://test@example.com:12345678@localhost:465?ssl=True'
|
|
|
|
parsed_email = parse_email_url(url)
|
|
|
|
assert parsed_email['username'] == 'test@example.com'
|
|
|
|
assert parsed_email['password'] == '12345678'
|
|
|
|
assert parsed_email['host'] == 'localhost'
|
|
|
|
assert parsed_email['port'] == 465
|
|
|
|
assert parsed_email['use_tls'] is False
|
|
|
|
assert parsed_email['use_ssl'] is True
|