diff --git a/API.md b/API.md index 44468b39..4dd6d726 100644 --- a/API.md +++ b/API.md @@ -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/` + +- **Output** + + ```json5 + { + "comment": + } + ``` + ...where `` 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** diff --git a/server/szurubooru/api/comment_api.py b/server/szurubooru/api/comment_api.py index 29b53abd..7b267121 100644 --- a/server/szurubooru/api/comment_api.py +++ b/server/szurubooru/api/comment_api.py @@ -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) diff --git a/server/szurubooru/tests/api/test_comment_retrieving.py b/server/szurubooru/tests/api/test_comment_retrieving.py new file mode 100644 index 00000000..03c7e460 --- /dev/null +++ b/server/szurubooru/tests/api/test_comment_retrieving.py @@ -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) diff --git a/server/szurubooru/tests/api/test_tag_retrieving.py b/server/szurubooru/tests/api/test_tag_retrieving.py index 9cfa8f08..f7430b2c 100644 --- a/server/szurubooru/tests/api/test_tag_retrieving.py +++ b/server/szurubooru/tests/api/test_tag_retrieving.py @@ -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')), '-') diff --git a/server/szurubooru/tests/api/test_user_retrieving.py b/server/szurubooru/tests/api/test_user_retrieving.py index bcb1df85..e74e479e 100644 --- a/server/szurubooru/tests/api/test_user_retrieving.py +++ b/server/szurubooru/tests/api/test_user_retrieving.py @@ -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')), '-')