Soy bastante nuevo en las pruebas de integración y estoy tratando de enviar una solicitud simulada usando mocha y chai http pero no tengo suerte. Quiero probar y verificar un jwt pasando un token simulado que creo, sin embargo, todos los datos se pasan al req.body.
¿Hay alguna forma de enviar el requerimiento completo en lugar de simplemente pasar los datos en el cuerpo del requerimiento a través de .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({ }); };