Updated canary api to handle type column correctly (#11006)

refs #10922

* Updated canary api to handle type column correctly

* Updated unit tests

* Updated regression tests
This commit is contained in:
Fabien O'Carroll 2019-08-12 20:09:59 +08:00 committed by Rishabh Garg
parent 730e307d18
commit c3e8ba0523
8 changed files with 71 additions and 34 deletions

View File

@ -1,9 +1,24 @@
const _ = require('lodash');
const debug = require('ghost-ignition').debug('api:canary:utils:serializers:input:pages');
const mapNQLKeyValues = require('nql-map-key-values');
const converters = require('../../../../../lib/mobiledoc/converters');
const url = require('./utils/url');
const localUtils = require('../../index');
const replacePageWithType = mapNQLKeyValues({
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});
function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {
frame.options.formats = frame.options.formats.filter((format) => {
@ -55,9 +70,9 @@ function defaultFormat(frame) {
*/
const forcePageFilter = (frame) => {
if (frame.options.filter) {
frame.options.filter = `(${frame.options.filter})+page:true`;
frame.options.filter = `(${frame.options.filter})+type:page`;
} else {
frame.options.filter = 'page:true';
frame.options.filter = 'type:page';
}
};
@ -86,6 +101,8 @@ module.exports = {
defaultRelations(frame);
}
frame.options.mongoTransformer = replacePageWithType;
debug(frame.options);
},
@ -123,7 +140,7 @@ module.exports = {
// @NOTE: force storing page
if (options.add) {
frame.data.pages[0].page = true;
frame.data.pages[0].type = 'page';
}
// CASE: Transform short to long format
@ -163,7 +180,7 @@ module.exports = {
destroy(apiConfig, frame) {
frame.options.destroyBy = {
id: frame.options.id,
page: true
type: 'page'
};
defaultFormat(frame);

View File

@ -1,10 +1,25 @@
const _ = require('lodash');
const debug = require('ghost-ignition').debug('api:canary:utils:serializers:input:posts');
const mapNQLKeyValues = require('nql-map-key-values');
const url = require('./utils/url');
const localUtils = require('../../index');
const labs = require('../../../../../services/labs');
const converters = require('../../../../../lib/mobiledoc/converters');
const replacePageWithType = mapNQLKeyValues({
key: {
from: 'page',
to: 'type'
},
values: [{
from: false,
to: 'post'
}, {
from: true,
to: 'page'
}]
});
function removeMobiledocFormat(frame) {
if (frame.options.formats && frame.options.formats.includes('mobiledoc')) {
frame.options.formats = frame.options.formats.filter((format) => {
@ -64,9 +79,9 @@ function defaultFormat(frame) {
*/
const forcePageFilter = (frame) => {
if (frame.options.filter) {
frame.options.filter = `(${frame.options.filter})+page:false`;
frame.options.filter = `(${frame.options.filter})+type:post`;
} else {
frame.options.filter = 'page:false';
frame.options.filter = 'type:post';
}
};
@ -108,6 +123,8 @@ module.exports = {
defaultRelations(frame);
}
frame.options.mongoTransformer = replacePageWithType;
debug(frame.options);
},
@ -158,7 +175,7 @@ module.exports = {
// @NOTE: force adding post
if (options.add) {
frame.data.posts[0].page = false;
frame.data.posts[0].type = 'post';
}
// CASE: Transform short to long format
@ -196,7 +213,7 @@ module.exports = {
destroy(apiConfig, frame) {
frame.options.destroyBy = {
id: frame.options.id,
page: false
type: 'post'
};
defaultFormat(frame);

View File

@ -57,6 +57,8 @@ const mapPost = (model, frame) => {
});
}
delete jsonModel.type;
return jsonModel;
};

View File

@ -80,7 +80,7 @@ describe('Schedules API', function () {
published_at: moment().add(30, 'seconds').toDate(),
status: 'scheduled',
slug: 'fifth',
page: true
type: 'page'
}));
return Promise.mapSeries(resources, function (post) {

View File

@ -25,6 +25,7 @@ const expectedProperties = {
.without('status')
// @TODO: https://github.com/TryGhost/Ghost/issues/10335
// .without('page')
.without('type')
// canary returns a calculated excerpt field
.concat('excerpt')
,

View File

@ -3659,7 +3659,7 @@ describe('Integration - Web - Site', function () {
response.statusCode.should.eql(200);
response.template.should.eql('tag');
postSpy.args[0][0].options.filter.should.eql('(tags:\'bacon\'+tags.visibility:public)+page:false');
postSpy.args[0][0].options.filter.should.eql('(tags:\'bacon\'+tags.visibility:public)+type:post');
postSpy.args[0][0].options.page.should.eql(1);
postSpy.args[0][0].options.limit.should.eql(2);
});

View File

@ -13,7 +13,7 @@ describe('Unit: canary/utils/serializers/input/pages', function () {
};
serializers.input.pages.browse(apiConfig, frame);
frame.options.filter.should.eql('page:true');
frame.options.filter.should.eql('type:page');
});
it('combine filters', function () {
@ -27,7 +27,7 @@ describe('Unit: canary/utils/serializers/input/pages', function () {
};
serializers.input.pages.browse(apiConfig, frame);
frame.options.filter.should.eql('(status:published+tag:eins)+page:true');
frame.options.filter.should.eql('(status:published+tag:eins)+type:page');
});
it('combine filters', function () {
@ -41,7 +41,7 @@ describe('Unit: canary/utils/serializers/input/pages', function () {
};
serializers.input.pages.browse(apiConfig, frame);
frame.options.filter.should.eql('(page:false+tag:eins)+page:true');
frame.options.filter.should.eql('(page:false+tag:eins)+type:page');
});
it('combine filters', function () {
@ -55,7 +55,7 @@ describe('Unit: canary/utils/serializers/input/pages', function () {
};
serializers.input.pages.browse(apiConfig, frame);
frame.options.filter.should.eql('(page:false)+page:true');
frame.options.filter.should.eql('(page:false)+type:page');
});
it('remove mobiledoc option from formats', function () {
@ -87,7 +87,7 @@ describe('Unit: canary/utils/serializers/input/pages', function () {
};
serializers.input.pages.read(apiConfig, frame);
frame.options.filter.should.eql('page:true');
frame.options.filter.should.eql('type:page');
});
it('content api default', function () {
@ -107,7 +107,7 @@ describe('Unit: canary/utils/serializers/input/pages', function () {
};
serializers.input.pages.read(apiConfig, frame);
frame.options.filter.should.eql('page:true');
frame.options.filter.should.eql('type:page');
});
it('admin api default', function () {
@ -127,7 +127,7 @@ describe('Unit: canary/utils/serializers/input/pages', function () {
};
serializers.input.pages.read(apiConfig, frame);
frame.options.filter.should.eql('(page:true)+status:[draft,published,scheduled]');
frame.options.filter.should.eql('(type:page)+status:[draft,published,scheduled]');
});
it('custom page filter', function () {
@ -142,7 +142,7 @@ describe('Unit: canary/utils/serializers/input/pages', function () {
};
serializers.input.pages.read(apiConfig, frame);
frame.options.filter.should.eql('(page:false)+page:true');
frame.options.filter.should.eql('(page:false)+type:page');
});
it('custom status filter', function () {
@ -163,7 +163,7 @@ describe('Unit: canary/utils/serializers/input/pages', function () {
};
serializers.input.pages.read(apiConfig, frame);
frame.options.filter.should.eql('(status:draft)+page:true');
frame.options.filter.should.eql('(status:draft)+type:page');
});
it('remove mobiledoc option from formats', function () {

View File

@ -21,7 +21,7 @@ describe('Unit: canary/utils/serializers/input/posts', function () {
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.filter.should.eql('page:false');
frame.options.filter.should.eql('type:post');
});
it('should not work for non public context', function () {
@ -36,7 +36,7 @@ describe('Unit: canary/utils/serializers/input/posts', function () {
};
serializers.input.posts.browse(apiConfig, frame);
should.equal(frame.options.filter, '(page:false)+status:[draft,published,scheduled]');
should.equal(frame.options.filter, '(type:post)+status:[draft,published,scheduled]');
});
it('combine filters', function () {
@ -56,7 +56,7 @@ describe('Unit: canary/utils/serializers/input/posts', function () {
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.filter.should.eql('(status:published+tag:eins)+page:false');
frame.options.filter.should.eql('(status:published+tag:eins)+type:post');
});
it('combine filters', function () {
@ -76,7 +76,7 @@ describe('Unit: canary/utils/serializers/input/posts', function () {
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.filter.should.eql('(page:true+tag:eins)+page:false');
frame.options.filter.should.eql('(page:true+tag:eins)+type:post');
});
it('combine filters', function () {
@ -96,7 +96,7 @@ describe('Unit: canary/utils/serializers/input/posts', function () {
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.filter.should.eql('(page:true)+page:false');
frame.options.filter.should.eql('(page:true)+type:post');
});
it('combine filters', function () {
@ -116,7 +116,7 @@ describe('Unit: canary/utils/serializers/input/posts', function () {
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.filter.should.eql('((page:true,page:false))+page:false');
frame.options.filter.should.eql('((page:true,page:false))+type:post');
});
it('remove mobiledoc option from formats', function () {
@ -137,7 +137,7 @@ describe('Unit: canary/utils/serializers/input/posts', function () {
});
describe('read', function () {
it('with apiType of "content" it forces page filter', function () {
it('with apiType of "content" it forces type filter', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
@ -146,24 +146,24 @@ describe('Unit: canary/utils/serializers/input/posts', function () {
};
serializers.input.posts.read(apiConfig, frame);
frame.options.filter.should.eql('page:false');
frame.options.filter.should.eql('type:post');
});
it('with apiType of "content" it forces page false filter', function () {
it('with apiType of "content" it forces type:post filter', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
options: {
filter: 'page:true'
filter: 'type:page'
},
data: {}
};
serializers.input.posts.read(apiConfig, frame);
frame.options.filter.should.eql('(page:true)+page:false');
frame.options.filter.should.eql('(type:page)+type:post');
});
it('with apiType of "admin" it forces page & status false filter', function () {
it('with apiType of "admin" it forces type & status false filter', function () {
const apiConfig = {};
const frame = {
apiType: 'admin',
@ -179,10 +179,10 @@ describe('Unit: canary/utils/serializers/input/posts', function () {
};
serializers.input.posts.read(apiConfig, frame);
frame.options.filter.should.eql('(page:false)+status:[draft,published,scheduled]');
frame.options.filter.should.eql('(type:post)+status:[draft,published,scheduled]');
});
it('with apiType of "admin" it forces page filter & respects custom status filter', function () {
it('with apiType of "admin" it forces type:post filter & respects custom status filter', function () {
const apiConfig = {};
const frame = {
apiType: 'admin',
@ -199,7 +199,7 @@ describe('Unit: canary/utils/serializers/input/posts', function () {
};
serializers.input.posts.read(apiConfig, frame);
frame.options.filter.should.eql('(status:draft)+page:false');
frame.options.filter.should.eql('(status:draft)+type:post');
});
it('remove mobiledoc option from formats', function () {