2016-04-24 11:22:18 +02:00
|
|
|
import datetime
|
|
|
|
import pytest
|
|
|
|
from szurubooru import api, db, errors
|
|
|
|
from szurubooru.func import util, comments
|
|
|
|
|
|
|
|
@pytest.fixture
|
2016-05-01 20:15:28 +02:00
|
|
|
def test_ctx(
|
|
|
|
tmpdir, context_factory, config_injector, user_factory, comment_factory):
|
2016-04-24 11:22:18 +02:00
|
|
|
config_injector({
|
2016-05-01 20:15:28 +02:00
|
|
|
'data_dir': str(tmpdir),
|
2016-04-30 23:17:08 +02:00
|
|
|
'data_url': 'http://example.com',
|
2016-04-24 11:22:18 +02:00
|
|
|
'privileges': {
|
2016-05-08 16:59:25 +02:00
|
|
|
'comments:list': db.User.RANK_REGULAR,
|
|
|
|
'comments:view': db.User.RANK_REGULAR,
|
|
|
|
'users:edit:any:email': db.User.RANK_MODERATOR,
|
2016-04-24 11:22:18 +02:00
|
|
|
},
|
|
|
|
'thumbnails': {'avatar_width': 200},
|
|
|
|
})
|
|
|
|
ret = util.dotdict()
|
|
|
|
ret.context_factory = context_factory
|
|
|
|
ret.user_factory = user_factory
|
|
|
|
ret.comment_factory = comment_factory
|
2016-04-24 11:24:52 +02:00
|
|
|
ret.list_api = api.CommentListApi()
|
2016-04-24 11:22:18 +02:00
|
|
|
ret.detail_api = api.CommentDetailApi()
|
|
|
|
return ret
|
|
|
|
|
2016-04-24 11:24:52 +02:00
|
|
|
def test_retrieving_multiple(test_ctx):
|
|
|
|
comment1 = test_ctx.comment_factory(text='text 1')
|
|
|
|
comment2 = test_ctx.comment_factory(text='text 2')
|
|
|
|
db.session.add_all([comment1, comment2])
|
|
|
|
result = test_ctx.list_api.get(
|
|
|
|
test_ctx.context_factory(
|
|
|
|
input={'query': '', 'page': 1},
|
2016-05-08 16:59:25 +02:00
|
|
|
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)))
|
2016-04-24 11:24:52 +02:00
|
|
|
assert result['query'] == ''
|
|
|
|
assert result['page'] == 1
|
|
|
|
assert result['pageSize'] == 100
|
|
|
|
assert result['total'] == 2
|
2016-04-28 18:20:50 +02:00
|
|
|
assert [c['text'] for c in result['results']] == ['text 1', 'text 2']
|
2016-04-24 11:24:52 +02:00
|
|
|
|
|
|
|
def test_trying_to_retrieve_multiple_without_privileges(test_ctx):
|
|
|
|
with pytest.raises(errors.AuthError):
|
|
|
|
test_ctx.list_api.get(
|
|
|
|
test_ctx.context_factory(
|
|
|
|
input={'query': '', 'page': 1},
|
2016-05-10 12:01:01 +02:00
|
|
|
user=test_ctx.user_factory(rank=db.User.RANK_ANONYMOUS)))
|
2016-04-24 11:24:52 +02:00
|
|
|
|
2016-04-24 11:22:18 +02:00
|
|
|
def test_retrieving_single(test_ctx):
|
|
|
|
comment = test_ctx.comment_factory(text='dummy text')
|
|
|
|
db.session.add(comment)
|
|
|
|
db.session.flush()
|
|
|
|
result = test_ctx.detail_api.get(
|
|
|
|
test_ctx.context_factory(
|
2016-05-08 16:59:25 +02:00
|
|
|
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
|
2016-04-24 11:22:18 +02:00
|
|
|
comment.comment_id)
|
|
|
|
assert 'comment' in result
|
|
|
|
assert 'id' in result['comment']
|
|
|
|
assert 'lastEditTime' in result['comment']
|
|
|
|
assert 'creationTime' in result['comment']
|
|
|
|
assert 'text' in result['comment']
|
|
|
|
assert 'user' in result['comment']
|
|
|
|
assert 'name' in result['comment']['user']
|
|
|
|
assert 'post' in result['comment']
|
|
|
|
assert 'id' in result['comment']['post']
|
|
|
|
|
|
|
|
def test_trying_to_retrieve_single_non_existing(test_ctx):
|
|
|
|
with pytest.raises(comments.CommentNotFoundError):
|
|
|
|
test_ctx.detail_api.get(
|
|
|
|
test_ctx.context_factory(
|
2016-05-08 16:59:25 +02:00
|
|
|
user=test_ctx.user_factory(rank=db.User.RANK_REGULAR)),
|
2016-04-24 11:22:18 +02:00
|
|
|
5)
|
|
|
|
|
|
|
|
def test_trying_to_retrieve_single_without_privileges(test_ctx):
|
|
|
|
with pytest.raises(errors.AuthError):
|
|
|
|
test_ctx.detail_api.get(
|
|
|
|
test_ctx.context_factory(
|
2016-05-10 12:01:01 +02:00
|
|
|
user=test_ctx.user_factory(rank=db.User.RANK_ANONYMOUS)),
|
2016-04-24 11:22:18 +02:00
|
|
|
5)
|