2022-02-07 19:02:04 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2021-12-09 14:10:06 +03:00
|
|
|
const sinon = require('sinon');
|
2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2022-02-15 16:08:58 +03:00
|
|
|
const nock = require('nock');
|
2023-05-02 23:43:47 +03:00
|
|
|
const MailgunClient = require('@tryghost/mailgun-client');
|
2021-12-09 14:10:06 +03:00
|
|
|
|
2022-02-08 23:21:03 +03:00
|
|
|
// Helper services
|
|
|
|
const configUtils = require('./configUtils');
|
2022-05-30 10:03:27 +03:00
|
|
|
const WebhookMockReceiver = require('@tryghost/webhook-mock-receiver');
|
2023-01-13 14:45:58 +03:00
|
|
|
const EmailMockReceiver = require('@tryghost/email-mock-receiver');
|
2022-05-30 10:03:27 +03:00
|
|
|
const {snapshotManager} = require('@tryghost/express-test').snapshot;
|
2022-02-08 23:21:03 +03:00
|
|
|
|
2022-02-07 19:02:04 +03:00
|
|
|
let mocks = {};
|
2022-02-07 20:28:53 +03:00
|
|
|
let emailCount = 0;
|
2022-02-07 19:02:04 +03:00
|
|
|
|
2022-02-08 23:21:03 +03:00
|
|
|
// Mockable services
|
2021-12-09 14:10:06 +03:00
|
|
|
const mailService = require('../../core/server/services/mail/index');
|
2023-11-23 12:25:30 +03:00
|
|
|
const originalMailServiceSendMail = mailService.GhostMailer.prototype.sendMail;
|
2022-02-08 23:21:03 +03:00
|
|
|
const labs = require('../../core/shared/labs');
|
2022-03-25 14:44:03 +03:00
|
|
|
const events = require('../../core/server/lib/common/events');
|
2022-12-01 15:43:49 +03:00
|
|
|
const settingsCache = require('../../core/shared/settings-cache');
|
2023-03-21 13:23:29 +03:00
|
|
|
const dns = require('dns');
|
|
|
|
const dnsPromises = dns.promises;
|
2023-03-10 20:43:35 +03:00
|
|
|
const StripeMocker = require('./stripe-mocker');
|
2022-03-25 14:44:03 +03:00
|
|
|
|
2022-04-28 11:55:20 +03:00
|
|
|
let fakedLabsFlags = {};
|
2023-03-08 14:40:21 +03:00
|
|
|
let allowedNetworkDomains = [];
|
2022-04-28 11:55:20 +03:00
|
|
|
const originalLabsIsSet = labs.isSet;
|
2023-03-10 20:43:35 +03:00
|
|
|
const stripeMocker = new StripeMocker();
|
2022-04-28 11:55:20 +03:00
|
|
|
|
2022-03-25 14:44:03 +03:00
|
|
|
/**
|
|
|
|
* Stripe Mocks
|
|
|
|
*/
|
2021-12-09 14:10:06 +03:00
|
|
|
|
2022-03-10 13:55:22 +03:00
|
|
|
const disableStripe = async () => {
|
|
|
|
// This must be required _after_ startGhost has been called, because the models will
|
|
|
|
// not have been loaded otherwise. Consider moving the dependency injection of models
|
|
|
|
// into the init method of the Stripe service.
|
|
|
|
const stripeService = require('../../core/server/services/stripe');
|
|
|
|
await stripeService.disconnect();
|
|
|
|
};
|
|
|
|
|
2023-02-22 18:19:09 +03:00
|
|
|
const disableNetwork = () => {
|
|
|
|
nock.disableNetConnect();
|
|
|
|
|
|
|
|
// externalRequest does dns lookup; stub to make sure we don't fail with fake domain names
|
2023-03-07 15:20:28 +03:00
|
|
|
if (!dnsPromises.lookup.restore) {
|
|
|
|
sinon.stub(dnsPromises, 'lookup').callsFake(() => {
|
|
|
|
return Promise.resolve({address: '123.123.123.123', family: 4});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:23:29 +03:00
|
|
|
if (!dns.resolveMx.restore) {
|
|
|
|
// without this, Node will try and resolve the domain name but local DNS
|
|
|
|
// resolvers can take a while to timeout, which causes the tests to timeout
|
|
|
|
// `nodemailer-direct-transport` calls `dns.resolveMx`, so if we stub that
|
|
|
|
// function and return an empty array, we can avoid any real DNS lookups
|
|
|
|
sinon.stub(dns, 'resolveMx').yields(null, []);
|
|
|
|
}
|
|
|
|
|
2023-03-07 15:20:28 +03:00
|
|
|
// Allow localhost
|
2023-03-08 14:40:21 +03:00
|
|
|
// Multiple enableNetConnect with different hosts overwrite each other, so we need to add one and use the allowedNetworkDomains variable
|
|
|
|
nock.enableNetConnect((host) => {
|
|
|
|
if (host.includes('127.0.0.1')) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (const h of allowedNetworkDomains) {
|
|
|
|
if (host.includes(h)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const allowStripe = () => {
|
|
|
|
disableNetwork();
|
|
|
|
allowedNetworkDomains.push('stripe.com');
|
2023-03-07 15:20:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const mockStripe = () => {
|
|
|
|
disableNetwork();
|
2023-08-11 15:25:53 +03:00
|
|
|
stripeMocker.reset();
|
2023-03-10 20:43:35 +03:00
|
|
|
stripeMocker.stub();
|
2023-03-07 15:20:28 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const mockSlack = () => {
|
|
|
|
disableNetwork();
|
|
|
|
|
|
|
|
nock(/hooks.slack.com/)
|
|
|
|
.persist()
|
|
|
|
.post('/')
|
|
|
|
.reply(200, 'ok');
|
2023-02-22 18:19:09 +03:00
|
|
|
};
|
|
|
|
|
2022-03-25 14:44:03 +03:00
|
|
|
/**
|
|
|
|
* Email Mocks & Assertions
|
|
|
|
*/
|
|
|
|
|
2022-03-25 13:41:58 +03:00
|
|
|
/**
|
|
|
|
* @param {String|Object} response
|
|
|
|
*/
|
|
|
|
const mockMail = (response = 'Mail is disabled') => {
|
2023-01-13 14:45:58 +03:00
|
|
|
const mockMailReceiver = new EmailMockReceiver({
|
|
|
|
snapshotManager: snapshotManager,
|
|
|
|
sendResponse: response
|
|
|
|
});
|
|
|
|
|
2023-11-23 12:25:30 +03:00
|
|
|
mailService.GhostMailer.prototype.sendMail = mockMailReceiver.send.bind(mockMailReceiver);
|
|
|
|
mocks.mail = sinon.spy(mailService.GhostMailer.prototype, 'sendMail');
|
2023-01-13 14:45:58 +03:00
|
|
|
mocks.mockMailReceiver = mockMailReceiver;
|
2022-02-07 19:02:04 +03:00
|
|
|
|
2023-01-13 14:45:58 +03:00
|
|
|
return mockMailReceiver;
|
2022-02-07 19:02:04 +03:00
|
|
|
};
|
|
|
|
|
2023-06-29 12:10:56 +03:00
|
|
|
/**
|
|
|
|
* A reference to the send method when MailGun is mocked (required for some tests)
|
|
|
|
*/
|
|
|
|
let mailgunCreateMessageStub;
|
|
|
|
|
2023-03-07 15:20:28 +03:00
|
|
|
const mockMailgun = (customStubbedSend) => {
|
|
|
|
mockSetting('mailgun_api_key', 'test');
|
|
|
|
mockSetting('mailgun_domain', 'example.com');
|
|
|
|
mockSetting('mailgun_base_url', 'test');
|
|
|
|
|
2023-06-29 12:10:56 +03:00
|
|
|
mailgunCreateMessageStub = customStubbedSend ? sinon.stub().callsFake(customStubbedSend) : sinon.fake.resolves({
|
2023-03-07 15:20:28 +03:00
|
|
|
id: `<${new Date().getTime()}.${0}.5817@samples.mailgun.org>`
|
|
|
|
});
|
|
|
|
|
|
|
|
// We need to stub the Mailgun client before starting Ghost
|
|
|
|
sinon.stub(MailgunClient.prototype, 'getInstance').returns({
|
|
|
|
// @ts-ignore
|
|
|
|
messages: {
|
|
|
|
create: async function () {
|
2023-06-29 12:10:56 +03:00
|
|
|
return await mailgunCreateMessageStub.call(this, ...arguments);
|
2023-03-07 15:20:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-05-26 06:11:41 +03:00
|
|
|
const mockWebhookRequests = () => {
|
2022-05-30 10:03:27 +03:00
|
|
|
mocks.webhookMockReceiver = new WebhookMockReceiver({snapshotManager});
|
2022-05-26 06:11:41 +03:00
|
|
|
|
|
|
|
return mocks.webhookMockReceiver;
|
|
|
|
};
|
|
|
|
|
2023-01-18 07:55:33 +03:00
|
|
|
/**
|
2023-04-06 23:10:26 +03:00
|
|
|
* @deprecated use emailMockReceiver.assertSentEmailCount(count) instead
|
2023-01-18 07:55:33 +03:00
|
|
|
* @param {Number} count number of emails sent
|
|
|
|
*/
|
2022-02-07 20:28:53 +03:00
|
|
|
const sentEmailCount = (count) => {
|
2022-02-07 19:02:04 +03:00
|
|
|
if (!mocks.mail) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: 'Cannot assert on mail when mail has not been mocked'
|
|
|
|
});
|
|
|
|
}
|
2022-02-07 20:28:53 +03:00
|
|
|
|
2023-04-06 23:10:26 +03:00
|
|
|
mocks.mockMailReceiver.assertSentEmailCount(count);
|
2022-02-07 20:28:53 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const sentEmail = (matchers) => {
|
|
|
|
if (!mocks.mail) {
|
|
|
|
throw new errors.IncorrectUsageError({
|
|
|
|
message: 'Cannot assert on mail when mail has not been mocked'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
let spyCall = mocks.mail.getCall(emailCount);
|
|
|
|
|
2022-09-02 17:57:59 +03:00
|
|
|
assert.notEqual(spyCall, null, 'Expected at least ' + (emailCount + 1) + ' emails sent.');
|
|
|
|
|
2022-02-07 20:28:53 +03:00
|
|
|
// We increment here so that the messaging has an index of 1, whilst getting the call has an index of 0
|
|
|
|
emailCount += 1;
|
|
|
|
|
|
|
|
sinon.assert.called(mocks.mail);
|
|
|
|
|
|
|
|
Object.keys(matchers).forEach((key) => {
|
|
|
|
let value = matchers[key];
|
|
|
|
|
|
|
|
// We use assert, rather than sinon.assert.calledWith, as we end up with much better error messaging
|
|
|
|
assert.notEqual(spyCall.args[0][key], undefined, `Expected email to have property ${key}`);
|
2022-07-25 17:27:38 +03:00
|
|
|
|
|
|
|
if (value instanceof RegExp) {
|
|
|
|
assert.match(spyCall.args[0][key], value, `Expected Email ${emailCount} to have ${key} that matches ${value}, got ${spyCall.args[0][key]}`);
|
|
|
|
return;
|
|
|
|
}
|
2023-01-13 14:45:58 +03:00
|
|
|
|
2022-02-07 20:28:53 +03:00
|
|
|
assert.equal(spyCall.args[0][key], value, `Expected Email ${emailCount} to have ${key} of ${value}`);
|
|
|
|
});
|
2022-08-18 18:38:42 +03:00
|
|
|
|
|
|
|
return spyCall.args[0];
|
2021-12-09 14:10:06 +03:00
|
|
|
};
|
|
|
|
|
2022-03-25 14:44:03 +03:00
|
|
|
/**
|
|
|
|
* Events Mocks & Assertions
|
|
|
|
*/
|
|
|
|
|
|
|
|
const mockEvents = () => {
|
|
|
|
mocks.events = sinon.stub(events, 'emit');
|
|
|
|
};
|
|
|
|
|
|
|
|
const emittedEvent = (name) => {
|
|
|
|
sinon.assert.calledWith(mocks.events, name);
|
|
|
|
};
|
|
|
|
|
2022-12-01 15:43:49 +03:00
|
|
|
/**
|
|
|
|
* Settings Mocks
|
|
|
|
*/
|
|
|
|
|
|
|
|
let fakedSettings = {};
|
|
|
|
const originalSettingsGetter = settingsCache.get;
|
|
|
|
|
|
|
|
const fakeSettingsGetter = (setting) => {
|
|
|
|
if (fakedSettings.hasOwnProperty(setting)) {
|
|
|
|
return fakedSettings[setting];
|
|
|
|
}
|
|
|
|
|
|
|
|
return originalSettingsGetter(setting);
|
|
|
|
};
|
|
|
|
|
|
|
|
const mockSetting = (key, value) => {
|
|
|
|
if (!mocks.settings) {
|
|
|
|
mocks.settings = sinon.stub(settingsCache, 'get').callsFake(fakeSettingsGetter);
|
|
|
|
}
|
|
|
|
|
|
|
|
fakedSettings[key] = value;
|
|
|
|
};
|
|
|
|
|
2022-03-25 14:44:03 +03:00
|
|
|
/**
|
|
|
|
* Labs Mocks
|
|
|
|
*/
|
2022-04-28 11:55:20 +03:00
|
|
|
|
|
|
|
const fakeLabsIsSet = (flag) => {
|
|
|
|
if (fakedLabsFlags.hasOwnProperty(flag)) {
|
|
|
|
return fakedLabsFlags[flag];
|
|
|
|
}
|
|
|
|
|
|
|
|
return originalLabsIsSet(flag);
|
|
|
|
};
|
|
|
|
|
2022-03-25 14:44:03 +03:00
|
|
|
const mockLabsEnabled = (flag, alpha = true) => {
|
|
|
|
// We assume we should enable alpha experiments unless explicitly told not to!
|
|
|
|
if (!alpha) {
|
|
|
|
configUtils.set('enableDeveloperExperiments', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mocks.labs) {
|
2022-04-28 11:55:20 +03:00
|
|
|
mocks.labs = sinon.stub(labs, 'isSet').callsFake(fakeLabsIsSet);
|
2022-03-25 14:44:03 +03:00
|
|
|
}
|
|
|
|
|
2022-04-28 11:55:20 +03:00
|
|
|
fakedLabsFlags[flag] = true;
|
2022-03-25 14:44:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const mockLabsDisabled = (flag, alpha = true) => {
|
|
|
|
// We assume we should enable alpha experiments unless explicitly told not to!
|
|
|
|
if (!alpha) {
|
|
|
|
configUtils.set('enableDeveloperExperiments', true);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mocks.labs) {
|
2022-04-28 11:55:20 +03:00
|
|
|
mocks.labs = sinon.stub(labs, 'isSet').callsFake(fakeLabsIsSet);
|
2022-03-25 14:44:03 +03:00
|
|
|
}
|
|
|
|
|
2022-04-28 11:55:20 +03:00
|
|
|
fakedLabsFlags[flag] = false;
|
2022-03-25 14:44:03 +03:00
|
|
|
};
|
|
|
|
|
2022-02-07 19:02:04 +03:00
|
|
|
const restore = () => {
|
Fixed configUtils and adapter cache issues in E2E tests (#16167)
no issue
There are a couple of issues with resetting the Ghost instance between
E2E test files:
These issues came to the surface because of new tests written in
https://github.com/TryGhost/Ghost/pull/16117
**1. configUtils.restore does not work correctly**
`config.reset()` is a callback based method. On top of that, it doesn't
really work reliably (https://github.com/indexzero/nconf/issues/93)
What kinda happens, is that you first call `config.reset` but
immediately after you correcty reset the config using the `config.set`
calls afterwards. But since `config.reset` is async, that reset will
happen after all those sets, and the end result is that it isn't reset
correctly.
This mainly caused issues in the new updated images tests, which were
updating the config `imageOptimization.contentImageSizes`, which is a
deeply nested config value. Maybe some references to objects are reused
in nconf that cause this issue?
Wrapping `config.reset()` in a promise does fix the issue.
**2. Adapters cache not reset between tests**
At the start of each test, we set `paths:contentPath` to a nice new
temporary directory. But if a previous test already requests a
localStorage adapter, that adapter would have been created and in the
constructor `paths:contentPath` would have been passed. That same
instance will be reused in the next test run. So it won't read the new
config again. To fix this, we need to reset the adapter instances
between E2E tests.
How was this visible? Test uploads were stored in the actual git
repository, and not in a temporary directory. When writing the new image
upload tests, this also resulted in unreliable test runs because some
image names were already taken (from previous test runs).
**3. Old 2E2 test Ghost server not stopped**
Sometimes we still need access to the frontend test server using
`getAgentsWithFrontend`. But that does start a new Ghost server which is
actually listening for HTTP traffic. This could result in a fatal error
in tests because the port is already in use. The issue is that old E2E
tests also start a HTTP server, but they don't stop the server. When you
used the old `startGhost` util, it would check if a server was already
running and stop it first. The new `getAgentsWithFrontend` now also has
the same functionality to fix that issue.
2023-01-30 16:06:20 +03:00
|
|
|
// eslint-disable-next-line no-console
|
|
|
|
configUtils.restore().catch(console.error);
|
2022-02-07 19:02:04 +03:00
|
|
|
sinon.restore();
|
|
|
|
mocks = {};
|
2022-04-28 11:55:20 +03:00
|
|
|
fakedLabsFlags = {};
|
2022-12-01 15:43:49 +03:00
|
|
|
fakedSettings = {};
|
2022-02-07 20:28:53 +03:00
|
|
|
emailCount = 0;
|
2023-03-08 14:40:21 +03:00
|
|
|
allowedNetworkDomains = [];
|
2022-02-15 16:08:58 +03:00
|
|
|
nock.cleanAll();
|
|
|
|
nock.enableNetConnect();
|
2023-03-10 20:43:35 +03:00
|
|
|
stripeMocker.reset();
|
2022-05-26 06:11:41 +03:00
|
|
|
|
|
|
|
if (mocks.webhookMockReceiver) {
|
|
|
|
mocks.webhookMockReceiver.reset();
|
|
|
|
}
|
2023-01-13 14:45:58 +03:00
|
|
|
|
2023-11-23 12:25:30 +03:00
|
|
|
mailService.GhostMailer.prototype.sendMail = originalMailServiceSendMail;
|
2023-03-07 15:20:28 +03:00
|
|
|
|
|
|
|
// Disable network again after restoring sinon
|
|
|
|
disableNetwork();
|
2022-02-07 19:02:04 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
2022-03-25 14:44:03 +03:00
|
|
|
mockEvents,
|
2022-02-07 19:02:04 +03:00
|
|
|
mockMail,
|
2022-03-10 13:55:22 +03:00
|
|
|
disableStripe,
|
2022-02-15 16:08:58 +03:00
|
|
|
mockStripe,
|
2023-03-07 15:20:28 +03:00
|
|
|
mockSlack,
|
2023-03-08 14:40:21 +03:00
|
|
|
allowStripe,
|
2023-03-07 15:20:28 +03:00
|
|
|
mockMailgun,
|
2022-02-08 23:21:03 +03:00
|
|
|
mockLabsEnabled,
|
2022-02-10 15:03:47 +03:00
|
|
|
mockLabsDisabled,
|
2022-05-26 06:11:41 +03:00
|
|
|
mockWebhookRequests,
|
2022-12-01 15:43:49 +03:00
|
|
|
mockSetting,
|
2023-02-22 18:19:09 +03:00
|
|
|
disableNetwork,
|
2022-02-07 20:28:53 +03:00
|
|
|
restore,
|
2023-03-10 20:43:35 +03:00
|
|
|
stripeMocker,
|
2022-02-07 20:28:53 +03:00
|
|
|
assert: {
|
|
|
|
sentEmailCount,
|
2022-03-25 14:44:03 +03:00
|
|
|
sentEmail,
|
|
|
|
emittedEvent
|
2023-06-29 12:10:56 +03:00
|
|
|
},
|
|
|
|
getMailgunCreateMessageStub: () => mailgunCreateMessageStub
|
2022-02-07 19:02:04 +03:00
|
|
|
};
|