Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions api/elections/index.e2e.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
'use strict'

const {expect} = require('chai')
const chai = require('chai')
const supertest = require('supertest')
const app = require('../../app')
const mongoose = require('mongoose')
const {model: electionsSchema} = require('../../models/elections/schema')

const mongodbUrl = 'mongodb://root:password123@localhost:27017,localhost:27018,localhost:27019/elecTest?replicaSet=replicat&authSource=admin'

describe('POST /elections', () => {

before(async () => {
await mongoose.connect(mongodbUrl)
if (mongoose.connection.readyState !== 1) throw new Error('mongodb connection failed')
})

beforeEach(async () => {
})

afterEach(async () => {
await electionsSchema.deleteMany({})
});

after(async () => {
// await mongoose.connection.db.dropDatabase();
});

it('should return validation error on party field missing', async () => {
await supertest(app)
.post('/elections')
.send({
candidate: 'some'
})
.expect(400)
.then(res => {
expect(res.body.details[0].message).to.equal('"party" is required')
});
});

it('should return success on validation pass', async () => {
await supertest(app)
.post('/elections')
.send({
candidate: 'some',
party: 'jubilee'
})
.expect(200)
.then(res => {
const {candidate, votes, party, __v} = res.body.data

return expect({candidate, votes, party, __v}).to.eql({
candidate: 'some',
votes: '0',
party: 'jubilee',
__v: 0
});
});
});

it('should return duplication error on candidate field', async () => {
await electionsSchema.create({
candidate: 'some',
party: 'jubilee',
votes: 10
})

await supertest(app)
.post('/elections')
.send({
candidate: 'some',
party: 'jubilee',
votes: 10
})
.expect(400)
.then(res => {
return expect(res.body).to.eql({
name: 'DUPLICATE_KEY_ERROR',
param: 'votes',
value: '10'
});
});
});
})
3 changes: 2 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ app.get('/', (req, res, next) => {

app.use('/elections', electionsRouter)

// app.use(errorTransformer) - TO BE ADDED EVENTUALLY

app.use((err, req, res, next) => {
console.log(err)
res.status(400).send(err)
})

Expand Down
11 changes: 10 additions & 1 deletion models/elections/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ const { model } = require('./schema')

const electionsModel = {
create: (data) => {
return model.create(data)
return model.create(data).catch((err) => {
if (err.code === 11000) {
const error = new Error('unique error on key candidate')
error.name = 'DUPLICATE_KEY_ERROR'
error.param = Object.keys(err.keyPattern)[0]
error.value = err.keyValue[error.param]
throw error
}
throw err;
});
},

fetch: ({ query } = {}) => {
Expand Down
7 changes: 4 additions & 3 deletions models/elections/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ const mongoose = require('mongoose')
const schema = mongoose.Schema({
candidate: {
type: String,
unique: true,
required: true
required: true,
unique: true
},
votes: {
type: String,
required: true
required: true,
unique: true
},
party: {
type: String,
Expand Down
Loading