Matteo Scandolo | bd13aab | 2017-08-08 13:05:24 -0700 | [diff] [blame] | 1 | |
| 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 Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 19 | (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 Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 28 | const port = 4000; |
| 29 | describe('Websocket', function() { |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 30 | |
| 31 | var client; |
| 32 | |
| 33 | beforeEach(function(done) { |
| 34 | // Start our server |
Matteo Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 35 | server.start(port); |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 36 | |
| 37 | // connect a client to the server |
Matteo Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 38 | client = io.connect(`http://localhost:${port}`, { |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 39 | 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 Scandolo | f05d8a6 | 2016-12-06 13:36:49 -0800 | [diff] [blame] | 65 | const client2 = io.connect(`http://localhost:${port}`, { |
Matteo Scandolo | e3ed016 | 2016-12-01 10:09:12 -0800 | [diff] [blame] | 66 | 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 | })(); |