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
|
from unittest.mock import patch
import pytest
from szurubooru import api, db, model, errors
from szurubooru.func import comments
@pytest.fixture(autouse=True)
def inject_config(config_injector):
config_injector({
'privileges': {
'comments:list': model.User.RANK_REGULAR,
'comments:view': model.User.RANK_REGULAR,
},
})
def test_retrieving_multiple(user_factory, comment_factory, context_factory):
comment1 = comment_factory(text='text 1')
comment2 = comment_factory(text='text 2')
db.session.add_all([comment1, comment2])
db.session.flush()
with patch('szurubooru.func.comments.serialize_comment'):
comments.serialize_comment.return_value = 'serialized comment'
result = api.comment_api.get_comments(
context_factory(
params={'query': '', 'offset': 0},
user=user_factory(rank=model.User.RANK_REGULAR)))
assert result == {
'query': '',
'offset': 0,
'limit': 100,
'total': 2,
'results': ['serialized comment', 'serialized comment'],
}
def test_trying_to_retrieve_multiple_without_privileges(
user_factory, context_factory):
with pytest.raises(errors.AuthError):
api.comment_api.get_comments(
context_factory(
params={'query': '', 'offset': 0},
user=user_factory(rank=model.User.RANK_ANONYMOUS)))
def test_retrieving_single(user_factory, comment_factory, context_factory):
comment = comment_factory(text='dummy text')
db.session.add(comment)
db.session.flush()
with patch('szurubooru.func.comments.serialize_comment'):
comments.serialize_comment.return_value = 'serialized comment'
result = api.comment_api.get_comment(
context_factory(
user=user_factory(rank=model.User.RANK_REGULAR)),
{'comment_id': comment.comment_id})
assert result == 'serialized comment'
def test_trying_to_retrieve_single_non_existing(user_factory, context_factory):
with pytest.raises(comments.CommentNotFoundError):
api.comment_api.get_comment(
context_factory(
user=user_factory(rank=model.User.RANK_REGULAR)),
{'comment_id': 5})
def test_trying_to_retrieve_single_without_privileges(
user_factory, context_factory):
with pytest.raises(errors.AuthError):
api.comment_api.get_comment(
context_factory(user=user_factory(rank=model.User.RANK_ANONYMOUS)),
{'comment_id': 5})
|