1 year ago
#345185
Joe Thompson
How to send mocked request using chai http
I am fairly new to integration testing and I am trying to send a mock request using mocha and chai http but having no luck. I want to try and have a jwt verified by passing in a mocked token which I create, however, all of the data is being passed into the req.body.
Is there a way of sending the entire req instead of just passing data in the req.body through the .send()?
// TEST I AM RUNNING
it('should POST /newCertificate for an Accreditor ',async()=>{
let status,
json,
res;
status = stub();
json = spy();
res = { json, status };
status.returns(res);
let title = 'title';
let recipientEmail = 'student@email.com';
let grade = 'grade';
let type = 'type';
let department = 'department';
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6IjYyNDFhMjhlY2Y4Y2FlOWFiYmQ4Nzg5NSIsImlhdCI6MTY0ODQ2ODYyMiwiZXhwIjoxNjQ4NzI3ODIyfQ.KU6Un3YPq79TRruTrM8y2BrvgRH8xOn882LVy6vwhU4'
const body = JSON.stringify({ title, recipientEmail, grade, type, department })
const req = mockRequest(token, body);
let res2 = await chai
.request(app)
.post('/newCertificateRoutes/newCertificate')
.set('content-type', 'application/x-www-form-urlencoded')
.send(req) // I AM TRYING TO SEND THE ENTIRE req but it only adds req to req.body
expect(res2.status).toHaveBeenCalledWith(201);
});
// FUNCTION I AM CALLING
module.exports.newCertificate_post = async (req, res) => {
let { title, recipientEmail, grade, type, department } = req.body;
const token = req.cookies.jwt;
let accreditorId = null;
let departmentId = null;
console.log("token: ", token);
console.log("req.body: ", req.body);
// check if department or accreditor/admin is issueing degree
jwt.verify(token, 'ultralongsecretkeywhichnooneisabletoguessaccreditor', (err, decodedToken) => {
if (err) {
jwt.verify(token, 'ultralongsecretkeywhichnooneisabletoguessdepartment', (err, decodedToken) => {
if (err) {
console.log(err.message);
} else {
departmentId = decodedToken.id
}
});
} else {
accreditorId = decodedToken.id;
}
});
console.log("departmentId: ", departmentId);
console.log("accreditorId: ", accreditorId);
res.status(201).json({ });
};
javascript
node.js
testing
mocha.js
chai
0 Answers
Your Answer