async def test_file_transport(message): # should create from EmailURL backend = transports.FileTransport.from_url(EmailURL("file:///tmp")) assert isinstance(backend, transports.FileTransport) # should create from URL string backend = transports.FileTransport.from_url("file:///tmp") assert isinstance(backend, transports.FileTransport) # URL must have path parameter with pytest.raises(ImproperlyConfiguredError) as ex: url = EmailURL("file://") transports.FileTransport.from_url(url) assert str(ex.value) == 'Argument "path" of FileTransport cannot be None.' tmpdir = tempfile.TemporaryDirectory() with tmpdir as directory: backend = transports.FileTransport(directory) await backend.send(message) files = os.listdir(directory) assert len(files) == 1 with mock.patch("mailers.transports.aiofiles", None): with pytest.raises(DependencyNotFound) as ex: transports.FileTransport(directory) text = 'FileTransport requires "aiofiles" library installed.' assert str(ex.value) == text
def test_email_url_equals(): u = EmailURL("smtp://localhost") u2 = EmailURL("smtp://localhost") assert u == u2 u3 = EmailURL("file://localhost") assert u != u3
def test_email_url_repr(): u = EmailURL("smtp://localhost") assert repr(u) == "EmailURL('smtp://localhost')" u = EmailURL("smtp://username@localhost") assert repr(u) == "EmailURL('smtp://username@localhost')" u = EmailURL("smtp://*****:*****@localhost") assert repr(u) == "EmailURL('smtp://*****:*****@localhost')"
def test_email_url_properties(): u = EmailURL("smtp+sync://username:password@localhost:1025/") assert u.transport == "smtp+sync" assert u.username == "username" assert u.password == "password" assert u.hostname == "localhost" assert u.netloc == "username:password@localhost:1025" assert u.port == 1025
async def test_null_transport(message): # should create from EmailURL backend = transports.NullTransport.from_url(EmailURL("null://")) assert isinstance(backend, transports.NullTransport) # should create from URL string backend = transports.NullTransport.from_url("null://") assert isinstance(backend, transports.NullTransport) backend = transports.NullTransport() await backend.send(message)
async def test_in_memory_transport(message): # should create from EmailURL backend = transports.InMemoryTransport.from_url(EmailURL("memory://")) assert isinstance(backend, transports.InMemoryTransport) # should create from URL string backend = transports.InMemoryTransport.from_url("memory://") assert isinstance(backend, transports.InMemoryTransport) storage = [] backend = transports.InMemoryTransport(storage) await backend.send(message) assert len(storage) == 1 assert backend.mailbox == storage assert len(backend.mailbox) == 1
async def test_gmail_transport(message, smtpd_server, mailbox): # should create from EmailURL backend = transports.GMailTransport.from_url(EmailURL("gmail://username:password")) assert isinstance(backend, transports.GMailTransport) # should create from URL string backend = transports.GMailTransport.from_url("gmail://username:password") assert isinstance(backend, transports.GMailTransport) backend = transports.GMailTransport("username", "password", timeout=1) assert backend._host == "smtp.gmail.com" assert backend._port == 465 assert backend._use_tls is True assert backend._user == "username" assert backend._password == "password"
async def test_smtp_transport(message, smtpd_server, mailbox): # should create from EmailURL backend = transports.SMTPTransport.from_url(EmailURL("smtp://?timeout=1")) assert isinstance(backend, transports.SMTPTransport) # should create from URL string backend = transports.SMTPTransport.from_url("smtp://") assert isinstance(backend, transports.SMTPTransport) backend = transports.SMTPTransport( smtpd_server.hostname, smtpd_server.port, timeout=1 ) await backend.send(message) assert len(mailbox) == 1 with mock.patch("mailers.transports.aiosmtplib", None): with pytest.raises(DependencyNotFound) as ex: transports.SMTPTransport( smtpd_server.hostname, smtpd_server.port, timeout=1 ) assert str(ex.value) == 'SMTPTransport requires "aiosmtplib" library installed.'
def test_email_url_returns_path(): u = EmailURL("file:///tmp/some/path") assert u.path == "/tmp/some/path"
def test_replace_email_url_components(): # transport u = EmailURL("smtp://localhost") new = u.replace(transport="file") assert new.transport == "file" assert str(new) == "file://localhost" # username u = EmailURL("smtp://localhost") new = u.replace(username="******") assert new.username == "username" assert str(new) == "smtp://username@localhost" # password u = EmailURL("smtp://localhost") new = u.replace(username="******", password="******") assert new.password == "password" assert str(new) == "smtp://*****:*****@localhost" # hostname u = EmailURL("smtp://localhost") new = u.replace(hostname="otherhost") assert new.hostname == "otherhost" assert str(new) == "smtp://otherhost" # port u = EmailURL("smtp://localhost") new = u.replace(port=123) assert new.port == 123 assert str(new) == "smtp://localhost:123"
def test_email_url_options(): u = EmailURL( "smtp+sync://username:password@localhost:1025/?use_ssl=true&timeout=2") assert u.options == {"use_ssl": "true", "timeout": "2"}