|
| 1 | +# Copyright 2021 The Matrix.org Foundation C.I.C. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | + |
| 16 | +from typing import List, Tuple |
| 17 | + |
| 18 | +from zope.interface import implementer |
| 19 | + |
| 20 | +from twisted.internet import defer |
| 21 | +from twisted.internet.address import IPv4Address |
| 22 | +from twisted.internet.defer import ensureDeferred |
| 23 | +from twisted.mail import interfaces, smtp |
| 24 | + |
| 25 | +from tests.server import FakeTransport |
| 26 | +from tests.unittest import HomeserverTestCase |
| 27 | + |
| 28 | + |
| 29 | +@implementer(interfaces.IMessageDelivery) |
| 30 | +class _DummyMessageDelivery: |
| 31 | + def __init__(self): |
| 32 | + # (recipient, message) tuples |
| 33 | + self.messages: List[Tuple[smtp.Address, bytes]] = [] |
| 34 | + |
| 35 | + def receivedHeader(self, helo, origin, recipients): |
| 36 | + return None |
| 37 | + |
| 38 | + def validateFrom(self, helo, origin): |
| 39 | + return origin |
| 40 | + |
| 41 | + def record_message(self, recipient: smtp.Address, message: bytes): |
| 42 | + self.messages.append((recipient, message)) |
| 43 | + |
| 44 | + def validateTo(self, user: smtp.User): |
| 45 | + return lambda: _DummyMessage(self, user) |
| 46 | + |
| 47 | + |
| 48 | +@implementer(interfaces.IMessageSMTP) |
| 49 | +class _DummyMessage: |
| 50 | + """IMessageSMTP implementation which saves the message delivered to it |
| 51 | + to the _DummyMessageDelivery object. |
| 52 | + """ |
| 53 | + |
| 54 | + def __init__(self, delivery: _DummyMessageDelivery, user: smtp.User): |
| 55 | + self._delivery = delivery |
| 56 | + self._user = user |
| 57 | + self._buffer: List[bytes] = [] |
| 58 | + |
| 59 | + def lineReceived(self, line): |
| 60 | + self._buffer.append(line) |
| 61 | + |
| 62 | + def eomReceived(self): |
| 63 | + message = b"\n".join(self._buffer) + b"\n" |
| 64 | + self._delivery.record_message(self._user.dest, message) |
| 65 | + return defer.succeed(b"saved") |
| 66 | + |
| 67 | + def connectionLost(self): |
| 68 | + pass |
| 69 | + |
| 70 | + |
| 71 | +class SendEmailHandlerTestCase(HomeserverTestCase): |
| 72 | + def test_send_email(self): |
| 73 | + """Happy-path test that we can send email to a non-TLS server.""" |
| 74 | + h = self.hs.get_send_email_handler() |
| 75 | + d = ensureDeferred( |
| 76 | + h.send_email( |
| 77 | + "foo@bar.com", "test subject", "Tests", "HTML content", "Text content" |
| 78 | + ) |
| 79 | + ) |
| 80 | + # there should be an attempt to connect to localhost:25 |
| 81 | + self.assertEqual(len(self.reactor.tcpClients), 1) |
| 82 | + (host, port, client_factory, _timeout, _bindAddress) = self.reactor.tcpClients[ |
| 83 | + 0 |
| 84 | + ] |
| 85 | + self.assertEqual(host, "localhost") |
| 86 | + self.assertEqual(port, 25) |
| 87 | + |
| 88 | + # wire it up to an SMTP server |
| 89 | + message_delivery = _DummyMessageDelivery() |
| 90 | + server_protocol = smtp.ESMTP() |
| 91 | + server_protocol.delivery = message_delivery |
| 92 | + # make sure that the server uses the test reactor to set timeouts |
| 93 | + server_protocol.callLater = self.reactor.callLater # type: ignore[assignment] |
| 94 | + |
| 95 | + client_protocol = client_factory.buildProtocol(None) |
| 96 | + client_protocol.makeConnection(FakeTransport(server_protocol, self.reactor)) |
| 97 | + server_protocol.makeConnection( |
| 98 | + FakeTransport( |
| 99 | + client_protocol, |
| 100 | + self.reactor, |
| 101 | + peer_address=IPv4Address("TCP", "127.0.0.1", 1234), |
| 102 | + ) |
| 103 | + ) |
| 104 | + |
| 105 | + # the message should now get delivered |
| 106 | + self.get_success(d, by=0.1) |
| 107 | + |
| 108 | + # check it arrived |
| 109 | + self.assertEqual(len(message_delivery.messages), 1) |
| 110 | + user, msg = message_delivery.messages.pop() |
| 111 | + self.assertEqual(str(user), "foo@bar.com") |
| 112 | + self.assertIn(b"Subject: test subject", msg) |
0 commit comments