0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-01-20 22:42:53 -05:00

Added label to known filter params for members

no issue

- updated `filter` plugin with appropriate label relationship and replacement config for NQL
- fleshed out member and label fixture data and tools to facilitate tests
This commit is contained in:
Kevin Ansfield 2020-05-21 18:44:35 +01:00
parent 225544331c
commit 21d38b2d37
5 changed files with 143 additions and 31 deletions

View file

@ -16,6 +16,13 @@ const RELATIONS = {
joinTable: 'posts_authors',
joinFrom: 'post_id',
joinTo: 'author_id'
},
labels: {
tableName: 'labels',
type: 'manyToMany',
joinTable: 'members_labels',
joinFrom: 'member_id',
joinTo: 'label_id'
}
};
@ -39,6 +46,12 @@ const EXPANSIONS = [{
}, {
key: 'tags',
replacement: 'tags.slug'
}, {
key: 'label',
replacement: 'labels.slug'
}, {
key: 'labels',
replacement: 'labels.slug'
}];
const filter = function filter(Bookshelf) {

View file

@ -26,7 +26,7 @@ describe('Members API', function () {
request = supertest.agent(config.get('url'));
})
.then(function () {
return localUtils.doAuth(request, 'member');
return localUtils.doAuth(request, 'members');
});
});
@ -42,7 +42,7 @@ describe('Members API', function () {
const jsonResponse = res.body;
should.exist(jsonResponse);
should.exist(jsonResponse.members);
jsonResponse.members.should.have.length(1);
jsonResponse.members.should.have.length(2);
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');
testUtils.API.isISO8601(jsonResponse.members[0].created_at).should.be.true();
@ -51,12 +51,31 @@ describe('Members API', function () {
jsonResponse.meta.pagination.should.have.property('page', 1);
jsonResponse.meta.pagination.should.have.property('limit', 15);
jsonResponse.meta.pagination.should.have.property('pages', 1);
jsonResponse.meta.pagination.should.have.property('total', 1);
jsonResponse.meta.pagination.should.have.property('total', 2);
jsonResponse.meta.pagination.should.have.property('next', null);
jsonResponse.meta.pagination.should.have.property('prev', null);
});
});
it('Can browse with filter', function () {
return request
.get(localUtils.API.getApiQuery('members/?filter=label:label-1'))
.set('Origin', config.get('url'))
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200)
.then((res) => {
should.not.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
should.exist(jsonResponse);
should.exist(jsonResponse.members);
jsonResponse.members.should.have.length(1);
localUtils.API.checkResponse(jsonResponse, 'members');
localUtils.API.checkResponse(jsonResponse.members[0], 'member', 'stripe');
localUtils.API.checkResponse(jsonResponse.meta.pagination, 'pagination');
});
});
it('Can read', function () {
return request
.get(localUtils.API.getApiQuery(`members/${testUtils.DataGenerator.Content.members[0].id}/`))

View file

@ -19,6 +19,7 @@ const expectedProperties = {
invites: ['invites', 'meta'],
themes: ['themes'],
actions: ['actions', 'meta'],
members: ['members', 'meta'],
action: ['id', 'resource_type', 'actor_type', 'event', 'created_at', 'actor'],

View file

@ -319,6 +319,19 @@ DataGenerator.Content = {
}
],
labels: [
{
id: ObjectId.generate(),
name: 'Label 1',
slug: 'label-1'
},
{
id: ObjectId.generate(),
name: 'Label 2',
slug: 'label-2'
}
],
webhooks: [
{
id: ObjectId.generate(),
@ -574,6 +587,29 @@ DataGenerator.forKnex = (function () {
});
}
function createLabel(overrides) {
const newObj = _.cloneDeep(overrides);
return _.defaults(newObj, {
id: ObjectId.generate(),
name: 'label',
slug: 'slug',
created_by: DataGenerator.Content.users[0].id,
created_at: new Date(),
updated_by: DataGenerator.Content.users[0].id,
updated_at: new Date()
});
}
function createMembersLabels(member_id, label_id, sort_order = 0) {
return {
id: ObjectId.generate(),
member_id,
label_id,
sort_order
};
}
function createSetting(overrides) {
const newObj = _.cloneDeep(overrides);
@ -837,38 +873,61 @@ DataGenerator.forKnex = (function () {
createBasic(DataGenerator.Content.emails[1])
];
const members = [
createMember(DataGenerator.Content.members[0]),
createMember(DataGenerator.Content.members[1])
];
const labels = [
createLabel(DataGenerator.Content.labels[0])
];
const members_labels = [
{
id: ObjectId.generate(),
member_id: DataGenerator.Content.members[0].id,
label_id: DataGenerator.Content.labels[0].id,
sort_order: 0
}
];
return {
createPost: createPost,
createGenericPost: createGenericPost,
createTag: createTag,
createUser: createUser,
createUsersRoles: createUsersRoles,
createPostsAuthors: createPostsAuthors,
createClient: createClient,
createGenericUser: createGenericUser,
createBasic: createBasic,
createPost,
createGenericPost,
createTag,
createUser,
createUsersRoles,
createPostsAuthors,
createClient,
createGenericUser,
createBasic,
createRole: createBasic,
createPermission: createBasic,
createPostsTags: createPostsTags,
createSetting: createSetting,
createToken: createToken,
createMember: createMember,
createInvite: createInvite,
createWebhook: createWebhook,
createIntegration: createIntegration,
createPostsTags,
createSetting,
createToken,
createMember,
createLabel,
createMembersLabels,
createInvite,
createWebhook,
createIntegration,
invites: invites,
posts: posts,
tags: tags,
posts_tags: posts_tags,
posts_authors: posts_authors,
roles: roles,
users: users,
roles_users: roles_users,
webhooks: webhooks,
integrations: integrations,
api_keys: api_keys,
emails: emails
invites,
posts,
tags,
posts_tags,
posts_authors,
roles,
users,
roles_users,
webhooks,
integrations,
api_keys,
emails,
labels,
members,
members_labels
};
}());

View file

@ -469,6 +469,23 @@ fixtures = {
return Promise.map(DataGenerator.forKnex.emails, function (email) {
return models.Email.add(email, module.exports.context.internal);
});
},
insertMembersAndLabels: function insertMembersAndLabels() {
return Promise.map(DataGenerator.forKnex.labels, function (label) {
return models.Label.add(label, module.exports.context.internal);
}).then(function () {
return Promise.each(_.cloneDeep(DataGenerator.forKnex.members), function (member) {
let memberLabelRelations = _.filter(DataGenerator.forKnex.members_labels, {member_id: member.id});
memberLabelRelations = _.map(memberLabelRelations, function (memberLabelRelation) {
return _.find(DataGenerator.forKnex.labels, {id: memberLabelRelation.label_id});
});
member.labels = memberLabelRelations;
return models.Member.add(member, module.exports.context.internal);
});
});
}
};
@ -537,6 +554,9 @@ toDoList = {
member: function insertMember() {
return fixtures.insertOne('Member', 'members', 'createMember');
},
members: function insertMembersAndLabels() {
return fixtures.insertMembersAndLabels();
},
posts: function insertPostsAndTags() {
return fixtures.insertPostsAndTags();
},