Mercurial > libervia-backend
annotate tests/unit/test_email_gateway.py @ 4351:6a0a081485b8
plugin autocrypt: Autocrypt protocol implementation:
Implementation of autocrypt: `autocrypt` header is checked, and if present and no public
key is known for the peer, the key is imported.
`autocrypt` header is also added to outgoing message (only if an email gateway is
detected).
For the moment, the JID is use as identifier, but the real email used by gateway should be
used in the future.
rel 456
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 28 Feb 2025 09:23:35 +0100 |
parents | 699aa8788d98 |
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 |
4339
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
27 from wokkel import disco |
4318
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
28 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
29 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
|
30 EmailGatewayComponent, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
31 SendMailExtra, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
32 ) |
4310
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
33 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
|
34 Credentials, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
35 UserData, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
36 ) |
4339
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
37 from libervia.backend.plugins.plugin_comp_email_gateway.pubsub_service import ( |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
38 EmailGWPubsubService, |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
39 NODE_CONFIG, |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
40 NODE_CONFIG_VALUES, |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
41 NODE_OPTIONS, |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
42 ) |
4318
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
43 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
|
44 |
4339
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
45 @pytest.fixture |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
46 def email_gw(host): |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
47 email_gw = EmailGatewayComponent(host) |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
48 email_gw.storage = MagicMock() |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
49 email_gw.users_data = {} |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
50 return email_gw |
4310
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 class TestEmailGatewayComponent: |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 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
|
55 """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
|
56 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 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
|
59 credentials = {"user_email": "user@example.org"} |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 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
|
61 assert result == "user@some-email-domain.example" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 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
|
64 """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
|
65 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 address_jid = jid.JID("external@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 credentials = {"user_email": "user@example.org"} |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 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
|
70 assert result == '"xmpp:external@example.org" <user@example.org>' |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 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
|
73 """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
|
74 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 email_name = "" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 email_addr = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 user_email = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 user_jid = jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 result, name = email_gw.email_to_jid( |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 client, user_email, user_jid, email_name, email_addr |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 ) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 assert result == jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 assert name is None |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 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
|
87 """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
|
88 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 email_name = "xmpp:user@example.net" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 email_addr = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 user_email = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 user_jid = jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 result, name = email_gw.email_to_jid( |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 client, user_email, user_jid, email_name, email_addr |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 ) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 assert result == jid.JID("user@example.net") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 assert name is None |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 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
|
101 """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
|
102 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 email_name = "User Name" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 email_addr = "user@some-email-domain.example" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 user_email = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 user_jid = jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 result, name = email_gw.email_to_jid( |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 client, user_email, user_jid, email_name, email_addr |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 ) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 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
|
112 assert name == "User Name" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 @ed |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 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
|
116 """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
|
117 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 client.get_virtual_client = lambda __: client |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 client.sendMessage = AsyncMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 email_gw.client = client |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 email = EmailMessage() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 email["from"] = formataddr(("User Name", "sender@somewhere.example")) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 email["to"] = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 email.set_content("Hello, world!") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 to_jid = jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 user_email = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 user_data = UserData(Credentials({"user_email": user_email})) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 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
|
132 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 client.sendMessage.assert_called_once() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 call_args = client.sendMessage.call_args[0] |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 assert call_args[0] == to_jid |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 assert call_args[1] == {"": "Hello, world!\n"} |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 assert call_args[2] == None |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 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
|
139 to_jid, {"": "Hello, world!\n"}, None, extra={} |
4310
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 ) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 @ed |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 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
|
144 """Email with multiple recipients is correctly processed and sent .""" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 client = MagicMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 client.get_virtual_client = lambda __: client |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 client.jid = jid.JID("gateway.example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 client.sendMessage = AsyncMock() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 email_gw.client = client |
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 email = EmailMessage() |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 email["from"] = formataddr(("User Name", "user@example.org")) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 email["to"] = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 email["cc"] = "recipient2@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 email["bcc"] = "recipient3@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 email.set_content("Hello, world!") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 user_jid = jid.JID("gw-user@example.org") |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 user_email = "user@example.org" |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 user_data = UserData(Credentials({"user_email": user_email})) |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 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
|
162 |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 client.sendMessage.assert_called_once_with( |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 user_jid, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 {"": "Hello, world!\n"}, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 None, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 extra={ |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 "addresses": { |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 "to": [{"jid": "gw-user@example.org", "delivered": True}], |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 "cc": [ |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 { |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 "jid": "recipient2\\40example.org@gateway.example.org", |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 "delivered": True, |
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 "bcc": [ |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 { |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 "jid": "recipient3\\40example.org@gateway.example.org", |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 "delivered": True, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 } |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 ], |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 } |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 }, |
d27228b3c704
test (unit): add test for email gateway:
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 ) |
4318
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
185 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
186 @ed |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
187 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
|
188 """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
|
189 email_gw.client = MagicMock() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
190 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
|
191 email_gw.storage = MagicMock() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
192 email_gw.storage.get = AsyncMock( |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
193 return_value={ |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
194 "user_email": "user@example.org", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
195 "user_name": "Sender Name", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
196 "smtp_host": "smtp.example.org", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
197 "smtp_port": "587", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
198 "smtp_username": "sender", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
199 "smtp_password": "password", |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
200 } |
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 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
203 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
|
204 to_email = "recipient@example.com" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
205 body = "Hello, world!" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
206 subject = "Test email" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
207 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
|
208 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
209 # Mock the smtp.sendmail function |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
210 sendmail_mock = AsyncMock() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
211 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
|
212 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
213 await email_gw.send_email( |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
214 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
|
215 ) |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
216 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
217 sendmail_mock.assert_called_once() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
218 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
219 # 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
|
220 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
|
221 _, _, _, email_content_bytes = call_args |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
222 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
223 # Parse the email content |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
224 parser = BytesParser() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
225 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
|
226 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
227 # 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
|
228 assert msg["Keywords"] == headers.keywords |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
229 assert msg["Importance"] == "high" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
230 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
231 @ed |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
232 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
|
233 """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
|
234 client = MagicMock() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
235 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
|
236 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
|
237 client.sendMessage = AsyncMock() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
238 email_gw.client = client |
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 email = EmailMessage() |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
241 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
|
242 email["to"] = "user@example.org" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
243 email.set_content("Hello, world!") |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
244 email["Keywords"] = "test, example" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
245 email["Importance"] = "high" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
246 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
247 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
|
248 user_email = "user@example.org" |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
249 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
|
250 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
|
251 |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
252 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
|
253 to_jid, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
254 {"": "Hello, world!\n"}, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
255 None, |
27bb22eace65
tests (unit/email gateway): add test for XEP-0131 handling:
Goffi <goffi@goffi.org>
parents:
4310
diff
changeset
|
256 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
|
257 ) |
4339
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
258 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
259 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
260 class TestPubsubService: |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
261 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
262 @pytest.fixture |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
263 def pubsub_service(self, email_gw, client): |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
264 email_gw.client = client |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
265 service = EmailGWPubsubService(email_gw) |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
266 return service |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
267 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
268 @pytest.fixture |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
269 def pubsub_resource(self, pubsub_service): |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
270 return pubsub_service.resource |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
271 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
272 def test_getNodes(self, pubsub_resource, client): |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
273 """XEP-0498 well-known node is returned.""" |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
274 requestor = client.jid |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
275 service = client.pubsub_service |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
276 nodeIdentifier = "test_node" |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
277 result = pubsub_resource.getNodes(requestor, service, nodeIdentifier) |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
278 assert result.result == [pubsub_resource._pfs.namespace] |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
279 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
280 @ed |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
281 async def test_items(self, pubsub_resource, client, host): |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
282 """Items are retrieved from the storage""" |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
283 request = MagicMock() |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
284 request.sender = client.jid |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
285 request.nodeIdentifier = pubsub_resource._pfs.namespace |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
286 files = [ |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
287 {"id": "1", "name": "file1", "media_type": "application", "media_subtype": |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
288 "octet-stream", "size": 123, "hash_algo": "sha-256", "file_hash": "0123456789abcdef", |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
289 "created": 123}, |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
290 {"id": "2", "name": "file2", "media_type": "application", "media_subtype": |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
291 "octet-stream", "size": 456, "hash_algo": "sha-256", "file_hash": "0123456789abcdef", |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
292 "created": 123}, |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
293 ] |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
294 host.memory.get_files = AsyncMock(return_value=files) |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
295 result, _ = await pubsub_resource.items(request) |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
296 assert len(result) == 2 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
297 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
298 @ed |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
299 async def test_retract(self, pubsub_resource, client, host): |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
300 """Items are retracted from the storage""" |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
301 request = MagicMock() |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
302 request.sender = client.jid |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
303 request.nodeIdentifier = pubsub_resource._pfs.namespace |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
304 request.itemIdentifiers = ["item_1"] |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
305 host.memory.file_delete = AsyncMock() |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
306 await pubsub_resource.retract(request) |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
307 host.memory.file_delete.assert_called_once() |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
308 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
309 def test_getConfigurationOptions(self, pubsub_resource): |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
310 """Configuration options are returned""" |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
311 options = pubsub_resource.getConfigurationOptions() |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
312 assert options == NODE_OPTIONS |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
313 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
314 def test_getConfiguration(self, pubsub_resource, client): |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
315 """Configuration values are returned""" |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
316 requestor = client.jid |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
317 service = client.pubsub_service |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
318 nodeIdentifier = "test_node" |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
319 result = pubsub_resource.getConfiguration(requestor, service, nodeIdentifier) |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
320 assert result.result == NODE_CONFIG_VALUES |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
321 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
322 def test_getNodeInfo(self, pubsub_resource, client): |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
323 """Node information is returned""" |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
324 requestor = client.jid |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
325 service = client.pubsub_service |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
326 nodeIdentifier = pubsub_resource._pfs.namespace |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
327 info = pubsub_resource.getNodeInfo(requestor, service, nodeIdentifier) |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
328 assert info == {"type": "leaf", "meta-data": NODE_CONFIG} |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
329 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
330 @ed |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
331 async def test_getDiscoInfo(self, pubsub_service, client): |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
332 """Disco information is returned""" |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
333 requestor = client.jid |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
334 target = client.pubsub_service |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
335 nodeIdentifier = "" |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
336 result = await pubsub_service.getDiscoInfo(requestor, target, nodeIdentifier) |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
337 assert len(result) > 0 |
699aa8788d98
tests (unit/email gateway): add tests for pubsub service:
Goffi <goffi@goffi.org>
parents:
4318
diff
changeset
|
338 assert any(isinstance(info, disco.DiscoFeature) for info in result) |