Skip to content

Commit

Permalink
feature/implement-edit-event
Browse files Browse the repository at this point in the history
- implement edit event feature
- write tests for editing event feature
  • Loading branch information
Billmike committed Apr 17, 2018
1 parent 17f6906 commit 50dd5e0
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 7 deletions.
111 changes: 111 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"body-parser": "^1.18.2",
"dotenv": "^5.0.1",
"express": "^4.16.3",
"helmet": "^3.12.0",
"jest": "^22.4.3",
"jsonwebtoken": "^8.2.1",
"lodash": "^4.17.5",
Expand Down
7 changes: 4 additions & 3 deletions server/app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import express from 'express';
import bodyParser from 'body-parser';
import helmet from 'helmet';
import logger from 'morgan';
import dotenv from 'dotenv';

dotenv.config();

const app = express();

app.use(helmet());
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

require('./routes')(app);

Expand Down
30 changes: 30 additions & 0 deletions server/controllers/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,36 @@ class Events {
});
});
}
static modifyEvent(request, response) {
Event.findById(request.params.eventId)
.then((event) => {
if (!event) {
return response.status(404).json({
message: 'No event found.'
});
}
if (event.organizer != request.userDetails.id) {
return response.status(401).json({
message: 'You do not have the privilege to modify this resource'
});
}
return event.update({
name: request.body.name || event.name,
image: request.body.image || event.image,
date: request.body.date || event.date,
duration: request.body.duration || event.duration
}).then((updatedEvent) => {
return response.status(201).json({
message: 'Event modified successfully.',
eventDetails: updatedEvent
});
});
}).catch((err) => {
return response.status(500).json({
message: serverError
});
});
}
}

export default Events;
5 changes: 5 additions & 0 deletions server/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,9 @@ module.exports = (app) => {
eventController.addEvent
);
app.get('/api/v1/events', eventController.getEvents);
app.put(
'/api/v1/event/:eventId',
sessionControl.isLoggedIn, sessionControl.isUser,
eventController.modifyEvent
);
};
46 changes: 42 additions & 4 deletions server/tests/event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import supertest from 'supertest';
import { expect } from 'chai';
import app from '../app';
import eventSeed from './seed/eventSeed';
import dummyUser from './seed/userseed';
import dummyUser, { secondDummyUser } from './seed/userseed';

const request = supertest(app);
const eventApi = '/api/v1/events';

describe('Tests for Events API', () => {
before((done) => {
request.post('/api/v1/users/signup')
.send(secondDummyUser)
.end(() => {
done();
});
});
describe('Tests for events creation', () => {
it('should fail to create an event if the user is not signed in', (done) => {
request.post(eventApi)
Expand All @@ -26,7 +33,7 @@ describe('Tests for Events API', () => {
it('should fail to create an event if no name is provided', (done) => {
const noName = { ...eventSeed };
noName.name = '';
request.post(`/api/v1/events?token=${dummyUser.token}`)
request.post(`/api/v1/events?token=${secondDummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
Expand Down Expand Up @@ -57,7 +64,7 @@ describe('Tests for Events API', () => {
it('should fail to create an event if no duration is supplied', (done) => {
const noDuration = { ...eventSeed };
noDuration.duration = '';
request.post(`/api/v1/events?token=${dummyUser.token}`)
request.post(`/api/v1/events?token=${secondDummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
Expand Down Expand Up @@ -103,7 +110,7 @@ describe('Tests for Events API', () => {
it(
'should create an event successfully if all values are supplied',
(done) => {
request.post(`/api/v1/events?token=${dummyUser.token}`)
request.post(`/api/v1/events?token=${secondDummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
Expand Down Expand Up @@ -135,4 +142,35 @@ describe('Tests for Events API', () => {
});
});
});
describe('Edit events test', () => {
it('should fail to edit an event if a user is not signed in', (done) => {
request.put(`/api/v1/event/${eventSeed.id}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.send(eventSeed)
.end((error, response) => {
expect(response.status).to.equal(401);
expect(response.body).to.be.an('object');
expect(response.body.message).to
.equal('Please sign into your account to access this resource.');
done();
});
});
it('should edit an event successfully', (done) => {
request.put(`/api/v1/event/${eventSeed.id}?token=${secondDummyUser.token}`)
.set('Connection', 'keep alive')
.set('Content-Type', 'application/json')
.type('form')
.send(eventSeed)
.end((error, response) => {
expect(response.status).to.equal(201);
expect(response.body).to.be.an('object');
expect(response.body.message)
.to.equal('Event modified successfully.');
expect(response.body.eventDetails).to.be.an('object');
done();
});
});
});
});
8 changes: 8 additions & 0 deletions server/tests/seed/eventSeed.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
const eventSeed = {
id: 1,
name: 'Biker sports event',
duration: 2,
date: '2018-03-05 12:01:18.936+01',
venue: 'Yaba Beach'
};

export const secondEvent = {
id: 2,
name: 'Dripping migos',
duration: '2018-03-05 12:01:18.936+01',
venue: 'Yaba Beach'
};

export default eventSeed;
Loading

0 comments on commit 50dd5e0

Please sign in to comment.