From 6a4c0a20a1812c71089a6190e75354686f1878fa Mon Sep 17 00:00:00 2001 From: Bastian Wegge Date: Mon, 31 Aug 2015 10:15:17 +0200 Subject: [PATCH 1/2] wip with socket.io tests, problem is that I used Automattic/socket.io which does resolve to 1.3.6 right now but is different fork (i guess) --- config/lib/socket.io.js | 2 + .../chat/tests/server/chat.socket.tests.js | 117 +++++++++++++++++- package.json | 3 +- 3 files changed, 119 insertions(+), 3 deletions(-) diff --git a/config/lib/socket.io.js b/config/lib/socket.io.js index 3df7359efb..66b3544447 100644 --- a/config/lib/socket.io.js +++ b/config/lib/socket.io.js @@ -73,6 +73,8 @@ module.exports = function (app, db) { // Get the session id from the request cookies var sessionId = socket.request.signedCookies ? socket.request.signedCookies[config.sessionKey] : undefined; + console.log('sessionId:',sessionId); + if (!sessionId) return next(new Error('sessionId was not found in socket.request'), false); // Use the mongoStorage instance to get the Express session information diff --git a/modules/chat/tests/server/chat.socket.tests.js b/modules/chat/tests/server/chat.socket.tests.js index 21f94e4056..49b9b39dc4 100644 --- a/modules/chat/tests/server/chat.socket.tests.js +++ b/modules/chat/tests/server/chat.socket.tests.js @@ -3,6 +3,119 @@ /** * Chat socket tests */ -describe('Chat Socket Tests:', function () { - // TODO: Add chat socket tests + +var should = require('should'), + request = require('supertest'), + path = require('path'), + mongoose = require('mongoose'), + sinon = require('sinon'), + User = mongoose.model('User'), + express = require(path.resolve('./config/lib/express')), + io = require('socket.io/node_modules/socket.io-client'), + querystring = require('querystring'), + http = require('http'); + +/** + * Globals + */ +var app, agent, credentials, ioc, user, admin, sessionId, clock; + +describe('Socket Chat tests:', function() { + + // Get application + before(function(done) { + app = express.init(mongoose); + agent = request.agent(app); + + done(); + }); + + // create the user + beforeEach(function(done) { + // Create user credentials + credentials = { + username: 'username', + password: 'password' + }; + + // Create a new user + user = new User({ + firstName: 'Full', + lastName: 'Name', + displayName: 'Full Name', + email: 'test@test.com', + username: credentials.username, + password: credentials.password, + provider: 'local' + }); + + // Save a user to the test db + user.save(function () { + done(); + }); + }); + + it('should be able to connect on socket', function(done) { + clock = sinon.useFakeTimers(Date.now()); + + var testMessage = { + created: Date.now(), + profileImageURL: 'modules/users/client/img/profile/default.png', + text: 'Is now connected', + type: 'status', + username: 'username' + }; + + agent.post('/api/auth/signin') + .send(credentials) + .expect(200) + .end(function (signinErr, res) { + console.log('---agent: ',agent); + + // Handle signin error + if (signinErr) { + return done(signinErr); + } + + var socket, cookies; + if (res.headers && res.headers['set-cookie'] && res.headers['set-cookie'].length > 0) { + cookies = res.headers['set-cookie'].join(';'); + } + + if (cookies) { + socket = io('http://localhost:3001', { + extraHeaders: { + 'Cookie': cookies + } + }); + } else { + socket = io('http://localhost:3001'); + } + + // socket.on('disconnect', function () { + // console.log('disconnected'); + // }); + + socket.on('error', function (err) { + return done(err); + }); + + socket.on('connect', function () { + // console.log('connected'); + + socket.on('chatMessage', function (data) { + console.log(data); + data.should.eql(testMessage); + done(); + }); + }); + + }); + + }); + + afterEach(function(done) { + User.remove().exec(done); + clock.restore(); + }); }); diff --git a/package.json b/package.json index de793ce0d8..b2da87a4f0 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "passport-twitter": "^1.0.2", "phantomjs": ">=1.9.0", "serve-favicon": "^2.3.0", - "socket.io": "^1.3.5", + "socket.io": "Automattic/socket.io", "swig": "^1.4.2", "validator": "^3.41.2" }, @@ -107,6 +107,7 @@ "load-grunt-tasks": "^3.2.0", "run-sequence": "^1.1.1", "should": "^7.0.1", + "sinon": "^1.16.1", "supertest": "^1.0.1" } } From bdabca116df184acdff69ba5ea1dfcbdc846157d Mon Sep 17 00:00:00 2001 From: Bastian Wegge Date: Mon, 31 Aug 2015 11:41:46 +0200 Subject: [PATCH 2/2] modified to make the outcome more clear --- config/lib/socket.io.js | 4 ++-- modules/chat/tests/server/chat.socket.tests.js | 8 +++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/config/lib/socket.io.js b/config/lib/socket.io.js index 66b3544447..691bc4f561 100644 --- a/config/lib/socket.io.js +++ b/config/lib/socket.io.js @@ -73,8 +73,8 @@ module.exports = function (app, db) { // Get the session id from the request cookies var sessionId = socket.request.signedCookies ? socket.request.signedCookies[config.sessionKey] : undefined; - console.log('sessionId:',sessionId); - + console.log('sessionId from socket.io.js:76 -->',sessionId); + if (!sessionId) return next(new Error('sessionId was not found in socket.request'), false); // Use the mongoStorage instance to get the Express session information diff --git a/modules/chat/tests/server/chat.socket.tests.js b/modules/chat/tests/server/chat.socket.tests.js index 49b9b39dc4..b7cc0563c2 100644 --- a/modules/chat/tests/server/chat.socket.tests.js +++ b/modules/chat/tests/server/chat.socket.tests.js @@ -11,9 +11,7 @@ var should = require('should'), sinon = require('sinon'), User = mongoose.model('User'), express = require(path.resolve('./config/lib/express')), - io = require('socket.io/node_modules/socket.io-client'), - querystring = require('querystring'), - http = require('http'); + io = require('socket.io/node_modules/socket.io-client'); /** * Globals @@ -70,7 +68,7 @@ describe('Socket Chat tests:', function() { .send(credentials) .expect(200) .end(function (signinErr, res) { - console.log('---agent: ',agent); + // console.log('---agent: ',agent); // Handle signin error if (signinErr) { @@ -104,7 +102,7 @@ describe('Socket Chat tests:', function() { // console.log('connected'); socket.on('chatMessage', function (data) { - console.log(data); + console.log('test-result from chatMessage (chat.socket.tests.js:105) -->',data); data.should.eql(testMessage); done(); });