Skip to content

Commit

Permalink
chore: merge pull request #55 from AEGEE/fix-organizers-management
Browse files Browse the repository at this point in the history
Fix organizers management
  • Loading branch information
serge1peshcoff authored Mar 13, 2019
2 parents ac56a99 + a71dbcc commit ed873af
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 24 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
# [0.6.0](https://github.com/AEGEE/oms-events/compare/0.5.0...0.6.0) (2019-03-13)


### Features

* **event:** fix organizers management. Fixes MEMB-191 ([b95aae0](https://github.com/AEGEE/oms-events/commit/b95aae0))



# [0.5.0](https://github.com/AEGEE/oms-events/compare/0.4.3...0.5.0) (2019-03-11)


Expand Down
48 changes: 40 additions & 8 deletions lib/events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
const request = require('request-promise-native');

const errors = require('./errors');
const config = require('../config');
const merge = require('./merge');
const helpers = require('./helpers');
const { Event, Application } = require('../models');
Expand Down Expand Up @@ -233,12 +236,37 @@ exports.addOrganizer = async (req, res) => {
return errors.makeBadRequestError(res, 'User with id ' + req.body.user_id + ' is already an organizer.');
}

req.event.organizers.push({
// Fetching the organizer from core.
const user = await request({
url: config.core.url + ':' + config.core.port + '/members/' + req.body.user_id,
method: 'GET',
headers: {
'X-Requested-With': 'XMLHttpRequest',
'X-Auth-Token': req.headers['x-auth-token'],
},
simple: false,
json: true
});

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

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

const organizers = req.event.organizers;
organizers.push({
user_id: req.body.user_id,
comment: req.body.comment
comment: req.body.comment,
first_name: user.data.first_name,
last_name: user.data.last_name
});

await req.event.save();
await req.event.update({
organizers
});

return res.json({
success: true,
Expand All @@ -260,10 +288,11 @@ exports.editOrganizer = async (req, res) => {
if (!organizer) {
return errors.makeNotFoundError(res, 'Organizer with id ' + userId + ' is not found.');
}
organizer.comment = req.body.comment;

if (req.body.comment) organizer.comment = req.body.comment;

await req.event.save();
await req.event.update({
organizers: req.event.organizers
});

return res.json({
success: true,
Expand All @@ -286,9 +315,12 @@ exports.deleteOrganizer = async (req, res) => {
return errors.makeNotFoundError(res, 'Organizer with id ' + userId + ' is not found.');
}

req.event.organizers.splice(organizerIndex, 1);
const organizers = JSON.parse(JSON.stringify(req.event.organizers));
organizers.splice(organizerIndex, 1);

await req.event.save();
await req.event.update({
organizers
});

return res.json({
success: true,
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.5.0",
"version": "0.6.0",
"description": "Events-module of the OMS",
"main": "server.js",
"directories": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ describe('Events organization management', () => {
});

it('should disallow adding organizer for event you cannot edit', async () => {
const notMineEvent = await generator.createEvent({ organizers: [{ user_id: 1337 }] });
const notMineEvent = await generator.createEvent({ organizers: [{
user_id: 1337,
first_name: 'test',
last_name: 'test'
}] });
mock.mockAll({ mainPermissions: { noPermissions: true } });

const res = await request({
Expand All @@ -29,6 +33,8 @@ describe('Events organization management', () => {
headers: { 'X-Auth-Token': 'blablabla' },
body: {
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name,
comment: 'Good guy'
}
});
Expand All @@ -40,14 +46,20 @@ describe('Events organization management', () => {
});

it('should disallow adding organizer if the user is already an organizer', async () => {
const eventImOrganizing = await generator.createEvent({ organizers: [{ user_id: user.id }] });
const eventImOrganizing = await generator.createEvent({ organizers: [{
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name
}] });

const res = await request({
uri: '/single/' + eventImOrganizing.id + '/organizers/',
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: {
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name,
comment: 'Not that good guy'
}
});
Expand All @@ -59,14 +71,20 @@ describe('Events organization management', () => {
});

it('should allow adding organizer for event you can edit', async () => {
const event = await generator.createEvent({ organizers: [{ user_id: 1337 }] });
const event = await generator.createEvent({ organizers: [{
user_id: 1337,
first_name: 'test',
last_name: 'test'
}] });

const res = await request({
uri: '/single/' + event.id + '/organizers/',
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: {
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name,
comment: 'Good guy'
}
});
Expand All @@ -81,8 +99,87 @@ describe('Events organization management', () => {
expect(newEvent.organizers.map(org => org.comment)).toContain('Good guy');
});

it('should return 500 on adding organizer if oms-core returns net error', async () => {
mock.mockAll({ member: { netError: true } });
const event = await generator.createEvent({ organizers: [{
user_id: 1337,
first_name: 'test',
last_name: 'test'
}] });

const res = await request({
uri: '/single/' + event.id + '/organizers/',
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: {
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name,
comment: 'Good guy'
}
});

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

it('should return 500 on adding organizer if oms-core returns bad response', async () => {
mock.mockAll({ member: { badResponse: true } });
const event = await generator.createEvent({ organizers: [{
user_id: 1337,
first_name: 'test',
last_name: 'test'
}] });

const res = await request({
uri: '/single/' + event.id + '/organizers/',
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: {
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name,
comment: 'Good guy'
}
});

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

it('should return 500 on adding organizer if oms-core returns unsuccessful response', async () => {
mock.mockAll({ member: { unsuccessfulResponse: true } });
const event = await generator.createEvent({ organizers: [{
user_id: 1337,
first_name: 'test',
last_name: 'test'
}] });

const res = await request({
uri: '/single/' + event.id + '/organizers/',
method: 'POST',
headers: { 'X-Auth-Token': 'blablabla' },
body: {
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name,
comment: 'Good guy'
}
});

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

it('should disallow editing organizer for event you cannot edit', async () => {
const notMineEvent = await generator.createEvent({ organizers: [{ user_id: 1337 }] });
const notMineEvent = await generator.createEvent({ organizers: [{
user_id: 1337,
first_name: 'test',
last_name: 'test'
}] });
mock.mockAll({ mainPermissions: { noPermissions: true } });

const res = await request({
Expand All @@ -101,7 +198,11 @@ describe('Events organization management', () => {
});

it('should disallow editing organizer if userId is not a number', async () => {
const event = await generator.createEvent({ organizers: [{ user_id: user.id }] });
const event = await generator.createEvent({ organizers: [{
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name
}] });
const res = await request({
uri: '/single/' + event.id + '/organizers/lalala',
method: 'PUT',
Expand All @@ -118,7 +219,11 @@ describe('Events organization management', () => {
});

it('should disallow editing organizer if the user is not an organizer', async () => {
const event = await generator.createEvent({ organizers: [{ user_id: user.id }] });
const event = await generator.createEvent({ organizers: [{
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name
}] });
const res = await request({
uri: '/single/' + event.id + '/organizers/1337',
method: 'PUT',
Expand All @@ -135,7 +240,11 @@ describe('Events organization management', () => {
});

it('should allow editing organizer for event you can edit', async () => {
const event = await generator.createEvent({ organizers: [{ user_id: user.id }] });
const event = await generator.createEvent({ organizers: [{
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name
}] });
const res = await request({
uri: '/single/' + event.id + '/organizers/' + user.id,
method: 'PUT',
Expand All @@ -156,7 +265,11 @@ describe('Events organization management', () => {
});

it('should disallow deleting organizer for event you cannot edit', async () => {
const notMineEvent = await generator.createEvent({ organizers: [{ user_id: 1337 }] });
const notMineEvent = await generator.createEvent({ organizers: [{
user_id: 1337,
first_name: 'test',
last_name: 'test'
}] });
mock.mockAll({ mainPermissions: { noPermissions: true } });

const res = await request({
Expand All @@ -172,7 +285,11 @@ describe('Events organization management', () => {
});

it('should disallow deleting organizer if userId is not a number', async () => {
const event = await generator.createEvent({ organizers: [{ user_id: user.id }] });
const event = await generator.createEvent({ organizers: [{
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name
}] });
const res = await request({
uri: '/single/' + event.id + '/organizers/lalala',
method: 'DELETE',
Expand All @@ -186,7 +303,11 @@ describe('Events organization management', () => {
});

it('should disallow deleting organizer if the user is not an organizer', async () => {
const event = await generator.createEvent({ organizers: [{ user_id: user.id }] });
const event = await generator.createEvent({ organizers: [{
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name
}] });
const res = await request({
uri: '/single/' + event.id + '/organizers/1337',
method: 'DELETE',
Expand All @@ -200,7 +321,11 @@ describe('Events organization management', () => {
});

it('should disallow deleting organizer if the user is only organizer', async () => {
const myEvent = await generator.createEvent({ organizers: [{ user_id: user.id }] });
const myEvent = await generator.createEvent({ organizers: [{
user_id: 1337,
first_name: 'test',
last_name: 'test'
}] });
const res = await request({
uri: '/single/' + myEvent.id + '/organizers/1337',
method: 'DELETE',
Expand All @@ -210,11 +335,20 @@ describe('Events organization management', () => {
expect(res.statusCode).toEqual(422);

expect(res.body.success).toEqual(false);
expect(res.body).toHaveProperty('message');
expect(res.body).toHaveProperty('errors');
expect(res.body.errors).toHaveProperty('organizers');
});

it('should allow deleting organizer for event you can edit', async () => {
const myEvent = await generator.createEvent({ organizers: [{ user_id: user.id }, { user_id: 1337 }] });
const myEvent = await generator.createEvent({ organizers: [{
user_id: user.id,
first_name: user.first_name,
last_name: user.last_name
}, {
user_id: 1337,
first_name: 'test',
last_name: 'test'
}] });

const res = await request({
uri: '/single/' + myEvent.id + '/organizers/1337',
Expand Down
Loading

0 comments on commit ed873af

Please sign in to comment.