2023-06-21 11:56:59 +03:00
|
|
|
const assert = require('assert/strict');
|
2022-11-23 14:37:07 +03:00
|
|
|
const sinon = require('sinon');
|
|
|
|
const configUtils = require('../../utils/configUtils');
|
2023-11-23 05:46:50 +03:00
|
|
|
const errors = require('@tryghost/errors');
|
2022-11-23 14:37:07 +03:00
|
|
|
|
|
|
|
const Sentry = require('@sentry/node');
|
|
|
|
|
|
|
|
const fakeDSN = 'https://aaabbbccc000111222333444555667@sentry.io/1234567';
|
|
|
|
let sentry;
|
|
|
|
|
|
|
|
describe('UNIT: sentry', function () {
|
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
|
|
|
afterEach(async function () {
|
|
|
|
await configUtils.restore();
|
2022-11-23 14:37:07 +03:00
|
|
|
sinon.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('No sentry config', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
delete require.cache[require.resolve('../../../core/shared/sentry')];
|
|
|
|
sentry = require('../../../core/shared/sentry');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns expected function signature', function () {
|
|
|
|
assert.equal(sentry.requestHandler.name, 'expressNoop', 'Should return noop');
|
|
|
|
assert.equal(sentry.errorHandler.name, 'expressNoop', 'Should return noop');
|
|
|
|
assert.equal(sentry.captureException.name, 'noop', 'Should return noop');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('With sentry config', function () {
|
|
|
|
beforeEach(function () {
|
|
|
|
configUtils.set({sentry: {disabled: false, dsn: fakeDSN}});
|
|
|
|
delete require.cache[require.resolve('../../../core/shared/sentry')];
|
|
|
|
|
|
|
|
sinon.spy(Sentry, 'init');
|
|
|
|
|
|
|
|
sentry = require('../../../core/shared/sentry');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns expected function signature', function () {
|
|
|
|
assert.equal(sentry.requestHandler.name, 'sentryRequestMiddleware', 'Should return sentry');
|
|
|
|
assert.equal(sentry.errorHandler.name, 'sentryErrorMiddleware', 'Should return sentry');
|
|
|
|
assert.equal(sentry.captureException.name, 'captureException', 'Should return sentry');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('initialises sentry correctly', function () {
|
|
|
|
const initArgs = Sentry.init.getCall(0).args;
|
|
|
|
|
|
|
|
assert.equal(initArgs[0].dsn, fakeDSN, 'shoudl be our fake dsn');
|
|
|
|
assert.match(initArgs[0].release, /ghost@\d+\.\d+\.\d+/, 'should be a valid version');
|
|
|
|
assert.equal(initArgs[0].environment, 'testing', 'should be the testing env');
|
2022-11-22 21:22:52 +03:00
|
|
|
assert.ok(initArgs[0].hasOwnProperty('beforeSend'), 'should have a beforeSend function');
|
2022-11-23 14:37:07 +03:00
|
|
|
});
|
2024-01-05 15:10:39 +03:00
|
|
|
|
|
|
|
it('initialises sentry with the correct environment', function () {
|
|
|
|
const env = 'staging';
|
|
|
|
|
|
|
|
configUtils.set({
|
|
|
|
PRO_ENV: env
|
|
|
|
});
|
|
|
|
|
|
|
|
delete require.cache[require.resolve('../../../core/shared/sentry')];
|
|
|
|
require('../../../core/shared/sentry');
|
|
|
|
|
|
|
|
const initArgs = Sentry.init.getCall(1).args;
|
|
|
|
|
|
|
|
assert.equal(initArgs[0].environment, env, 'should be the correct env');
|
|
|
|
});
|
2022-11-23 14:37:07 +03:00
|
|
|
});
|
2023-11-23 05:46:50 +03:00
|
|
|
|
|
|
|
describe('beforeSend', function () {
|
|
|
|
this.beforeEach(function () {
|
|
|
|
configUtils.set({sentry: {disabled: false, dsn: fakeDSN}});
|
|
|
|
delete require.cache[require.resolve('../../../core/shared/sentry')];
|
|
|
|
|
|
|
|
sentry = require('../../../core/shared/sentry');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the event', function () {
|
|
|
|
sinon.stub(errors.utils, 'isGhostError').returns(false);
|
|
|
|
const beforeSend = sentry.beforeSend;
|
|
|
|
const event = {tags: {}};
|
|
|
|
const hint = {};
|
|
|
|
|
|
|
|
const result = beforeSend(event, hint);
|
|
|
|
|
|
|
|
assert.deepEqual(result, event);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('returns the event, even if an exception is thrown internally', function () {
|
|
|
|
// Trigger an internal exception
|
|
|
|
sinon.stub(errors.utils, 'isGhostError').throws(new Error('test'));
|
|
|
|
const beforeSend = sentry.beforeSend;
|
|
|
|
const event = {tags: {}};
|
|
|
|
const hint = {};
|
|
|
|
|
|
|
|
const result = beforeSend(event, hint);
|
|
|
|
|
|
|
|
assert.deepEqual(result, event);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('sets sql context for mysql2 errors', function () {
|
|
|
|
sinon.stub(errors.utils, 'isGhostError').returns(true);
|
|
|
|
const beforeSend = sentry.beforeSend;
|
|
|
|
const event = {
|
|
|
|
tags: {},
|
|
|
|
exception: {
|
|
|
|
values: [{
|
|
|
|
value: 'test',
|
|
|
|
type: 'test'
|
|
|
|
}]
|
|
|
|
}
|
|
|
|
};
|
|
|
|
const exception = {
|
|
|
|
sql: 'SELECT * FROM test',
|
|
|
|
errno: 123,
|
|
|
|
sqlErrorCode: 456,
|
|
|
|
sqlMessage: 'test message',
|
|
|
|
sqlState: 'test state',
|
|
|
|
code: 'UNEXPECTED_ERROR',
|
|
|
|
errorType: 'InternalServerError',
|
|
|
|
id: 'a1b2c3d4e5f6',
|
|
|
|
statusCode: 500
|
|
|
|
};
|
|
|
|
const hint = {
|
|
|
|
originalException: exception
|
|
|
|
};
|
|
|
|
|
|
|
|
const result = beforeSend(event, hint);
|
|
|
|
|
|
|
|
const expected = {
|
|
|
|
tags: {
|
|
|
|
type: 'InternalServerError',
|
|
|
|
code: 'UNEXPECTED_ERROR',
|
|
|
|
id: 'a1b2c3d4e5f6',
|
|
|
|
status_code: 500
|
|
|
|
},
|
|
|
|
exception: {
|
|
|
|
values: [{
|
|
|
|
value: 'test message',
|
|
|
|
type: 'SQL Error 123: 456'
|
|
|
|
}]
|
|
|
|
},
|
|
|
|
contexts: {
|
|
|
|
mysql: {
|
|
|
|
errno: 123,
|
|
|
|
code: 456,
|
|
|
|
sql: 'SELECT * FROM test',
|
|
|
|
message: 'test message',
|
|
|
|
state: 'test state'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.deepEqual(result, expected);
|
|
|
|
});
|
|
|
|
});
|
2024-01-23 11:22:57 +03:00
|
|
|
|
|
|
|
describe('beforeTransaction', function () {
|
|
|
|
it('filters transactions based on an allow list', function () {
|
|
|
|
sentry = require('../../../core/shared/sentry');
|
|
|
|
|
|
|
|
const beforeSendTransaction = sentry. beforeSendTransaction;
|
|
|
|
|
|
|
|
const allowedTransactions = [
|
|
|
|
{transaction: 'GET /ghost/api/settings'},
|
|
|
|
{transaction: 'PUT /members/api/member'},
|
|
|
|
{transaction: 'POST /ghost/api/tiers'},
|
|
|
|
{transaction: 'DELETE /members/api/member'}
|
|
|
|
];
|
|
|
|
|
|
|
|
allowedTransactions.forEach((transaction) => {
|
|
|
|
assert.equal(beforeSendTransaction(transaction), transaction);
|
|
|
|
});
|
|
|
|
|
|
|
|
assert.equal(beforeSendTransaction({transaction: 'GET /foo/bar'}), null);
|
|
|
|
assert.equal(beforeSendTransaction({transaction: 'Some other transaction'}), null);
|
|
|
|
});
|
|
|
|
});
|
2022-11-23 14:37:07 +03:00
|
|
|
});
|