comparison libervia/backend/test/test_plugin_xep_0313.py @ 4071:4b842c1fb686

refactoring: renamed `sat` package to `libervia.backend`
author Goffi <goffi@goffi.org>
date Fri, 02 Jun 2023 11:49:51 +0200
parents sat/test/test_plugin_xep_0313.py@524856bd7b19
children
comparison
equal deleted inserted replaced
4070:d10748475025 4071:4b842c1fb686
1 #!/usr/bin/env python3
2
3
4 # SAT: a jabber client
5 # Copyright (C) 2009-2021 Jérôme Poisson (goffi@goffi.org)
6 # Copyright (C) 2013-2016 Adrien Cossa (souliane@mailoo.org)
7
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU Affero General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU Affero General Public License for more details.
17
18 # You should have received a copy of the GNU Affero General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 """ Plugin XEP-0313 """
22
23 from .constants import Const as C
24 from libervia.backend.test import helpers
25 from libervia.backend.plugins.plugin_xep_0313 import XEP_0313
26 from twisted.words.protocols.jabber.jid import JID
27 from twisted.words.xish import domish
28 from wokkel.data_form import Field
29 from dateutil.tz import tzutc
30 import datetime
31
32 # TODO: change this when RSM and MAM are in wokkel
33 from sat_tmp.wokkel.rsm import RSMRequest
34 from sat_tmp.wokkel.mam import buildForm, MAMRequest
35
36 NS_PUBSUB = "http://jabber.org/protocol/pubsub"
37 SERVICE = "sat-pubsub.tazar.int"
38 SERVICE_JID = JID(SERVICE)
39
40
41 class XEP_0313Test(helpers.SatTestCase):
42 def setUp(self):
43 self.host = helpers.FakeSAT()
44 self.plugin = XEP_0313(self.host)
45 self.client = self.host.get_client(C.PROFILE[0])
46 mam_client = self.plugin.get_handler(C.PROFILE[0])
47 mam_client.makeConnection(self.host.get_client(C.PROFILE[0]).xmlstream)
48
49 def test_query_archive(self):
50 xml = """
51 <iq type='set' id='%s' to='%s'>
52 <query xmlns='urn:xmpp:mam:1'/>
53 </iq>
54 """ % (
55 ("H_%d" % domish.Element._idCounter),
56 SERVICE,
57 )
58 d = self.plugin.queryArchive(self.client, MAMRequest(), SERVICE_JID)
59 d.addCallback(
60 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
61 )
62 return d
63
64 def test_query_archive_pubsub(self):
65 xml = """
66 <iq type='set' id='%s' to='%s'>
67 <query xmlns='urn:xmpp:mam:1' node='fdp/submitted/capulet.lit/sonnets' />
68 </iq>
69 """ % (
70 ("H_%d" % domish.Element._idCounter),
71 SERVICE,
72 )
73 d = self.plugin.queryArchive(
74 self.client, MAMRequest(node="fdp/submitted/capulet.lit/sonnets"), SERVICE_JID
75 )
76 d.addCallback(
77 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
78 )
79 return d
80
81 def test_query_archive_with(self):
82 xml = """
83 <iq type='set' id='%s' to='%s'>
84 <query xmlns='urn:xmpp:mam:1'>
85 <x xmlns='jabber:x:data' type='submit'>
86 <field var='FORM_TYPE' type='hidden'>
87 <value>urn:xmpp:mam:1</value>
88 </field>
89 <field var='with' type='jid-single'>
90 <value>juliet@capulet.lit</value>
91 </field>
92 </x>
93 </query>
94 </iq>
95 """ % (
96 ("H_%d" % domish.Element._idCounter),
97 SERVICE,
98 )
99 form = buildForm(with_jid=JID("juliet@capulet.lit"))
100 d = self.plugin.queryArchive(self.client, MAMRequest(form), SERVICE_JID)
101 d.addCallback(
102 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
103 )
104 return d
105
106 def test_query_archive_start_end(self):
107 xml = """
108 <iq type='set' id='%s' to='%s'>
109 <query xmlns='urn:xmpp:mam:1'>
110 <x xmlns='jabber:x:data' type='submit'>
111 <field var='FORM_TYPE' type='hidden'>
112 <value>urn:xmpp:mam:1</value>
113 </field>
114 <field var='start' type='text-single'>
115 <value>2010-06-07T00:00:00Z</value>
116 </field>
117 <field var='end' type='text-single'>
118 <value>2010-07-07T13:23:54Z</value>
119 </field>
120 </x>
121 </query>
122 </iq>
123 """ % (
124 ("H_%d" % domish.Element._idCounter),
125 SERVICE,
126 )
127 start = datetime.datetime(2010, 6, 7, 0, 0, 0, tzinfo=tzutc())
128 end = datetime.datetime(2010, 7, 7, 13, 23, 54, tzinfo=tzutc())
129 form = buildForm(start=start, end=end)
130 d = self.plugin.queryArchive(self.client, MAMRequest(form), SERVICE_JID)
131 d.addCallback(
132 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
133 )
134 return d
135
136 def test_query_archive_start(self):
137 xml = """
138 <iq type='set' id='%s' to='%s'>
139 <query xmlns='urn:xmpp:mam:1'>
140 <x xmlns='jabber:x:data' type='submit'>
141 <field var='FORM_TYPE' type='hidden'>
142 <value>urn:xmpp:mam:1</value>
143 </field>
144 <field var='start' type='text-single'>
145 <value>2010-08-07T00:00:00Z</value>
146 </field>
147 </x>
148 </query>
149 </iq>
150 """ % (
151 ("H_%d" % domish.Element._idCounter),
152 SERVICE,
153 )
154 start = datetime.datetime(2010, 8, 7, 0, 0, 0, tzinfo=tzutc())
155 form = buildForm(start=start)
156 d = self.plugin.queryArchive(self.client, MAMRequest(form), SERVICE_JID)
157 d.addCallback(
158 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
159 )
160 return d
161
162 def test_query_archive_rsm(self):
163 xml = """
164 <iq type='set' id='%s' to='%s'>
165 <query xmlns='urn:xmpp:mam:1'>
166 <x xmlns='jabber:x:data' type='submit'>
167 <field var='FORM_TYPE' type='hidden'>
168 <value>urn:xmpp:mam:1</value>
169 </field>
170 <field var='start' type='text-single'>
171 <value>2010-08-07T00:00:00Z</value>
172 </field>
173 </x>
174 <set xmlns='http://jabber.org/protocol/rsm'>
175 <max>10</max>
176 </set>
177 </query>
178 </iq>
179 """ % (
180 ("H_%d" % domish.Element._idCounter),
181 SERVICE,
182 )
183 start = datetime.datetime(2010, 8, 7, 0, 0, 0, tzinfo=tzutc())
184 form = buildForm(start=start)
185 rsm = RSMRequest(max_=10)
186 d = self.plugin.queryArchive(self.client, MAMRequest(form, rsm), SERVICE_JID)
187 d.addCallback(
188 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
189 )
190 return d
191
192 def test_query_archive_rsm_paging(self):
193 xml = """
194 <iq type='set' id='%s' to='%s'>
195 <query xmlns='urn:xmpp:mam:1'>
196 <x xmlns='jabber:x:data' type='submit'>
197 <field var='FORM_TYPE' type='hidden'><value>urn:xmpp:mam:1</value></field>
198 <field var='start' type='text-single'><value>2010-08-07T00:00:00Z</value></field>
199 </x>
200 <set xmlns='http://jabber.org/protocol/rsm'>
201 <max>10</max>
202 <after>09af3-cc343-b409f</after>
203 </set>
204 </query>
205 </iq>
206 """ % (
207 ("H_%d" % domish.Element._idCounter),
208 SERVICE,
209 )
210 start = datetime.datetime(2010, 8, 7, 0, 0, 0, tzinfo=tzutc())
211 form = buildForm(start=start)
212 rsm = RSMRequest(max_=10, after="09af3-cc343-b409f")
213 d = self.plugin.queryArchive(self.client, MAMRequest(form, rsm), SERVICE_JID)
214 d.addCallback(
215 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
216 )
217 return d
218
219 def test_query_fields(self):
220 xml = """
221 <iq type='get' id="%s" to='%s'>
222 <query xmlns='urn:xmpp:mam:1'/>
223 </iq>
224 """ % (
225 ("H_%d" % domish.Element._idCounter),
226 SERVICE,
227 )
228 d = self.plugin.queryFields(self.client, SERVICE_JID)
229 d.addCallback(
230 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
231 )
232 return d
233
234 def test_query_archive_fields(self):
235 xml = """
236 <iq type='set' id='%s' to='%s'>
237 <query xmlns='urn:xmpp:mam:1'>
238 <x xmlns='jabber:x:data' type='submit'>
239 <field type='hidden' var='FORM_TYPE'>
240 <value>urn:xmpp:mam:1</value>
241 </field>
242 <field type='text-single' var='urn:example:xmpp:free-text-search'>
243 <value>Where arth thou, my Juliet?</value>
244 </field>
245 <field type='text-single' var='urn:example:xmpp:stanza-content'>
246 <value>{http://jabber.org/protocol/mood}mood/lonely</value>
247 </field>
248 </x>
249 </query>
250 </iq>
251 """ % (
252 ("H_%d" % domish.Element._idCounter),
253 SERVICE,
254 )
255 extra_fields = [
256 Field(
257 "text-single",
258 "urn:example:xmpp:free-text-search",
259 "Where arth thou, my Juliet?",
260 ),
261 Field(
262 "text-single",
263 "urn:example:xmpp:stanza-content",
264 "{http://jabber.org/protocol/mood}mood/lonely",
265 ),
266 ]
267 form = buildForm(extra_fields=extra_fields)
268 d = self.plugin.queryArchive(self.client, MAMRequest(form), SERVICE_JID)
269 d.addCallback(
270 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
271 )
272 return d
273
274 def test_query_prefs(self):
275 xml = """
276 <iq type='get' id='%s' to='%s'>
277 <prefs xmlns='urn:xmpp:mam:1'>
278 <always/>
279 <never/>
280 </prefs>
281 </iq>
282 """ % (
283 ("H_%d" % domish.Element._idCounter),
284 SERVICE,
285 )
286 d = self.plugin.get_prefs(self.client, SERVICE_JID)
287 d.addCallback(
288 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
289 )
290 return d
291
292 def test_set_prefs(self):
293 xml = """
294 <iq type='set' id='%s' to='%s'>
295 <prefs xmlns='urn:xmpp:mam:1' default='roster'>
296 <always>
297 <jid>romeo@montague.lit</jid>
298 </always>
299 <never>
300 <jid>montague@montague.lit</jid>
301 </never>
302 </prefs>
303 </iq>
304 """ % (
305 ("H_%d" % domish.Element._idCounter),
306 SERVICE,
307 )
308 always = [JID("romeo@montague.lit")]
309 never = [JID("montague@montague.lit")]
310 d = self.plugin.setPrefs(self.client, SERVICE_JID, always=always, never=never)
311 d.addCallback(
312 lambda __: self.assert_equal_xml(self.host.get_sent_message_xml(0), xml, True)
313 )
314 return d