Added missing e2e mail API retry test

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

- changed mail API  tests to use the new e2e test framework
- added the missing retry test - which has the wrong response format :(

mail
This commit is contained in:
Hannah Wolfe 2022-03-25 10:41:58 +00:00
parent 2ca452f4b0
commit 15da07f324
No known key found for this signature in database
GPG Key ID: AB586C3B5AE5C037
3 changed files with 89 additions and 31 deletions

View File

@ -0,0 +1,48 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Mail API Can send a test mail 1: [body] 1`] = `
Object {
"message": "sent",
}
`;
exports[`Mail API Can send a test mail 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": "18",
"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[`Mail API Can send mail 1: [body] 1`] = `
Object {
"mail": Array [
Object {
"message": Object {
"html": "<p>This</p>",
"subject": "testemail",
"to": "joe@example.com",
},
"status": Object {
"message": "sent",
},
},
],
}
`;
exports[`Mail API Can send mail 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": "118",
"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

@ -1,33 +1,27 @@
const should = require('should');
const supertest = require('supertest');
const sinon = require('sinon');
const testUtils = require('../../utils');
const config = require('../../../core/shared/config');
const mailService = require('../../../core/server/services/mail');
const localUtils = require('./utils');
const {agentProvider, fixtureManager, matchers, mockManager} = require('../../utils/e2e-framework');
const {anyEtag} = matchers;
describe('Mail API', function () {
let request;
let agent;
before(async function () {
await localUtils.startGhost();
request = supertest.agent(config.get('url'));
await localUtils.doAuth(request, 'invites');
agent = await agentProvider.getAdminAPIAgent();
await fixtureManager.init('invites');
await agent.loginAsOwner();
});
beforeEach(function () {
sinon.stub(mailService.GhostMailer.prototype, 'send').resolves({message: 'sent'});
mockManager.mockMail({message: 'sent'});
});
afterEach(function () {
sinon.restore();
mockManager.restore();
});
it('Can send mail', async function () {
const res = await request
.post(localUtils.API.getApiQuery('mail/'))
.set('Origin', config.get('url'))
.send({
await agent
.post('mail/')
.body({
mail: [{
message: {
to: 'joe@example.com',
@ -36,20 +30,31 @@ describe('Mail API', function () {
}
}]
})
.expect('Content-Type', /json/)
.expect('Cache-Control', testUtils.cacheRules.private)
.expect(200);
.expectStatus(200)
.matchBodySnapshot()
.matchHeaderSnapshot({
etag: anyEtag
});
should.not.exist(res.headers['x-cache-invalidate']);
const jsonResponse = res.body;
mockManager.assert.sentEmail({
to: 'joe@example.com',
subject: 'testemail'
});
});
should.exist(jsonResponse);
should.exist(jsonResponse.mail);
should.exist(jsonResponse.mail[0].message);
should.exist(jsonResponse.mail[0].status);
it('Can send a test mail', async function () {
// @TODO: either remove this endpoint or fix its response body
await agent
.post('mail/test')
.expectStatus(200)
.matchBodySnapshot()
.matchHeaderSnapshot({
etag: anyEtag
});
jsonResponse.mail[0].status.should.eql({message: 'sent'});
jsonResponse.mail[0].message.subject.should.eql('testemail');
mailService.GhostMailer.prototype.send.called.should.be.true();
mockManager.assert.sentEmail({
to: 'jbloggs@example.com',
subject: 'Test Ghost Email'
});
});
});

View File

@ -21,10 +21,15 @@ const disableStripe = async () => {
await stripeService.disconnect();
};
const mockMail = () => {
/**
*
* @param {String|Object} response
* @returns
*/
const mockMail = (response = 'Mail is disabled') => {
mocks.mail = sinon
.stub(mailService.GhostMailer.prototype, 'send')
.resolves('Mail is disabled');
.resolves(response);
return mocks.mail;
};