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();
});
});
});