File: test_websupport.py

package info (click to toggle)
sphinxcontrib-websupport 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,112 kB
  • sloc: python: 1,351; javascript: 635; makefile: 53; sh: 7; ansic: 1
file content (288 lines) | stat: -rw-r--r-- 9,646 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
"""Test the Web Support Package"""

from __future__ import annotations

import pytest

from sphinxcontrib.websupport import WebSupport
from sphinxcontrib.websupport.errors import (
    CommentNotAllowedError,
    DocumentNotFoundError,
    UserNotAuthorizedError,
)
from sphinxcontrib.websupport.storage import StorageBackend
from sphinxcontrib.websupport.storage.differ import CombinedHtmlDiff

try:
    from sphinxcontrib.websupport.storage.sqlalchemy_db import Node
    from sphinxcontrib.websupport.storage.sqlalchemystorage import (  # type: ignore[attr-defined]
        Comment,
        CommentVote,
        Session,
    )
    sqlalchemy_missing = False
except ImportError:
    sqlalchemy_missing = True

skip_if_sqlalchemy_missing = pytest.mark.skipif(
    sqlalchemy_missing,
    reason='needs sqlalchemy',
)


@pytest.fixture
def support(rootdir, tmp_path, request):
    settings = {
        'srcdir': rootdir / 'test-root' / 'root',
        # to use same directory for 'builddir' in each 'support' fixture, using
        # 'tempdir' (static) value instead of 'tempdir' fixture value.
        # each test expect result of db value at previous test case.
        'builddir': tmp_path / 'websupport',
    }
    marker = request.node.get_closest_marker('support')
    if marker:
        settings.update(marker.kwargs)

    support = WebSupport(**settings)
    return support


with_support = pytest.mark.support


class NullStorage(StorageBackend):
    pass


@with_support(storage=NullStorage(), srcdir=None)
def test_no_srcdir(support):
    # make sure the correct exception is raised if srcdir is not given.
    with pytest.raises(RuntimeError):
        support.build()


@skip_if_sqlalchemy_missing
@with_support()
def test_build(support):
    support.build()


@skip_if_sqlalchemy_missing
@with_support()
def test_get_document(support):
    support.build()
    with pytest.raises(DocumentNotFoundError):
        support.get_document('nonexisting')

    contents = support.get_document('contents')
    assert contents['title']
    assert contents['body']
    assert contents['sidebar']
    assert contents['relbar']


@skip_if_sqlalchemy_missing
@with_support()
def test_comments(support):
    support.build()
    session = Session()
    nodes = session.query(Node).all()
    first_node = nodes[0]
    second_node = nodes[1]

    # Create a displayed comment and a non displayed comment.
    comment = support.add_comment('First test comment',
                                  node_id=first_node.id,
                                  username='user_one')
    hidden_comment = support.add_comment('Hidden comment',
                                         node_id=first_node.id,
                                         displayed=False)
    # Make sure that comments can't be added to a comment where
    # displayed == False, since it could break the algorithm that
    # converts a nodes comments to a tree.
    with pytest.raises(CommentNotAllowedError):
        support.add_comment('Not allowed', parent_id=str(hidden_comment['id']))
    # Add a displayed and not displayed child to the displayed comment.
    support.add_comment('Child test comment', parent_id=str(comment['id']),
                        username='user_one')
    support.add_comment('Hidden child test comment',
                        parent_id=str(comment['id']), displayed=False)
    # Add a comment to another node to make sure it isn't returned later.
    support.add_comment('Second test comment',
                        node_id=second_node.id,
                        username='user_two')

    # Access the comments as a moderator.
    data = support.get_data(first_node.id, moderator=True)
    comments = data['comments']
    children = comments[0]['children']
    assert len(comments) == 2
    assert comments[1]['text'] == '<p>Hidden comment</p>\n'
    assert len(children) == 2
    assert children[1]['text'] == '<p>Hidden child test comment</p>\n'

    # Access the comments without being a moderator.
    data = support.get_data(first_node.id)
    comments = data['comments']
    children = comments[0]['children']
    assert len(comments) == 1
    assert comments[0]['text'] == '<p>First test comment</p>\n'
    assert len(children) == 1
    assert children[0]['text'] == '<p>Child test comment</p>\n'


@skip_if_sqlalchemy_missing
@with_support()
def test_user_delete_comments(support):
    def get_comment():
        session = Session()
        node = session.query(Node).first()
        session.close()
        return support.get_data(node.id)['comments'][0]

    test_comments(support)
    comment = get_comment()
    assert comment['username'] == 'user_one'
    # Make sure other normal users can't delete someone elses comments.
    with pytest.raises(UserNotAuthorizedError):
        support.delete_comment(comment['id'], username='user_two')
    # Now delete the comment using the correct username.
    support.delete_comment(comment['id'], username='user_one')
    comment = get_comment()
    assert comment['username'] == '[deleted]'
    assert comment['text'] == '[deleted]'


called = False


def moderation_callback(comment):
    global called
    called = True


@skip_if_sqlalchemy_missing
@with_support(moderation_callback=moderation_callback)
def test_moderation(support):
    support.build()
    session = Session()
    nodes = session.query(Node).all()
    node = nodes[7]
    session.close()
    accepted = support.add_comment('Accepted Comment', node_id=node.id,
                                   displayed=False)
    deleted = support.add_comment('Comment to delete', node_id=node.id,
                                  displayed=False)
    # Make sure the moderation_callback is called.
    assert called
    # Make sure the user must be a moderator.
    with pytest.raises(UserNotAuthorizedError):
        support.accept_comment(accepted['id'])
    with pytest.raises(UserNotAuthorizedError):
        support.delete_comment(deleted['id'])
    support.accept_comment(accepted['id'], moderator=True)
    support.delete_comment(deleted['id'], moderator=True)
    comments = support.get_data(node.id)['comments']
    assert len(comments) == 1
    comments = support.get_data(node.id, moderator=True)['comments']
    assert len(comments) == 1


@skip_if_sqlalchemy_missing
@with_support()
def test_moderator_delete_comments(support):
    def get_comment():
        session = Session()
        node = session.query(Node).first()
        session.close()
        return support.get_data(node.id, moderator=True)['comments'][1]

    test_comments(support)
    comment = get_comment()
    support.delete_comment(comment['id'], username='user_two',
                           moderator=True)
    with pytest.raises(IndexError):
        get_comment()


@skip_if_sqlalchemy_missing
@with_support()
def test_update_username(support):
    test_comments(support)
    support.update_username('user_two', 'new_user_two')
    session = Session()
    comments = session.query(Comment).\
        filter(Comment.username == 'user_two').all()
    assert len(comments) == 0
    votes = session.query(CommentVote).\
        filter(CommentVote.username == 'user_two').all()
    assert len(votes) == 0
    comments = session.query(Comment).\
        filter(Comment.username == 'new_user_two').all()
    assert len(comments) == 1
    votes = session.query(CommentVote).\
        filter(CommentVote.username == 'new_user_two').all()
    assert len(votes) == 0


@skip_if_sqlalchemy_missing
@with_support()
def test_proposals(support):
    support.build()
    session = Session()
    node = session.query(Node).first()

    data = support.get_data(node.id)

    source = data['source']
    proposal = source[:5] + source[10:15] + 'asdf' + source[15:]

    support.add_comment('Proposal comment',
                        node_id=node.id,
                        proposal=proposal)


@skip_if_sqlalchemy_missing
@with_support()
def test_voting(support):
    test_comments(support)
    session = Session()
    nodes = session.query(Node).all()
    node = nodes[0]

    comment = support.get_data(node.id)['comments'][0]

    def check_rating(val):
        data = support.get_data(node.id)
        comment = data['comments'][0]
        assert comment['rating'] == val, f'{comment["rating"]} != {val}'

    support.process_vote(comment['id'], 'user_one', '1')
    support.process_vote(comment['id'], 'user_two', '1')
    support.process_vote(comment['id'], 'user_three', '1')
    check_rating(3)
    support.process_vote(comment['id'], 'user_one', '-1')
    check_rating(1)
    support.process_vote(comment['id'], 'user_one', '0')
    check_rating(2)

    # Make sure a vote with value > 1 or < -1 can't be cast.
    with pytest.raises(ValueError, match=r'vote value 2 out of range \(-1, 1\)'):
        support.process_vote(comment['id'], 'user_one', '2')
    with pytest.raises(ValueError, match=r'vote value -2 out of range \(-1, 1\)'):
        support.process_vote(comment['id'], 'user_one', '-2')

    # Make sure past voting data is associated with comments when they are
    # fetched.
    data = support.get_data(str(node.id), username='user_two')
    comment = data['comments'][0]
    assert comment['vote'] == 1, '{} != 1'.format(comment['vote'])


def test_differ() -> None:
    source = 'Lorem ipsum dolor sit amet,\nconsectetur adipisicing elit,\n' \
        'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
    prop = 'Lorem dolor sit amet,\nconsectetur nihil adipisicing elit,\n' \
        'sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.'
    differ = CombinedHtmlDiff(source, prop)
    differ.make_html()