Mercurial > libervia-backend
annotate tests/unit/test_email_gateway.py @ 4322:00837fa13e5a default tip @
tools (common/template), cli (call/gui): use font-awesome instead of fontello:
following change in Libervia Media, code has been updated to use font-awesome now instead
of fontello.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 26 Oct 2024 22:42:17 +0200 |
parents | 27bb22eace65 |
children |
rev | line source |
---|---|
4310
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia: an XMPP client |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
4318
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
19 from email.message import EmailMessage |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
20 from email.parser import BytesParser |
4310
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from email.utils import formataddr |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 from unittest.mock import AsyncMock, MagicMock |
4318
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
23 |
4310
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 import pytest |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 from pytest_twisted import ensureDeferred as ed |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.words.protocols.jabber import jid |
4318
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
27 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
28 from libervia.backend.plugins.plugin_comp_email_gateway import ( |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
29 EmailGatewayComponent, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
30 SendMailExtra, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
31 ) |
4310
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
32 from libervia.backend.plugins.plugin_comp_email_gateway.models import ( |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 Credentials, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
34 UserData, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 ) |
4318
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
36 from libervia.backend.plugins.plugin_xep_0131 import HeadersData, Urgency |
4310
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
37 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
38 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
39 class TestEmailGatewayComponent: |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 @pytest.fixture |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 def email_gw(self, host): |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 email_gw = EmailGatewayComponent(host) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 email_gw.storage = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 email_gw.users_data = {} |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 return email_gw |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 def test_jid_to_email_gateway_jid(self, email_gw): |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 """JID from the gateway is converted to an email address.""" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 address_jid = jid.JID(r"user\40some-email-domain.example@gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 credentials = {"user_email": "user@example.org"} |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 result = email_gw.jid_to_email(client, address_jid, credentials) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 assert result == "user@some-email-domain.example" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 def test_jid_to_email_non_gateway_jid(self, email_gw): |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 """Non-gateway JID is converted to an email address with ``xmpp:``.""" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 address_jid = jid.JID("external@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 credentials = {"user_email": "user@example.org"} |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 result = email_gw.jid_to_email(client, address_jid, credentials) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 assert result == '"xmpp:external@example.org" <user@example.org>' |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 def test_email_to_jid_user_email(self, email_gw): |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 """User email returns user JID if no "xmpp:" scheme is used.""" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 email_name = "" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 email_addr = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 user_email = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 user_jid = jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 result, name = email_gw.email_to_jid( |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 client, user_email, user_jid, email_name, email_addr |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 ) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 assert result == jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 assert name is None |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 def test_email_to_jid_xmpp_address(self, email_gw): |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 """Email address with XMPP in name part is converted to a JID.""" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 email_name = "xmpp:user@example.net" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 email_addr = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 user_email = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 user_jid = jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 result, name = email_gw.email_to_jid( |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 client, user_email, user_jid, email_name, email_addr |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 ) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 assert result == jid.JID("user@example.net") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 assert name is None |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 def test_email_to_jid_regular_email(self, email_gw): |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 """Regular email address is converted to a JID with escaped local part.""" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 email_name = "User Name" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 email_addr = "user@some-email-domain.example" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 user_email = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 user_jid = jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 result, name = email_gw.email_to_jid( |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 client, user_email, user_jid, email_name, email_addr |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 ) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 assert result == jid.JID(r"user\40some-email-domain.example@gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 assert name == "User Name" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 @ed |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 async def test_on_new_email_single_recipient(self, email_gw): |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 """Email with a single recipient is correctly processed and sent as a message.""" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 client.get_virtual_client = lambda __: client |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 client.sendMessage = AsyncMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 email_gw.client = client |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 email = EmailMessage() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 email["from"] = formataddr(("User Name", "sender@somewhere.example")) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 email["to"] = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 email.set_content("Hello, world!") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 to_jid = jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 user_email = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 user_data = UserData(Credentials({"user_email": user_email})) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 await email_gw.on_new_email(user_data, to_jid, email) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 client.sendMessage.assert_called_once() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 call_args = client.sendMessage.call_args[0] |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 assert call_args[0] == to_jid |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 assert call_args[1] == {"": "Hello, world!\n"} |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 assert call_args[2] == None |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 client.sendMessage.assert_called_once_with( |
4318
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
132 to_jid, {"": "Hello, world!\n"}, None, extra={} |
4310
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 ) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 @ed |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 async def test_on_new_email_multiple_recipients(self, email_gw): |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 """Email with multiple recipients is correctly processed and sent .""" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 client.get_virtual_client = lambda __: client |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 client.sendMessage = AsyncMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 email_gw.client = client |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 email = EmailMessage() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 email["from"] = formataddr(("User Name", "user@example.org")) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 email["to"] = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 email["cc"] = "recipient2@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 email["bcc"] = "recipient3@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 email.set_content("Hello, world!") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 user_jid = jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 user_email = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 user_data = UserData(Credentials({"user_email": user_email})) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 await email_gw.on_new_email(user_data, user_jid, email) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 client.sendMessage.assert_called_once_with( |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 user_jid, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 {"": "Hello, world!\n"}, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 None, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 extra={ |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 "addresses": { |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 "to": [{"jid": "gw-user@example.org", "delivered": True}], |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 "cc": [ |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 { |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 "jid": "recipient2\\40example.org@gateway.example.org", |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 "delivered": True, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 } |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 ], |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 "bcc": [ |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 { |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 "jid": "recipient3\\40example.org@gateway.example.org", |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 "delivered": True, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 } |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 ], |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 } |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 }, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 ) |
4318
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
178 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
179 @ed |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
180 async def test_send_email_with_headers(self, email_gw, monkeypatch): |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
181 """Email is sent with correct headers.""" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
182 email_gw.client = MagicMock() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
183 email_gw.client.jid = jid.JID("gateway.example.org") |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
184 email_gw.storage = MagicMock() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
185 email_gw.storage.get = AsyncMock( |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
186 return_value={ |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
187 "user_email": "user@example.org", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
188 "user_name": "Sender Name", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
189 "smtp_host": "smtp.example.org", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
190 "smtp_port": "587", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
191 "smtp_username": "sender", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
192 "smtp_password": "password", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
193 } |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
194 ) |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
195 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
196 from_jid = jid.JID("user@example.org") |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
197 to_email = "recipient@example.com" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
198 body = "Hello, world!" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
199 subject = "Test email" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
200 headers = HeadersData(keywords="important,urgent", urgency=Urgency.high) |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
201 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
202 # Mock the smtp.sendmail function |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
203 sendmail_mock = AsyncMock() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
204 monkeypatch.setattr("twisted.mail.smtp.sendmail", sendmail_mock) |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
205 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
206 await email_gw.send_email( |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
207 from_jid, to_email, body, subject, extra=SendMailExtra(headers=headers) |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
208 ) |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
209 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
210 sendmail_mock.assert_called_once() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
211 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
212 # Extract the email content from the call arguments |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
213 call_args = sendmail_mock.call_args[0] |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
214 _, _, _, email_content_bytes = call_args |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
215 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
216 # Parse the email content |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
217 parser = BytesParser() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
218 msg = parser.parsebytes(email_content_bytes) |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
219 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
220 # Assert the headers are correctly set |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
221 assert msg["Keywords"] == headers.keywords |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
222 assert msg["Importance"] == "high" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
223 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
224 @ed |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
225 async def test_on_new_email_with_headers(self, email_gw): |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
226 """Headers from the email are correctly processed and included in the message.""" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
227 client = MagicMock() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
228 client.get_virtual_client = lambda __: client |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
229 client.jid = jid.JID("gateway.example.org") |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
230 client.sendMessage = AsyncMock() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
231 email_gw.client = client |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
232 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
233 email = EmailMessage() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
234 email["from"] = formataddr(("User Name", "sender@somewhere.example")) |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
235 email["to"] = "user@example.org" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
236 email.set_content("Hello, world!") |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
237 email["Keywords"] = "test, example" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
238 email["Importance"] = "high" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
239 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
240 to_jid = jid.JID("gw-user@example.org") |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
241 user_email = "user@example.org" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
242 user_data = UserData(Credentials({"user_email": user_email})) |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
243 await email_gw.on_new_email(user_data, to_jid, email) |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
244 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
245 client.sendMessage.assert_called_once_with( |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
246 to_jid, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
247 {"": "Hello, world!\n"}, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
248 None, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
249 extra={"headers": {"keywords": "test, example", "urgency": "high"}}, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
250 ) |