Mercurial > libervia-backend
comparison tests/unit/test_plugin_xep_0234.py @ 4232:0fbe5c605eb6
tests (unit/webrtc,XEP-0176, XEP-0234): Fix tests and add webrtc file transfer tests:
fix 441
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 06 Apr 2024 12:59:50 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
4231:e11b13418ba6 | 4232:0fbe5c605eb6 |
---|---|
1 #!/usr/bin/env python3 | |
2 | |
3 # Libervia: an XMPP client | |
4 # Copyright (C) 2009-2024 Jérôme Poisson (goffi@goffi.org) | |
5 | |
6 # This program is free software: you can redistribute it and/or modify | |
7 # it under the terms of the GNU Affero General Public License as published by | |
8 # the Free Software Foundation, either version 3 of the License, or | |
9 # (at your option) any later version. | |
10 | |
11 # This program is distributed in the hope that it will be useful, | |
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 # GNU Affero General Public License for more details. | |
15 | |
16 # You should have received a copy of the GNU Affero General Public License | |
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | |
19 | |
20 from unittest.mock import AsyncMock, MagicMock | |
21 | |
22 import pytest | |
23 from pytest_twisted import ensureDeferred as ed | |
24 from twisted.words.protocols.jabber import jid | |
25 | |
26 from libervia.backend.core import exceptions | |
27 from libervia.backend.plugins.plugin_xep_0166 import XEP_0166 | |
28 from libervia.backend.plugins.plugin_xep_0234 import XEP_0234 | |
29 from libervia.backend.tools import xml_tools | |
30 | |
31 | |
32 @pytest.fixture(autouse=True) | |
33 def no_application_register(monkeypatch): | |
34 """Do not register the application in XEP-0166""" | |
35 monkeypatch.setattr(XEP_0166, "register_application", lambda *a, **kw: None) | |
36 | |
37 | |
38 class TestXEP0234: | |
39 | |
40 @ed | |
41 async def test_parse_file_element(self, host, client): | |
42 """``parse_file_element`` updates file_data dictionary correctly.""" | |
43 xep_0234 = XEP_0234(host) | |
44 file_elt = xml_tools.parse( | |
45 """ | |
46 <file xmlns='urn:xmpp:jingle:apps:file-transfer:5'> | |
47 <media-type>text/plain</media-type> | |
48 <name>test.txt</name> | |
49 <date>2015-07-26T21:46:00+01:00</date> | |
50 <size>6144</size> | |
51 </file> | |
52 """ | |
53 ) | |
54 file_data = {} | |
55 | |
56 result = await xep_0234.parse_file_element(client, file_elt, file_data) | |
57 | |
58 expected = { | |
59 "mime_type": "text/plain", | |
60 "modified": 1437943560, | |
61 "name": "test.txt", | |
62 "size": 6144, | |
63 } | |
64 | |
65 assert result == expected | |
66 | |
67 @ed | |
68 async def test_parse_file_element_no_file_element(self, host, client): | |
69 """Raise NotFound exception if no <file> element is provided.""" | |
70 xep_0234 = XEP_0234(host) | |
71 with pytest.raises(exceptions.DataError): | |
72 await xep_0234.parse_file_element(client, None) | |
73 | |
74 @ed | |
75 async def test_parse_file_element_invalid_file_element(self, host, client): | |
76 """Raise DataError exception if <file> element is invalid.""" | |
77 xep_0234 = XEP_0234(host) | |
78 file_elt = xml_tools.parse( | |
79 """ | |
80 <file xmlns='invalid_namespace'> | |
81 <name>example.txt</name> | |
82 </file> | |
83 """ | |
84 ) | |
85 with pytest.raises(exceptions.DataError): | |
86 await xep_0234.parse_file_element(client, file_elt) | |
87 | |
88 @ed | |
89 async def test_file_receiving_request_conf(self, monkeypatch, host, client): | |
90 """Normal use case call "get_dest_dir".""" | |
91 | |
92 async def mock_defer_confirm(*args, **kwargs): | |
93 """Simulate defer_confirm always confirming.""" | |
94 return True | |
95 | |
96 monkeypatch.setattr(xml_tools, "defer_confirm", mock_defer_confirm) | |
97 | |
98 xep_0234 = XEP_0234(host) | |
99 monkeypatch.setattr( | |
100 xep_0234._hash, "parse_hash_elt", MagicMock(return_value=(None, None)) | |
101 ) | |
102 mock_get_dest_dir = AsyncMock() | |
103 mock_get_dest_dir.return_value = True | |
104 monkeypatch.setattr(xep_0234._f, "get_dest_dir", mock_get_dest_dir) | |
105 | |
106 session = { | |
107 "peer_jid": jid.JID("peer@example.com"), | |
108 "file_accepted": False, | |
109 "id": "session_id", | |
110 } | |
111 content_data = {"application_data": {}, "transport_data": {"webrtc": False}} | |
112 content_name = "dummy_content" | |
113 file_data = {"progress_id": "123"} | |
114 file_elt = xml_tools.parse( | |
115 """ | |
116 <file xmlns='urn:xmpp:jingle:apps:file-transfer:5'> | |
117 <media-type>text/plain</media-type> | |
118 <name>test.txt</name> | |
119 <date>2015-07-26T21:46:00+01:00</date> | |
120 <size>6144</size> | |
121 </file> | |
122 """ | |
123 ) | |
124 | |
125 confirmed = await xep_0234._file_receiving_request_conf( | |
126 client, session, content_data, content_name, file_data, file_elt | |
127 ) | |
128 | |
129 assert confirmed is True | |
130 assert ( | |
131 mock_get_dest_dir.called | |
132 ), '"get_dest_dir" must be called for non WebRTC case.' | |
133 | |
134 @ed | |
135 async def test_file_receiving_request_conf_webrtc(self, monkeypatch, host, client): | |
136 """WebRTC use case is handled correctly for received confirmation.""" | |
137 | |
138 async def mock_defer_confirm(*args, **kwargs): | |
139 """Simulate defer_confirm always confirming.""" | |
140 return True | |
141 | |
142 monkeypatch.setattr(xml_tools, "defer_confirm", mock_defer_confirm) | |
143 | |
144 xep_0234 = XEP_0234(host) | |
145 monkeypatch.setattr( | |
146 xep_0234._hash, "parse_hash_elt", MagicMock(return_value=(None, None)) | |
147 ) | |
148 mock_get_dest_dir = AsyncMock() | |
149 mock_get_dest_dir.return_value = True | |
150 monkeypatch.setattr(xep_0234._f, "get_dest_dir", mock_get_dest_dir) | |
151 | |
152 session = { | |
153 "peer_jid": jid.JID("peer@example.com"), | |
154 "file_accepted": False, | |
155 "id": "session_id", | |
156 } | |
157 content_data = {"application_data": {}, "transport_data": {"webrtc": True}} | |
158 content_name = "dummy_content" | |
159 file_data = {"progress_id": "123"} | |
160 file_elt = xml_tools.parse( | |
161 """ | |
162 <file xmlns='urn:xmpp:jingle:apps:file-transfer:5'> | |
163 <media-type>text/plain</media-type> | |
164 <name>test.txt</name> | |
165 <date>2015-07-26T21:46:00+01:00</date> | |
166 <size>6144</size> | |
167 </file> | |
168 """ | |
169 ) | |
170 | |
171 confirmed = await xep_0234._file_receiving_request_conf( | |
172 client, session, content_data, content_name, file_data, file_elt | |
173 ) | |
174 | |
175 assert confirmed is True | |
176 # The file is handled by frontend in WebRTC case, so "get_dest_dir" must not be | |
177 # called. | |
178 assert ( | |
179 not mock_get_dest_dir.called | |
180 ), '"get_dest_dir" must not be called for WebRTC case.' |