0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-10 23:36:14 -05:00
ghost/test/unit/api/canary/utils/serializers/input/posts.test.js

432 lines
15 KiB
JavaScript
Raw Normal View History

const should = require('should');
const sinon = require('sinon');
const serializers = require('../../../../../../../core/server/api/canary/utils/serializers');
const urlUtils = require('../../../../../../utils/urlUtils');
describe('Unit: canary/utils/serializers/input/posts', function () {
describe('browse', function () {
it('default', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
options: {
context: {
user: 0,
api_key: {
id: 1,
type: 'content'
}
}
}
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.filter.should.eql('type:post');
});
it('should not work for non public context', function () {
const apiConfig = {};
const frame = {
apiType: 'admin',
options: {
context: {
user: 1
}
}
};
serializers.input.posts.browse(apiConfig, frame);
should.equal(frame.options.filter, '(type:post)+status:[draft,published,scheduled]');
});
it('combine filters', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
options: {
context: {
user: 0,
api_key: {
id: 1,
type: 'content'
}
},
filter: 'status:published+tag:eins'
}
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.filter.should.eql('(status:published+tag:eins)+type:post');
});
it('combine filters', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
options: {
context: {
user: 0,
api_key: {
id: 1,
type: 'content'
}
},
filter: 'page:true+tag:eins'
}
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.filter.should.eql('(page:true+tag:eins)+type:post');
});
it('combine filters', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
options: {
context: {
user: 0,
api_key: {
id: 1,
type: 'content'
}
},
filter: 'page:true'
}
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.filter.should.eql('(page:true)+type:post');
});
it('combine filters', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
options: {
context: {
user: 0,
api_key: {
id: 1,
type: 'content'
}
},
filter: '(page:true,page:false)'
}
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.filter.should.eql('((page:true,page:false))+type:post');
});
it('remove mobiledoc option from formats', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
options: {
formats: ['html', 'mobiledoc', 'plaintext'],
context: {}
}
};
serializers.input.posts.browse(apiConfig, frame);
frame.options.formats.should.not.containEql('mobiledoc');
frame.options.formats.should.containEql('html');
frame.options.formats.should.containEql('plaintext');
});
});
describe('read', function () {
it('with apiType of "content" it forces type filter', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
options: {},
data: {}
};
serializers.input.posts.read(apiConfig, frame);
frame.options.filter.should.eql('type:post');
});
it('with apiType of "content" it forces type:post filter', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
options: {
filter: 'type:page'
},
data: {}
};
serializers.input.posts.read(apiConfig, frame);
frame.options.filter.should.eql('(type:page)+type:post');
});
it('with apiType of "admin" it forces type & status false filter', function () {
const apiConfig = {};
const frame = {
apiType: 'admin',
options: {
context: {
api_key: {
id: 1,
type: 'admin'
}
}
},
data: {}
};
serializers.input.posts.read(apiConfig, frame);
frame.options.filter.should.eql('(type:post)+status:[draft,published,scheduled]');
});
it('with apiType of "admin" it forces type:post filter & respects custom status filter', function () {
const apiConfig = {};
const frame = {
apiType: 'admin',
options: {
context: {
api_key: {
id: 1,
type: 'admin'
}
},
filter: 'status:draft'
},
data: {}
};
serializers.input.posts.read(apiConfig, frame);
frame.options.filter.should.eql('(status:draft)+type:post');
});
it('remove mobiledoc option from formats', function () {
const apiConfig = {};
const frame = {
apiType: 'content',
options: {
formats: ['html', 'mobiledoc', 'plaintext'],
context: {}
},
data: {}
};
serializers.input.posts.read(apiConfig, frame);
frame.options.formats.should.not.containEql('mobiledoc');
frame.options.formats.should.containEql('html');
frame.options.formats.should.containEql('plaintext');
});
});
describe('edit', function () {
describe('Ensure html to mobiledoc conversion', function () {
it('no transformation when no html source option provided', function () {
const apiConfig = {};
const mobiledoc = '{"version":"0.3.1","atoms":[],"cards":[],"sections":[]}';
const frame = {
options: {},
data: {
posts: [
{
id: 'id1',
html: '<p>convert me</p>',
mobiledoc: mobiledoc
}
]
}
};
serializers.input.posts.edit(apiConfig, frame);
let postData = frame.data.posts[0];
postData.mobiledoc.should.equal(mobiledoc);
});
it('no transformation when html data is empty', function () {
const apiConfig = {};
const mobiledoc = '{"version":"0.3.1","atoms":[],"cards":[],"sections":[]}';
const frame = {
options: {
source: 'html'
},
data: {
posts: [
{
id: 'id1',
html: '',
mobiledoc: mobiledoc
}
]
}
};
serializers.input.posts.edit(apiConfig, frame);
let postData = frame.data.posts[0];
postData.mobiledoc.should.equal(mobiledoc);
});
it('transforms html when html is present in data and source options', function () {
const apiConfig = {};
const mobiledoc = '{"version":"0.3.1","atoms":[],"cards":[],"sections":[]}';
const frame = {
options: {
source: 'html'
},
data: {
posts: [
{
id: 'id1',
html: '<p>this is great feature</p>',
mobiledoc: mobiledoc
}
]
}
};
serializers.input.posts.edit(apiConfig, frame);
let postData = frame.data.posts[0];
postData.mobiledoc.should.not.equal(mobiledoc);
postData.mobiledoc.should.equal('{"version":"0.3.1","atoms":[],"cards":[],"markups":[],"sections":[[1,"p",[[0,[],0,"this is great feature"]]]]}');
});
it('preserves html cards in transformed html', function () {
const apiConfig = {};
const frame = {
options: {
source: 'html'
},
data: {
posts: [
{
id: 'id1',
html: '<p>this is great feature</p>\n<!--kg-card-begin: html--><div class="custom">My Custom HTML</div><!--kg-card-end: html-->\n<p>custom html preserved!</p>'
}
]
}
};
serializers.input.posts.edit(apiConfig, frame);
let postData = frame.data.posts[0];
postData.mobiledoc.should.equal('{"version":"0.3.1","atoms":[],"cards":[["html",{"html":"<div class=\\"custom\\">My Custom HTML</div>"}]],"markups":[],"sections":[[1,"p",[[0,[],0,"this is great feature"]]],[10,0],[1,"p",[[0,[],0,"custom html preserved!"]]]]}');
});
});
describe('Ensure relations format', function () {
it('relations is array of objects', function () {
const apiConfig = {};
const frame = {
options: {},
data: {
posts: [
{
id: 'id1',
authors: [{id: 'id'}],
tags: [{slug: 'slug1', name: 'hey'}, {slug: 'slug2'}]
}
]
}
};
serializers.input.posts.edit(apiConfig, frame);
frame.data.posts[0].authors.should.eql([{id: 'id'}]);
frame.data.posts[0].tags.should.eql([{slug: 'slug1', name: 'hey'}, {slug: 'slug2'}]);
});
it('authors is array of strings', function () {
const apiConfig = {};
const frame = {
options: {},
data: {
posts: [
{
id: 'id1',
authors: ['email1', 'email2'],
tags: ['name1', 'name2']
}
]
}
};
serializers.input.posts.edit(apiConfig, frame);
frame.data.posts[0].authors.should.eql([{email: 'email1'}, {email: 'email2'}]);
frame.data.posts[0].tags.should.eql([{name: 'name1'}, {name: 'name2'}]);
});
});
✨ Added ability to send a newsletter to members with a certain label or product (#12932) refs https://github.com/TryGhost/Team/issues/581 refs https://github.com/TryGhost/Team/issues/582 When publishing a post via the API it was possible to send it using `?email_recipient_filter=all/free/paid` which allowed you to send to members only based on their payment status which is quite limiting for some sites. This PR updates the `?email_recipient_filter` query param to support Ghost's `?filter` param syntax which enables more specific recipient lists, eg: `?email_recipient_filter=status:free` = free members only `?email_recipient_filter=status:paid` = paid members only `?email_recipient_filter=label:vip` = members that have the `vip` label attached `?email_recipient_filter=status:paid,label:vip` = paid members and members that have the `vip` label attached The older `free/paid` values are still supported by the API for backwards compatibility. - updates `Post` and `Email` models to transform legacy `free` and `paid` values to their NQL equivalents on read/write - lets us not worry about supporting legacy values elsewhere in the code - cleanup migration to transform all rows slated for 5.0 - removes schema and API `isIn` validations for recipient filters so allow free-form filters - updates posts API input serializers to transform `free` and `paid` values in the `?email_recipient_filter` param to their NQL equivalents for backwards compatibility - updates Post API controllers `edit` methods to run a query using the supplied filter to verify that it's valid - updates `mega` service to use the filter directly when selecting recipients
2021-05-07 11:56:41 +01:00
describe('transforms legacy email recipient filter values', function () {
it('free becomes status:free', function () {
const frame = {
options: {
email_recipient_filter: 'free'
},
data: {
posts: [{id: '1'}]
}
};
serializers.input.posts.edit({}, frame);
frame.options.email_recipient_filter.should.eql('status:free');
});
it('paid becomes status:-free', function () {
const frame = {
options: {
email_recipient_filter: 'paid'
},
data: {
posts: [{id: '1'}]
}
};
serializers.input.posts.edit({}, frame);
frame.options.email_recipient_filter.should.eql('status:-free');
});
});
});
describe('add', function () {
describe('transforms legacy email recipient filter values', function () {
it('free becomes status:free', function () {
const frame = {
options: {
email_recipient_filter: 'free'
},
data: {
posts: [{id: '1'}]
}
};
serializers.input.posts.add({}, frame);
frame.options.email_recipient_filter.should.eql('status:free');
});
it('paid becomes status:-free', function () {
const frame = {
options: {
email_recipient_filter: 'paid'
},
data: {
posts: [{id: '1'}]
}
};
serializers.input.posts.add({}, frame);
frame.options.email_recipient_filter.should.eql('status:-free');
});
});
});
});