Skip to content

Commit

Permalink
fix(members): update listserv to temp rest endpoint (#868)
Browse files Browse the repository at this point in the history
Co-authored-by: WikiRik <WikiRik@users.noreply.github.com>
  • Loading branch information
WikiRik and WikiRik authored Jan 24, 2024
1 parent 1a781a1 commit 80f1545
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 98 deletions.
6 changes: 3 additions & 3 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ const config = {
url: 'http://mailer',
port: 4000
},
listserv_email: [
process.env.LISTSERV_EMAIL || 'listserv@example.com'
],
listserv_email: process.env.LISTSERV_EMAIL || 'listserv@example.com',
listserv_endpoint: process.env.LISTSERV_ENDPOINT || 'https://lists.example.com/subscribe',
listserv_token: process.env.LISTSERV_TOKEN || 'CHANGEME',
logger: {
silent: false,
level: process.env.LOGLEVEL || 'info'
Expand Down
2 changes: 2 additions & 0 deletions docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ services:
CORE_LOGIN: "${CORE_LOGIN}"
CORE_PASSWORD: "${CORE_PASSWORD}"
LISTSERV_EMAIL: "${LISTSERV_EMAIL}"
LISTSERV_ENDPOINT: "${LISTSERV_ENDPOINT}"
LISTSERV_TOKEN: "${LISTSERV_TOKEN}"
healthcheck:
test: ["CMD", "curl", "-f", "http://localhost:8084/healthcheck"]
interval: 30s
Expand Down
3 changes: 2 additions & 1 deletion lib/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,5 +92,6 @@ module.exports = {
NEW_JOIN_REQUEST: 'MyAEGEE: New join request for your body',
NEW_MEMBER: 'MyAEGEE: Welcome to AEGEE'
},
RESTRICTED_EMAILS: ['aegee.org', 'aegee.eu']
RESTRICTED_EMAILS: ['aegee.org', 'aegee.eu'],
LISTSERV_LISTS: ['AEGEE-L', 'AEGEENEWS-L', 'ANNOUNCE-L', 'AEGEE-EVENT-L']
};
2 changes: 1 addition & 1 deletion lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ function getRandomBytes(length) {
function getMailText({ user, mailinglists }) {
return `OK BEGIN
REG ${user.first_name} ${user.last_name}
SUBSCRIBE ${mailinglists.join('\nSUBSCRIBE ')}
SUBSCRIBE ${mailinglists.join('\r\nSUBSCRIBE ')}
OK END`;
}

Expand Down
1 change: 1 addition & 0 deletions lib/mailer.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module.exports.sendMail = async (options) => {
simple: false,
json: true,
body: {
from: options.from,
to: options.to,
subject: options.subject,
template: options.template,
Expand Down
32 changes: 20 additions & 12 deletions middlewares/members.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const moment = require('moment');
const _ = require('lodash');
const request = require('request-promise-native');

const { User, Body, MailChange, MailConfirmation } = require('../models');
const config = require('../config');
Expand Down Expand Up @@ -319,18 +320,25 @@ exports.subscribeListserv = async (req, res) => {

const mailinglists = req.body.mailinglists.map((list) => list.toUpperCase());

await mailer.sendMail({
to: config.listserv_email,
from: req.user.notification_email,
subject: `SUBSCRIBE ${req.user.notification_email}`,
template: 'custom.html',
parameters: {
body: helpers.getMailText({
user: req.user,
mailinglists
})
}
});
if (mailinglists.some((list) => !constants.LISTSERV_LISTS.includes(list))) {
return errors.makeValidationError(res, `Mailinglists must be one of the following: ${constants.LISTSERV_LISTS.join(', ')}.`);
}

try {
await request({
url: config.listserv_endpoint,
method: 'POST',
simple: false,
form: {
token: config.listserv_token,
email: req.user.notification_email,
name: `${req.user.first_name} ${req.user.last_name}`,
lists: mailinglists.join(','),
}
});
} catch (err) {
return errors.makeInternalError(res, err);
}

return res.json({
success: true,
Expand Down
181 changes: 100 additions & 81 deletions test/api/users-listserv.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('User subscribe listserv', () => {
expect(res.body).toHaveProperty('message');
});

test('should fail if there mailinglists is not an array', async () => {
test('should fail if mailinglists is not an array', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken(user);

Expand All @@ -78,7 +78,7 @@ describe('User subscribe listserv', () => {
expect(res.body).toHaveProperty('message');
});

test('should fail if there mailinglists is an empty array', async () => {
test('should fail if mailinglists is an empty array', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken(user);

Expand All @@ -97,9 +97,7 @@ describe('User subscribe listserv', () => {
expect(res.body).toHaveProperty('message');
});

test('should fail if mailer fails', async () => {
mock.mockAll({ mailer: { netError: true } });

test('should fail if mailinglists includes invalid mailing lists', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken(user);

Expand All @@ -109,88 +107,109 @@ describe('User subscribe listserv', () => {
uri: '/members/' + user.id + '/listserv',
method: 'POST',
headers: { 'X-Auth-Token': token.value },
body: { mailinglists: ['ANNOUNCE-L'] }
body: { mailinglists: ['ANNOUNCE-L', 'BOARDINF-L'] }
});

expect(res.statusCode).toEqual(500);
expect(res.statusCode).toEqual(422);
expect(res.body.success).toEqual(false);
expect(res.body).not.toHaveProperty('data');
expect(res.body).toHaveProperty('message');
});

test('should succeed for one mailinglist if everything is okay', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken(user);

await generator.createPermission({ scope: 'global', action: 'subscribe', object: 'listserv' });

const res = await request({
uri: '/members/' + user.id + '/listserv',
method: 'POST',
headers: { 'X-Auth-Token': token.value },
body: { mailinglists: ['ANNOUNCE-L'] }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).not.toHaveProperty('errors');
expect(res.body).toHaveProperty('message');
expect(res.body.message).toEqual('Request for subscribing to ANNOUNCE-L has been sent.');
});

test('should succeed for multiple mailinglists if everything is okay', async () => {
const user = await generator.createUser({ superadmin: true });
const token = await generator.createAccessToken(user);

await generator.createPermission({ scope: 'global', action: 'subscribe', object: 'listserv' });

const res = await request({
uri: '/members/' + user.id + '/listserv',
method: 'POST',
headers: { 'X-Auth-Token': token.value },
body: { mailinglists: ['announce-l, AEGEE-L, AeGeEnEwS-l'] }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).not.toHaveProperty('errors');
expect(res.body).toHaveProperty('message');
expect(res.body.message).toEqual('Request for subscribing to ANNOUNCE-L, AEGEE-L, AEGEENEWS-L has been sent.');
expect(res.body.message).toEqual('Mailinglists must be one of the following: AEGEE-L, AEGEENEWS-L, ANNOUNCE-L, AEGEE-EVENT-L.');
});

test('should work for current user for /me without permission', async () => {
const user = await generator.createUser();
const token = await generator.createAccessToken(user);

const res = await request({
uri: '/members/' + user.id + '/listserv',
method: 'POST',
headers: { 'X-Auth-Token': token.value },
body: { mailinglists: ['ANNOUNCE-L'] }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).not.toHaveProperty('errors');
expect(res.body).toHaveProperty('message');
});

test('should work for current user for /:user_id without permission', async () => {
const user = await generator.createUser();
const token = await generator.createAccessToken(user);

const res = await request({
uri: '/members/' + user.id + '/listserv',
method: 'POST',
headers: { 'X-Auth-Token': token.value },
body: { mailinglists: ['ANNOUNCE-L'] }
});

expect(res.statusCode).toEqual(200);
expect(res.body.success).toEqual(true);
expect(res.body).not.toHaveProperty('errors');
expect(res.body).toHaveProperty('message');
});
// test('should fail if mailer fails', async () => {
// mock.mockAll({ mailer: { netError: true } });

// const user = await generator.createUser({ superadmin: true });
// const token = await generator.createAccessToken(user);

// await generator.createPermission({ scope: 'global', action: 'subscribe', object: 'listserv' });

// const res = await request({
// uri: '/members/' + user.id + '/listserv',
// method: 'POST',
// headers: { 'X-Auth-Token': token.value },
// body: { mailinglists: ['ANNOUNCE-L'] }
// });

// expect(res.statusCode).toEqual(500);
// expect(res.body.success).toEqual(false);
// expect(res.body).not.toHaveProperty('data');
// expect(res.body).toHaveProperty('message');
// });

// test('should succeed for one mailinglist if everything is okay', async () => {
// const user = await generator.createUser({ superadmin: true });
// const token = await generator.createAccessToken(user);

// await generator.createPermission({ scope: 'global', action: 'subscribe', object: 'listserv' });

// const res = await request({
// uri: '/members/' + user.id + '/listserv',
// method: 'POST',
// headers: { 'X-Auth-Token': token.value },
// body: { mailinglists: ['ANNOUNCE-L'] }
// });

// expect(res.statusCode).toEqual(200);
// expect(res.body.success).toEqual(true);
// expect(res.body).not.toHaveProperty('errors');
// expect(res.body).toHaveProperty('message');
// expect(res.body.message).toEqual('Request for subscribing to ANNOUNCE-L has been sent.');
// });

// test('should succeed for multiple mailinglists if everything is okay', async () => {
// const user = await generator.createUser({ superadmin: true });
// const token = await generator.createAccessToken(user);

// await generator.createPermission({ scope: 'global', action: 'subscribe', object: 'listserv' });

// const res = await request({
// uri: '/members/' + user.id + '/listserv',
// method: 'POST',
// headers: { 'X-Auth-Token': token.value },
// body: { mailinglists: ['announce-l, AEGEE-L, AeGeEnEwS-l'] }
// });

// expect(res.statusCode).toEqual(200);
// expect(res.body.success).toEqual(true);
// expect(res.body).not.toHaveProperty('errors');
// expect(res.body).toHaveProperty('message');
// expect(res.body.message).toEqual('Request for subscribing to ANNOUNCE-L, AEGEE-L, AEGEENEWS-L has been sent.');
// });

// test('should work for current user for /me without permission', async () => {
// const user = await generator.createUser();
// const token = await generator.createAccessToken(user);

// const res = await request({
// uri: '/members/' + user.id + '/listserv',
// method: 'POST',
// headers: { 'X-Auth-Token': token.value },
// body: { mailinglists: ['ANNOUNCE-L'] }
// });

// expect(res.statusCode).toEqual(200);
// expect(res.body.success).toEqual(true);
// expect(res.body).not.toHaveProperty('errors');
// expect(res.body).toHaveProperty('message');
// });

// test('should work for current user for /:user_id without permission', async () => {
// const user = await generator.createUser();
// const token = await generator.createAccessToken(user);

// const res = await request({
// uri: '/members/' + user.id + '/listserv',
// method: 'POST',
// headers: { 'X-Auth-Token': token.value },
// body: { mailinglists: ['ANNOUNCE-L'] }
// });

// expect(res.statusCode).toEqual(200);
// expect(res.body.success).toEqual(true);
// expect(res.body).not.toHaveProperty('errors');
// expect(res.body).toHaveProperty('message');
// });

test('should not work with local permission', async () => {
const user = await generator.createUser();
Expand Down

0 comments on commit 80f1545

Please sign in to comment.