Mercurial > libervia-backend
comparison tests/unit/test_plugin_xep_0373.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 |
---|---|
1 from datetime import datetime, timedelta, timezone | 1 from datetime import datetime, timedelta, timezone |
2 | 2 |
3 import pytest | 3 import pytest |
4 from libervia.backend.core import exceptions | 4 from libervia.backend.core import exceptions |
5 | |
5 try: | 6 try: |
6 from libervia.backend.plugins.plugin_xep_0373 import XEP_0373, NS_OX | 7 from libervia.backend.plugins.plugin_xep_0373 import XEP_0373, NS_OX |
7 except exceptions.MissingModule as e: | 8 except exceptions.MissingModule as e: |
8 pytest.skip(f"Can't test XEP-0373: {e}", allow_module_level=True) | 9 pytest.skip(f"Can't test XEP-0373: {e}", allow_module_level=True) |
9 from libervia.backend.tools.xmpp_datetime import parse_datetime | 10 from libervia.backend.tools.xmpp_datetime import parse_datetime |
18 with pytest.raises(ValueError): | 19 with pytest.raises(ValueError): |
19 XEP_0373.build_signcrypt_element([]) | 20 XEP_0373.build_signcrypt_element([]) |
20 | 21 |
21 | 22 |
22 def test_signcrypt_element() -> None: | 23 def test_signcrypt_element() -> None: |
23 signcrypt_elt, payload_elt = XEP_0373.build_signcrypt_element([ a, b ]) | 24 signcrypt_elt, payload_elt = XEP_0373.build_signcrypt_element([a, b]) |
24 payload_elt.addElement("signcrypt-test-content", content="signcrypt test content") | 25 payload_elt.addElement("signcrypt-test-content", content="signcrypt test content") |
25 | 26 |
26 rpad_elt = next(signcrypt_elt.elements(NS_OX, "rpad")) | 27 rpad_elt = next(signcrypt_elt.elements(NS_OX, "rpad")) |
27 time_elt = next(signcrypt_elt.elements(NS_OX, "time")) | 28 time_elt = next(signcrypt_elt.elements(NS_OX, "time")) |
28 | 29 |
34 | 35 |
35 assert rpad | 36 assert rpad |
36 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) | 37 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) |
37 assert signcrypt_elt.toXml() == ( | 38 assert signcrypt_elt.toXml() == ( |
38 "<signcrypt xmlns='urn:xmpp:openpgp:0'>" | 39 "<signcrypt xmlns='urn:xmpp:openpgp:0'>" |
39 "<to jid='foo@example.com'/>" | 40 "<to jid='foo@example.com'/>" |
40 "<to jid='bar@example.com'/>" | 41 "<to jid='bar@example.com'/>" |
41 "<payload>" | 42 "<payload>" |
42 "<signcrypt-test-content>signcrypt test content</signcrypt-test-content>" | 43 "<signcrypt-test-content>signcrypt test content</signcrypt-test-content>" |
43 "</payload>" | 44 "</payload>" |
44 "</signcrypt>" | 45 "</signcrypt>" |
45 ) | 46 ) |
46 | 47 |
47 | 48 |
48 def test_sign_element_args() -> None: | 49 def test_sign_element_args() -> None: |
49 with pytest.raises(ValueError): | 50 with pytest.raises(ValueError): |
50 XEP_0373.build_sign_element([], True) | 51 XEP_0373.build_sign_element([], True) |
51 | 52 |
52 | 53 |
53 def test_sign_element_with_rpad() -> None: | 54 def test_sign_element_with_rpad() -> None: |
54 sign_elt, payload_elt = XEP_0373.build_sign_element([ a, b ], True) | 55 sign_elt, payload_elt = XEP_0373.build_sign_element([a, b], True) |
55 payload_elt.addElement("sign-test-content", content="sign test content") | 56 payload_elt.addElement("sign-test-content", content="sign test content") |
56 | 57 |
57 rpad_elt = next(sign_elt.elements(NS_OX, "rpad")) | 58 rpad_elt = next(sign_elt.elements(NS_OX, "rpad")) |
58 time_elt = next(sign_elt.elements(NS_OX, "time")) | 59 time_elt = next(sign_elt.elements(NS_OX, "time")) |
59 | 60 |
65 | 66 |
66 assert rpad | 67 assert rpad |
67 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) | 68 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) |
68 assert sign_elt.toXml() == ( | 69 assert sign_elt.toXml() == ( |
69 "<sign xmlns='urn:xmpp:openpgp:0'>" | 70 "<sign xmlns='urn:xmpp:openpgp:0'>" |
70 "<to jid='foo@example.com'/>" | 71 "<to jid='foo@example.com'/>" |
71 "<to jid='bar@example.com'/>" | 72 "<to jid='bar@example.com'/>" |
72 "<payload>" | 73 "<payload>" |
73 "<sign-test-content>sign test content</sign-test-content>" | 74 "<sign-test-content>sign test content</sign-test-content>" |
74 "</payload>" | 75 "</payload>" |
75 "</sign>" | 76 "</sign>" |
76 ) | 77 ) |
77 | 78 |
78 | 79 |
79 def test_sign_element_without_rpad() -> None: | 80 def test_sign_element_without_rpad() -> None: |
80 sign_elt, payload_elt = XEP_0373.build_sign_element([ a, b ], False) | 81 sign_elt, payload_elt = XEP_0373.build_sign_element([a, b], False) |
81 payload_elt.addElement("sign-test-content", content="sign test content") | 82 payload_elt.addElement("sign-test-content", content="sign test content") |
82 | 83 |
83 rpad_elt = next(sign_elt.elements(NS_OX, "rpad"), None) | 84 rpad_elt = next(sign_elt.elements(NS_OX, "rpad"), None) |
84 time_elt = next(sign_elt.elements(NS_OX, "time")) | 85 time_elt = next(sign_elt.elements(NS_OX, "time")) |
85 | 86 |
89 | 90 |
90 assert rpad_elt is None | 91 assert rpad_elt is None |
91 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) | 92 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) |
92 assert sign_elt.toXml() == ( | 93 assert sign_elt.toXml() == ( |
93 "<sign xmlns='urn:xmpp:openpgp:0'>" | 94 "<sign xmlns='urn:xmpp:openpgp:0'>" |
94 "<to jid='foo@example.com'/>" | 95 "<to jid='foo@example.com'/>" |
95 "<to jid='bar@example.com'/>" | 96 "<to jid='bar@example.com'/>" |
96 "<payload>" | 97 "<payload>" |
97 "<sign-test-content>sign test content</sign-test-content>" | 98 "<sign-test-content>sign test content</sign-test-content>" |
98 "</payload>" | 99 "</payload>" |
99 "</sign>" | 100 "</sign>" |
100 ) | 101 ) |
101 | 102 |
102 | 103 |
103 def test_crypt_element_with_recipients() -> None: | 104 def test_crypt_element_with_recipients() -> None: |
104 crypt_elt, payload_elt = XEP_0373.build_crypt_element([ a, b ]) | 105 crypt_elt, payload_elt = XEP_0373.build_crypt_element([a, b]) |
105 payload_elt.addElement("crypt-test-content", content="crypt test content") | 106 payload_elt.addElement("crypt-test-content", content="crypt test content") |
106 | 107 |
107 rpad_elt = next(crypt_elt.elements(NS_OX, "rpad")) | 108 rpad_elt = next(crypt_elt.elements(NS_OX, "rpad")) |
108 time_elt = next(crypt_elt.elements(NS_OX, "time")) | 109 time_elt = next(crypt_elt.elements(NS_OX, "time")) |
109 | 110 |
115 | 116 |
116 assert rpad | 117 assert rpad |
117 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) | 118 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) |
118 assert crypt_elt.toXml() == ( | 119 assert crypt_elt.toXml() == ( |
119 "<crypt xmlns='urn:xmpp:openpgp:0'>" | 120 "<crypt xmlns='urn:xmpp:openpgp:0'>" |
120 "<to jid='foo@example.com'/>" | 121 "<to jid='foo@example.com'/>" |
121 "<to jid='bar@example.com'/>" | 122 "<to jid='bar@example.com'/>" |
122 "<payload>" | 123 "<payload>" |
123 "<crypt-test-content>crypt test content</crypt-test-content>" | 124 "<crypt-test-content>crypt test content</crypt-test-content>" |
124 "</payload>" | 125 "</payload>" |
125 "</crypt>" | 126 "</crypt>" |
126 ) | 127 ) |
127 | 128 |
128 | 129 |
129 def test_crypt_element_without_recipients() -> None: | 130 def test_crypt_element_without_recipients() -> None: |
141 | 142 |
142 assert rpad | 143 assert rpad |
143 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) | 144 assert (datetime.now(timezone.utc) - timestamp) < timedelta(seconds=10) |
144 assert crypt_elt.toXml() == ( | 145 assert crypt_elt.toXml() == ( |
145 "<crypt xmlns='urn:xmpp:openpgp:0'>" | 146 "<crypt xmlns='urn:xmpp:openpgp:0'>" |
146 "<payload>" | 147 "<payload>" |
147 "<crypt-test-content>crypt test content</crypt-test-content>" | 148 "<crypt-test-content>crypt test content</crypt-test-content>" |
148 "</payload>" | 149 "</payload>" |
149 "</crypt>" | 150 "</crypt>" |
150 ) | 151 ) |