1643
|
1 #!/usr/bin/env python3 |
|
2 |
|
3 # Libervia Web |
|
4 # Copyright (C) 2009-2025 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 from collections import defaultdict |
|
20 import sys |
|
21 from libervia.backend.tools.common import data_format |
|
22 import pytest |
|
23 from unittest.mock import MagicMock, AsyncMock, patch |
|
24 |
|
25 # We mock all non-stdlib modules, so the module can be imported. |
|
26 sys.modules["bridge"] = MagicMock(AsyncBridge=AsyncMock) |
|
27 sys.modules["cache"] = MagicMock(cache=MagicMock()) |
|
28 sys.modules["dialog"] = MagicMock() |
|
29 sys.modules["editor"] = MagicMock(TagsEditor=MagicMock()) |
|
30 sys.modules["file_uploader"] = MagicMock(FileUploader=MagicMock()) |
|
31 sys.modules["javascript"] = MagicMock() |
|
32 sys.modules["jid"] = MagicMock(JID=MagicMock()) |
|
33 sys.modules["loading"] = MagicMock() |
|
34 sys.modules["popup"] = MagicMock(create_popup=MagicMock()) |
|
35 sys.modules["js_modules"] = MagicMock() |
|
36 sys.modules["js_modules.quill"] = MagicMock(Quill=MagicMock()) |
|
37 sys.modules["js_modules.quill_mention"] = MagicMock() |
|
38 sys.modules["template"] = MagicMock() |
|
39 sys.modules["identities"] = MagicMock() |
|
40 |
|
41 |
|
42 class FakeElement: |
|
43 |
|
44 def __init__(self): |
|
45 self.attrs = defaultdict(MagicMock) |
|
46 |
|
47 def __getattr__(self, attr) -> MagicMock: |
|
48 return self.attrs[attr] |
|
49 |
|
50 |
|
51 class FakeDocument: |
|
52 |
|
53 def __init__(self): |
|
54 self.attrs = defaultdict(MagicMock) |
|
55 self.items = defaultdict(FakeElement) |
|
56 |
|
57 def __getattr__(self, attr) -> MagicMock: |
|
58 return self.attrs[attr] |
|
59 |
|
60 def __getitem__(self, item) -> FakeElement: |
|
61 return self.items[item] |
|
62 |
|
63 def __setitem__(self, key, item) -> None: |
|
64 self.items[key] = item |
|
65 |
|
66 |
|
67 class FakeEvent: |
|
68 """Fake event object with preventDefault and stopPropagation methods.""" |
|
69 |
|
70 def __init__(self): |
|
71 self.defaultPrevented = False |
|
72 self.currentTarget = None |
|
73 |
|
74 def preventDefault(self): |
|
75 self.defaultPrevented = True |
|
76 |
|
77 def stopPropagation(self): |
|
78 pass |
|
79 |
|
80 |
|
81 document = FakeDocument() |
|
82 browser = sys.modules["browser"] = MagicMock( |
|
83 aio=MagicMock(), |
|
84 console=MagicMock(), |
|
85 document=document, |
|
86 window=MagicMock( |
|
87 service="test_service", |
|
88 node="test_node", |
|
89 subscribed=True, |
|
90 location=MagicMock(href="http://example.com/test"), |
|
91 URL=MagicMock( |
|
92 new=MagicMock( |
|
93 return_value=MagicMock(href="http://example.com/test?search=test") |
|
94 ) |
|
95 ), |
|
96 RegExp=MagicMock(), |
|
97 ), |
|
98 ) |
|
99 |
|
100 |
|
101 class FakeTemplate(MagicMock): |
|
102 |
|
103 def get_elt(self, *args, **kwargs): |
|
104 return FakeElement() |
|
105 |
|
106 |
|
107 sys.modules["template"] = MagicMock(Template=FakeTemplate) |
|
108 |
|
109 # We can now import the actual module |
|
110 from libervia.web.pages.forums.view import _browser as forums_module |
|
111 |
|
112 |
|
113 class TestLiberviaForumView: |
|
114 @pytest.fixture |
|
115 def forum_view(self) -> forums_module.LiberviaForumView: |
|
116 """Fixture to create a LiberviaForumView instance with mocks.""" |
|
117 # Create a mock for the Quill editor |
|
118 quill_mock = MagicMock() |
|
119 quill_mock.getSemanticHTML.return_value = "<p>test content</p>" |
|
120 quill_mock.getContents.return_value = {"ops": []} |
|
121 quill_mock.getModule.return_value = MagicMock(container=MagicMock()) |
|
122 |
|
123 # Mock the Quill constructor |
|
124 with patch("js_modules.quill.Quill.new", return_value=quill_mock): |
|
125 forum_view = forums_module.LiberviaForumView() |
|
126 |
|
127 # Mock the tag editor |
|
128 forum_view.tag_editor = MagicMock(current_tags=set()) |
|
129 |
|
130 return forum_view |
|
131 |
|
132 @pytest.fixture |
|
133 def bridge(self, monkeypatch) -> AsyncMock: |
|
134 bridge = AsyncMock() |
|
135 monkeypatch.setattr(forums_module, "bridge", bridge) |
|
136 return bridge |
|
137 |
|
138 @pytest.mark.asyncio |
|
139 async def test_toggle_subscription_subscribed(self, forum_view, bridge) -> None: |
|
140 """Toggle subscription calls unsubscribe when already subscribed.""" |
|
141 with patch.object(browser.window, "subscribed", True): |
|
142 button = FakeElement() |
|
143 document["subscribe-button"] = button |
|
144 |
|
145 await forum_view.toggle_subscription() |
|
146 |
|
147 bridge.ps_unsubscribe.assert_awaited_once_with("test_service", "test_node") |
|
148 |
|
149 @pytest.mark.asyncio |
|
150 async def test_toggle_subscription_not_subscribed(self, forum_view, bridge) -> None: |
|
151 """Toggle subscription calls subscribe when not subscribed.""" |
|
152 with patch.object(browser.window, "subscribed", False): |
|
153 button = FakeElement() |
|
154 document["subscribe-button"] = button |
|
155 |
|
156 await forum_view.toggle_subscription() |
|
157 |
|
158 bridge.ps_subscribe.assert_awaited_once_with("test_service", "test_node", "") |
|
159 |
|
160 @pytest.mark.asyncio |
|
161 async def test_send_message_with_mentions(self, forum_view, bridge) -> None: |
|
162 """Sending message with mentions updates ``mb_data``.""" |
|
163 # Mock Quill content with mentions |
|
164 quill_mock = MagicMock() |
|
165 quill_mock.getSemanticHTML.return_value = "<p>test message</p>" |
|
166 quill_mock.getContents.return_value = { |
|
167 "ops": [{"insert": {"mention": {"id": "user@example.com"}}}] |
|
168 } |
|
169 forum_view.message_editor = quill_mock |
|
170 |
|
171 forum_view.tag_editor.current_tags = set() |
|
172 |
|
173 await forum_view.send_message() |
|
174 |
|
175 bridge.mb_send.assert_awaited() |
|
176 mb_data = data_format.deserialise(bridge.mb_send.call_args[0][0]) |
|
177 assert mb_data["mentions"] == ["user@example.com"] |
|
178 |
|
179 @pytest.mark.asyncio |
|
180 async def test_send_message_error_handling(self, forum_view, bridge) -> None: |
|
181 """Error in ``bridge.mb_send`` is reported.""" |
|
182 bridge.mb_send.side_effect = Exception("Send failed") |
|
183 |
|
184 quill_mock = MagicMock() |
|
185 quill_mock.getSemanticHTML.return_value = "<p>test message</p>" |
|
186 quill_mock.getContents.return_value = {"ops": []} |
|
187 forum_view.message_editor = quill_mock |
|
188 |
|
189 forum_view.tag_editor.current_tags = set() |
|
190 |
|
191 with patch("dialog.notification.show") as mock_show: |
|
192 await forum_view.send_message() |
|
193 |
|
194 # Verify that error notification was shown |
|
195 mock_show.assert_called_with("Can't send message: Send failed.", "error") |
|
196 |
|
197 def test_on_add_tag(self, forum_view) -> None: |
|
198 """``tags_container`` become visible when ``on_add_tag``is called.""" |
|
199 tags_container = FakeElement() |
|
200 tags_container.classList = MagicMock() |
|
201 document["tags_container"] = tags_container |
|
202 |
|
203 forum_view.on_add_tag(FakeEvent()) |
|
204 |
|
205 # Verify that the class list was modified |
|
206 tags_container.classList.remove.assert_called_once_with("is-hidden") |
|
207 |
|
208 def test_on_add_attachment(self, forum_view) -> None: |
|
209 """``file_input`` click is called from ``on_add_attachment``.""" |
|
210 file_input = FakeElement() |
|
211 document["forum-file-input"] = file_input |
|
212 |
|
213 file_input.click = MagicMock() |
|
214 |
|
215 forum_view.on_add_attachment(FakeEvent()) |
|
216 |
|
217 file_input.click.assert_called_once() |