annotate tests/unit/test_plugin_xep_0131.py @ 4318:27bb22eace65

tests (unit/email gateway): add test for XEP-0131 handling: rel 451
author Goffi <goffi@goffi.org>
date Sat, 28 Sep 2024 15:59:48 +0200
parents 8ee369e6eb99
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4315
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia: an XMPP client
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org)
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from unittest.mock import MagicMock
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
20
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from pytest_twisted import ensureDeferred as ed
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from twisted.internet import defer
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
24
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from libervia.backend.models.core import MessageData
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from libervia.backend.plugins.plugin_xep_0131 import (
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
27 HeadersData,
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 NS_SHIM,
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 XEP_0131,
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 Urgency,
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 Priority,
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 )
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
33
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
34
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
35 class TestXEP0131:
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
36
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
37 def test_headers_data_to_element(self):
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
38 """HeadersData instance is correctly converted to an XML element"""
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
39 headers = HeadersData(
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
40 keywords="test,keyword",
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
41 urgency=Urgency.high,
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
42 priority=Priority.urgent,
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
43 custom_header="custom_value", # type: ignore
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
44 )
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
45 headers_elt = headers.to_element()
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 assert headers_elt.uri == NS_SHIM
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 assert headers_elt.name == "headers"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 header_elts = list(headers_elt.elements(NS_SHIM, "header"))
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 assert len(header_elts) == 4
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
51
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 header_names = set()
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 for header_elt in header_elts:
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 header_names.add(header_elt["name"])
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 if header_elt["name"] == "keywords":
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
56 assert str(header_elt) == "test,keyword"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
57 elif header_elt["name"] == "urgency":
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
58 assert str(header_elt) == "high"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
59 elif header_elt["name"] == "priority":
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
60 assert str(header_elt) == "urgent"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
61 elif header_elt["name"] == "custom_header":
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
62 assert str(header_elt) == "custom_value"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
63
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 assert header_names == {"keywords", "urgency", "priority", "custom_header"}
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
65
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
66 def test_headers_data_from_element(self):
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 """HeadersData instance is correctly created from an XML element"""
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 headers_elt = domish.Element((NS_SHIM, "headers"))
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
69 keywords_elt = headers_elt.addElement("header")
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
70 keywords_elt["name"] = "keywords"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 keywords_elt.addContent("test,keyword")
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
72 urgency_elt = headers_elt.addElement("header")
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
73 urgency_elt["name"] = "urgency"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
74 urgency_elt.addContent("high")
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
75 custom_elt = headers_elt.addElement("header")
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
76 custom_elt["name"] = "custom_header"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
77 custom_elt.addContent("custom_value")
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
78
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
79 headers = HeadersData.from_element(headers_elt)
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
80
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
81 assert headers.keywords == "test,keyword"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
82 assert headers.urgency == Urgency.high
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
83 assert headers.priority is None
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
84 assert headers.custom_header == "custom_value" # type: ignore
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
85
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
86 def test_quote_value(self):
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
87 """Values are correctly quoted when necessary"""
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
88 xep_0131 = XEP_0131(MagicMock())
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
89
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
90 assert xep_0131.quote_value("simple") == "simple"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
91 assert xep_0131.quote_value("with space") == '"with space"'
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
92 assert xep_0131.quote_value('with "quotes"') == '"with \\"quotes\\""'
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
93 assert xep_0131.quote_value("with,comma") == '"with,comma"'
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
94
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
95 def test_unquote_values(self):
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
96 """Raw header values are correctly unquoted"""
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
97 xep_0131 = XEP_0131(MagicMock())
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
98
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
99 assert xep_0131.unquote_values("simple,another") == ["simple", "another"]
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
100 assert xep_0131.unquote_values('"quoted value",simple') == [
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
101 "quoted value",
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
102 "simple",
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
103 ]
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
104 assert xep_0131.unquote_values('"with,comma",simple') == ["with,comma", "simple"]
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
105 assert xep_0131.unquote_values('"with \\"quotes\\"",simple') == [
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
106 'with "quotes"',
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
107 "simple",
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
108 ]
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
109
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
110 @ed
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
111 async def test_send_message_trigger(self):
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
112 """Headers are correctly added to the message when sending"""
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
113 xep_0131 = XEP_0131(MagicMock())
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
114 client = MagicMock()
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
115
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
116 mess_data = MessageData(
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
117 {
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
118 "extra": {
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
119 "keywords": ["test", "keyword"],
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
120 "headers": {"urgency": "high", "custom_header": "custom_value"},
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
121 },
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
122 "xml": domish.Element(("jabber:client", "message")),
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
123 }
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
124 )
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
125
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
126 pre_xml_treatments = MagicMock()
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
127 post_xml_treatments = defer.Deferred()
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
128
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
129 result = xep_0131.send_message_trigger(
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
130 client, mess_data, pre_xml_treatments, post_xml_treatments
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
131 )
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
132 assert result is True
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
133
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
134 post_xml_treatments.callback(mess_data)
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
135 await post_xml_treatments
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
136
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
137 # Check that headers were added to the XML
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
138 headers_elt = next(mess_data["xml"].elements(NS_SHIM, "headers"))
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
139
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
140 header_names = set()
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
141 for header_elt in headers_elt.elements(NS_SHIM, "header"):
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
142 header_names.add(header_elt["name"])
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
143 if header_elt["name"] == "keywords":
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
144 assert str(header_elt) == "test,keyword"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
145 elif header_elt["name"] == "urgency":
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
146 assert str(header_elt) == "high"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
147 elif header_elt["name"] == "custom_header":
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
148 assert str(header_elt) == "custom_value"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
149
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
150 assert header_names == {"keywords", "urgency", "custom_header"}
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
151
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
152 @ed
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
153 async def test_message_received_trigger(self):
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
154 """Headers are correctly parsed from received messages"""
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
155 xep_0131 = XEP_0131(MagicMock())
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
156 client = MagicMock()
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
157
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
158 message_elt = domish.Element(("jabber:client", "message"))
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
159 headers_elt = message_elt.addElement((NS_SHIM, "headers"))
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
160 keywords_elt = headers_elt.addElement("header")
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
161 keywords_elt["name"] = "keywords"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
162 keywords_elt.addContent('test,"keyword with space"')
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
163 urgency_elt = headers_elt.addElement("header")
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
164 urgency_elt["name"] = "urgency"
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
165 urgency_elt.addContent("high")
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
166
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
167 post_treat = defer.Deferred()
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
168
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
169 result = xep_0131.message_received_trigger(client, message_elt, post_treat)
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
170
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
171 assert result is True
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
172
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
173 mess_data = MessageData({"extra": {}})
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
174 post_treat.callback(mess_data)
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
175 await post_treat
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
176
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
177 assert mess_data["extra"]["keywords"] == ["test", "keyword with space"]
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
178 headers = mess_data["extra"]["headers"]
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
179 assert "keywords" not in headers
8ee369e6eb99 tests (unit): add XEP-0131 tests:
Goffi <goffi@goffi.org>
parents:
diff changeset
180 assert headers["urgency"] == "high"