From fa6b80865983a0e3911bc9c68120df860d9f8eee Mon Sep 17 00:00:00 2001 From: rr- Date: Sat, 16 Apr 2016 17:26:10 +0200 Subject: [PATCH] server/tags: add single tag retrieving --- API.md | 22 ++++++- server/szurubooru/api/tag_api.py | 8 ++- .../tests/api/test_tag_retrieving.py | 58 +++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 server/szurubooru/tests/api/test_tag_retrieving.py diff --git a/API.md b/API.md index fe02c43f..bf5d659a 100644 --- a/API.md +++ b/API.md @@ -180,7 +180,27 @@ Not yet implemented. ## Getting tag -Not yet implemented. +- **Request** + + `GET /tag/` + +- **Output** + + ```json5 + { + "tag": + } + ``` + ...where `` is a [tag resource](#tag). + +- **Errors** + + - the tag does not exist + - privileges are too low + +- **Description** + + Retrieves information about an existing tag. ## Deleting tag diff --git a/server/szurubooru/api/tag_api.py b/server/szurubooru/api/tag_api.py index 8d7353d1..ac1b4716 100644 --- a/server/szurubooru/api/tag_api.py +++ b/server/szurubooru/api/tag_api.py @@ -33,8 +33,12 @@ class TagListApi(BaseApi): return {'tag': _serialize_tag(tag)} class TagDetailApi(BaseApi): - def get(self, ctx): - raise NotImplementedError() + def get(self, ctx, tag_name): + auth.verify_privilege(ctx.user, 'tags:view') + tag = tags.get_by_name(ctx.session, tag_name) + if not tag: + raise tags.TagNotFoundError('Tag %r not found.' % tag_name) + return {'tag': _serialize_tag(tag)} def put(self, ctx, tag_name): tag = tags.get_by_name(ctx.session, tag_name) diff --git a/server/szurubooru/tests/api/test_tag_retrieving.py b/server/szurubooru/tests/api/test_tag_retrieving.py new file mode 100644 index 00000000..7b13f3d3 --- /dev/null +++ b/server/szurubooru/tests/api/test_tag_retrieving.py @@ -0,0 +1,58 @@ +import datetime +import pytest +from szurubooru import api, db, errors +from szurubooru.util import misc, tags + +@pytest.fixture +def test_ctx( + session, context_factory, config_injector, user_factory, tag_factory): + config_injector({ + 'privileges': { + 'tags:list': 'regular_user', + 'tags:view': 'regular_user', + }, + 'thumbnails': {'avatar_width': 200}, + 'ranks': ['anonymous', 'regular_user', 'mod', 'admin'], + 'rank_names': {'regular_user': 'Peasant'}, + }) + ret = misc.dotdict() + ret.session = session + ret.context_factory = context_factory + ret.user_factory = user_factory + ret.tag_factory = tag_factory + ret.detail_api = api.TagDetailApi() + return ret + +def test_retrieving_single(test_ctx): + test_ctx.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 == { + 'tag': { + 'names': ['tag'], + 'category': 'dummy', + 'creationTime': datetime.datetime(1996, 1, 1), + 'lastEditTime': None, + 'suggestions': [], + 'implications': [], + } + } + +def test_retrieving_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')), + '-') + +def test_retrieving_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')), + '-')