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
|
from datetime import datetime
from unittest.mock import patch
import pytest
from szurubooru import api, db, errors, model
from szurubooru.func import posts
@pytest.fixture(autouse=True)
def inject_config(config_injector):
config_injector(
{
"privileges": {
"posts:list": model.User.RANK_REGULAR,
"posts:view": model.User.RANK_REGULAR,
"posts:view:unsafe": model.User.RANK_REGULAR,
"posts:list:unsafe": model.User.RANK_REGULAR,
},
}
)
def test_retrieving_multiple(user_factory, post_factory, context_factory):
post1 = post_factory(id=1)
post2 = post_factory(id=2)
db.session.add_all([post1, post2])
db.session.flush()
with patch("szurubooru.func.posts.serialize_post"):
posts.serialize_post.return_value = "serialized post"
result = api.post_api.get_posts(
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 post", "serialized post"],
}
def test_using_special_tokens(user_factory, post_factory, context_factory):
auth_user = user_factory(rank=model.User.RANK_REGULAR)
post1 = post_factory(id=1)
post2 = post_factory(id=2)
post1.favorited_by = [
model.PostFavorite(user=auth_user, time=datetime.utcnow())
]
db.session.add_all([post1, post2, auth_user])
db.session.flush()
with patch("szurubooru.func.posts.serialize_post"):
posts.serialize_post.side_effect = (
lambda post, *_args, **_kwargs: "serialized post %d" % post.post_id
)
result = api.post_api.get_posts(
context_factory(
params={"query": "special:fav", "offset": 0}, user=auth_user
)
)
assert result == {
"query": "special:fav",
"offset": 0,
"limit": 100,
"total": 1,
"results": ["serialized post 1"],
}
def test_trying_to_use_special_tokens_without_logging_in(
user_factory, context_factory, config_injector
):
config_injector(
{
"privileges": {
"posts:list": "anonymous",
"posts:list:unsafe": "regular",
},
}
)
with pytest.raises(errors.SearchError):
api.post_api.get_posts(
context_factory(
params={"query": "special:fav", "offset": 0},
user=user_factory(rank=model.User.RANK_ANONYMOUS),
)
)
def test_trying_to_retrieve_multiple_without_privileges(
user_factory, context_factory
):
with pytest.raises(errors.AuthError):
api.post_api.get_posts(
context_factory(
params={"query": "", "offset": 0},
user=user_factory(rank=model.User.RANK_ANONYMOUS),
)
)
def test_retrieving_single(user_factory, post_factory, context_factory):
db.session.add(post_factory(id=1))
db.session.flush()
with patch("szurubooru.func.posts.serialize_post"):
posts.serialize_post.return_value = "serialized post"
result = api.post_api.get_post(
context_factory(user=user_factory(rank=model.User.RANK_REGULAR)),
{"post_id": 1},
)
assert result == "serialized post"
def test_trying_to_retrieve_single_non_existing(user_factory, context_factory):
with pytest.raises(posts.PostNotFoundError):
api.post_api.get_post(
context_factory(user=user_factory(rank=model.User.RANK_REGULAR)),
{"post_id": 999},
)
def test_trying_to_retrieve_single_without_privileges(
user_factory, context_factory
):
with pytest.raises(errors.AuthError):
api.post_api.get_post(
context_factory(user=user_factory(rank=model.User.RANK_ANONYMOUS)),
{"post_id": 999},
)
def test_trying_to_retrieve_unsafe_without_privileges(
user_factory, context_factory, post_factory, config_injector
):
config_injector(
{
"privileges": {
"posts:view": "anonymous",
"posts:view:unsafe": "regular",
},
}
)
db.session.add(post_factory(id=1, safety=model.Post.SAFETY_UNSAFE))
db.session.flush()
with pytest.raises(errors.AuthError):
api.post_api.get_post(
context_factory(user=user_factory(rank=model.User.RANK_ANONYMOUS)),
{"post_id": 1},
)
|