Skip to content

Instantly share code, notes, and snippets.

View WaleedAshraf's full-sized avatar
:octocat:
Focusing

Waleed Ashraf WaleedAshraf

:octocat:
Focusing
View GitHub Profile
@WaleedAshraf
WaleedAshraf / cookies.md
Last active March 21, 2023 08:42
Mocking Express Session For Tests

How to mock express session?

sinon is being used to mock methods in tests. It is one of the most used package for testing. But Is there a way to mock session objects in tests for express?

I'm working on a project where I upload images to s3 in bulk (albums) and than further apply some processing like resizing/compression/editing on them.

It's a express app with middlewares for authorization, authentication, setting context and is working fine. Recently I started adding testcases for different routes using mocha. I'm using cookie-session for session handling in app.

For one route, I needed to mock req.session object. (Need to set it's value before calling the other route.)

@WaleedAshraf
WaleedAshraf / mock-session-1.js
Last active March 18, 2018 19:07
Mock Express Session
exports.create = function(req, res) {
// upload image to s3
var photos = PhotosTable.create(req.files.names); // add entries in database
req.session.count = photos.length; // set count in req.session
res.redirect(`api/image/submit`);
}
@WaleedAshraf
WaleedAshraf / mock-session-2.js
Last active March 18, 2018 19:07
Mock Express Session
exports.submit = function(req, res) {
if (req.session.count && req.session.count > 0) { // get count from req.session
// perform further tasks
res.sendStatus(200);
} else {
res.sendStatus(400);
}
}
@WaleedAshraf
WaleedAshraf / mock-session-3.js
Last active March 18, 2018 19:07
Mock Express Session
// name = "my-session" -> base64 value of required object (cookie)
// name.sig = "my-session.sig" -> signed value of cookie using Keygrip
let cookie = Buffer.from(JSON.stringify({"count":2})).toString('base64'); // base64 converted value of cookie
let kg = Keygrip(['testKey']) // same key as I'm using in my app
let hash = kg.sign('my-session=' + cookie);
await request(app).get('api/image/submit').set("Accept", "text/html")
.set('cookie', ['my-session=' + cookie + '; ' + 'my-session.sig=' + hash + ';'])
@WaleedAshraf
WaleedAshraf / mock-session-4.js
Last active March 18, 2018 19:06
Mock Express Session
exports.submit = function(req, res) {
if(req.session.count && req.session.count > 0) { // true
//do something
res.sendStatus(200);
}
}
@WaleedAshraf
WaleedAshraf / cluster-module.js
Last active October 31, 2018 02:50
Switching from cluster module to PM2 & RabbitMQ
var cluster = require('cluster');
const numCPUs = require('os').cpus().length;
module.exports.create = function(options, callback){
if (cluster.isMaster) {
// fork child process for notif/sms/email worker
global.smsWorker = require('child_process').fork('./smsWorker');
global.emailWorker = require('child_process').fork('./emailWorker');
global.notifiWorker = require('child_process').fork('./notifWorker');
@WaleedAshraf
WaleedAshraf / cluster-module.js
Last active March 25, 2018 13:21
Switching from cluster module to PM2 & RabbitMQ
var cluster = require('cluster');
const numCPUs = require('os').cpus().length;
module.exports.create = function(options, callback){
if (cluster.isMaster) {
// fork child process for notif/sms/email worker
global.smsWorker = require('child_process').fork('./smsWorker');
global.emailWorker = require('child_process').fork('./emailWorker');
global.notifiWorker = require('child_process').fork('./notifWorker');
@WaleedAshraf
WaleedAshraf / cluster-module.js
Last active July 5, 2018 02:57
Switching from cluster module to PM2 & RabbitMQ
var cluster = require('cluster');
const numCPUs = require('os').cpus().length;
module.exports.create = function (options, callback) {
if (cluster.isMaster) {
// fork child process for notif/sms/email worker
global.smsWorker = require('child_process').fork('./smsWorker');
global.emailWorker = require('child_process').fork('./emailWorker');
global.notifiWorker = require('child_process').fork('./notifWorker');
@WaleedAshraf
WaleedAshraf / smsWorker.js
Created March 23, 2018 17:26
Switching from cluster module to PM2 & RabbitMQ
process.on('message', function(data) {
sendSMS(data); // use twilio or something to send sms
});
var pm2Config = {
"apps": [
{
"name": "app",
"script": "app.js",
"exec_mode": "cluster_mode",
"instances": "max"
},
{
"name": "smsWorker",