2022-08-04 16:51:23 +03:00
|
|
|
const {agentProvider, mockManager, fixtureManager, matchers} = require('../../utils/e2e-framework');
|
|
|
|
const {anyEtag, anyObjectId, anyUuid, anyISODateTime} = matchers;
|
|
|
|
const models = require('../../../core/server/models');
|
2023-07-24 12:47:57 +03:00
|
|
|
const should = require('should');
|
2022-08-04 16:51:23 +03:00
|
|
|
|
|
|
|
let membersAgent;
|
|
|
|
|
|
|
|
const memberMatcher = (newslettersCount) => {
|
|
|
|
return {
|
|
|
|
uuid: anyUuid,
|
2023-09-13 08:06:27 +03:00
|
|
|
// @NOTE: check if this field is even needed? it differs to the output in the other matcher
|
2023-04-04 21:55:31 +03:00
|
|
|
created_at: anyISODateTime,
|
2022-08-04 16:51:23 +03:00
|
|
|
newsletters: new Array(newslettersCount).fill(
|
|
|
|
{
|
2023-11-08 14:32:12 +03:00
|
|
|
id: anyObjectId,
|
|
|
|
uuid: anyUuid
|
2022-08-04 16:51:23 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-09-13 08:06:27 +03:00
|
|
|
const buildMemberMatcher = (newslettersCount) => {
|
2022-08-04 16:51:23 +03:00
|
|
|
return {
|
|
|
|
uuid: anyUuid,
|
|
|
|
newsletters: new Array(newslettersCount).fill(
|
|
|
|
{
|
2023-11-08 14:32:12 +03:00
|
|
|
id: anyObjectId,
|
|
|
|
uuid: anyUuid
|
2022-08-04 16:51:23 +03:00
|
|
|
}
|
|
|
|
)
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
describe('Comments API', function () {
|
|
|
|
before(async function () {
|
|
|
|
membersAgent = await agentProvider.getMembersAPIAgent();
|
|
|
|
|
|
|
|
await fixtureManager.init('newsletters', 'members:newsletters');
|
|
|
|
});
|
|
|
|
|
|
|
|
beforeEach(function () {
|
|
|
|
mockManager.mockMail();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(function () {
|
|
|
|
mockManager.restore();
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when not authenticated but enabled', function () {
|
|
|
|
it('can not get member data', async function () {
|
|
|
|
await membersAgent
|
|
|
|
.get(`/api/member/`)
|
|
|
|
.expectStatus(204)
|
|
|
|
.expectEmptyBody();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can update comment notifications', async function () {
|
|
|
|
// Only via updateMemberNewsletters
|
|
|
|
let member = await models.Member.findOne({id: fixtureManager.get('members', 0).id}, {require: true});
|
|
|
|
member.get('enable_comment_notifications').should.eql(true, 'This test requires the initial value to be true');
|
|
|
|
|
|
|
|
await membersAgent
|
|
|
|
.put(`/api/member/newsletters/?uuid=${member.get('uuid')}`)
|
|
|
|
.body({
|
|
|
|
enable_comment_notifications: false
|
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
2023-09-13 08:06:27 +03:00
|
|
|
.matchBodySnapshot(buildMemberMatcher(1))
|
2022-08-04 16:51:23 +03:00
|
|
|
.expect(({body}) => {
|
|
|
|
body.email.should.eql(member.get('email'));
|
|
|
|
body.enable_comment_notifications.should.eql(false);
|
|
|
|
});
|
|
|
|
member = await models.Member.findOne({id: member.id}, {require: true});
|
|
|
|
member.get('enable_comment_notifications').should.eql(false);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe('when authenticated', function () {
|
|
|
|
let member;
|
|
|
|
|
|
|
|
before(async function () {
|
|
|
|
await membersAgent.loginAs('member@example.com');
|
|
|
|
member = await models.Member.findOne({email: 'member@example.com'}, {require: true});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can get member data', async function () {
|
|
|
|
await membersAgent
|
|
|
|
.get(`/api/member/`)
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot(memberMatcher(2))
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.email.should.eql(member.get('email'));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-09-09 11:14:49 +03:00
|
|
|
it('can update member expertise', async function () {
|
2022-08-04 16:51:23 +03:00
|
|
|
await membersAgent
|
|
|
|
.put(`/api/member/`)
|
|
|
|
.body({
|
2022-09-09 11:14:49 +03:00
|
|
|
expertise: 'Head of Testing'
|
2022-08-04 16:51:23 +03:00
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot(memberMatcher(2))
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.email.should.eql(member.get('email'));
|
2022-09-09 11:14:49 +03:00
|
|
|
body.expertise.should.eql('Head of Testing');
|
2022-08-04 16:51:23 +03:00
|
|
|
});
|
|
|
|
member = await models.Member.findOne({id: member.id}, {require: true});
|
2022-09-09 11:14:49 +03:00
|
|
|
member.get('expertise').should.eql('Head of Testing');
|
2022-08-04 16:51:23 +03:00
|
|
|
});
|
|
|
|
|
2022-09-09 11:14:49 +03:00
|
|
|
it('trims whitespace from expertise', async function () {
|
2022-08-05 18:09:48 +03:00
|
|
|
await membersAgent
|
|
|
|
.put(`/api/member/`)
|
|
|
|
.body({
|
2022-09-09 11:14:49 +03:00
|
|
|
expertise: ' test '
|
2022-08-05 18:09:48 +03:00
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot(memberMatcher(2))
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.email.should.eql(member.get('email'));
|
2022-09-09 11:14:49 +03:00
|
|
|
body.expertise.should.eql('test');
|
2022-08-05 18:09:48 +03:00
|
|
|
});
|
|
|
|
member = await models.Member.findOne({id: member.id}, {require: true});
|
2022-09-09 11:14:49 +03:00
|
|
|
member.get('expertise').should.eql('test');
|
2022-08-05 18:09:48 +03:00
|
|
|
});
|
|
|
|
|
2022-08-04 16:51:23 +03:00
|
|
|
it('can update name', async function () {
|
|
|
|
await membersAgent
|
|
|
|
.put(`/api/member/`)
|
|
|
|
.body({
|
|
|
|
name: 'Test User'
|
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot(memberMatcher(2))
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.email.should.eql(member.get('email'));
|
|
|
|
body.name.should.eql('Test User');
|
|
|
|
body.firstname.should.eql('Test');
|
|
|
|
});
|
|
|
|
member = await models.Member.findOne({id: member.id}, {require: true});
|
|
|
|
member.get('name').should.eql('Test User');
|
|
|
|
});
|
|
|
|
|
|
|
|
it('can update comment notifications', async function () {
|
|
|
|
member.get('enable_comment_notifications').should.eql(true, 'This test requires the initial value to be true');
|
|
|
|
|
|
|
|
// Via general way
|
|
|
|
await membersAgent
|
|
|
|
.put(`/api/member/`)
|
|
|
|
.body({
|
|
|
|
enable_comment_notifications: false
|
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
|
|
|
.matchBodySnapshot(memberMatcher(2))
|
|
|
|
.expect(({body}) => {
|
|
|
|
body.email.should.eql(member.get('email'));
|
|
|
|
body.enable_comment_notifications.should.eql(false);
|
|
|
|
});
|
|
|
|
member = await models.Member.findOne({id: member.id}, {require: true});
|
|
|
|
member.get('enable_comment_notifications').should.eql(false);
|
|
|
|
|
|
|
|
// Via updateMemberNewsletters
|
|
|
|
await membersAgent
|
|
|
|
.put(`/api/member/newsletters/?uuid=${member.get('uuid')}`)
|
|
|
|
.body({
|
|
|
|
enable_comment_notifications: true
|
|
|
|
})
|
|
|
|
.expectStatus(200)
|
|
|
|
.matchHeaderSnapshot({
|
|
|
|
etag: anyEtag
|
|
|
|
})
|
2023-09-13 08:06:27 +03:00
|
|
|
.matchBodySnapshot(buildMemberMatcher(2))
|
2022-08-04 16:51:23 +03:00
|
|
|
.expect(({body}) => {
|
|
|
|
body.email.should.eql(member.get('email'));
|
|
|
|
body.enable_comment_notifications.should.eql(true);
|
|
|
|
});
|
|
|
|
member = await models.Member.findOne({id: member.id}, {require: true});
|
|
|
|
member.get('enable_comment_notifications').should.eql(true);
|
|
|
|
});
|
2022-11-23 13:41:00 +03:00
|
|
|
|
2023-07-24 12:47:57 +03:00
|
|
|
it('can remove a member\'s email from the suppression list', async function () {
|
|
|
|
// add member's email to the suppression list
|
|
|
|
await models.Suppression.add({
|
|
|
|
email: member.get('email'),
|
|
|
|
reason: 'bounce'
|
|
|
|
});
|
2022-12-13 12:31:50 +03:00
|
|
|
|
2023-07-24 12:47:57 +03:00
|
|
|
// disable member's email
|
|
|
|
await member.save({email_disabled: true});
|
2022-12-13 12:31:50 +03:00
|
|
|
|
2023-07-24 12:47:57 +03:00
|
|
|
// remove suppression
|
2022-11-23 13:41:00 +03:00
|
|
|
await membersAgent
|
|
|
|
.delete(`/api/member/suppression`)
|
|
|
|
.expectStatus(204)
|
|
|
|
.expectEmptyBody();
|
2022-12-13 12:31:50 +03:00
|
|
|
|
2023-07-24 12:47:57 +03:00
|
|
|
// check that member is removed from suppression list
|
|
|
|
const suppression = await models.Suppression.findOne({email: member.get('email')});
|
|
|
|
|
|
|
|
should(suppression).be.null();
|
|
|
|
|
|
|
|
// check that member's email is enabled
|
|
|
|
await member.refresh();
|
|
|
|
|
|
|
|
should(member.get('email_disabled')).be.false();
|
2022-11-23 13:41:00 +03:00
|
|
|
});
|
2022-08-04 16:51:23 +03:00
|
|
|
});
|
|
|
|
});
|