Skip to content

Commit

Permalink
Bug fix on updateUserSession: ensure deepcopy of req.user and adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
helenzhou6 committed May 12, 2018
1 parent 10a8d34 commit cf19d02
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 24 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"test": "NODE_ENV=test nyc tape src/test/*.test.js | tap-spec",
"test:app": "NODE_ENV=test nodemon src/test/app.test.js | tap-spec",
"test:db": "NODE_ENV=test nodemon src/test/db.test.js | tap-spec",
"test:unit": "NODE_ENV=test nodemon src/test/unit.test.js | tap-spec",
"test:coverage": "NODE_ENV=test nyc --reporter=lcov --reporter=text-lcov tape src/test/*.test.js | tap-spec && open -a \"Google Chrome\" ./coverage/lcov-report/index.html",
"build": "node src/model/database/db_build.js",
"lint": "./node_modules/.bin/eslint **/*.js --ignore-pattern node_modules/"
Expand Down Expand Up @@ -49,4 +50,4 @@
"passport-github2": "^0.1.11",
"pg-promise": "^8.4.3"
}
}
}
4 changes: 2 additions & 2 deletions src/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const passport = require('passport');
const home = require('./home');
const profile = require('./profile');
const error = require('./error');
const { updateUserSession, protectedRoute, ensureAuthenticated } = require('./middleware');
const { ensureAuthenticated } = require('./middleware');

// UNPROTECTED ROUTES //
router.get('/', home.get);
Expand All @@ -14,7 +14,7 @@ router.get('/notmember', (req, res) => {
});

// PROTECTED ROUTES //
router.get('/myprofile/:github_id', updateUserSession, ensureAuthenticated, profile.get);
router.get('/myprofile/:github_id', ensureAuthenticated, profile.get);
router.post('/saveDetails', ensureAuthenticated, (req, res) => {
// post user data (req.body) to database
console.log('form data: ', req.body);
Expand Down
31 changes: 11 additions & 20 deletions src/controllers/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,20 @@ exports.ensureAuthenticated = (req, res, next) => {
res.redirect('/');
};

exports.updateUserSession = (req, res, next) => {
let userInfo;
if (req.isAuthenticated()) {
userInfo = req.user;
if (req.session.registeredProfile) {
userInfo.session = {
login: true,
signup: false,
};
return next(null, userInfo);
}
// Updates user object to include user session
exports.updateUserSession = (req) => {
// Deep copy of req.user
let userInfo = JSON.parse(JSON.stringify((req.user)));
if (req.session.registeredProfile) {
userInfo.session = {
login: true,
signup: false,
};
} else {
userInfo.session = {
login: false,
signup: true,
};
return next(null, userInfo);
}

userInfo = {
session: {
login: false,
signup: false,
},
};
return next();
return userInfo;
};
3 changes: 2 additions & 1 deletion src/controllers/profile.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { updateUserSession } = require('./middleware');
exports.get = (req, res) => {
const { user } = req;
let user = updateUserSession(req);
res.render('profile', { activePage: { profile: true }, user });
};
69 changes: 69 additions & 0 deletions src/test/unit.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const test = require('tape');

const { updateUserSession } = require('../controllers/middleware');

test('Test if tape is working', (t) => {
t.ok(true, 'tape is working');
t.end();
});

// UPDATE USER SESSION
test('Test updateUserSession - registeredProfile: true', (t) => {
const originalRequest = {
user: {
id: 1,
},
session: {
registeredProfile: true,
}
}
const request = {
user: {
id: 1,
},
session: {
registeredProfile: true,
}
}
const actual = updateUserSession(request);
const expected = {
id: 1,
session: {
login: true,
signup: false,
}
}
t.deepEquals(actual, expected, 'returns expected result (deepequals)');
t.deepEquals(request, originalRequest, 'not mutated original request object');
t.end();
});

test('Test updateUserSession - registeredProfile: false', (t) => {
const originalRequest = {
user: {
id: 1,
},
session: {
registeredProfile: false,
}
}
const request = {
user: {
id: 1,
},
session: {
registeredProfile: false,
}
}
const actual = updateUserSession(request);
const expected = {
id: 1,
session: {
login: false,
signup: true,
}
}
t.deepEquals(actual, expected, 'returns expected result (deepequals)');
t.deepEquals(request, originalRequest, 'not mutated original request object');
t.end();
});

0 comments on commit cf19d02

Please sign in to comment.