From 05fe09293f7bc48c9ba45c3c091b6cb733878141 Mon Sep 17 00:00:00 2001 From: Augustus Yuan Date: Wed, 10 Jun 2015 18:42:17 -0700 Subject: [PATCH] Add support for GET /tags/slug/:slug with unit tests closes #5371 - added new endpoint to the api routes - created unit tests based on PostModel and PostAPI for testing slug links --- core/server/routes/api.js | 1 + core/test/integration/api/api_tags_spec.js | 21 ++++++++++++++++ .../test/integration/model/model_tags_spec.js | 25 +++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/core/server/routes/api.js b/core/server/routes/api.js index 6d32d130ec..a17accef41 100644 --- a/core/server/routes/api.js +++ b/core/server/routes/api.js @@ -39,6 +39,7 @@ apiRoutes = function apiRoutes(middleware) { // ## Tags router.get('/tags', api.http(api.tags.browse)); router.get('/tags/:id', api.http(api.tags.read)); + router.get('/tags/slug/:slug', api.http(api.tags.read)); router.post('/tags', api.http(api.tags.add)); router.put('/tags/:id', api.http(api.tags.edit)); router.del('/tags/:id', api.http(api.tags.destroy)); diff --git a/core/test/integration/api/api_tags_spec.js b/core/test/integration/api/api_tags_spec.js index 8a693c57aa..8f841b59ad 100644 --- a/core/test/integration/api/api_tags_spec.js +++ b/core/test/integration/api/api_tags_spec.js @@ -177,6 +177,10 @@ describe('Tags API', function () { }); describe('Read', function () { + function extractFirstTag(tags) { + return _.filter(tags, {id: 1})[0]; + } + it('returns post_count with include post_count', function (done) { TagAPI.read({context: {user: 1}, include: 'post_count', slug: 'kitchen-sink'}).then(function (results) { should.exist(results); @@ -190,5 +194,22 @@ describe('Tags API', function () { done(); }).catch(done); }); + + it('with slug', function (done) { + TagAPI.browse({context: {user: 1}}).then(function (results) { + should.exist(results); + should.exist(results.tags); + results.tags.length.should.be.above(0); + + var firstTag = extractFirstTag(results.tags); + + return TagAPI.read({context: {user: 1}, slug: firstTag.slug}); + }).then(function (found) { + should.exist(found); + testUtils.API.checkResponse(found.tags[0], 'tag'); + + done(); + }).catch(done); + }); }); }); diff --git a/core/test/integration/model/model_tags_spec.js b/core/test/integration/model/model_tags_spec.js index 010dac2bfb..ceae6a1f40 100644 --- a/core/test/integration/model/model_tags_spec.js +++ b/core/test/integration/model/model_tags_spec.js @@ -91,6 +91,31 @@ describe('Tag Model', function () { }); }); + describe('findOne', function () { + beforeEach(function (done) { + testUtils.fixtures.insertPosts().then(function () { + done(); + }).catch(done); + }); + + it('with slug', function (done) { + var firstTag; + + TagModel.findPage().then(function (results) { + should.exist(results); + should.exist(results.tags); + results.tags.length.should.be.above(0); + firstTag = results.tags[0]; + + return TagModel.findOne({slug: firstTag.slug}); + }).then(function (found) { + should.exist(found); + + done(); + }).catch(done); + }); + }); + describe('a Post', function () { it('can add a tag', function (done) { var newPost = testUtils.DataGenerator.forModel.posts[0],