annotate tests/browser/unit/test_forums.py @ 1644:8a09eea9003f default tip

doc (user): Add doc for forum feature: fix 463
author Goffi <goffi@goffi.org>
date Sat, 06 Sep 2025 17:28:07 +0200
parents 0e11dbbcefe7
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1643
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/env python3
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
2
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # Libervia Web
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
4 # Copyright (C) 2009-2025 Jérôme Poisson (goffi@goffi.org)
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
5
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
10
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
15
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
18
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
19 from collections import defaultdict
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
20 import sys
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
21 from libervia.backend.tools.common import data_format
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
22 import pytest
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from unittest.mock import MagicMock, AsyncMock, patch
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
24
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
25 # We mock all non-stdlib modules, so the module can be imported.
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
26 sys.modules["bridge"] = MagicMock(AsyncBridge=AsyncMock)
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
27 sys.modules["cache"] = MagicMock(cache=MagicMock())
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
28 sys.modules["dialog"] = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
29 sys.modules["editor"] = MagicMock(TagsEditor=MagicMock())
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
30 sys.modules["file_uploader"] = MagicMock(FileUploader=MagicMock())
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
31 sys.modules["javascript"] = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
32 sys.modules["jid"] = MagicMock(JID=MagicMock())
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
33 sys.modules["loading"] = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
34 sys.modules["popup"] = MagicMock(create_popup=MagicMock())
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
35 sys.modules["js_modules"] = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
36 sys.modules["js_modules.quill"] = MagicMock(Quill=MagicMock())
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
37 sys.modules["js_modules.quill_mention"] = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
38 sys.modules["template"] = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
39 sys.modules["identities"] = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
40
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
41
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
42 class FakeElement:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
43
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
44 def __init__(self):
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
45 self.attrs = defaultdict(MagicMock)
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
46
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
47 def __getattr__(self, attr) -> MagicMock:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
48 return self.attrs[attr]
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
49
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
50
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
51 class FakeDocument:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
52
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
53 def __init__(self):
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
54 self.attrs = defaultdict(MagicMock)
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
55 self.items = defaultdict(FakeElement)
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
56
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def __getattr__(self, attr) -> MagicMock:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
58 return self.attrs[attr]
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
59
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
60 def __getitem__(self, item) -> FakeElement:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
61 return self.items[item]
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
62
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
63 def __setitem__(self, key, item) -> None:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
64 self.items[key] = item
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
65
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
66
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
67 class FakeEvent:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
68 """Fake event object with preventDefault and stopPropagation methods."""
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
69
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def __init__(self):
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
71 self.defaultPrevented = False
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
72 self.currentTarget = None
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
73
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
74 def preventDefault(self):
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
75 self.defaultPrevented = True
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
76
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
77 def stopPropagation(self):
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
78 pass
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
79
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
80
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
81 document = FakeDocument()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
82 browser = sys.modules["browser"] = MagicMock(
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
83 aio=MagicMock(),
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
84 console=MagicMock(),
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
85 document=document,
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
86 window=MagicMock(
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
87 service="test_service",
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
88 node="test_node",
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
89 subscribed=True,
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
90 location=MagicMock(href="http://example.com/test"),
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
91 URL=MagicMock(
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
92 new=MagicMock(
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
93 return_value=MagicMock(href="http://example.com/test?search=test")
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
94 )
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
95 ),
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
96 RegExp=MagicMock(),
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
97 ),
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
98 )
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
99
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
100
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
101 class FakeTemplate(MagicMock):
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
102
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
103 def get_elt(self, *args, **kwargs):
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
104 return FakeElement()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
105
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
106
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
107 sys.modules["template"] = MagicMock(Template=FakeTemplate)
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
108
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
109 # We can now import the actual module
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
110 from libervia.web.pages.forums.view import _browser as forums_module
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
111
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
112
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
113 class TestLiberviaForumView:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
114 @pytest.fixture
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
115 def forum_view(self) -> forums_module.LiberviaForumView:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
116 """Fixture to create a LiberviaForumView instance with mocks."""
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
117 # Create a mock for the Quill editor
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
118 quill_mock = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
119 quill_mock.getSemanticHTML.return_value = "<p>test content</p>"
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
120 quill_mock.getContents.return_value = {"ops": []}
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
121 quill_mock.getModule.return_value = MagicMock(container=MagicMock())
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
122
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
123 # Mock the Quill constructor
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
124 with patch("js_modules.quill.Quill.new", return_value=quill_mock):
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
125 forum_view = forums_module.LiberviaForumView()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
126
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
127 # Mock the tag editor
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
128 forum_view.tag_editor = MagicMock(current_tags=set())
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
129
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
130 return forum_view
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
131
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
132 @pytest.fixture
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
133 def bridge(self, monkeypatch) -> AsyncMock:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
134 bridge = AsyncMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
135 monkeypatch.setattr(forums_module, "bridge", bridge)
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
136 return bridge
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
137
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
138 @pytest.mark.asyncio
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
139 async def test_toggle_subscription_subscribed(self, forum_view, bridge) -> None:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
140 """Toggle subscription calls unsubscribe when already subscribed."""
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
141 with patch.object(browser.window, "subscribed", True):
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
142 button = FakeElement()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
143 document["subscribe-button"] = button
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
144
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
145 await forum_view.toggle_subscription()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
146
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
147 bridge.ps_unsubscribe.assert_awaited_once_with("test_service", "test_node")
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
148
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
149 @pytest.mark.asyncio
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
150 async def test_toggle_subscription_not_subscribed(self, forum_view, bridge) -> None:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
151 """Toggle subscription calls subscribe when not subscribed."""
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
152 with patch.object(browser.window, "subscribed", False):
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
153 button = FakeElement()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
154 document["subscribe-button"] = button
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
155
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
156 await forum_view.toggle_subscription()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
157
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
158 bridge.ps_subscribe.assert_awaited_once_with("test_service", "test_node", "")
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
159
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
160 @pytest.mark.asyncio
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
161 async def test_send_message_with_mentions(self, forum_view, bridge) -> None:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
162 """Sending message with mentions updates ``mb_data``."""
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
163 # Mock Quill content with mentions
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
164 quill_mock = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
165 quill_mock.getSemanticHTML.return_value = "<p>test message</p>"
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
166 quill_mock.getContents.return_value = {
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
167 "ops": [{"insert": {"mention": {"id": "user@example.com"}}}]
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
168 }
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
169 forum_view.message_editor = quill_mock
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
170
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
171 forum_view.tag_editor.current_tags = set()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
172
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
173 await forum_view.send_message()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
174
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
175 bridge.mb_send.assert_awaited()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
176 mb_data = data_format.deserialise(bridge.mb_send.call_args[0][0])
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
177 assert mb_data["mentions"] == ["user@example.com"]
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
178
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
179 @pytest.mark.asyncio
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
180 async def test_send_message_error_handling(self, forum_view, bridge) -> None:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
181 """Error in ``bridge.mb_send`` is reported."""
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
182 bridge.mb_send.side_effect = Exception("Send failed")
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
183
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
184 quill_mock = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
185 quill_mock.getSemanticHTML.return_value = "<p>test message</p>"
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
186 quill_mock.getContents.return_value = {"ops": []}
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
187 forum_view.message_editor = quill_mock
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
188
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
189 forum_view.tag_editor.current_tags = set()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
190
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
191 with patch("dialog.notification.show") as mock_show:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
192 await forum_view.send_message()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
193
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
194 # Verify that error notification was shown
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
195 mock_show.assert_called_with("Can't send message: Send failed.", "error")
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
196
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
197 def test_on_add_tag(self, forum_view) -> None:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
198 """``tags_container`` become visible when ``on_add_tag``is called."""
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
199 tags_container = FakeElement()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
200 tags_container.classList = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
201 document["tags_container"] = tags_container
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
202
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
203 forum_view.on_add_tag(FakeEvent())
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
204
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
205 # Verify that the class list was modified
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
206 tags_container.classList.remove.assert_called_once_with("is-hidden")
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
207
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
208 def test_on_add_attachment(self, forum_view) -> None:
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
209 """``file_input`` click is called from ``on_add_attachment``."""
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
210 file_input = FakeElement()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
211 document["forum-file-input"] = file_input
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
212
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
213 file_input.click = MagicMock()
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
214
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
215 forum_view.on_add_attachment(FakeEvent())
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
216
0e11dbbcefe7 tests (browser/unit): forums;
Goffi <goffi@goffi.org>
parents:
diff changeset
217 file_input.click.assert_called_once()