I have a route that records user data and gives a unique identifier.
router.post('/', (req, res) => {
const user_email = req.body.user_email;
const username = req.body.username;
const user_phone = req.body.user_phone;
const service_sid = serviceSidVerification(user_phone);
const entry_id = uuidv4();
const user = new User(
username,
user_email,
user_password,
user_phone,
entry_id,
service_sid,
);
await db.query(user.setUser());
return res.status(200).json({
entry_id: entry_id,
});
});
I want to write a test for this route. I need to replace the return value from the serviceSidVerification() function with my own. For example, 12345. How can this be done with a test?
This is what my test looks like.
describe(`Users Test`, () => {
describe('POST /api/users/', () => {
it(`Should create user`, (done) => {
let stub = sinon.createStubInstance(server);
stub.getEvents.returns('12345')
chai
.request(server)
.post('/api/users/')
.send({ 'username': faker.internet.userName() })
.send({ 'user_email': faker.internet.email() })
.send({ 'user_password': faker.internet.password() })
.send({ 'user_phone': faker.phone.phoneNumber() })
.end((err, res) => {
expect(res).have.status(200);
expect(res.body).have.property('entry_id');
expect(res.body.entry_id).to.be.an('string');
done();
});
});
});
});
});
...
function ServiceSidVerification(phone) {
const client = new twilio(config.smsVerify.accountSid, config.smsVerify.authToken);
const service_sid = (await client.verify.services
.create({
friendlyName: config.smsVerify.title,
codeLength: config.smsVerify.codeLength,
})
).sid;
const sid = (await client.verify.services(service_sid).verifications
.create({
to: phone,
channel: 'sms'
})
).sid;
return { service_sid, sid };
}
How do I test my route?