Ghost/ghost/core/test/e2e-api/members/feedback.test.js
Simon Backx 8f8ca481a6
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 14:06:20 +01:00

283 lines
8.7 KiB
JavaScript

const assert = require('assert');
const {agentProvider, mockManager, fixtureManager, matchers, configUtils} = require('../../utils/e2e-framework');
const {anyEtag, anyObjectId, anyLocationFor, anyErrorId} = matchers;
const models = require('../../../core/server/models');
const sinon = require('sinon');
describe('Members Feedback', function () {
let membersAgent, membersAgent2, memberUuid;
let clock;
before(async function () {
membersAgent = await agentProvider.getMembersAPIAgent();
membersAgent2 = membersAgent.duplicate();
await fixtureManager.init('posts', 'members');
memberUuid = fixtureManager.get('members', 0).uuid;
});
beforeEach(function () {
mockManager.mockMail();
});
afterEach(async function () {
clock?.restore();
clock = undefined;
await configUtils.restore();
mockManager.restore();
});
describe('Authentication', function () {
it('Allows authentication via session', async function () {
const postId = fixtureManager.get('posts', 0).id;
await membersAgent2.loginAs('authenticationtest@email.com');
await membersAgent2
.post('/api/feedback/')
.body({
feedback: [{
score: 1,
post_id: postId
}]
})
.expectStatus(201)
.matchHeaderSnapshot({
etag: anyEtag,
location: anyLocationFor('feedback')
})
.matchBodySnapshot({
feedback: [
{
id: anyObjectId,
memberId: anyObjectId,
postId: anyObjectId
}
]
});
});
});
describe('Validation', function () {
const postId = fixtureManager.get('posts', 0).id;
it('Throws for invalid score', async function () {
await membersAgent
.post(`/api/feedback/?uuid=${memberUuid}`)
.body({
feedback: [{
score: 2,
post_id: postId
}]
})
.expectStatus(422)
.matchBodySnapshot({
errors: [
{
id: anyErrorId
}
]
});
});
it('Throws for invalid score type', async function () {
await membersAgent
.post(`/api/feedback/?uuid=${memberUuid}`)
.body({
feedback: [{
score: 'text',
post_id: postId
}]
})
.expectStatus(422)
.matchBodySnapshot({
errors: [
{
id: anyErrorId
}
]
});
});
it('Throws for invalid uuid', async function () {
await membersAgent
.post(`/api/feedback/?uuid=1234`)
.body({
feedback: [{
score: 1,
post_id: postId
}]
})
.expectStatus(401)
.matchBodySnapshot({
errors: [
{
id: anyErrorId
}
]
});
});
it('Throws for nonexisting uuid', async function () {
const uuid = '00000000-0000-0000-0000-000000000000';
await membersAgent
.post(`/api/feedback/?uuid=${uuid}`)
.body({
feedback: [{
score: 1,
post_id: postId
}]
})
.expectStatus(401)
.matchBodySnapshot({
errors: [
{
id: anyErrorId
}
]
});
});
it('Throws for nonexisting post', async function () {
await membersAgent
.post(`/api/feedback/?uuid=${memberUuid}`)
.body({
feedback: [{
score: 1,
post_id: '123'
}]
})
.expectStatus(404)
.matchBodySnapshot({
errors: [
{
id: anyErrorId
}
]
});
});
});
it('Can add positive feedback', async function () {
const postId = fixtureManager.get('posts', 0).id;
await membersAgent
.post(`/api/feedback/?uuid=${memberUuid}`)
.body({
feedback: [{
score: 1,
post_id: postId
}]
})
.expectStatus(201)
.matchHeaderSnapshot({
etag: anyEtag,
location: anyLocationFor('feedback')
})
.matchBodySnapshot({
feedback: [
{
id: anyObjectId,
memberId: anyObjectId,
postId: anyObjectId
}
]
});
});
it('Can change existing feedback', async function () {
clock = sinon.useFakeTimers(new Date());
const postId = fixtureManager.get('posts', 1).id;
const {body} = await membersAgent
.post(`/api/feedback/?uuid=${memberUuid}`)
.body({
feedback: [{
score: 0,
post_id: postId
}]
})
.expectStatus(201)
.matchHeaderSnapshot({
etag: anyEtag,
location: anyLocationFor('feedback')
})
.matchBodySnapshot({
feedback: [
{
id: anyObjectId,
memberId: anyObjectId,
postId: anyObjectId
}
]
});
assert.equal(body.feedback[0].score, 0);
const feedbackId = body.feedback[0].id;
// Fetch real model
const model1 = await models.MemberFeedback.findOne({id: feedbackId}, {require: true});
clock.tick(10 * 60 * 1000);
const {body: body2} = await membersAgent
.post(`/api/feedback/?uuid=${memberUuid}`)
.body({
feedback: [{
score: 1,
post_id: postId
}]
})
.expectStatus(201)
.matchHeaderSnapshot({
etag: anyEtag,
location: anyLocationFor('feedback')
})
.matchBodySnapshot({
feedback: [
{
id: anyObjectId,
memberId: anyObjectId,
postId: anyObjectId
}
]
});
assert.equal(body2.feedback[0].id, feedbackId);
assert.equal(body2.feedback[0].score, 1);
const model2 = await models.MemberFeedback.findOne({id: feedbackId}, {require: true});
assert.notEqual(model2.get('updated_at').getTime(), model1.get('updated_at').getTime());
clock.tick(10 * 60 * 1000);
// Do the same change again, and the model shouldn't change
const {body: body3} = await membersAgent
.post(`/api/feedback/?uuid=${memberUuid}`)
.body({
feedback: [{
score: 1,
post_id: postId
}]
})
.expectStatus(201)
.matchHeaderSnapshot({
etag: anyEtag,
location: anyLocationFor('feedback')
})
.matchBodySnapshot({
feedback: [
{
id: anyObjectId,
memberId: anyObjectId,
postId: anyObjectId
}
]
});
const model3 = await models.MemberFeedback.findOne({id: feedbackId}, {require: true});
assert.equal(body3.feedback[0].id, feedbackId);
assert.equal(body3.feedback[0].score, 1);
assert.equal(model3.get('updated_at').getTime(), model2.get('updated_at').getTime());
});
});