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_protocol_connect(event_loop, hostname, echo_server_port):
connect_future = event_loop.create_connection(
SMTPProtocol, host=hostname, port=echo_server_port
)
transport, protocol = await asyncio.wait_for(connect_future, timeout=1.0)
assert protocol.transport is transport
assert not protocol.transport.is_closing()
transport.close()
async def test_protocol_reader_connected_check_on_start_tls(client_tls_context):
smtp_protocol = SMTPProtocol()
with pytest.raises(SMTPServerDisconnected):
await smtp_protocol.start_tls(client_tls_context, timeout=1.0)
async def test_protocol_read_response_with_timeout_times_out(
event_loop, echo_server, hostname, echo_server_port
):
connect_future = event_loop.create_connection(
SMTPProtocol, host=hostname, port=echo_server_port
)
transport, protocol = await asyncio.wait_for(connect_future, timeout=1.0)
with pytest.raises(SMTPTimeoutError) as exc:
await protocol.read_response(timeout=0.0)
transport.close()
assert str(exc.value) == "Timed out waiting for server response"
async def test_protocol_timeout_on_starttls(
event_loop, bind_address, hostname, client_tls_context
):
async def client_connected(reader, writer):
await asyncio.sleep(1.0)
server = await asyncio.start_server(
client_connected, host=bind_address, port=0, family=socket.AF_INET
)
server_port = server.sockets[0].getsockname()[1]
connect_future = event_loop.create_connection(
SMTPProtocol, host=hostname, port=server_port
)
_, protocol = await asyncio.wait_for(connect_future, timeout=1.0)
with pytest.raises(SMTPTimeoutError):
# STARTTLS timeout must be > 0
await protocol.start_tls(client_tls_context, timeout=0.00001)
server.close()
await server.wait_closed()
async def test_protocol_connected_check_on_read_response(monkeypatch):
protocol = SMTPProtocol()
monkeypatch.setattr(protocol, "transport", None)
with pytest.raises(SMTPServerDisconnected):
await protocol.read_response(timeout=1.0)
async def _create_connection(self) -> SMTPResponse:
if self.loop is None:
raise RuntimeError("No event loop set")
protocol = SMTPProtocol(
loop=self.loop, connection_lost_callback=self._connection_lost
)
tls_context = None # type: Optional[ssl.SSLContext]
ssl_handshake_timeout = None # type: Optional[float]
if self.use_tls:
tls_context = self._get_tls_context()
ssl_handshake_timeout = self.timeout
if self.sock:
connect_coro = create_connection(
self.loop,
lambda: protocol,
sock=self.sock,
ssl=tls_context,
ssl_handshake_timeout=ssl_handshake_timeout,