I am trying to use Lab to test some code. However, for some reason when I put the right payload my code does nothing. The promise doesn't seem to get returned:
'use strict';
const Server = require('../server');
const options = {
method: 'POST',
url: '/api/users',
payload: JSON.stringify({ mobile: '3342329224' })
};
Server.inject(options, (response) => {
if (response) {
console.log(response.payload);
}
else {
console.log('Nada');
}
});
If I edit the code so that there is no payload or it doesn't match my Joi validation then it I get response:
{"statusCode":400,"error":"Bad Request","message":"child \"mobile\" fails because [\"mobile\" must be larger than or equal to 10]","validation":{"source":"payload","keys":["mobile"]}}
module.exports = {
method: 'POST',
path: '/api/users',
config: {
auth: false,
handler: (request, reply) => {
//looks up payload in db otherwise creates entry
User.findOne({
mobile: request.payload.mobile
}, (err, user) => {
if (err) {
throw err;
}
if (user) {
// uses twillio to send code
sendVerificationText(user, (err, result) => {
if (err){
throw err;
}
if (result === true) {
// this is what I expect to happen when testing
reply('code sent').code(201);
}
else {
throw Boom.badRequest(err);
}
});
}
else {
// the user should exist so....
const user = new User();
user.mobile = request.payload.mobile;
user.admin = false;
user.save((err, user) => {
if (err) {
throw Boom.badRequest(err);
}
sendVerificationText(user, (err, result) => {
if (err){
throw err;
}
if (result === true) {
reply('code sent').code(201);
}
else {
throw Boom.badRequest(err);
}
});
});
}
});
},
// Validate the payload against the Joi schema
validate: {
payload: createUserSchema
}
}
};
I should mention that this code works when I run the server and test the api by hand. I can't figure it out.