Add GET routes for /user/tags and /user/tags/{id}

An operation that is useful for tools to be able to do is to list a
user's tags without having to pull down the entire user object (which is
likely significantly larger than the tags). This commit adds a route to
GET /user/tags, to get a list of the user's tags, as well as a GET
/user/tags/{id} to get the details of a particular tag.

This commit also includes tests for the two new routes.
This commit is contained in:
Carlo Zancanaro 2015-10-20 00:50:41 +11:00
parent c04b27ffd5
commit 4d6bd38648
4 changed files with 72 additions and 1 deletions

View file

@ -673,6 +673,14 @@ api.wrap = (user, main=true) ->
user.tags.splice to, 0, user.tags.splice(from, 1)[0]
cb? null, user.tags
getTags: (req, cb) ->
cb? null, user.tags
getTag: (req, cb) ->
tid = req.params.id
i = _.findIndex user.tags, {id: tid}
return cb?({code:404,message:i18n.t('messageTagNotFound', req.language)}) if !~i
cb? null, user.tags[i]
updateTag: (req, cb) ->
tid = req.params.id

View file

@ -0,0 +1,20 @@
import {
generateUser,
requester,
} from '../../helpers/api.helper';
describe('GET /user/tags', () => {
let api, user;
beforeEach(() => {
return generateUser().then((usr) => {
user = usr;
api = requester(usr);
});
});
it('gets the user\'s tags', () => {
return expect(api.get('/user/tags'))
.to.eventually.eql(user.tags);
});
});

View file

@ -0,0 +1,25 @@
import {
generateUser,
requester,
} from '../../helpers/api.helper';
describe('GET /user/tags/id', () => {
let api, user;
beforeEach(() => {
return generateUser().then((usr) => {
user = usr;
api = requester(usr);
});
});
it('gets a user\'s tag by id', () => {
return expect(api.get('/user/tags/' + user.tags[0].id))
.to.eventually.eql(user.tags[0]);
});
it('fails for non-existent tags', () => {
return expect(api.get('/user/tags/not-an-id'))
.to.eventually.be.rejected.with.property('code', 404);
});
});

View file

@ -347,8 +347,19 @@ module.exports = (swagger, v2) ->
action: user.batchUpdate
# Tags
"/user/tags":
"/user/tags/{id}:GET":
spec:
path: '/user/tags/{id}'
method: 'GET'
description: "Get a tag"
parameters: [
path 'id','The id of the tag to get','string'
]
action: user.getTag
"/user/tags:POST":
spec:
path: "/user/tags"
method: 'POST'
description: 'Create a new tag'
parameters: [
@ -356,6 +367,13 @@ module.exports = (swagger, v2) ->
]
action: user.addTag
"/user/tags:GET":
spec:
path: "/user/tags"
method: 'GET'
description: 'List all of a user\'s tags'
action: user.getTags
"/user/tags/sort":
spec:
method: 'POST'