Skip to content

Commit

Permalink
chore: merge pull request #157 from AEGEE/send-emails-to-eqac
Browse files Browse the repository at this point in the history
Send emails to EQAC
  • Loading branch information
serge1peshcoff authored Jan 22, 2020
2 parents 6162d46 + a2bfe34 commit 00d2a8c
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 245 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
<a name="0.22.2"></a>
## [0.22.2](https://github.com/AEGEE/oms-events/compare/0.22.1...0.22.2) (2020-01-22)


### Bug Fixes

* **events:** send emails to addresses specified. Fixes MEMB-774 ([0da7f7f](https://github.com/AEGEE/oms-events/commit/0da7f7f))



<a name="0.22.1"></a>
## [0.22.1](https://github.com/AEGEE/oms-events/compare/0.22.0...0.22.1) (2020-01-12)

Expand Down
3 changes: 3 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ const config = {
url: 'http://oms-mailer',
port: 4000
},
new_event_notifications: [
'events-approval@aegee.eu'
],
logger: {
silent: false,
level: process.env.LOGLEVEL || 'debug'
Expand Down
63 changes: 1 addition & 62 deletions lib/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ const makeRequest = (options) => {
resolveWithFullResponse: options.resolveWithFullResponse || false
};

if (options.body) {
requestOptions.body = options.body;
}

return request(requestOptions);
};

Expand Down Expand Up @@ -70,64 +66,7 @@ const fetchBody = async (body, token) => {
};
};

const fetchUsersWithPermission = async (permission) => {
// Getting access and refresh token.
const authRequest = await makeRequest({
url: config.core.url + ':' + config.core.port + '/login',
method: 'POST',
body: {
username: config.core.user.login,
password: config.core.user.password
}
});

if (typeof authRequest !== 'object') {
throw new Error('Malformed response when fetching auth: ' + authRequest);
}

if (!authRequest.success) {
throw new Error('Error fetching auth: ' + JSON.stringify(authRequest));
}

// Fetching permissions.
const permissionsResponse = await makeRequest({
url: config.core.url + ':' + config.core.port + '/permissions',
token: authRequest.access_token
});

if (typeof permissionsResponse !== 'object') {
throw new Error('Malformed response when fetching permissions: ' + permissionsResponse);
}

if (!permissionsResponse.success) {
throw new Error('Error fetching permissions: ' + JSON.stringify(permissionsResponse));
}

// Finding a permission.
const permissionToFind = permissionsResponse.data.find((elt) => elt.combined.includes(permission));
if (!permissionToFind) {
throw new Error(`No permission found: "${permission}".`);
}

// Fetching permissions users.
const permissionsMembersResponse = await makeRequest({
url: config.core.url + ':' + config.core.port + '/permissions/' + permissionToFind.id + '/members',
token: authRequest.access_token
});

if (typeof permissionsMembersResponse !== 'object') {
throw new Error('Malformed response when fetching permission members: ' + permissionsMembersResponse);
}

if (!permissionsMembersResponse.success) {
throw new Error('Error fetching permission members: ' + JSON.stringify(permissionsMembersResponse));
}

return permissionsMembersResponse.data;
};

module.exports = {
fetchUser,
fetchBody,
fetchUsersWithPermission
fetchBody
};
11 changes: 2 additions & 9 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { Event, Application } = require('../models');
const { Sequelize, sequelize } = require('./sequelize');
const core = require('./core');
const mailer = require('./mailer');
const config = require('../config');

exports.listEvents = async (req, res) => {
// Get default query obj.
Expand Down Expand Up @@ -275,16 +276,8 @@ exports.setApprovalStatus = async (req, res) => {
return;
}

// GET /permissions/:id/members can return the same user multiple times (I suppose
// if a user is a member of multiple bodies which have this permission), so we need
// to filter it so emails list won't contain duplicates.
const membersWthPermissions = await core.fetchUsersWithPermission('approve_event:' + req.event.type);
const emails = membersWthPermissions
.map((member) => member.user.email)
.filter((elt, index, array) => array.indexOf(elt) === index);

await mailer.sendMail({
to: emails,
to: config.new_event_notifications,
subject: 'A new event was submitted.',
template: 'events_submitted.html',
parameters: {
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "oms-events",
"version": "0.22.1",
"version": "0.22.2",
"description": "Events-module of the OMS",
"main": "server.js",
"directories": {
Expand Down
172 changes: 0 additions & 172 deletions test/api/events-approval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -496,176 +496,4 @@ describe('Events status change', () => {
expect(res.body).toHaveProperty('message');
});
});

describe('if core fails', () => {
it('should return 500 if core login returns net error', async () => {
mock.mockAll({ login: { netError: true } });

const event = await generator.createEvent({ status: 'draft' });

const res = await request({
uri: '/single/' + event.id + '/status',
headers: { 'X-Auth-Token': 'foobar' },
method: 'PUT',
body: { status: 'submitted' }
});

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

it('should return 500 if core login returns bad response', async () => {
mock.mockAll({ login: { badResponse: true } });

const event = await generator.createEvent({ status: 'draft' });

const res = await request({
uri: '/single/' + event.id + '/status',
headers: { 'X-Auth-Token': 'foobar' },
method: 'PUT',
body: { status: 'submitted' }
});

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

it('should return 500 if core login returns unsuccessful response', async () => {
mock.mockAll({ login: { unsuccessfulResponse: true } });

const event = await generator.createEvent({ status: 'draft' });

const res = await request({
uri: '/single/' + event.id + '/status',
headers: { 'X-Auth-Token': 'foobar' },
method: 'PUT',
body: { status: 'submitted' }
});

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

it('should return 500 if core permissions returns net error', async () => {
mock.mockAll({ permissions: { netError: true } });

const event = await generator.createEvent({ status: 'draft' });

const res = await request({
uri: '/single/' + event.id + '/status',
headers: { 'X-Auth-Token': 'foobar' },
method: 'PUT',
body: { status: 'submitted' }
});

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

it('should return 500 if core permissions returns bad response', async () => {
mock.mockAll({ permissions: { badResponse: true } });

const event = await generator.createEvent({ status: 'draft' });

const res = await request({
uri: '/single/' + event.id + '/status',
headers: { 'X-Auth-Token': 'foobar' },
method: 'PUT',
body: { status: 'submitted' }
});

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

it('should return 500 if core permissions returns unsuccessful response', async () => {
mock.mockAll({ permissions: { unsuccessfulResponse: true } });

const event = await generator.createEvent({ status: 'draft' });

const res = await request({
uri: '/single/' + event.id + '/status',
headers: { 'X-Auth-Token': 'foobar' },
method: 'PUT',
body: { status: 'submitted' }
});

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

it('should return 500 if core permissions returns empty response', async () => {
mock.mockAll({ permissions: { noPermissions: true } });

const event = await generator.createEvent({ status: 'draft' });

const res = await request({
uri: '/single/' + event.id + '/status',
headers: { 'X-Auth-Token': 'foobar' },
method: 'PUT',
body: { status: 'submitted' }
});

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

it('should return 500 if core permission members returns net error', async () => {
mock.mockAll({ permissionMembers: { netError: true } });

const event = await generator.createEvent({ status: 'draft' });

const res = await request({
uri: '/single/' + event.id + '/status',
headers: { 'X-Auth-Token': 'foobar' },
method: 'PUT',
body: { status: 'submitted' }
});

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

it('should return 500 if core permission members returns bad response', async () => {
mock.mockAll({ permissionMembers: { badResponse: true } });

const event = await generator.createEvent({ status: 'draft' });

const res = await request({
uri: '/single/' + event.id + '/status',
headers: { 'X-Auth-Token': 'foobar' },
method: 'PUT',
body: { status: 'submitted' }
});

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

it('should return 500 if core permission members returns unsuccessful response', async () => {
mock.mockAll({ permissionMembers: { unsuccessfulResponse: true } });

const event = await generator.createEvent({ status: 'draft' });

const res = await request({
uri: '/single/' + event.id + '/status',
headers: { 'X-Auth-Token': 'foobar' },
method: 'PUT',
body: { status: 'submitted' }
});

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

0 comments on commit 00d2a8c

Please sign in to comment.