From 66a046b00bfcb910ef874e1a7468df78af3d19a2 Mon Sep 17 00:00:00 2001 From: lennerd Date: Fri, 2 May 2014 17:03:26 +0200 Subject: [PATCH] Return new tags when posts are created via API closes #2680 - added include options when adding a post - added functional and integrational tests --- core/server/api/posts.js | 10 ++++-- core/test/functional/routes/api/posts_test.js | 32 ++++++++++++++++--- core/test/integration/api/api_posts_spec.js | 17 +++++++--- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/core/server/api/posts.js b/core/server/api/posts.js index faf4ef5366..ea486c996a 100644 --- a/core/server/api/posts.js +++ b/core/server/api/posts.js @@ -129,11 +129,17 @@ posts = { // #### Add // **takes:** a json object representing a post, add: function add(postData) { - var self = this; + var self = this, + include; + // **returns:** a promise for the resulting post in a json object return canThis(this.user).create.post().then(function () { return checkPostData(postData).then(function (checkedPostData) { - return dataProvider.Post.add(checkedPostData.posts[0], {user: self.user}); + if (postData.include) { + include = prepareInclude(postData.include); + } + + return dataProvider.Post.add(checkedPostData.posts[0], {user: self.user, include: include}); }).then(function (result) { var omitted = result.toJSON(); if (!_.isNumber(omitted.author)) { diff --git a/core/test/functional/routes/api/posts_test.js b/core/test/functional/routes/api/posts_test.js index 7b4b9c7e7f..a273b83eab 100644 --- a/core/test/functional/routes/api/posts_test.js +++ b/core/test/functional/routes/api/posts_test.js @@ -318,11 +318,12 @@ describe('Post API', function () { describe('Add', function () { it('can create a new draft, publish post, update post', function (done) { var newTitle = 'My Post', - changedTitle = 'My Post changed', + newTagName = 'My Tag', publishedState = 'published', - newPost = {posts: [{status: 'draft', title: newTitle, markdown: 'my post'}]}; + newTag = {id: null, name: newTagName}, + newPost = {posts: [{status: 'draft', title: newTitle, markdown: 'my post', tags: [newTag]}]}; - request.post(testUtils.API.getApiQuery('posts/')) + request.post(testUtils.API.getApiQuery('posts/?include=tags')) .set('X-CSRF-Token', csrfToken) .send(newPost) .expect(200) @@ -334,11 +335,17 @@ describe('Post API', function () { res.should.be.json; var draftPost = res.body; draftPost.posts.should.exist; + draftPost.posts.length.should.be.above(0); draftPost.posts[0].title.should.eql(newTitle); draftPost.posts[0].status = publishedState; testUtils.API.checkResponse(draftPost.posts[0], 'post'); - request.put(testUtils.API.getApiQuery('posts/' + draftPost.posts[0].id + '/')) + draftPost.posts[0].tags.should.exist; + draftPost.posts[0].tags.length.should.be.above(0); + draftPost.posts[0].tags[0].name.should.eql(newTagName); + testUtils.API.checkResponse(draftPost.posts[0].tags[0], 'tag'); + + request.put(testUtils.API.getApiQuery('posts/' + draftPost.posts[0].id + '/?include=tags')) .set('X-CSRF-Token', csrfToken) .send(draftPost) .expect(200) @@ -351,13 +358,20 @@ describe('Post API', function () { _.has(res.headers, 'x-cache-invalidate').should.equal(true); res.headers['x-cache-invalidate'].should.eql('/, /page/*, /rss/, /rss/*, /tag/*, /' + publishedPost.posts[0].slug + '/'); res.should.be.json; + publishedPost.should.exist; publishedPost.posts.should.exist; + publishedPost.posts.length.should.be.above(0); publishedPost.posts[0].title.should.eql(newTitle); publishedPost.posts[0].status.should.eql(publishedState); testUtils.API.checkResponse(publishedPost.posts[0], 'post'); - request.put(testUtils.API.getApiQuery('posts/' + publishedPost.posts[0].id + '/')) + publishedPost.posts[0].tags.should.exist; + publishedPost.posts[0].tags.length.should.be.above(0); + publishedPost.posts[0].tags[0].name.should.eql(newTagName); + testUtils.API.checkResponse(publishedPost.posts[0].tags[0], 'tag'); + + request.put(testUtils.API.getApiQuery('posts/' + publishedPost.posts[0].id + '/?include=tags')) .set('X-CSRF-Token', csrfToken) .send(publishedPost) .expect(200) @@ -369,10 +383,18 @@ describe('Post API', function () { var updatedPost = res.body; _.has(res.headers, 'x-cache-invalidate').should.equal(false); res.should.be.json; + updatedPost.should.exist; updatedPost.posts.should.exist; + updatedPost.posts.length.should.be.above(0); updatedPost.posts[0].title.should.eql(newTitle); testUtils.API.checkResponse(updatedPost.posts[0], 'post'); + + updatedPost.posts[0].tags.should.exist; + updatedPost.posts[0].tags.length.should.be.above(0); + updatedPost.posts[0].tags[0].name.should.eql(newTagName); + testUtils.API.checkResponse(updatedPost.posts[0].tags[0], 'tag'); + done(); }); }); diff --git a/core/test/integration/api/api_posts_spec.js b/core/test/integration/api/api_posts_spec.js index a332adfd90..28eb2acefb 100644 --- a/core/test/integration/api/api_posts_spec.js +++ b/core/test/integration/api/api_posts_spec.js @@ -30,10 +30,10 @@ describe('Post API', function () { }, done); }); - it('browse', function (done) { + it('can browse', function (done) { PostAPI.browse().then(function (results) { should.exist(results); - testUtils.API.checkResponse(results, 'posts'); + testUtils.API.checkResponse(results, 'posts'); should.exist(results.posts); results.posts.length.should.be.above(0); testUtils.API.checkResponse(results.posts[0], 'post'); @@ -41,7 +41,7 @@ describe('Post API', function () { }).then(null, done); }); - it('read', function (done) { + it('can read', function (done) { var firstPost; PostAPI.browse().then(function (results) { @@ -49,10 +49,19 @@ describe('Post API', function () { should.exist(results.posts); results.posts.length.should.be.above(0); firstPost = results.posts[0]; - return PostAPI.read({slug: firstPost.slug}); + return PostAPI.read({slug: firstPost.slug, include: 'tags'}); }).then(function (found) { + var post; + should.exist(found); testUtils.API.checkResponse(found.posts[0], 'post'); + + post = found.posts[0]; + + should.exist(post.tags); + post.tags.length.should.be.above(0); + testUtils.API.checkResponse(post.tags[0], 'tag'); + done(); }).then(null, done); });