Nodejs Mocha: Unable to test a POST and GET by ID - javascript

Trying to test a POST request and GET by ID. For the POST error it states: "expected 200, got 400". Then for the 3 GET by IDs, the first two is "Error: Timeout of 2000ms exceeded......", then gives me the two IDs a few minutes later. Then the third: "Expected 404, got 400".
Tried looking at docs for expect, supertest and mocha and couldnt find a solution. Those 3 is what i use for this testing
Here is the POST test
describe('POST /drinks', () => {
it('should create a new drink', (done) => {
let type = 'coffee';
let name = 'testName';
let image = 'testImage';
request(app)
.post('/drinks')
.send({
type,
name,
image
}).expect(200).expect((res) => {
expect(res.body.type, res.body.name, res.body.image).toBe(text);
}).expect((res) => {
expect(res.body.rating).toBe(number);
}).end((err, res) => {
if (err) {
return done(err);
}
Coffee.find({
type
}).then((feedData) => {
expect(feedData.length).toBe(1);
expect(feedData[0].type).toBe(text);
done();
}).catch(e => done(e));
});
});
});
Then heres the GET by ID:
describe('GET /drinks/:id', () => {
it('should return individual drink document', (done) => {
request(app)
.get(`/drinks/${feedData[0]._id.toHexString()}`)
.expect(200)
.expect(res => {
expect(res.body.drink.text).toBe(feedData[0].text);
})
.end((err, res) => {
if (err) return done(err);
done();
});
});
it('should return 404 if drink is not found', (done) => {
let hexId = new ObjectID().toHexString();
request(app)
.get(`/drinks/${hexId}`)
.expect(404)
.end((err, res) => {
if (err) return done(err);
done();
});
});
it('should return 404 for non-object ids', (done) => {
request(app)
.get('/drinks/123abc')
.expect(404)
.end((err, res) => {
if (err) return done(err);
done();
});
});
});
Heres my route for POST:
// POST a drink
exports.postDrinks = (req, res) => {
let type = req.body.type;
if (!type) {
res.status(400).send('Request parameters missing');
}
let newDrink;
// Default Drink Fields
let defaultFields = {
type,
name: req.body.name,
tastingNotes: req.body.tastingNotes,
comments: req.body.comments,
image: req.body.image,
rating: req.body.rating
}
// Determine which type and store it as that type
switch (type) {
case 'beer':
newDrink = new Beer({
...defaultFields,
style: req.body.style,
source: req.body.source,
});
break;
case 'coffee':
newDrink = new Coffee({
...defaultFields,
beanType: req.body.beanType,
brewTime: req.body.brewTime,
strength: req.body.strength
});
break;
case 'liquor':
newDrink = new Liquor({
...defaultFields,
typOfLiquor: req.body.typOfLiquor
});
break;
case 'tea':
newDrink = new Tea({
...defaultFields,
leafType: req.body.leafType,
steepTime: req.body.steepTime,
});
break;
default:
console.log('Please select an apprioriate drink');
break;
}
// Saves POST and sends it back as well. If not, then error
newDrink.save().then((drink) => {
res.send(drink);
}, (e) => {
res.status(400).send(e);
});
}
Heres my route for GET by ID:
/ GET by ID
exports.getIndividualDrink = (req, res) => {
let id = req.params.id;
// Show everything but id and v
Drink.findById(id).select('-_id -__v').then((drink) => {
// Check if theres that drink and ID is valid
if (!drink && !ObjectID.isValid(id)) {
return res.status(401).send();
}
// If there is, then send it back
res.send({
drink
});
}, (e) => {
res.status(400).send(e);
});
};
Expected should be passing, but like i said the results are:
1) POST: 'Error: expected 200, got 400'
2) First two GET by ID: 'Error: Timeout of 2000ms exceeded. ....'
3) Last GET by ID: 'Expected 404, got 400'

The 400 Bad Request error is an HTTP status code that means that the request you sent to the server, was somehow incorrect or corrupted and the server couldn't understand it.
Try to check your schema, you should post all required item if you miss something 400 is thrown.

Related

RESTful API for HTTP DELETE doesnt check for null

Im currently writing a RESTful API for a webservice but im having trouble. Im trying to delete an mail, but first i want to check if the mail even exists. My problem is that it doesn't check if mail is null and doesn't respond with a 404. Im working with express and mongoose
router.delete('/:id', (req, res) => {
const { id } = req.params;
Mail.findById(id)
.exec()
.then((mail) => {
if (!mail) {
console.log(mail) // returns null
return res.status(404);
}
})
.then(
Mail.deleteOne({ _id: id })
.exec()
.then(() => {
res.status(200).json({
message: 'Mail deleted',
});
})
.catch((err) => {
res.status(500).json({ error: err });
})
);
});
I think you have to do the the deletion part of the code inside the first then block as an else statement. You are not returning anything that the next then block can use.
you could do:
Mail.findById(id)
.exec()
.then((mail) => {
if (!mail) {
console.log(mail) // returns null
return res.status(404).send() //need to send response;
}
Mail.deleteOne({ _id: id })
.exec()
.then(() => {
res.status(200).json({
message: 'Mail deleted',
});
})
}).catch((err) => {
res.status(500).json({ error: err });
})
PRO TIP: if you don't know it, learn async await. Code will look much cleaner!
Then it would look like this:
router.delete('/:id', async (req, res) => {
const { id } = req.params;
try {
const mail = await Mail.findById(id);
if(!mail) {
return res.status(404).send();
}
await Mail.deleteOne({_id: id});
res.status(200).json({
message: 'Mail deleted',
});
} catch(e) {
res.status(500).json({ error: err });
}

Sinon test failing after change made to function

I have this test:
describe('createNote', () => {
beforeEach(() => {
res = {
json: sinon.spy(),
sendStatus: sinon.spy(),
};
});
afterEach(() => {
noteService.createUserNote.restore();
});
it('should return user note object', async () => {
// Arrange
modelResponse = {
id: 1,
userId: req.user.id,
...req.body,
};
sinon.stub(noteService, 'createUserNote')
.resolves(modelResponse);
// Act
await userController.createNote(req, res);
// Assert
sinon.assert.calledWith(
noteService.createUserNote,
req.user,
req.body.note,
);
sinon.assert.calledWith(res.json, { note: modelResponse });
});
It fails on line sinon.assert.calledWith(res.json, { note: modelResponse });
I don't really understand sinon so I'm not sure why though.
This is my userController code:
createNote: async (req, res, next) => {
try {
const createNote = await noteService.createUserNote(
req.user,
req.body.note,
);
const note = await noteService.getUserNote(
req.user.id,
createNote.id,
);
return res.json({ note });
} catch (err) {
return next(err);
}
},
I recently changed it from this so assume something in what I've done has caused the test to fail:
createNote: async (req, res, next) => {
try {
const note = await noteService.createUserNote(
req.user,
req.body.note,
);
return res.json({ note });
} catch (err) {
return next(err);
}
},
This is the error I get:
1) User userController
createNote
should return user note object:
AssertError: async (user, text) => {
const [note] = await db.Note.createUserNote(user.id, text, db);
await emailService.userAlert(text, user.name);
return note;
} is not stubbed
at Object.fail (node_modules/sinon/lib/sinon/assert.js:106:21)
at /opt/atlassian/pipelines/agent/build/node_modules/sinon/lib/sinon/assert.js:35:24
at Array.forEach (<anonymous>)
at verifyIsStub (node_modules/sinon/lib/sinon/assert.js:22:5)
at Object.assert.(anonymous function) [as calledWith] (node_modules/sinon/lib/sinon/assert.js:77:9)
at Context.it (app/__tests__/controllers/user/userController.test.js:56:20)
at <anonymous>
Can anybody explain what is wrong and how to fix this?
You need to mock getUserNote as well. After the change, you are getting note from getUserNote and then sending it to res.json
But in the test case you have not stubbed it. Try adding this in the test case:
sinon.stub(noteService, 'getUserNote')
.resolves(modelResponse);

Dynamic response status in Express async/await error handler

I want to replace promises chain inside my Express routing with async/await. This makes code clean and more readable. First look at my code.
What i've had before:
app.post('/search', (req,res) => {
sendRequest(req.body).then( searchDetails => {
res.send(searchDetails);
}).catch( error => {
res.status(404).send(error)
});
});
Current code:
app.post('/search', asyncMiddleware(async (req,res) => {
const result = await sendRequest(req.body);
res.send(result);
}));
And this how looks asyncMiddleware:
const asyncMiddleware = checkedFunction => (req, res) => {
Promise
.resolve(
checkedFunction(req, res)
)
.catch( error => {
res.status(400).send(error)
});
};
The problem starts when I have routing which includes more than one error status.
app.delete('/delete/:id', authenticate, (req, res) => {
const id = req.params.id;
if (!ObjectID.isValid(id)) {
return res.status(404).send();
}
User.findOneAndDelete({
_id: id,
_user: req.user._id
}).then((todo) => {
if (!todo) {
return res.status(404).send();
}
res.send({todo});
}).catch((e) => {
res.status(400).send();
});
});
How can I make to asyncMiddleware will return status depends on error?
asyncMiddleware here checks if any error has occured or is deliberately thrown by checkedFunction, namely express route handler. If you would like to say something to asyncMiddleware, you need to wrap your route handler with it as you did for /search, then you need to throw specific errors/objects involving your error information:
app.delete('/delete/:id', authenticate, asyncMiddleware(async (req, res) => {
const id = req.params.id;
if (!ObjectID.isValid(id)) {
throw {
status: 404,
message: 'id not valid'
}
}
try {
const todo = await User.findOneAndDelete({
_id: id,
_user: req.user._id
});
if (!todo) {
throw {
status: 404,
message: 'todo not found'
}
}
res.send({todo});
} catch (e) {
throw {
status: 400,
message: 'mongodb error'
}
}
}));
then asyncMiddleware can send status in response
const asyncMiddleware = checkedFunction => (req, res) => {
Promise
.resolve(
checkedFunction(req, res)
)
.catch( error => {
res.status(error.status).send(error.message)
});
};
You can create built-in Error objects instead of custom ones to track error call stack but I don't think you need here.

Keep getting error when trying to send email with firebase functions

Anybody able to help? This always gives me an error. Any advice? I get the following error: Error: could not handle the request. I have removed sensitive information.
const functions = require('firebase-functions');
const sgMail = require('#sendgrid/mail');
sgMail.setApiKey("API KEY"); /*PLACEHOLDER KEY*/
const cors = require('cors')({
origin: ['https://DOMAIN.co.uk'],
methods: ['POST', 'OPTIONS'],
allowedHeaders: ['Content-Type'],
preflightContinue: false,
optionsSuccessStatus: 204
});
exports.sendEmailConfirmation = functions.https.onRequest((req, res) => {
cors(req, res, () => {
return Promise.resolve()
.then(() => {
if (req.method !== 'POST') {
const error = new Error('Only POST requests are accepted');
error.code = 405;
throw error;
}
const message = {
to: 'email#email.com',
from: 'email#email.com',
subject: 'subject',
text: 'some text',
html: 'some html'
};
return sgMail.send(message);
})
.then((response) => {
if (response.body) {
res.send(response.body);
} else {
res.end();
}
})
.catch((err) => {
console.error(err);
return Promise.reject(err);
});
});
});
It loads normally using functions, and shows no errors on there, I can't work out why it is not working.
Many thanks in advice

Bluebird with mongoose using Promise.Each

I'm stuck in a function I'm working with ( I can be doing this all wrong ). So a quick explanation, I want to add bulk data in a collection, the collection is called "Sites" the format of the CSV is site,country,type. I'm trying to use promises for this (Bluebird). So consider the code:
Promise.each(sites, sites => new Promise((resolve, reject) => {
//console.log(sites);
let name = tools.extractDomain(req, res, sites[0]);
let country = sites[1];
let group = sites[2];
if (name != "" && country != "" && group != "") {
Site.findOne({ name: name }, "_id", function(err, duplicate) {
if (false) {
console.log("Duplicate site: " + duplicate);
} else {
//console.log("Adding " + name)
let site = new Site()
site.name = name
site.meta = {}
site.group = group
site.country = country
site.geomix = []
site.addedBy = req.user._id
site.addedAt = Date.now()
site.saveAsync().then(function(response){
tools.saveHistory(req, res, response._id, response.name, "Website Meta fetched.");
tools.saveHistory(req, res, response._id, response.name, "Link added for the first time."); //Save in history
resolve(site);
}).catch(function (e){
console.log(name);
reject();
});
}
});
}else{
console.log('Wrong Format');
}
}).then((data) => {
console.log('All websites processed!');
addedSites.push(data);
}).catch(err => {
//console.error('Failed');
}));
res.send({ status: 'ok', message: ''});
I'm making ajax calls so I return a res.send({ status: 'ok', message: ''}), I know that its in the incorrect place and I want to send some data along the res.send. Currently it sends the headers before the code actually finishes. I want to send the headers after all the data is added in Mongo but for every each in this case he resolve() so if I send the headers inside the ".then" of the ".each" I will get headers already sent error.
This might be a bit confusing. I feel I'm not doing this right. I'm going a bit crazy as well as I can't find a proper example that I can understand and implement.
But in the end my main question is: using an Ajax call what's the proper way to add let's say 1000 records in a collection using promises and actually control properly those who fail to add and those who don't?
Right now my code actually works but the logic is wrong for sure.
Thanks.
You can use bulkWrite on your model.
Ref: http://mongoosejs.com/docs/api.html#model_Model.bulkWrite
EDIT:
Sorry I misunderstood you. You need to move res.send({ status: 'ok', message: ''}); to then() and catch() blocks, so you will get something like this:
Promise.each(sites, sites => new Promise((resolve, reject) => {
// stuff you did before
}).then((data) => {
console.log('All websites processed!');
addedSites.push(data);
res.send({ status: 'ok', message: ''});
}).catch(err => {
res.send({ status: 'failed', message: err.message});
}));
This is what I came too, if someone can tell me if this is a good arch.
exports.addBulkSite = function(req, res, next) {
let siteArray = csv.parse((req.body.sites).trim()),
addedSites = [],
failedSites = [],
duplicated = [],
sites = siteArray,
size = sites.length,
processed = 0,
meta;
Promise.each(sites, sites => new Promise((resolve, reject) => {
let name = tools.extractDomain(req, res, sites[0]),
country = sites[1],
group = sites[2];
if (name != "" && country != "" && group != "") {
Site.findOneAsync({ name: name }, "_id").then(function(duplicate) {
duplicated.push(duplicate);
reject({name:name, message: 'Duplicated', critical:false});
}).catch(function(notDuplicated){
let site = new Site()
site = {
name: name,
meta: {},
group: group,
country: country, geomix:{},
addedBy: req.user._id,
addedAt:Date.now()
}
site.saveAsync().then(function(response){
tools.saveHistory(req, res, response._id, response.name, "Website Meta fetched.");
tools.saveHistory(req, res, response._id, response.name, "Link added for the first time."); //Save in history
resolve(site);
}).catch(function (e){
console.log(e);
reject({name:name, message: 'Error saving in the database. Please contact the administrator.', critical: true});
});
});
}else{
reject({name:name, message: 'Paramaters are missing', critical:false});
}
}).then((data) => {
processed++;
addedSites.push(data);
if(processed==size){
console.log('out');
res.send({ status: 'ok', addedSites: addedSites, failedSites: failedSites, duplicated: duplicated});
}
}).catch((err) => {
processed++;
console.log(err);
failedSites.push(err);
if(processed==size){
console.log('out');
res.send({ status: 'ok', addedSites: addedSites, failedSites: failedSites, duplicated: duplicated});
}
}));
}

Categories