Mercurial > libervia-backend
comparison tests/unit/test_email_gateway.py @ 4310:d27228b3c704
test (unit): add test for email gateway:
rel 450
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 26 Sep 2024 16:12:01 +0200 |
parents | |
children | 27bb22eace65 |
comparison
equal
deleted
inserted
replaced
4309:b56b1eae7994 | 4310:d27228b3c704 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 # Libervia: an XMPP client | |
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 from email.utils import formataddr | |
20 from unittest.mock import AsyncMock, MagicMock | |
21 import pytest | |
22 from pytest_twisted import ensureDeferred as ed | |
23 from twisted.words.protocols.jabber import jid | |
24 from email.message import EmailMessage | |
25 from libervia.backend.plugins.plugin_comp_email_gateway import EmailGatewayComponent | |
26 from libervia.backend.plugins.plugin_comp_email_gateway.models import ( | |
27 Credentials, | |
28 UserData, | |
29 ) | |
30 | |
31 | |
32 class TestEmailGatewayComponent: | |
33 @pytest.fixture | |
34 def email_gw(self, host): | |
35 email_gw = EmailGatewayComponent(host) | |
36 email_gw.storage = MagicMock() | |
37 email_gw.users_data = {} | |
38 return email_gw | |
39 | |
40 def test_jid_to_email_gateway_jid(self, email_gw): | |
41 """JID from the gateway is converted to an email address.""" | |
42 client = MagicMock() | |
43 client.jid = jid.JID("gateway.example.org") | |
44 address_jid = jid.JID(r"user\40some-email-domain.example@gateway.example.org") | |
45 credentials = {"user_email": "user@example.org"} | |
46 result = email_gw.jid_to_email(client, address_jid, credentials) | |
47 assert result == "user@some-email-domain.example" | |
48 | |
49 def test_jid_to_email_non_gateway_jid(self, email_gw): | |
50 """Non-gateway JID is converted to an email address with ``xmpp:``.""" | |
51 client = MagicMock() | |
52 client.jid = jid.JID("gateway.example.org") | |
53 address_jid = jid.JID("external@example.org") | |
54 credentials = {"user_email": "user@example.org"} | |
55 result = email_gw.jid_to_email(client, address_jid, credentials) | |
56 assert result == '"xmpp:external@example.org" <user@example.org>' | |
57 | |
58 def test_email_to_jid_user_email(self, email_gw): | |
59 """User email returns user JID if no "xmpp:" scheme is used.""" | |
60 client = MagicMock() | |
61 client.jid = jid.JID("gateway.example.org") | |
62 email_name = "" | |
63 email_addr = "user@example.org" | |
64 user_email = "user@example.org" | |
65 user_jid = jid.JID("gw-user@example.org") | |
66 result, name = email_gw.email_to_jid( | |
67 client, user_email, user_jid, email_name, email_addr | |
68 ) | |
69 assert result == jid.JID("gw-user@example.org") | |
70 assert name is None | |
71 | |
72 def test_email_to_jid_xmpp_address(self, email_gw): | |
73 """Email address with XMPP in name part is converted to a JID.""" | |
74 client = MagicMock() | |
75 client.jid = jid.JID("gateway.example.org") | |
76 email_name = "xmpp:user@example.net" | |
77 email_addr = "user@example.org" | |
78 user_email = "user@example.org" | |
79 user_jid = jid.JID("gw-user@example.org") | |
80 result, name = email_gw.email_to_jid( | |
81 client, user_email, user_jid, email_name, email_addr | |
82 ) | |
83 assert result == jid.JID("user@example.net") | |
84 assert name is None | |
85 | |
86 def test_email_to_jid_regular_email(self, email_gw): | |
87 """Regular email address is converted to a JID with escaped local part.""" | |
88 client = MagicMock() | |
89 client.jid = jid.JID("gateway.example.org") | |
90 email_name = "User Name" | |
91 email_addr = "user@some-email-domain.example" | |
92 user_email = "user@example.org" | |
93 user_jid = jid.JID("gw-user@example.org") | |
94 result, name = email_gw.email_to_jid( | |
95 client, user_email, user_jid, email_name, email_addr | |
96 ) | |
97 assert result == jid.JID(r"user\40some-email-domain.example@gateway.example.org") | |
98 assert name == "User Name" | |
99 | |
100 @ed | |
101 async def test_on_new_email_single_recipient(self, email_gw): | |
102 """Email with a single recipient is correctly processed and sent as a message.""" | |
103 client = MagicMock() | |
104 client.get_virtual_client = lambda __: client | |
105 client.jid = jid.JID("gateway.example.org") | |
106 client.sendMessage = AsyncMock() | |
107 email_gw.client = client | |
108 | |
109 email = EmailMessage() | |
110 email["from"] = formataddr(("User Name", "sender@somewhere.example")) | |
111 email["to"] = "user@example.org" | |
112 email.set_content("Hello, world!") | |
113 | |
114 to_jid = jid.JID("gw-user@example.org") | |
115 user_email = "user@example.org" | |
116 user_data = UserData(Credentials({"user_email": user_email})) | |
117 await email_gw.on_new_email(user_data, to_jid, email) | |
118 | |
119 client.sendMessage.assert_called_once() | |
120 call_args = client.sendMessage.call_args[0] | |
121 assert call_args[0] == to_jid | |
122 assert call_args[1] == {"": "Hello, world!\n"} | |
123 assert call_args[2] == None | |
124 client.sendMessage.assert_called_once_with( | |
125 to_jid, {"": "Hello, world!\n"}, None, extra=None | |
126 ) | |
127 | |
128 @ed | |
129 async def test_on_new_email_multiple_recipients(self, email_gw): | |
130 """Email with multiple recipients is correctly processed and sent .""" | |
131 client = MagicMock() | |
132 client.get_virtual_client = lambda __: client | |
133 client.jid = jid.JID("gateway.example.org") | |
134 client.sendMessage = AsyncMock() | |
135 email_gw.client = client | |
136 | |
137 email = EmailMessage() | |
138 email["from"] = formataddr(("User Name", "user@example.org")) | |
139 email["to"] = "user@example.org" | |
140 email["cc"] = "recipient2@example.org" | |
141 email["bcc"] = "recipient3@example.org" | |
142 email.set_content("Hello, world!") | |
143 | |
144 user_jid = jid.JID("gw-user@example.org") | |
145 user_email = "user@example.org" | |
146 user_data = UserData(Credentials({"user_email": user_email})) | |
147 await email_gw.on_new_email(user_data, user_jid, email) | |
148 | |
149 client.sendMessage.assert_called_once_with( | |
150 user_jid, | |
151 {"": "Hello, world!\n"}, | |
152 None, | |
153 extra={ | |
154 "addresses": { | |
155 "to": [{"jid": "gw-user@example.org", "delivered": True}], | |
156 "cc": [ | |
157 { | |
158 "jid": "recipient2\\40example.org@gateway.example.org", | |
159 "delivered": True, | |
160 } | |
161 ], | |
162 "bcc": [ | |
163 { | |
164 "jid": "recipient3\\40example.org@gateway.example.org", | |
165 "delivered": True, | |
166 } | |
167 ], | |
168 } | |
169 }, | |
170 ) |