Mercurial > libervia-backend
comparison tests/unit/test_plugin_xep_0373.py @ 3933:cecf45416403
plugin XEP-0373 and XEP-0374: Implementation of OX and OXIM:
GPGME is used as the GPG provider.
rel 374
author | Syndace <me@syndace.dev> |
---|---|
date | Tue, 20 Sep 2022 16:22:18 +0200 |
parents | |
children | 3d91d0aa68db |
comparison
equal
deleted
inserted
replaced
3932:7af29260ecb8 | 3933:cecf45416403 |
---|---|
1 from datetime import datetime, timedelta, timezone | |
2 from sat.plugins.plugin_xep_0373 import XEP_0373, NS_OX | |
3 from sat.tools.xmpp_datetime import parse_datetime | |
4 | |
5 import pytest | |
6 from twisted.words.protocols.jabber import jid | |
7 | |
8 | |
9 a = jid.JID("foo@example.com") | |
10 b = jid.JID("bar@example.com") | |
11 | |
12 | |
13 def test_signcrypt_element_args() -> None: | |
14 with pytest.raises(ValueError): | |
15 XEP_0373.build_signcrypt_element([]) | |
16 | |
17 | |
18 def test_signcrypt_element() -> None: | |
19 signcrypt_elt, payload_elt = XEP_0373.build_signcrypt_element([ a, b ]) | |
20 payload_elt.addElement("signcrypt-test-content", content="signcrypt test content") | |
21 | |
22 rpad_elt = next(signcrypt_elt.elements(NS_OX, "rpad")) | |
23 time_elt = next(signcrypt_elt.elements(NS_OX, "time")) | |
24 | |
25 rpad = str(rpad_elt) | |
26 timestamp = parse_datetime(time_elt["stamp"]) | |
27 | |
28 signcrypt_elt.children.remove(rpad_elt) | |
29 signcrypt_elt.children.remove(time_elt) | |
30 | |
31 assert rpad | |
32 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) | |
33 assert signcrypt_elt.toXml() == ( | |
34 "<signcrypt xmlns='urn:xmpp:openpgp:0'>" | |
35 "<to jid='foo@example.com'/>" | |
36 "<to jid='bar@example.com'/>" | |
37 "<payload>" | |
38 "<signcrypt-test-content>signcrypt test content</signcrypt-test-content>" | |
39 "</payload>" | |
40 "</signcrypt>" | |
41 ) | |
42 | |
43 | |
44 def test_sign_element_args() -> None: | |
45 with pytest.raises(ValueError): | |
46 XEP_0373.build_sign_element([], True) | |
47 | |
48 | |
49 def test_sign_element_with_rpad() -> None: | |
50 sign_elt, payload_elt = XEP_0373.build_sign_element([ a, b ], True) | |
51 payload_elt.addElement("sign-test-content", content="sign test content") | |
52 | |
53 rpad_elt = next(sign_elt.elements(NS_OX, "rpad")) | |
54 time_elt = next(sign_elt.elements(NS_OX, "time")) | |
55 | |
56 rpad = str(rpad_elt) | |
57 timestamp = parse_datetime(time_elt["stamp"]) | |
58 | |
59 sign_elt.children.remove(rpad_elt) | |
60 sign_elt.children.remove(time_elt) | |
61 | |
62 assert rpad | |
63 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) | |
64 assert sign_elt.toXml() == ( | |
65 "<sign xmlns='urn:xmpp:openpgp:0'>" | |
66 "<to jid='foo@example.com'/>" | |
67 "<to jid='bar@example.com'/>" | |
68 "<payload>" | |
69 "<sign-test-content>sign test content</sign-test-content>" | |
70 "</payload>" | |
71 "</sign>" | |
72 ) | |
73 | |
74 | |
75 def test_sign_element_without_rpad() -> None: | |
76 sign_elt, payload_elt = XEP_0373.build_sign_element([ a, b ], False) | |
77 payload_elt.addElement("sign-test-content", content="sign test content") | |
78 | |
79 rpad_elt = next(sign_elt.elements(NS_OX, "rpad"), None) | |
80 time_elt = next(sign_elt.elements(NS_OX, "time")) | |
81 | |
82 timestamp = parse_datetime(time_elt["stamp"]) | |
83 | |
84 sign_elt.children.remove(time_elt) | |
85 | |
86 assert rpad_elt is None | |
87 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) | |
88 assert sign_elt.toXml() == ( | |
89 "<sign xmlns='urn:xmpp:openpgp:0'>" | |
90 "<to jid='foo@example.com'/>" | |
91 "<to jid='bar@example.com'/>" | |
92 "<payload>" | |
93 "<sign-test-content>sign test content</sign-test-content>" | |
94 "</payload>" | |
95 "</sign>" | |
96 ) | |
97 | |
98 | |
99 def test_crypt_element_with_recipients() -> None: | |
100 crypt_elt, payload_elt = XEP_0373.build_crypt_element([ a, b ]) | |
101 payload_elt.addElement("crypt-test-content", content="crypt test content") | |
102 | |
103 rpad_elt = next(crypt_elt.elements(NS_OX, "rpad")) | |
104 time_elt = next(crypt_elt.elements(NS_OX, "time")) | |
105 | |
106 rpad = str(rpad_elt) | |
107 timestamp = parse_datetime(time_elt["stamp"]) | |
108 | |
109 crypt_elt.children.remove(rpad_elt) | |
110 crypt_elt.children.remove(time_elt) | |
111 | |
112 assert rpad | |
113 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) | |
114 assert crypt_elt.toXml() == ( | |
115 "<crypt xmlns='urn:xmpp:openpgp:0'>" | |
116 "<to jid='foo@example.com'/>" | |
117 "<to jid='bar@example.com'/>" | |
118 "<payload>" | |
119 "<crypt-test-content>crypt test content</crypt-test-content>" | |
120 "</payload>" | |
121 "</crypt>" | |
122 ) | |
123 | |
124 | |
125 def test_crypt_element_without_recipients() -> None: | |
126 crypt_elt, payload_elt = XEP_0373.build_crypt_element([]) | |
127 payload_elt.addElement("crypt-test-content", content="crypt test content") | |
128 | |
129 rpad_elt = next(crypt_elt.elements(NS_OX, "rpad")) | |
130 time_elt = next(crypt_elt.elements(NS_OX, "time")) | |
131 | |
132 rpad = str(rpad_elt) | |
133 timestamp = parse_datetime(time_elt["stamp"]) | |
134 | |
135 crypt_elt.children.remove(rpad_elt) | |
136 crypt_elt.children.remove(time_elt) | |
137 | |
138 assert rpad | |
139 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) | |
140 assert crypt_elt.toXml() == ( | |
141 "<crypt xmlns='urn:xmpp:openpgp:0'>" | |
142 "<payload>" | |
143 "<crypt-test-content>crypt test content</crypt-test-content>" | |
144 "</payload>" | |
145 "</crypt>" | |
146 ) |