This repository has been archived on 2025-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
szurubooru/server/szurubooru/tests/api/test_post_retrieving.py
rr- af62f8c45a server/general: ditch falcon for in-house WSGI app
For quite some time, I hated Falcon's class maps approach that caused
more chaos than good for Szurubooru. I've taken a look at the other
frameworks (hug, flask, etc) again, but they all looked too
bloated/over-engineered. I decided to just talk to WSGI myself.

Regex-based routing may not be the fastest in the world, but I'm fine
with response time of 10 ms for cached /posts.
2016-08-14 16:43:35 +02:00

97 lines
3.6 KiB
Python

import pytest
import unittest.mock
from datetime import datetime
from szurubooru import api, db, errors
from szurubooru.func import posts
@pytest.fixture(autouse=True)
def inject_config(tmpdir, config_injector):
config_injector({
'privileges': {
'posts:list': db.User.RANK_REGULAR,
'posts:view': db.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])
with unittest.mock.patch('szurubooru.func.posts.serialize_post'):
posts.serialize_post.return_value = 'serialized post'
result = api.post_api.get_posts(
context_factory(
params={'query': '', 'page': 1},
user=user_factory(rank=db.User.RANK_REGULAR)))
assert result == {
'query': '',
'page': 1,
'pageSize': 100,
'total': 2,
'results': ['serialized post', 'serialized post'],
}
def test_using_special_tokens(user_factory, post_factory, context_factory):
auth_user = user_factory(rank=db.User.RANK_REGULAR)
post1 = post_factory(id=1)
post2 = post_factory(id=2)
post1.favorited_by = [db.PostFavorite(
user=auth_user, time=datetime.utcnow())]
db.session.add_all([post1, post2, auth_user])
db.session.flush()
with unittest.mock.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', 'page': 1},
user=auth_user))
assert result == {
'query': 'special:fav',
'page': 1,
'pageSize': 100,
'total': 1,
'results': ['serialized post 1'],
}
def test_trying_to_use_special_tokens_without_logging_in(
user_factory, post_factory, context_factory, config_injector):
config_injector({
'privileges': {'posts:list': 'anonymous'},
})
with pytest.raises(errors.SearchError):
api.post_api.get_posts(
context_factory(
params={'query': 'special:fav', 'page': 1},
user=user_factory(rank=db.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': '', 'page': 1},
user=user_factory(rank=db.User.RANK_ANONYMOUS)))
def test_retrieving_single(user_factory, post_factory, context_factory):
db.session.add(post_factory(id=1))
with unittest.mock.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=db.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=db.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=db.User.RANK_ANONYMOUS)),
{'post_id': 999})