server/comments: add comment retrieving

This commit is contained in:
rr- 2016-04-24 11:22:18 +02:00
parent 10f8f443f1
commit b75cfff8f7
5 changed files with 85 additions and 8 deletions

26
API.md
View file

@ -42,7 +42,7 @@
- ~~Listing comments~~
- [Creating comment](#creating-comment)
- [Updating comment](#updating-comment)
- ~~Getting comment~~
- [Getting comment](#getting-comment)
- [Deleting comment](#deleting-comment)
- ~~Rating comment~~
- Users
@ -740,6 +740,30 @@ data.
Updates an existing comment text.
## Getting comment
- **Request**
`GET /comment/<id>`
- **Output**
```json5
{
"comment": <comment>
}
```
...where `<comment>` is a [comment resource](#comment).
- **Errors**
- the comment does not exist
- privileges are too low
- **Description**
Retrieves information about an existing comment.
## Deleting comment
- **Request**

View file

@ -21,7 +21,12 @@ class CommentListApi(BaseApi):
class CommentDetailApi(BaseApi):
def get(self, ctx, comment_id):
raise NotImplementedError()
auth.verify_privilege(ctx.user, 'comments:view')
comment = comments.get_comment_by_id(comment_id)
if not comment:
raise comments.CommentNotFoundError(
'Comment %r not found.' % comment_id)
return {'comment': comments.serialize_comment(comment, ctx.user)}
def put(self, ctx, comment_id):
comment = comments.get_comment_by_id(comment_id)

View file

@ -0,0 +1,54 @@
import datetime
import pytest
from szurubooru import api, db, errors
from szurubooru.func import util, comments
@pytest.fixture
def test_ctx(context_factory, config_injector, user_factory, comment_factory):
config_injector({
'privileges': {
'comments:list': 'regular_user',
'comments:view': 'regular_user',
},
'thumbnails': {'avatar_width': 200},
'ranks': ['anonymous', 'regular_user', 'mod', 'admin'],
'rank_names': {'regular_user': 'Peasant'},
})
ret = util.dotdict()
ret.context_factory = context_factory
ret.user_factory = user_factory
ret.comment_factory = comment_factory
ret.detail_api = api.CommentDetailApi()
return ret
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(
user=test_ctx.user_factory(rank='regular_user')),
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(
user=test_ctx.user_factory(rank='regular_user')),
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(
user=test_ctx.user_factory(rank='anonymous')),
5)

View file

@ -47,7 +47,6 @@ def test_retrieving_single(test_ctx):
db.session.add(test_ctx.tag_factory(names=['tag']))
result = test_ctx.detail_api.get(
test_ctx.context_factory(
input={'query': '', 'page': 1},
user=test_ctx.user_factory(rank='regular_user')),
'tag')
assert result == {
@ -66,7 +65,6 @@ def test_trying_to_retrieve_single_non_existing(test_ctx):
with pytest.raises(tags.TagNotFoundError):
test_ctx.detail_api.get(
test_ctx.context_factory(
input={'query': '', 'page': 1},
user=test_ctx.user_factory(rank='regular_user')),
'-')
@ -74,6 +72,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(
input={'query': '', 'page': 1},
user=test_ctx.user_factory(rank='anonymous')),
'-')

View file

@ -46,7 +46,6 @@ def test_retrieving_single(test_ctx):
db.session.add(test_ctx.user_factory(name='u1', rank='regular_user'))
result = test_ctx.detail_api.get(
test_ctx.context_factory(
input={'query': '', 'page': 1},
user=test_ctx.user_factory(rank='regular_user')),
'u1')
assert result == {
@ -66,7 +65,6 @@ def test_trying_to_retrieve_single_non_existing(test_ctx):
with pytest.raises(users.UserNotFoundError):
test_ctx.detail_api.get(
test_ctx.context_factory(
input={'query': '', 'page': 1},
user=test_ctx.user_factory(rank='regular_user')),
'-')
@ -74,6 +72,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(
input={'query': '', 'page': 1},
user=test_ctx.user_factory(rank='anonymous')),
'-')