Mocha test case for query based functions - node.js

I am new to Mocha. Can someone suggest how to write test case for "retrieveEmployee" function.
var mysql = require('mysql');
var dbconfig = {
host:'localhost',
user:'root',
password:'root',
database:'db',
port:'3306'
};
exports.retrieveEmployee = function(req, res){
var connection = mysql.createConnection(dbconfig);
var query ='SELECT emp_Id, emp_Name FROM employee';
connection.query(query, function(err, rows, fields){
if(err)
throw err;
res.render('index', { title: 'Employee', 'items':rows });
});
};
I am using 'MySql' for database.

If your intent is to assert page renderings/functionality I'd use something like Zombie.
Ex:
app.js
app.get("/", function(req, res, next) {
res.render("index", {title: 'Hello World!'});
});
test/app.js
var Browser = require('zombie');
var app = require('../app');
describe("a html endpoin", function() {
var server;
beforeEach(function(done) {
server = app.listen(7331, done);
});
afterEach(function(done) {
server.close(done);
});
it("has some title", function(done) {
var browser = new Browser();
browser.visit("http://localhost:7331/")
.then(function() {
var title = browser.text('title');
assert.equal(title, 'Hello World!');
done();
});
});
});
If your intent is more of an API endpoint, then something like supertest would work nicely.
Ex:
app.js
app.get("/api", function(req, res, next) {
res.send({foo: 'bar'});
});
test/app.js
var request = require('supertest');
var app = require('../app');
describe("an api endpoint", function() {
it("returns some sort of json", function(done) {
request(app)
.get("/api")
.expect(200, {foo: 'bar'}, done);
});
});
However, both Zombie or supertest can be used to do both of these tasks.
Zombie
supertest
Use the assertion library of your choice.

Related

Node js: res.json not displaying anything

I would like to fetch all the data (users) from a localdb using mongodb and node.js. but res.json(usersArray) is not displaying anything. I also tried to use .render but it would give me another error.
var express = require('express');
var mongojs = require('mongojs');
var db = mongojs('mongodb://localhost/mydb', ['users']);
var app = express();
const port = 5000;
app.use(express.static(__dirname + "/public"));
app.get('/', function(req, res){
console.log("I received a GET request")
/*
db.users.find(function(err, docs){
console.log("Getting data from db");
console.log(docs);
//res.json(docs);
});
*/
let usersArray = db.users.find().toArray(function(err, docs){
console.log("Getting data from db");
console.log(docs);
//res.json(docs);
});;
res.json(usersArray);
//usersString = JSON.stringify(usersArray);
//res.render(usersString);
//res.json(usersString);
console.log("Returned data");
});
app.listen(port,'0.0.0.0');
console.log('Server running on port '+port);
use async await to block promise-based asynchronous functions
app.get('/', async function(req, res){
console.log("I received a GET request")
let usersArray = await new Promise((resolve, reject) => {
db.users.find().toArray((err, docs) => {
if (err)
reject(err)
resolve(docs)
})
});
res.json(usersArray);
//usersString = JSON.stringify(usersArray);
//res.render(usersString);
//res.json(usersString);
console.log("Returned data");
});

How to fix 'headers already sent' error in node

So I'm trying to create a node app that calls an ldap serve and to authenticate users. In the code below, the app successfully connects to the server and processes the request. But when I try to send a response back, I get this error:
throw new ERR_HTTP_HEADERS_SENT('set');
^
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
I'm not really sure why this is occurring. I'm pretty new to node, express, and ldap
// ldapjs required for ldap connection
const ldap = require('ldapjs');
//express required for exposing endpoints
const express = require('express');
const app = express();
const assert = require('assert');
var client = ldap.createClient({
url: 'ldap://someserve.com'
});
//Search filter for users in the directory
var opts = {
filter: '(&(objectCategory=person)(objectClass=user))',
scope: 'sub',
};
//General Ldap serch user
var UserName = '123.test.com';
var Pass = '123longpass'
//Base URL
app.get('/', (req,res) => {
res.send('hello from node')
});
//Get all ldap users
app.get('/api/ldapUsers', (req, res) =>
{
client.bind(UserName, Pass, function (err)
{
client.search('DC=sdf,DC=sdfa,DC=gdfgd', opts, function (err, search)
{
search.on('searchEntry', function (entry)
{
res.setHeader('Content-Type', 'application/json');
var users = entry.object;
console.log(users);
res.json(users);
res.end();
});
});
});
// client.unbind( err => {
// assert.ifError(err);
// });
});
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Listening on port ${port}...`));
searchEntry event is called once for each found item which means you are calling res.json multiple times.
Try this:
app.get('/api/ldapUsers', (req, res) =>
{
client.bind(UserName, Pass, function (err)
{
client.search('DC=sdf,DC=sdfa,DC=gdfgd', opts, function (err, search)
{
var users = [];
search.on('searchEntry', function (entry) {
users.push(entry.object);
});
search.on('end', function (entry) {
res.setHeader('Content-Type', 'application/json');
console.log(users);
res.json(users);
res.end();
});
});
});
});

Cannot post membership/login/submit

I have some code to create an account and have a login page and say true or false if you entered the correct details but instead it says 'Cannot post /membership/login/submit'.
my main.js code (the html files are basic and the forms are just standard tags with an action and a method, tell me if you want these)
var app = require('express')();
var http = require('http').Server(app);
var httpuse = require('http');
var io = require('socket.io')(http);
var MongoClient = require('mongodb').MongoClient;
var users = require(__dirname + '/local_js/users.js');
var public = __dirname + "/www/";
var bodyParser = require('body-parser')
var url = "mongodb://localhost:27017/";
const bcrypt = require('bcrypt');
var needle = require('needle');
const saltRounds = 10;
app.use(bodyParser.urlencoded({ extended: false }));
function rand(n) {
Math.floor(Math.random() * (n+1));
}
function getToken(hashedpass, user) {
return hashedpass+"_._"+user;
}
app.get('/', function(req, res){
res.sendFile(public + 'index.html');
});
app.get('/membership/create/form', function(req, res){
res.sendFile(public + 'user/create.html');
});
app.post('/membership/create/submit', function(req, res){
MongoClient.connect(url, function(errdd, db) {
if (errdd) throw errdd;
var dbo = db.db("boaichat");
bcrypt.hash(req.body.password, saltRounds, function(err, hash) {
if (err) {throw err;}
var newUser = { nickname: req.body.nickname, password: hash };
dbo.collection("users").insertOne(newUser, function(errd, ress) {
if (errd) throw errd;
console.log("1 user document inserted");
db.close();
res.send("200 OK");
});
});
});
});
app.get('/membership/login/form', function(req, res) {
res.sendFile(public + 'user/login.html');
});
app.post('membership/login/submit', function(req, res) {
MongoClient.connect(url, function(errdd, db) {
if (errdd) throw errdd;
var dbo = db.db("boaichat");
dbo.collection("users").findOne({nickname: req.body.nickname}, function(err, result) {
if (err) throw err;
if (result) {
bcrypt.compare(req.body.password, result.password, function(err, ress) {
console.log(ress);
res.send(ress);
});
}
});
db.close();
});
});
// IGNORE THIS
app.post('api/v1/getSessionValid', function(req, res) {
let token = req.body.token;
let userInfo = token.split('_._');
if (userInfo[0] && userInfo[1]) {
}
});
io.on('connection', function(socket){
console.log('connection');
socket.on('disconnect', function(){
console.log("disconnection");
});
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
it says 'Cannot post /membership/login/submit'.
Don't forget the / at the beginning of your route.
// Try this
app.post('/membership/login/submit', ...)
// Instead of
app.post('membership/login/submit', ...)

NodeJS testing with Mocha.

I am currently trying to test an application with Mocha and Chai but I am having difficulties in connecting one of the modules to the test.
Here is my test case:
"use strict";
var chai = require('chai');
var expect = require("chai").expect;
var helloWorld = require("../routes");
var mongoose = require("mongoose");
var app = require("../app");
var application = require("../routes");
describe('helloWorld', function () {
it('Mongo Module extended', function () {
expect(helloWorld()).equal('Mongo Module Extended');
});
});
describe('application', function application(app){
it('connects properly', function(done) {
expect(application(app))
.request('http://localhost:80')
.get('/')
.end(function(err, res) {
expect(res).to.have.status(200);
done(); // <= Call done to signal callback end
});
});
});
and here is the file I am currently trying to test:
var passport = require('passport');
var Account = require('./models/account');
var path = require('path');
var mongojs = require('mongojs');
var dbx = mongojs('test', ['logs']);
var fs = require('fs');
var dbc = mongojs('test', ['accounts']);
function helloWorld() {
return 'Mongo Module Extended';
}
module.exports = helloWorld;
function application(app) {
app.get('/',
function(req, res){
res.sendFile('login.html', {root: __dirname});
});
app.get('/login',
function(req, res){
res.sendFile(path.join(__dirname + '/login.html'));
});
app.get('/index', isLoggedIn,
function(req, res){
req.flash('info', 'Flash is back!')
res.sendFile(path.join(__dirname + '/views/index.html'));
});
app.get('/', isLoggedIn,
function(req, res){
res.sendFile(path.join(__dirname + '/login.html'));
});
app.post('/login', function(req, res, next) {
passport.authenticate('local', function(err, user, info) {
if (err) {
return next(err);
}
if (!user) {
return res.json({success:false, message: "Wrong Username or Password"}); //sends json message to the Front end jQuery function
}
req.logIn(user, function(err) {
if (err) {
return next(err);
}
res.json({success:true, redirectTo: '/index', message: "You are logged-in."}); //sends json message to the Front end jQuery function
/* Add username and time of login to the collection in MongoDB */
dbc.accounts.findOne(function(err, info){
var users = info.username; //Gets the logging username from the collection
var date = new Date().toLocaleDateString(); // generates a new date.
console.log(date);
console.log(date +" "+ "date");
var stamp = {name: users, time:date}; // object to hold both variables.
//toLocaleDateString('en-GB')
dbx.logs.insert(stamp, function(err, result){ //query to insert one inside the "logs" collection.
if(err) { throw err; }else{console.log("added" + JSON.stringify(stamp));}
});
});
/* END of Collection Logging Method */
});
})(req, res, next);
});
app.get('/logout',
function(req, res){
req.logout();
res.redirect('/login');
});
function isLoggedIn(req, res, next) {
//console.log('here is Authenticated', req.isAuthenticated()) //prints out 'here is Authenticated' if the Passport login is successful
if (req.isAuthenticated()){
console.log('here is Authenticated');
return next();
}else{
console.log("you cannot access the routes without being logged in!");
}
}
module.exports = application;
I keep receiving this error:
TypeError: expect(...).request is not a function
which I guess is referencing the first get request I am trying to make in my application file:
app.get('/',
function(req, res){
res.sendFile('login.html', {root: __dirname});
});
At this point I am not really sure how to fix this. I know that my error is in the way that I am trying to test the get request but I cannot seem to bypass it.
How can I correct my code so I can reference the methodsGET and POST methods from module.exports = application; correctly?
Chai on its own doesn't support testing of http routes, you need chai-http to do just that.
You could see the link for more: https://scotch.io/tutorials/test-a-node-restful-api-with-mocha-and-chai

Express.js nested routes without parameters

I have a Express server resolving GET /companies/:id/populate/. Now, I would like to setup GET /companies/populate/ (without the :id). However, I can't make this route to work. If I try, for example, GET /companies/all/populate/, it works, so it seems the pattern for an Express route is path/:key/path/:key.
Is this true? Thanks!
Edit: Adding code.
server.js
'use strict';
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var cors = require('cors');
var mongoUri = 'mongodb://localhost:27017';
mongoose.connect(mongoUri);
var db = mongoose.connection;
db.on('error', function() {
throw new Error('Unable to connect to database at' + mongoUri);
});
// runs Express
var app = express();
// uses Cors
app.use(cors());
// uses bodyParser
app.use(bodyParser.json());
// requires Mongo models
require('./models');
// includes routes.js, passing the object 'app'
require('./routes')(app);
// listens for connections on port 3000
app.listen(3000, function() {
console.log("Express started on Port 3000");
});
routes.js
module.exports = function(app) {
// thumbs up if everything is working
app.get('/', function(req, res, next) {
res.send('👍');
console.log('Server Running');
res.end();
});
// companies
var companies = require('./controllers/companies');
app.get('/companies', companies.findAll);
app.get('/companies/:id', companies.findById);
app.get('/companies/:id/populate', companies.populate);
app.get('/companies/populate', companies.populateAll);
app.post('/companies', companies.add);
app.patch('/companies/:id', companies.patch);
app.delete('/companies/:id', companies.delete);
};
/controllers/companies.js
var mongoose = require('mongoose');
Company = mongoose.model('Company');
// GET /companies
// status: works, needs error handling
exports.findAll = function(req, res) {
Company.find({}, function(err, results) {
return res.send(results);
});
};
// GET /companies/:id
// status: works, needs to check error handling
exports.findById = function(req, res) {
var id = req.params.id;
Company.findById(id, function(err, results) {
if (results) res.send(results);
else res.send(204);
});
};
// POST /companies
// status: works, needs error handling
exports.add = function(req, res) {
Company.create(req.body, function(err, results) {
if (err) {
return console.log(err)
} else {
console.log('company created');
}
return res.send(results);
});
};
// PATCH /companies/:id
// status: works, needs error handling
exports.patch = function(req, res) {
var id = req.params.id;
var data = req.body;
Company.update( { '_id' : id }, data, function(err, numAffected) {
if (err) return console.log(err);
return res.send(200);
});
};
// DELETE /companies/:id
// status: works, needs error handling, make sure that we are sending a 204 error on delete
exports.delete = function(req, res) {
var id = req.params.id;
Company.remove({ '_id': id }, function(results) {
console.log('deleted ' + id); // tester
return res.send(results);
});
};
// GET /companies/:id/populate
exports.populate = function(req, res) {
var id = req.params.id;
Company
.findOne({ _id: id })
.populate('contacts country')
.exec(function (err, results) {
if (err) return handleError(err);
else res.send(results);
});
};
// GET /companies/populate
exports.populateAll = function(req, res) {
Company
.find({})
.populate('contacts country')
.exec(function (err, results) {
if (err) return handleError(err);
else res.send(results);
});
};

Resources