Mercurial > libervia-backend
comparison tests/unit/test_plugin_xep_0420.py @ 4285:f1d0cde61af7
tests (unit): fix tests + black reformatting.
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 14 Jul 2024 17:42:53 +0200 |
parents | 4b842c1fb686 |
children |
comparison
equal
deleted
inserted
replaced
4284:3a550e9a2b55 | 4285:f1d0cde61af7 |
---|---|
22 import pytest | 22 import pytest |
23 from libervia.backend.core import exceptions | 23 from libervia.backend.core import exceptions |
24 | 24 |
25 from libervia.backend.plugins.plugin_xep_0334 import NS_HINTS | 25 from libervia.backend.plugins.plugin_xep_0334 import NS_HINTS |
26 from libervia.backend.plugins.plugin_xep_0420 import ( | 26 from libervia.backend.plugins.plugin_xep_0420 import ( |
27 NS_SCE, XEP_0420, AffixVerificationFailed, ProfileRequirementsNotMet, SCEAffixPolicy, | 27 NS_SCE, |
28 SCECustomAffix, SCEProfile | 28 XEP_0420, |
29 AffixVerificationFailed, | |
30 ProfileRequirementsNotMet, | |
31 SCEAffixPolicy, | |
32 SCECustomAffix, | |
33 SCEProfile, | |
29 ) | 34 ) |
30 from libervia.backend.tools.xml_tools import ElementParser | 35 from libervia.backend.tools.xml_tools import ElementParser |
31 from twisted.words.xish import domish | 36 from twisted.words.xish import domish |
32 | 37 |
33 | 38 |
41 "test_to_affix", | 46 "test_to_affix", |
42 "test_from_affix", | 47 "test_from_affix", |
43 "test_custom_affixes", | 48 "test_custom_affixes", |
44 "test_namespace_conversion", | 49 "test_namespace_conversion", |
45 "test_non_encryptable_elements", | 50 "test_non_encryptable_elements", |
46 "test_schema_validation" | 51 "test_schema_validation", |
47 ] | 52 ] |
48 | 53 |
49 | 54 |
50 string_to_domish = cast(Callable[[str], domish.Element], ElementParser()) | 55 string_to_domish = cast(Callable[[str], domish.Element], ElementParser()) |
51 | 56 |
108 profile = SCEProfile( | 113 profile = SCEProfile( |
109 SCEAffixPolicy.REQUIRED, | 114 SCEAffixPolicy.REQUIRED, |
110 SCEAffixPolicy.OPTIONAL, | 115 SCEAffixPolicy.OPTIONAL, |
111 SCEAffixPolicy.NOT_NEEDED, | 116 SCEAffixPolicy.NOT_NEEDED, |
112 SCEAffixPolicy.OPTIONAL, | 117 SCEAffixPolicy.OPTIONAL, |
113 custom_policies={ CustomAffixImpl(): SCEAffixPolicy.NOT_NEEDED } | 118 custom_policies={CustomAffixImpl(): SCEAffixPolicy.NOT_NEEDED}, |
114 ) | 119 ) |
115 | 120 |
116 stanza_string = ( | 121 stanza_string = ( |
117 '<message from="foo@example.com" to="bar@example.com"><body>Test with both a body' | 122 '<message from="foo@example.com" to="bar@example.com"><body>Test with both a body' |
118 ' and some other custom element.</body><custom xmlns="urn:xmpp:example:0"' | 123 ' and some other custom element.</body><custom xmlns="urn:xmpp:example:0"' |
140 profile = SCEProfile( | 145 profile = SCEProfile( |
141 SCEAffixPolicy.REQUIRED, | 146 SCEAffixPolicy.REQUIRED, |
142 SCEAffixPolicy.OPTIONAL, | 147 SCEAffixPolicy.OPTIONAL, |
143 SCEAffixPolicy.NOT_NEEDED, | 148 SCEAffixPolicy.NOT_NEEDED, |
144 SCEAffixPolicy.OPTIONAL, | 149 SCEAffixPolicy.OPTIONAL, |
145 custom_policies={ custom_affix: SCEAffixPolicy.OPTIONAL } | 150 custom_policies={custom_affix: SCEAffixPolicy.OPTIONAL}, |
146 ) | 151 ) |
147 | 152 |
148 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> | 153 stanza = string_to_domish( |
154 """<message from="foo@example.com" to="bar@example.com"> | |
149 <body> | 155 <body> |
150 Make sure that both the REQUIRED and the OPTIONAL affixes are included. | 156 Make sure that both the REQUIRED and the OPTIONAL affixes are included. |
151 </body> | 157 </body> |
152 </message>""") | 158 </message>""" |
159 ) | |
153 | 160 |
154 affix_values = XEP_0420.unpack_stanza( | 161 affix_values = XEP_0420.unpack_stanza( |
155 profile, | 162 profile, stanza, XEP_0420.pack_stanza(profile, stanza) |
156 stanza, | |
157 XEP_0420.pack_stanza(profile, stanza) | |
158 ) | 163 ) |
159 | 164 |
160 assert affix_values.rpad is not None | 165 assert affix_values.rpad is not None |
161 assert affix_values.timestamp is not None | 166 assert affix_values.timestamp is not None |
162 assert affix_values.recipient is None | 167 assert affix_values.recipient is None |
168 packing_profile = SCEProfile( | 173 packing_profile = SCEProfile( |
169 SCEAffixPolicy.REQUIRED, | 174 SCEAffixPolicy.REQUIRED, |
170 SCEAffixPolicy.REQUIRED, | 175 SCEAffixPolicy.REQUIRED, |
171 SCEAffixPolicy.REQUIRED, | 176 SCEAffixPolicy.REQUIRED, |
172 SCEAffixPolicy.REQUIRED, | 177 SCEAffixPolicy.REQUIRED, |
173 custom_policies={} | 178 custom_policies={}, |
174 ) | 179 ) |
175 | 180 |
176 unpacking_profile = SCEProfile( | 181 unpacking_profile = SCEProfile( |
177 SCEAffixPolicy.NOT_NEEDED, | 182 SCEAffixPolicy.NOT_NEEDED, |
178 SCEAffixPolicy.NOT_NEEDED, | 183 SCEAffixPolicy.NOT_NEEDED, |
179 SCEAffixPolicy.NOT_NEEDED, | 184 SCEAffixPolicy.NOT_NEEDED, |
180 SCEAffixPolicy.NOT_NEEDED, | 185 SCEAffixPolicy.NOT_NEEDED, |
181 custom_policies={} | 186 custom_policies={}, |
182 ) | 187 ) |
183 | 188 |
184 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> | 189 stanza = string_to_domish( |
190 """<message from="foo@example.com" to="bar@example.com"> | |
185 <body> | 191 <body> |
186 When unpacking, all affixes are loaded, even those marked as NOT_NEEDED. | 192 When unpacking, all affixes are loaded, even those marked as NOT_NEEDED. |
187 </body> | 193 </body> |
188 </message>""") | 194 </message>""" |
195 ) | |
189 | 196 |
190 envelope_serialized = XEP_0420.pack_stanza(packing_profile, stanza) | 197 envelope_serialized = XEP_0420.pack_stanza(packing_profile, stanza) |
191 | 198 |
192 affix_values = XEP_0420.unpack_stanza(unpacking_profile, stanza, envelope_serialized) | 199 affix_values = XEP_0420.unpack_stanza(unpacking_profile, stanza, envelope_serialized) |
193 | 200 |
209 packing_profile = SCEProfile( | 216 packing_profile = SCEProfile( |
210 SCEAffixPolicy.NOT_NEEDED, | 217 SCEAffixPolicy.NOT_NEEDED, |
211 SCEAffixPolicy.NOT_NEEDED, | 218 SCEAffixPolicy.NOT_NEEDED, |
212 SCEAffixPolicy.NOT_NEEDED, | 219 SCEAffixPolicy.NOT_NEEDED, |
213 SCEAffixPolicy.NOT_NEEDED, | 220 SCEAffixPolicy.NOT_NEEDED, |
214 custom_policies={} | 221 custom_policies={}, |
215 ) | 222 ) |
216 | 223 |
217 unpacking_profile = SCEProfile( | 224 unpacking_profile = SCEProfile( |
218 SCEAffixPolicy.REQUIRED, | 225 SCEAffixPolicy.REQUIRED, |
219 SCEAffixPolicy.REQUIRED, | 226 SCEAffixPolicy.REQUIRED, |
220 SCEAffixPolicy.REQUIRED, | 227 SCEAffixPolicy.REQUIRED, |
221 SCEAffixPolicy.REQUIRED, | 228 SCEAffixPolicy.REQUIRED, |
222 custom_policies={} | 229 custom_policies={}, |
223 ) | 230 ) |
224 | 231 |
225 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> | 232 stanza = string_to_domish( |
233 """<message from="foo@example.com" to="bar@example.com"> | |
226 <body>Check that all affixes REQUIRED by the profile are present.</body> | 234 <body>Check that all affixes REQUIRED by the profile are present.</body> |
227 </message>""") | 235 </message>""" |
236 ) | |
228 | 237 |
229 with pytest.raises(ProfileRequirementsNotMet): | 238 with pytest.raises(ProfileRequirementsNotMet): |
230 XEP_0420.unpack_stanza( | 239 XEP_0420.unpack_stanza( |
231 unpacking_profile, | 240 unpacking_profile, stanza, XEP_0420.pack_stanza(packing_profile, stanza) |
232 stanza, | |
233 XEP_0420.pack_stanza(packing_profile, stanza) | |
234 ) | 241 ) |
235 | 242 |
236 # Do the same but with a custom affix missing | 243 # Do the same but with a custom affix missing |
237 custom_affix = CustomAffixImpl() | 244 custom_affix = CustomAffixImpl() |
238 | 245 |
239 packing_profile = SCEProfile( | 246 packing_profile = SCEProfile( |
240 SCEAffixPolicy.NOT_NEEDED, | 247 SCEAffixPolicy.NOT_NEEDED, |
241 SCEAffixPolicy.NOT_NEEDED, | 248 SCEAffixPolicy.NOT_NEEDED, |
242 SCEAffixPolicy.NOT_NEEDED, | 249 SCEAffixPolicy.NOT_NEEDED, |
243 SCEAffixPolicy.NOT_NEEDED, | 250 SCEAffixPolicy.NOT_NEEDED, |
244 custom_policies={ custom_affix: SCEAffixPolicy.NOT_NEEDED } | 251 custom_policies={custom_affix: SCEAffixPolicy.NOT_NEEDED}, |
245 ) | 252 ) |
246 | 253 |
247 unpacking_profile = SCEProfile( | 254 unpacking_profile = SCEProfile( |
248 SCEAffixPolicy.NOT_NEEDED, | 255 SCEAffixPolicy.NOT_NEEDED, |
249 SCEAffixPolicy.NOT_NEEDED, | 256 SCEAffixPolicy.NOT_NEEDED, |
250 SCEAffixPolicy.NOT_NEEDED, | 257 SCEAffixPolicy.NOT_NEEDED, |
251 SCEAffixPolicy.NOT_NEEDED, | 258 SCEAffixPolicy.NOT_NEEDED, |
252 custom_policies={ custom_affix: SCEAffixPolicy.REQUIRED } | 259 custom_policies={custom_affix: SCEAffixPolicy.REQUIRED}, |
253 ) | 260 ) |
254 | 261 |
255 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> | 262 stanza = string_to_domish( |
263 """<message from="foo@example.com" to="bar@example.com"> | |
256 <body> | 264 <body> |
257 Check that all affixes REQUIRED by the profile are present, including custom | 265 Check that all affixes REQUIRED by the profile are present, including custom |
258 affixes. | 266 affixes. |
259 </body> | 267 </body> |
260 </message>""") | 268 </message>""" |
269 ) | |
261 | 270 |
262 with pytest.raises(ProfileRequirementsNotMet): | 271 with pytest.raises(ProfileRequirementsNotMet): |
263 XEP_0420.unpack_stanza( | 272 XEP_0420.unpack_stanza( |
264 unpacking_profile, | 273 unpacking_profile, stanza, XEP_0420.pack_stanza(packing_profile, stanza) |
265 stanza, | |
266 XEP_0420.pack_stanza(packing_profile, stanza) | |
267 ) | 274 ) |
268 | 275 |
269 | 276 |
270 def test_rpad_affix() -> None: # pylint: disable=missing-function-docstring | 277 def test_rpad_affix() -> None: # pylint: disable=missing-function-docstring |
271 profile = SCEProfile( | 278 profile = SCEProfile( |
272 SCEAffixPolicy.REQUIRED, | 279 SCEAffixPolicy.REQUIRED, |
273 SCEAffixPolicy.NOT_NEEDED, | 280 SCEAffixPolicy.NOT_NEEDED, |
274 SCEAffixPolicy.NOT_NEEDED, | 281 SCEAffixPolicy.NOT_NEEDED, |
275 SCEAffixPolicy.NOT_NEEDED, | 282 SCEAffixPolicy.NOT_NEEDED, |
276 custom_policies={} | 283 custom_policies={}, |
277 ) | 284 ) |
278 | 285 |
279 for _ in range(100): | 286 for _ in range(100): |
280 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> | 287 stanza = string_to_domish( |
288 """<message from="foo@example.com" to="bar@example.com"> | |
281 <body>OK</body> | 289 <body>OK</body> |
282 </message>""") | 290 </message>""" |
291 ) | |
283 | 292 |
284 affix_values = XEP_0420.unpack_stanza( | 293 affix_values = XEP_0420.unpack_stanza( |
285 profile, | 294 profile, stanza, XEP_0420.pack_stanza(profile, stanza) |
286 stanza, | |
287 XEP_0420.pack_stanza(profile, stanza) | |
288 ) | 295 ) |
289 | 296 |
290 # Test that the rpad exists and that the content elements are always padded to at | 297 # Test that the rpad exists and that the content elements are always padded to at |
291 # least 53 characters | 298 # least 53 characters |
292 assert affix_values.rpad is not None | 299 assert affix_values.rpad is not None |
297 profile = SCEProfile( | 304 profile = SCEProfile( |
298 SCEAffixPolicy.NOT_NEEDED, | 305 SCEAffixPolicy.NOT_NEEDED, |
299 SCEAffixPolicy.REQUIRED, | 306 SCEAffixPolicy.REQUIRED, |
300 SCEAffixPolicy.NOT_NEEDED, | 307 SCEAffixPolicy.NOT_NEEDED, |
301 SCEAffixPolicy.NOT_NEEDED, | 308 SCEAffixPolicy.NOT_NEEDED, |
302 custom_policies={} | 309 custom_policies={}, |
303 ) | 310 ) |
304 | 311 |
305 stanza = string_to_domish( | 312 stanza = string_to_domish( |
306 """<message from="foo@example.com" to="bar@example.com"></message>""" | 313 """<message from="foo@example.com" to="bar@example.com"></message>""" |
307 ) | 314 ) |
312 The time affix is only parsed and not otherwise verified. Not much to test | 319 The time affix is only parsed and not otherwise verified. Not much to test |
313 here. | 320 here. |
314 </body> | 321 </body> |
315 </content> | 322 </content> |
316 <time stamp="1969-07-21T02:56:15Z"/> | 323 <time stamp="1969-07-21T02:56:15Z"/> |
317 </envelope>""".encode("utf-8") | 324 </envelope>""".encode( |
325 "utf-8" | |
326 ) | |
318 | 327 |
319 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) | 328 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
320 assert affix_values.timestamp == datetime(1969, 7, 21, 2, 56, 15, tzinfo=timezone.utc) | 329 assert affix_values.timestamp == datetime(1969, 7, 21, 2, 56, 15, tzinfo=timezone.utc) |
321 | 330 |
322 | 331 |
324 profile = SCEProfile( | 333 profile = SCEProfile( |
325 SCEAffixPolicy.NOT_NEEDED, | 334 SCEAffixPolicy.NOT_NEEDED, |
326 SCEAffixPolicy.NOT_NEEDED, | 335 SCEAffixPolicy.NOT_NEEDED, |
327 SCEAffixPolicy.REQUIRED, | 336 SCEAffixPolicy.REQUIRED, |
328 SCEAffixPolicy.NOT_NEEDED, | 337 SCEAffixPolicy.NOT_NEEDED, |
329 custom_policies={} | 338 custom_policies={}, |
330 ) | 339 ) |
331 | 340 |
332 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> | 341 stanza = string_to_domish( |
342 """<message from="foo@example.com" to="bar@example.com"> | |
333 <body>Check that the ``to`` affix is correctly added.</body> | 343 <body>Check that the ``to`` affix is correctly added.</body> |
334 </message>""") | 344 </message>""" |
345 ) | |
335 | 346 |
336 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) | 347 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) |
337 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) | 348 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
338 assert affix_values.recipient is not None | 349 assert affix_values.recipient is not None |
339 assert affix_values.recipient.userhost() == "bar@example.com" | 350 assert affix_values.recipient.userhost() == "bar@example.com" |
353 | 364 |
354 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) | 365 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
355 assert affix_values.recipient is not None | 366 assert affix_values.recipient is not None |
356 assert affix_values.recipient.userhost() == "bar@example.com" | 367 assert affix_values.recipient.userhost() == "bar@example.com" |
357 | 368 |
358 stanza = string_to_domish("""<message from="foo@example.com"> | 369 stanza = string_to_domish( |
370 """<message from="foo@example.com"> | |
359 <body> | 371 <body> |
360 Check that a missing "to" attribute on the stanza fails stanza packing. | 372 Check that a missing "to" attribute on the stanza fails stanza packing. |
361 </body> | 373 </body> |
362 </message>""") | 374 </message>""" |
375 ) | |
363 | 376 |
364 with pytest.raises(ValueError): | 377 with pytest.raises(ValueError): |
365 XEP_0420.pack_stanza(profile, stanza) | 378 XEP_0420.pack_stanza(profile, stanza) |
366 | 379 |
367 | 380 |
369 profile = SCEProfile( | 382 profile = SCEProfile( |
370 SCEAffixPolicy.NOT_NEEDED, | 383 SCEAffixPolicy.NOT_NEEDED, |
371 SCEAffixPolicy.NOT_NEEDED, | 384 SCEAffixPolicy.NOT_NEEDED, |
372 SCEAffixPolicy.NOT_NEEDED, | 385 SCEAffixPolicy.NOT_NEEDED, |
373 SCEAffixPolicy.REQUIRED, | 386 SCEAffixPolicy.REQUIRED, |
374 custom_policies={} | 387 custom_policies={}, |
375 ) | 388 ) |
376 | 389 |
377 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> | 390 stanza = string_to_domish( |
391 """<message from="foo@example.com" to="bar@example.com"> | |
378 <body>Check that the ``from`` affix is correctly added.</body> | 392 <body>Check that the ``from`` affix is correctly added.</body> |
379 </message>""") | 393 </message>""" |
394 ) | |
380 | 395 |
381 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) | 396 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) |
382 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) | 397 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
383 assert affix_values.sender is not None | 398 assert affix_values.sender is not None |
384 assert affix_values.sender.userhost() == "foo@example.com" | 399 assert affix_values.sender.userhost() == "foo@example.com" |
398 | 413 |
399 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) | 414 affix_values = XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
400 assert affix_values.sender is not None | 415 assert affix_values.sender is not None |
401 assert affix_values.sender.userhost() == "foo@example.com" | 416 assert affix_values.sender.userhost() == "foo@example.com" |
402 | 417 |
403 stanza = string_to_domish("""<message to="bar@example.com"> | 418 stanza = string_to_domish( |
419 """<message to="bar@example.com"> | |
404 <body> | 420 <body> |
405 Check that a missing "from" attribute on the stanza fails stanza packing. | 421 Check that a missing "from" attribute on the stanza fails stanza packing. |
406 </body> | 422 </body> |
407 </message>""") | 423 </message>""" |
424 ) | |
408 | 425 |
409 with pytest.raises(ValueError): | 426 with pytest.raises(ValueError): |
410 XEP_0420.pack_stanza(profile, stanza) | 427 XEP_0420.pack_stanza(profile, stanza) |
411 | 428 |
412 | 429 |
416 packing_profile = SCEProfile( | 433 packing_profile = SCEProfile( |
417 SCEAffixPolicy.NOT_NEEDED, | 434 SCEAffixPolicy.NOT_NEEDED, |
418 SCEAffixPolicy.NOT_NEEDED, | 435 SCEAffixPolicy.NOT_NEEDED, |
419 SCEAffixPolicy.NOT_NEEDED, | 436 SCEAffixPolicy.NOT_NEEDED, |
420 SCEAffixPolicy.NOT_NEEDED, | 437 SCEAffixPolicy.NOT_NEEDED, |
421 custom_policies={ custom_affix: SCEAffixPolicy.REQUIRED } | 438 custom_policies={custom_affix: SCEAffixPolicy.REQUIRED}, |
422 ) | 439 ) |
423 | 440 |
424 unpacking_profile = SCEProfile( | 441 unpacking_profile = SCEProfile( |
425 SCEAffixPolicy.NOT_NEEDED, | 442 SCEAffixPolicy.NOT_NEEDED, |
426 SCEAffixPolicy.NOT_NEEDED, | 443 SCEAffixPolicy.NOT_NEEDED, |
427 SCEAffixPolicy.NOT_NEEDED, | 444 SCEAffixPolicy.NOT_NEEDED, |
428 SCEAffixPolicy.NOT_NEEDED, | 445 SCEAffixPolicy.NOT_NEEDED, |
429 custom_policies={} | 446 custom_policies={}, |
430 ) | 447 ) |
431 | 448 |
432 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> | 449 stanza = string_to_domish( |
450 """<message from="foo@example.com" to="bar@example.com"> | |
433 <body> | 451 <body> |
434 If a custom affix is included in the envelope, but not excpected by the | 452 If a custom affix is included in the envelope, but not excpected by the |
435 recipient, the schema validation should fail. | 453 recipient, the schema validation should fail. |
436 </body> | 454 </body> |
437 </message>""") | 455 </message>""" |
456 ) | |
438 | 457 |
439 with pytest.raises(exceptions.ParsingError): | 458 with pytest.raises(exceptions.ParsingError): |
440 XEP_0420.unpack_stanza( | 459 XEP_0420.unpack_stanza( |
441 unpacking_profile, | 460 unpacking_profile, stanza, XEP_0420.pack_stanza(packing_profile, stanza) |
442 stanza, | |
443 XEP_0420.pack_stanza(packing_profile, stanza) | |
444 ) | 461 ) |
445 | 462 |
446 profile = packing_profile | 463 profile = packing_profile |
447 | 464 |
448 stanza = string_to_domish("""<message from="foo@example.com/device0" | 465 stanza = string_to_domish( |
466 """<message from="foo@example.com/device0" | |
449 to="bar@example.com/Libervia.123"> | 467 to="bar@example.com/Libervia.123"> |
450 <body>The affix element should be returned as part of the affix values.</body> | 468 <body>The affix element should be returned as part of the affix values.</body> |
451 </message>""") | 469 </message>""" |
470 ) | |
452 | 471 |
453 affix_values = XEP_0420.unpack_stanza( | 472 affix_values = XEP_0420.unpack_stanza( |
454 profile, | 473 profile, stanza, XEP_0420.pack_stanza(profile, stanza) |
455 stanza, | |
456 XEP_0420.pack_stanza(profile, stanza) | |
457 ) | 474 ) |
458 | 475 |
459 assert custom_affix in affix_values.custom | 476 assert custom_affix in affix_values.custom |
460 assert affix_values.custom[custom_affix].getAttribute("recipient") == \ | 477 assert ( |
461 "bar@example.com/Libervia.123" | 478 affix_values.custom[custom_affix].getAttribute("recipient") |
462 assert affix_values.custom[custom_affix].getAttribute("sender") == \ | 479 == "bar@example.com/Libervia.123" |
463 "foo@example.com/device0" | 480 ) |
481 assert ( | |
482 affix_values.custom[custom_affix].getAttribute("sender") | |
483 == "foo@example.com/device0" | |
484 ) | |
464 | 485 |
465 | 486 |
466 def test_namespace_conversion() -> None: # pylint: disable=missing-function-docstring | 487 def test_namespace_conversion() -> None: # pylint: disable=missing-function-docstring |
467 profile = SCEProfile( | 488 profile = SCEProfile( |
468 SCEAffixPolicy.NOT_NEEDED, | 489 SCEAffixPolicy.NOT_NEEDED, |
469 SCEAffixPolicy.NOT_NEEDED, | 490 SCEAffixPolicy.NOT_NEEDED, |
470 SCEAffixPolicy.NOT_NEEDED, | 491 SCEAffixPolicy.NOT_NEEDED, |
471 SCEAffixPolicy.NOT_NEEDED, | 492 SCEAffixPolicy.NOT_NEEDED, |
472 custom_policies={} | 493 custom_policies={}, |
473 ) | 494 ) |
474 | 495 |
475 stanza = domish.Element((None, "message")) | 496 stanza = domish.Element((None, "message")) |
476 stanza["from"] = "foo@example.com" | 497 stanza["from"] = "foo@example.com" |
477 stanza["to"] = "bar@example.com" | 498 stanza["to"] = "bar@example.com" |
478 stanza.addElement( | 499 stanza.addElement( |
479 "body", | 500 "body", |
480 content=( | 501 content=( |
481 "This body element has namespace ``None``, which has to be replaced with" | 502 "This body element has namespace ``None``, which has to be replaced with" |
482 " jabber:client." | 503 " jabber:client." |
483 ) | 504 ), |
484 ) | 505 ) |
485 | 506 |
486 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) | 507 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) |
487 envelope = string_to_domish(envelope_serialized.decode("utf-8")) | 508 envelope = string_to_domish(envelope_serialized.decode("utf-8")) |
488 content = next(envelope.elements(NS_SCE, "content")) | 509 content = next(envelope.elements(NS_SCE, "content")) |
500 profile = SCEProfile( | 521 profile = SCEProfile( |
501 SCEAffixPolicy.NOT_NEEDED, | 522 SCEAffixPolicy.NOT_NEEDED, |
502 SCEAffixPolicy.NOT_NEEDED, | 523 SCEAffixPolicy.NOT_NEEDED, |
503 SCEAffixPolicy.NOT_NEEDED, | 524 SCEAffixPolicy.NOT_NEEDED, |
504 SCEAffixPolicy.NOT_NEEDED, | 525 SCEAffixPolicy.NOT_NEEDED, |
505 custom_policies={} | 526 custom_policies={}, |
506 ) | 527 ) |
507 | 528 |
508 stanza = string_to_domish("""<message from="foo@example.com" to="bar@example.com"> | 529 stanza = string_to_domish( |
530 """<message from="foo@example.com" to="bar@example.com"> | |
509 <body>This stanza includes a store hint which must not be encrypted.</body> | 531 <body>This stanza includes a store hint which must not be encrypted.</body> |
510 <store xmlns="urn:xmpp:hints"/> | 532 <store xmlns="urn:xmpp:hints"/> |
511 </message>""") | 533 </message>""" |
534 ) | |
512 | 535 |
513 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) | 536 envelope_serialized = XEP_0420.pack_stanza(profile, stanza) |
514 envelope = string_to_domish(envelope_serialized.decode("utf-8")) | 537 envelope = string_to_domish(envelope_serialized.decode("utf-8")) |
515 content = next(envelope.elements(NS_SCE, "content")) | 538 content = next(envelope.elements(NS_SCE, "content")) |
516 | 539 |
527 <body xmlns="jabber:client"> | 550 <body xmlns="jabber:client"> |
528 The store hint must not be moved to the stanza. | 551 The store hint must not be moved to the stanza. |
529 </body> | 552 </body> |
530 <store xmlns="urn:xmpp:hints"/> | 553 <store xmlns="urn:xmpp:hints"/> |
531 </content> | 554 </content> |
532 </envelope>""".encode("utf-8") | 555 </envelope>""".encode( |
556 "utf-8" | |
557 ) | |
533 | 558 |
534 XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) | 559 XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |
535 | 560 |
536 assert next(stanza.elements(NS_HINTS, "store"), None) is None | 561 assert next(stanza.elements(NS_HINTS, "store"), None) is None |
537 | 562 |
540 profile = SCEProfile( | 565 profile = SCEProfile( |
541 SCEAffixPolicy.NOT_NEEDED, | 566 SCEAffixPolicy.NOT_NEEDED, |
542 SCEAffixPolicy.NOT_NEEDED, | 567 SCEAffixPolicy.NOT_NEEDED, |
543 SCEAffixPolicy.NOT_NEEDED, | 568 SCEAffixPolicy.NOT_NEEDED, |
544 SCEAffixPolicy.NOT_NEEDED, | 569 SCEAffixPolicy.NOT_NEEDED, |
545 custom_policies={} | 570 custom_policies={}, |
546 ) | 571 ) |
547 | 572 |
548 stanza = string_to_domish( | 573 stanza = string_to_domish( |
549 """<message from="foo@example.com" to="bar@example.com"></message>""" | 574 """<message from="foo@example.com" to="bar@example.com"></message>""" |
550 ) | 575 ) |
555 An unknwon affix should cause a schema validation error. | 580 An unknwon affix should cause a schema validation error. |
556 </body> | 581 </body> |
557 <store xmlns="urn:xmpp:hints"/> | 582 <store xmlns="urn:xmpp:hints"/> |
558 </content> | 583 </content> |
559 <unknown-affix unknown-attr="unknown"/> | 584 <unknown-affix unknown-attr="unknown"/> |
560 </envelope>""".encode("utf-8") | 585 </envelope>""".encode( |
586 "utf-8" | |
587 ) | |
561 | 588 |
562 with pytest.raises(exceptions.ParsingError): | 589 with pytest.raises(exceptions.ParsingError): |
563 XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) | 590 XEP_0420.unpack_stanza(profile, stanza, envelope_serialized) |