blob: 51b9cec61bab89a15eb0ad761d55465584faa8ee [file] [log] [blame]
Matteo Scandoloe3ed0162016-12-01 10:09:12 -08001(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 Scandolof05d8a62016-12-06 13:36:49 -080010 const port = 4000;
11 describe('Websocket', function() {
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080012
13 var client;
14
15 beforeEach(function(done) {
16 // Start our server
Matteo Scandolof05d8a62016-12-06 13:36:49 -080017 server.start(port);
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080018
19 // connect a client to the server
Matteo Scandolof05d8a62016-12-06 13:36:49 -080020 client = io.connect(`http://localhost:${port}`, {
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080021 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 Scandolof05d8a62016-12-06 13:36:49 -080047 const client2 = io.connect(`http://localhost:${port}`, {
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080048 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})();