From 62c1ce128e1c0d61b5e228ebb893740028f50735 Mon Sep 17 00:00:00 2001 From: Sebastian Gierlinger Date: Fri, 18 Jul 2014 10:48:48 +0200 Subject: [PATCH] Fix editing author no issue - author_id is converted to author for API responses but was never converted back for requests --- core/server/api/utils.js | 9 +++++++++ core/test/functional/routes/api/posts_test.js | 9 ++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/core/server/api/utils.js b/core/server/api/utils.js index 6c2ee9603d..d31cdae9f4 100644 --- a/core/server/api/utils.js +++ b/core/server/api/utils.js @@ -18,6 +18,15 @@ utils = { if (_.isEmpty(object) || _.isEmpty(object[docName]) || _.isEmpty(object[docName][0])) { return when.reject(new errors.BadRequestError('No root key (\'' + docName + '\') provided.')); } + + // convert author property to author_id to match the name in the database + // TODO: rename object in database + if (docName === 'posts') { + if (object.posts[0].hasOwnProperty('author')) { + object.posts[0].author_id = object.posts[0].author; + delete object.posts[0].author; + } + } return when.resolve(object); } }; diff --git a/core/test/functional/routes/api/posts_test.js b/core/test/functional/routes/api/posts_test.js index 904e68a46a..7b0bcdf563 100644 --- a/core/test/functional/routes/api/posts_test.js +++ b/core/test/functional/routes/api/posts_test.js @@ -438,9 +438,11 @@ describe('Post API', function () { } var jsonResponse = res.body, - changedValue = 'My new Title'; + changedTitle = 'My new Title', + changedAuthor = 2; jsonResponse.posts[0].should.exist; - jsonResponse.posts[0].title = changedValue; + jsonResponse.posts[0].title = changedTitle; + jsonResponse.posts[0].author = changedAuthor; request.put(testUtils.API.getApiQuery('posts/1/')) .set('Authorization', 'Bearer ' + accesstoken) @@ -455,7 +457,8 @@ describe('Post API', function () { var putBody = res.body; _.has(res.headers, 'x-cache-invalidate').should.equal(true); putBody.should.exist; - putBody.posts[0].title.should.eql(changedValue); + putBody.posts[0].title.should.eql(changedTitle); + putBody.posts[0].author.should.eql(changedAuthor); testUtils.API.checkResponse(putBody.posts[0], 'post'); done();