blob: eb6f52139fb818d4748aa5de8691cc0c9d6314bc [file] [log] [blame]
Matteo Scandolobd13aab2017-08-08 13:05:24 -07001
2/*
3 * Copyright 2017-present Open Networking Foundation
4
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8
9 * http://www.apache.org/licenses/LICENSE-2.0
10
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080019(function () {
20 'use strict';
21
22 const chai = require('chai');
23 const expect = chai.expect;
24 const sinonChai = require('sinon-chai');
25 chai.use(sinonChai);
26 const io = require('socket.io-client');
27 const server = require('../src/server.js');
Matteo Scandolof05d8a62016-12-06 13:36:49 -080028 const port = 4000;
29 describe('Websocket', function() {
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080030
31 var client;
32
33 beforeEach(function(done) {
34 // Start our server
Matteo Scandolof05d8a62016-12-06 13:36:49 -080035 server.start(port);
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080036
37 // connect a client to the server
Matteo Scandolof05d8a62016-12-06 13:36:49 -080038 client = io.connect(`http://localhost:${port}`, {
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080039 query: 'name=test@xos.org&token=testToken&sessionId=testSession&id=1'
40 });
41
42 // when is connected start testing
43 client.on('connect', () => {
44 done();
45 });
46 });
47
48 afterEach((done) => {
49 // disconnect the client
50 if(client.connected) {
51 client.disconnect();
52 }
53 done();
54 });
55
56 it('should store user details for a new connection', () => {
57 const clients = require('../src/controllers/clients.js');
58 const user = clients.clients[0];
59 expect(user.name).to.equal('test@xos.org')
60 });
61
62 it('should not store the same user twice', (done) => {
63
64 // connect a client to the server
Matteo Scandolof05d8a62016-12-06 13:36:49 -080065 const client2 = io.connect(`http://localhost:${port}`, {
Matteo Scandoloe3ed0162016-12-01 10:09:12 -080066 query: 'name=test@xos.org&token=testToken&sessionId=testSession&id=1'
67 });
68
69 // when is connected start testing
70 client2.on('connect', () => {
71 setTimeout(() => {
72 const clients = require('../src/controllers/clients.js');
73 expect(clients.clients.length).to.equal(1)
74 done();
75 }, 100);
76 });
77
78 });
79
80 it('should remove a user on disconnect', (done) => {
81 client.disconnect();
82 // we need to wait for the event to be dispatched
83 setTimeout(() => {
84 const clients = require('../src/controllers/clients.js');
85 expect(clients.clients.length).to.equal(0)
86 done();
87 }, 100);
88 });
89
90 });
91})();