Here is my controller code and below is my testing code… Please how do I test the secure api router?
import Joi from 'joi';
import Rsvp from '../models/rsvp';
import mongoose from 'mongoose';
/**
*Validator params
* @param {} post
*/
const validatePost = (rsvp) => {
const schema = Joi.object().keys({
meetup: Joi.string().trim().required(),
response: Joi.string().trim().insensitive().valid('yes', 'no', 'maybe')
});
return Joi.validate(rsvp, schema);
};
exports.post_rsvp = (req, res) => {
const { error } = validatePost(req.body);
if (error) return res.status(422).json({ message: error.details[0].message });
const rsvp = new Rsvp({
_id: new mongoose.Types.ObjectId(),
meetup: req.body.meetup,
response: req.body.response,
});
rsvp
.save()
.then((result) => {
res.status(201).json({
message: 'Created rsvp successfully',
createdRsvp: {
_id:result._id,
meetup: result.meetup,
response: result.response,
request: {
type: 'GET',
url: 'http://localhost:3000/api/v1/rsvp/' + result._id
}
}
});
})
.catch( err => {
return res.status(422).json(err);
});
};
exports.get_all_rsvp = (req, res) => {
Rsvp.find()
.populate('meetup','topic')
.exec()
.then((results) => {
const response = {
count: results.length,
Rsvp: results.map(result => {
return {
_id:result._id,
meetup: result.meetup,
response: result.response,
request: {
type: 'GET',
url: 'http://localhost:3000/api/v1/rsvp/' + result._id
}
};
})
};
res.status(200).json(response);
})
.catch((err) => {
res.status(500).json(err);
});
};
exports.get_rsvp = (req, res) => {
const id = req.params.id;
Rsvp.findById(id)
.populate('meetup')
.exec()
.then((result) => {
const response = {
Rsvp: {
_id:result._id,
meetup: result.meetup,
response: result.response,
request: {
type: 'GET',
url: 'http://localhost:3000/api/v1/rsvp/' + result._id
}
}
};
res.status(200).json(response);
})
.catch((err) => {
res.status(500).json(err);
});
};
exports.delete_rsvp = (req, res) => {
const id = req.params.id;
Rsvp.deleteOne({ _id: id })
.exec()
.then(() => {
return res.status(200).json({
message: `the rsvp with ID:${id} has successfully been deleted`,
request: {
type: 'GET',
url: 'http://localhost:3000/api/v1/rsvp/'
}
});
})
.catch(err => {
return res.status(500).json({
error: err
});
});
};
Here is testing code
process.env.NODE_ENV === 'test';
import chai from 'chai';
import request from 'supertest';
import {app, connect, close} from '../../app';
const { expect } = chai;
let token;
before((done) => {
connect()
.then(() => done())
.catch((err) => done(err));
});
after((done) => {
close()
.then(() => done())
.catch((err) => done(err));
});
/**
* Endpoint unit tests for rsvp api
*/
describe('/testing of rsvp module', () => {
const userCredentials = {
email: 'kazmobileapp@gmail.com',
password: 'Kazeem27'
};
before((done) => {
request(app)
.post('/api/v1/user/login')
.send(userCredentials)
.end((err) => done(err));
done();
});
describe('POST /rsvp', ()=> {
it('should post rsvp succesfully', (done) => {
request(app)
.post('/api/v1/rsvp')
.set('Authorization', 'Bearer' + token)
.send({ response: 'yes', meetup: '5c77ad6c60dd92011cb893a3' })
.then((res) => {
const body = res.body;
expect(res.status).be.equal(201);
expect(body).to.be.an('object');
expect(body).to.have.property('message').and.equal('Created rsvp successfully');
expect(body).to.have.property('createdRsvp').and.haveOwnProperty('_id');
expect(body).to.have.property('createdRsvp').and.haveOwnProperty('response');
expect(body).to.have.property('createdRsvp').and.haveOwnProperty('request');
done();
})
.catch((err) => done(err));
});
it('should fail to post rsvp', (done) => {
request(app)
.post('/api/v1/rsvp')
.send({ response: 'going' })
.then((res) => {
expect(res.status).be.equal(422);
done();
})
.catch((err) => done(err));
});
});
});
Here is my router
import express from 'express';
import checkAuth from '../middleware/check-auth';
import rsvpControllers from '../controllers/rsvp';
const router = express.Router();
/**
* Rsvp endpoint
*/
router.post('/', checkAuth, rsvpControllers.post_rsvp);
/**
* GET all rsvp api endpoint
*/
router.get('/', checkAuth, rsvpControllers.get_all_rsvp);
/**
* GET single rsvp
*/
router.get('/:id', checkAuth, rsvpControllers.get_rsvp);
/*
*restful api to delete rsvp
*/
router.delete('/:id', checkAuth, rsvpControllers.delete_rsvp);
module.exports = router;