Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def test_connect_tls_context_option_takes_precedence(
hostname, smtpd_server_port, client_tls_context, server_tls_context
):
client = SMTP(
hostname=hostname, port=smtpd_server_port, tls_context=server_tls_context
)
await client.connect(tls_context=client_tls_context)
assert client.tls_context is client_tls_context
await client.quit()
async def test_connect_event_loop_takes_precedence(
event_loop, event_loop_policy, hostname, smtpd_server_port
):
init_loop = event_loop_policy.new_event_loop()
with pytest.warns(DeprecationWarning):
client = SMTP(hostname=hostname, port=smtpd_server_port, loop=init_loop)
with pytest.warns(DeprecationWarning):
await client.connect(loop=event_loop)
assert init_loop is not event_loop
assert client.loop is event_loop
await client.quit()
def test_quit_reconnect_ok(self):
code, message = yield from self.smtp.quit()
self.assertTrue(200 <= code <= 299)
# Next command should fail
with self.assertRaises(SMTPServerDisconnected):
code, message = yield from self.smtp.noop()
yield from self.smtp.reconnect()
# after reconnect, it should work again
code, message = yield from self.smtp.noop()
self.assertTrue(200 <= code <= 299)
async def test_quit_then_connect_ok(smtp_client, smtpd_server):
async with smtp_client:
response = await smtp_client.quit()
assert response.code == SMTPStatus.closing
# Next command should fail
with pytest.raises(SMTPServerDisconnected):
response = await smtp_client.noop()
await smtp_client.connect()
# after reconnect, it should work again
response = await smtp_client.noop()
assert response.code == SMTPStatus.completed
async def test_connect_error_second_attempt(hostname, unused_tcp_port):
client = SMTP(hostname=hostname, port=unused_tcp_port, timeout=1.0)
with pytest.raises(SMTPConnectError):
await client.connect()
with pytest.raises(SMTPConnectError):
await client.connect()
async def test_default_port_on_connect(
event_loop, bind_address, use_tls, start_tls, expected_port
):
client = SMTP()
try:
await client.connect(
hostname=bind_address, use_tls=use_tls, start_tls=start_tls, timeout=0.001
)
except (asyncio.TimeoutError, OSError):
pass
assert client.port == expected_port
client.close()
async def test_close_works_on_stopped_loop(smtpd_server, event_loop, hostname, port):
client = SMTP(hostname=hostname, port=port)
await client.connect()
assert client.is_connected
assert client._writer is not None
event_loop.stop()
client.close()
assert not client.is_connected
def test_mock_server_starttls_with_smtplib(preset_server):
smtp = smtplib.SMTP()
smtp._host = preset_server.hostname # Hack around smtplib SNI bug
smtp.connect(host=preset_server.hostname, port=preset_server.port)
preset_server.responses.append(b'\n'.join([
b'250-localhost, hello',
b'250-SIZE 100000',
b'250 STARTTLS',
]))
code, message = smtp.ehlo()
assert code == SMTPStatus.completed
preset_server.responses.append(b'220 ready for TLS')
code, message = smtp.starttls()
assert code == SMTPStatus.ready
# make sure our connection was actually upgraded
assert isinstance(smtp.sock, ssl.SSLSocket)
preset_server.responses.append(b'250 all good')
code, message = smtp.ehlo()
assert code == SMTPStatus.completed
async def test_helo_ok(smtp_client, smtpd_server):
async with smtp_client:
response = await smtp_client.helo()
assert response.code == SMTPStatus.completed
async def test_starttls(smtp_client, smtpd_server):
async with smtp_client:
response = await smtp_client.starttls(validate_certs=False)
assert response.code == SMTPStatus.ready
# Make sure our state has been cleared
assert not smtp_client.esmtp_extensions
assert not smtp_client.supported_auth_methods
assert not smtp_client.supports_esmtp
# Make sure our connection was actually upgraded. ssl protocol transport is
# private in UVloop, so just check the class name.
assert "SSL" in type(smtp_client.transport).__name__
response = await smtp_client.ehlo()
assert response.code == SMTPStatus.completed