Added the newsletters endpoint

refs https://github.com/TryGhost/Team/issues/1463

- This enables listing, creating and editing newsletters
- The tests are commented out as the permissions will be added in a follow-up commit
This commit is contained in:
Thibaut Patel 2022-03-28 17:24:51 +02:00 committed by Thibaut Patel
parent bf7d141407
commit 9e4401d9f6
6 changed files with 350 additions and 0 deletions

View File

@ -177,6 +177,10 @@ module.exports = {
return require('./utils/serializers');
},
get newsletters() {
return shared.pipeline(require('./newsletters'), localUtils);
},
/**
* Content API Controllers
*

View File

@ -0,0 +1,45 @@
const models = require('../../models');
module.exports = {
docName: 'newsletters',
browse: {
options: [
'filter',
'fields',
'limit',
'order',
'page'
],
permissions: true,
query(frame) {
return models.Newsletter.findPage(frame.options);
}
},
add: {
statusCode: 201,
permissions: true,
async query(frame) {
return models.Newsletter.add(frame.data.newsletters[0], frame.options);
}
},
edit: {
headers: {},
options: [
'id'
],
validation: {
options: {
id: {
required: true
}
}
},
permissions: true,
async query(frame) {
return models.Newsletter.edit(frame.data.newsletters[0], frame.options);
}
}
};

View File

@ -0,0 +1,9 @@
const ghostBookshelf = require('./base');
const Newsletter = ghostBookshelf.Model.extend({
tableName: 'newsletters'
});
module.exports = {
Newsletter: ghostBookshelf.model('Newsletter', Newsletter)
};

View File

@ -308,5 +308,9 @@ module.exports = function apiRoutes() {
router.get('/custom_theme_settings', mw.authAdminApi, http(api.customThemeSettings.browse));
router.put('/custom_theme_settings', mw.authAdminApi, http(api.customThemeSettings.edit));
router.get('/newsletters', mw.authAdminApi, http(api.newsletters.browse));
router.post('/newsletters', mw.authAdminApi, http(api.newsletters.add));
router.put('/newsletters/:id', mw.authAdminApi, http(api.newsletters.edit));
return router;
};

View File

@ -0,0 +1,192 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Newsletters API Can add a newsletter 1: [body] 1`] = `
Object {
"newsletters": Array [
Object {
"default": false,
"description": null,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"name": "My test newsletter",
"recipient_filter": "",
"sender_email": "test@example.com",
"sender_name": "Test",
"sender_reply_to": "test@example.com",
"sort_order": 0,
"status": "active",
"subscribe_on_signup": true,
},
],
}
`;
exports[`Newsletters API Can add a newsletter 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "288",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"location": Any<String>,
"vary": "Origin, Accept-Encoding",
"x-powered-by": "Express",
}
`;
exports[`Newsletters API Can add a newsletter 3: [body] 1`] = `
Object {
"meta": Object {
"pagination": Object {
"limit": 15,
"next": null,
"page": 1,
"pages": 1,
"prev": null,
"total": 1,
},
},
"newsletters": Array [
Object {
"default": false,
"description": null,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"name": "My test newsletter",
"recipient_filter": "",
"sender_email": "test@example.com",
"sender_name": "Test",
"sender_reply_to": "test@example.com",
"sort_order": 0,
"status": "active",
"subscribe_on_signup": true,
},
],
}
`;
exports[`Newsletters API Can add a newsletter 4: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "376",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Origin, Accept-Encoding",
"x-powered-by": "Express",
}
`;
exports[`Newsletters API Can browse newsletters 1: [body] 1`] = `
Object {
"meta": Object {
"pagination": Object {
"limit": 15,
"next": null,
"page": 1,
"pages": 1,
"prev": null,
"total": 1,
},
},
"newsletters": Array [
Object {
"default": false,
"description": null,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"name": "My test newsletter",
"recipient_filter": "",
"sender_email": "test@example.com",
"sender_name": "Test",
"sender_reply_to": "test@example.com",
"sort_order": 0,
"status": "active",
"subscribe_on_signup": true,
},
],
}
`;
exports[`Newsletters API Can browse newsletters 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "376",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Origin, Accept-Encoding",
"x-powered-by": "Express",
}
`;
exports[`Newsletters API Can edit newsletters 1: [body] 1`] = `
Object {
"meta": Object {
"pagination": Object {
"limit": 1,
"next": null,
"page": 1,
"pages": 1,
"prev": null,
"total": 1,
},
},
"newsletters": Array [
Object {
"default": false,
"description": null,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"name": "My test newsletter",
"recipient_filter": "",
"sender_email": "test@example.com",
"sender_name": "Test",
"sender_reply_to": "test@example.com",
"sort_order": 0,
"status": "active",
"subscribe_on_signup": true,
},
],
}
`;
exports[`Newsletters API Can edit newsletters 2: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "375",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Origin, Accept-Encoding",
"x-powered-by": "Express",
}
`;
exports[`Newsletters API Can edit newsletters 3: [body] 1`] = `
Object {
"newsletters": Array [
Object {
"default": false,
"description": null,
"id": StringMatching /\\[a-f0-9\\]\\{24\\}/,
"name": "Updated newsletter name",
"recipient_filter": "",
"sender_email": "test@example.com",
"sender_name": "Test",
"sender_reply_to": "test@example.com",
"sort_order": 0,
"status": "active",
"subscribe_on_signup": true,
},
],
}
`;
exports[`Newsletters API Can edit newsletters 4: [headers] 1`] = `
Object {
"access-control-allow-origin": "http://127.0.0.1:2369",
"cache-control": "no-cache, private, no-store, must-revalidate, max-stale=0, post-check=0, pre-check=0",
"content-length": "293",
"content-type": "application/json; charset=utf-8",
"etag": StringMatching /\\(\\?:W\\\\/\\)\\?"\\(\\?:\\[ !#-\\\\x7E\\\\x80-\\\\xFF\\]\\*\\|\\\\r\\\\n\\[\\\\t \\]\\|\\\\\\\\\\.\\)\\*"/,
"vary": "Origin, Accept-Encoding",
"x-powered-by": "Express",
}
`;

View File

@ -0,0 +1,96 @@
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
const {anyEtag, anyObjectId, anyUuid, anyISODateTime, anyISODate, anyString, anyArray, anyLocationFor, anyErrorId} = matchers;
const newsletterSnapshot = {
id: anyObjectId
};
let agent;
describe('Newsletters API', function () {
before(async function () {
agent = await agentProvider.getAdminAPIAgent();
await fixtureManager.init();
await agent.loginAsOwner();
});
beforeEach(function () {
});
afterEach(function () {
mockManager.restore();
});
// it('Can add a newsletter', async function () {
// const newsletter = {
// name: 'My test newsletter',
// sender_name: 'Test',
// sender_email: 'test@example.com',
// sender_reply_to: 'test@example.com',
// default: false,
// status: 'active',
// recipient_filter: '',
// subscribe_on_signup: true,
// sort_order: 0
// };
// await agent
// .post(`newsletters/`)
// .body({newsletters: [newsletter]})
// .expectStatus(201)
// .matchBodySnapshot({
// newsletters: [newsletterSnapshot]
// })
// .matchHeaderSnapshot({
// etag: anyEtag,
// location: anyString
// });
// await agent.get('newsletters/')
// .expectStatus(200)
// .matchBodySnapshot({
// newsletters: [newsletterSnapshot]
// })
// .matchHeaderSnapshot({
// etag: anyEtag
// });
// });
// it('Can browse newsletters', async function () {
// await agent.get('newsletters/')
// .expectStatus(200)
// .matchBodySnapshot({
// newsletters: [newsletterSnapshot]
// })
// .matchHeaderSnapshot({
// etag: anyEtag
// });
// });
// it('Can edit newsletters', async function () {
// const res = await agent.get('newsletters?limit=1')
// .expectStatus(200)
// .matchBodySnapshot({
// newsletters: [newsletterSnapshot]
// })
// .matchHeaderSnapshot({
// etag: anyEtag
// });
// const id = res.body.newsletters[0].id;
// await agent.put(`newsletters/${id}`)
// .body({
// newsletters: [{
// name: 'Updated newsletter name'
// }]
// })
// .expectStatus(200)
// .matchBodySnapshot({
// newsletters: [newsletterSnapshot]
// })
// .matchHeaderSnapshot({
// etag: anyEtag
// });
// });
});