Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 1 | (function () { |
| 2 | 'use strict'; |
| 3 | |
| 4 | const chai = require('chai'); |
| 5 | const expect = chai.expect; |
| 6 | const sinonChai = require('sinon-chai'); |
| 7 | chai.use(sinonChai); |
| 8 | const io = require('socket.io-client'); |
| 9 | const server = require('../src/server.js'); |
Matteo Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 10 | const port = 4000; |
| 11 | describe('Websocket', function() { |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 12 | |
| 13 | var client; |
| 14 | |
| 15 | beforeEach(function(done) { |
| 16 | // Start our server |
Matteo Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 17 | server.start(port); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 18 | |
| 19 | // connect a client to the server |
Matteo Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 20 | client = io.connect(`http://localhost:${port}`, { |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 21 | query: 'name=test@xos.org&token=testToken&sessionId=testSession&id=1' |
| 22 | }); |
| 23 | |
| 24 | // when is connected start testing |
| 25 | client.on('connect', () => { |
| 26 | done(); |
| 27 | }); |
| 28 | }); |
| 29 | |
| 30 | afterEach((done) => { |
| 31 | // disconnect the client |
| 32 | if(client.connected) { |
| 33 | client.disconnect(); |
| 34 | } |
| 35 | done(); |
| 36 | }); |
| 37 | |
| 38 | it('should store user details for a new connection', () => { |
| 39 | const clients = require('../src/controllers/clients.js'); |
| 40 | const user = clients.clients[0]; |
| 41 | expect(user.name).to.equal('test@xos.org') |
| 42 | }); |
| 43 | |
| 44 | it('should not store the same user twice', (done) => { |
| 45 | |
| 46 | // connect a client to the server |
Matteo Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 47 | const client2 = io.connect(`http://localhost:${port}`, { |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 48 | query: 'name=test@xos.org&token=testToken&sessionId=testSession&id=1' |
| 49 | }); |
| 50 | |
| 51 | // when is connected start testing |
| 52 | client2.on('connect', () => { |
| 53 | setTimeout(() => { |
| 54 | const clients = require('../src/controllers/clients.js'); |
| 55 | expect(clients.clients.length).to.equal(1) |
| 56 | done(); |
| 57 | }, 100); |
| 58 | }); |
| 59 | |
| 60 | }); |
| 61 | |
| 62 | it('should remove a user on disconnect', (done) => { |
| 63 | client.disconnect(); |
| 64 | // we need to wait for the event to be dispatched |
| 65 | setTimeout(() => { |
| 66 | const clients = require('../src/controllers/clients.js'); |
| 67 | expect(clients.clients.length).to.equal(0) |
| 68 | done(); |
| 69 | }, 100); |
| 70 | }); |
| 71 | |
| 72 | }); |
| 73 | })(); |