Noel asked this 4 months ago

error in unit testing in node js

app.js file
// Dependencies
var express = require('express');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
let morgan = require('morgan');
let config = require('config');
var session = require('express-session');

// Initialize app object.
var app = express();
app.use(bodyParser());
app.use(session({secret:"zoriq",resave:false,saveUninitialized:true}))

// Use app.set to add the view engine.
app.set("view engine","pug")

// Set path to views.
app.set('views', './views');

//-------------------------------------------------------------------------
// Configuring the database
const dbConfig = require('./config/database.config.js');
// const mongoose = require('mongoose');

mongoose.Promise = global.Promise;

// Connecting to the database
mongoose.connect(dbConfig.url)
.then(() => {
console.log("Successfully connected to the database");
}).catch(err => {
console.log('Could not connect to the database. Exiting now...');
process.exit();
});

if(config.util.getEnv('NODE_ENV') !== 'test') {
app.use(morgan('combined'));
}

require('./api')(app);
//--------------------------------------------------------------------------

// Create server and listen on port 3000.
app.listen(3000, function(){
console.log('Server started on port 3000...');
});
 
 
//api.js file
 
module.exports = function(app){
var apiRoutes = require('./routes/apiRoutes');
 
app.get('/view', apiRoutes.view);
app.get('/add',apiRoutes.add);
app.post('/register',apiRoutes.register);
app.get('/user/:id',apiRoutes.Edit);
app.post('/update/:id',apiRoutes.Update);
//app.delete('/user/delete/:id',apiRoutes.delete);
app.post('/login',apiRoutes.login);
app.get('/homepage',apiRoutes.homepage);
};
//apiRoutes.js
var express = require('express');
//var router = express.Router();
var User = require('../models/model.app');
var session = require('express-session');
//----------------------------------------------------------------------
//view all data

exports.view = function(req, res){
User.find({},function(err,docs){
if(err) res.json(err);
else res.render('index',{users:docs});
});
};

//---------------------------------------------------------------
//register data in database
exports.add = function(req,res){
res.render("register");
}

exports.register = function(req,res){
var userData = {
firstname: req.body.firstname,
lastname: req.body.lastname,
email: req.body.email,
address: req.body.address,
username: req.body.username,
password: req.body.pass,
 
}
User.create(userData, function (err, user) {
if (err) {
return next(err)
} else {
res.render("login");
}
});
}

//--------------------------------------------------------------------------------------------------
//user login
exports.login = function(req,res){
var username = req.body.username;
var password = req.body.pass;

User.findOne({username:username,password:password},function(err,user){
if(err){
console.log(err);
return res.status(500).send();
}

if(!user){
return res.status(400).send();
}
req.session.user = user;
return res.redirect('/homepage');
//return res.status(200).send();
})
}
//----------------------------------------------------------------------------------------------------
//homepage of specific user

exports.homepage = function(req,res){
if(!req.session.user){
return res.status(400).send();
}

//return res.status(200).send("Welcome to the user " +req.session.user.username);
res.render("homepage");
}

//----------------------------------------------------------------------------------------------------
// get user data from database using id.
//OR Edit user data from database using id.

exports.Edit = function (req, res) {
User.find({username:req.params.id},function(err,docs){
if(err) res.json(err);
else res.render('edit',{user:docs[0]});
});
}

//-----------------------------------------------------------------------------------------------------
// update data using id

exports.Update = function(req, res){
User.findOneAndUpdate({username: req.params.id}, {$set:{firstname:req.body.firstname,lastname:req.body.lastname,email:req.body.email,address:req.body.address,username:req.body.username,password:req.body.pass,}}, {new: true}, function(err, doc){
if(err){
console.log("Something wrong when updating data!");
} else{
console.log(doc);
}
res.redirect("/view");
});
}
 
test.js file

process.env.NODE_ENV = 'test';

const chai = require('chai');
const should = chai.should();
const chaiHttp = require('chai-http');
chai.use(chaiHttp);

const server = require('../api');

describe('get', function () {
it('should return status 401 unauthorized due to lack of auth token on /users GET', function (done) {
chai
.request(server)
.get('/view')
.end(function (error, response, body) {
response.should.have.status(401);
response.should.be.json;
response.body.should.have.property('status', 'fail');
response.body.should.have.property('data').that.is.an('object');
response.body.data.should.have.property('message');
done();
});
});
});

No answers yet..