server/tags: add single tag retrieving
This commit is contained in:
parent
37a86816af
commit
fa6b808659
3 changed files with 85 additions and 3 deletions
22
API.md
22
API.md
|
@ -180,7 +180,27 @@ Not yet implemented.
|
|||
|
||||
|
||||
## Getting tag
|
||||
Not yet implemented.
|
||||
- **Request**
|
||||
|
||||
`GET /tag/<name>`
|
||||
|
||||
- **Output**
|
||||
|
||||
```json5
|
||||
{
|
||||
"tag": <tag>
|
||||
}
|
||||
```
|
||||
...where `<tag>` is a [tag resource](#tag).
|
||||
|
||||
- **Errors**
|
||||
|
||||
- the tag does not exist
|
||||
- privileges are too low
|
||||
|
||||
- **Description**
|
||||
|
||||
Retrieves information about an existing tag.
|
||||
|
||||
|
||||
## Deleting tag
|
||||
|
|
|
@ -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)
|
||||
|
|
58
server/szurubooru/tests/api/test_tag_retrieving.py
Normal file
58
server/szurubooru/tests/api/test_tag_retrieving.py
Normal file
|
@ -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')),
|
||||
'-')
|
Loading…
Reference in a new issue