1 | {afterEach, beforeEach, describe, it} = global
|
2 | {expect} = require 'chai'
|
3 | sinon = require 'sinon'
|
4 | URL = require 'url'
|
5 | _ = require 'lodash'
|
6 | enableDestroy = require 'server-destroy'
|
7 | shmock = require 'shmock'
|
8 | SocketIO = require 'socket.io'
|
9 | Inquisitor = require '..'
|
10 |
|
11 | describe 'connect', ->
|
12 | beforeEach 'meshblu', ->
|
13 | @meshblu = shmock 0xd00d
|
14 | enableDestroy(@meshblu)
|
15 |
|
16 | afterEach (done) ->
|
17 | @meshblu.destroy done
|
18 |
|
19 | beforeEach 'setup socket.io', ->
|
20 | @firehoseServer = new SocketIO 0xcaf1
|
21 |
|
22 | afterEach 'close socket.io', (done) ->
|
23 | _.delay =>
|
24 | @firehoseServer.close()
|
25 | done()
|
26 | , 50
|
27 |
|
28 | beforeEach ->
|
29 | meshbluConfig =
|
30 | uuid: 'user-uuid'
|
31 | token: 'user-token'
|
32 | hostname: 'localhost'
|
33 | port: 0xd00d
|
34 | protocol: 'http'
|
35 |
|
36 | firehoseConfig =
|
37 | hostname: 'localhost'
|
38 | port: 0xcaf1
|
39 | protocol: 'http'
|
40 |
|
41 | uuid = 'inquisitor-uuid'
|
42 |
|
43 | @userAuth = new Buffer('user-uuid:user-token').toString 'base64'
|
44 | @sut = new Inquisitor {meshbluConfig, firehoseConfig, uuid}
|
45 |
|
46 | it 'should exist', ->
|
47 | expect(@sut).to.exist
|
48 |
|
49 | describe '->connect', ->
|
50 | beforeEach ->
|
51 | @meshblu
|
52 | .post '/devices/inquisitor-uuid/tokens'
|
53 | .set 'Authorization', "Basic #{@userAuth}"
|
54 | .reply 201, uuid: "inquisitor-uuid", token: "inquisitor-token"
|
55 |
|
56 | beforeEach 'mock getMonitoredDevices, out of laziness', ->
|
57 | @deviceMap = [
|
58 | {
|
59 | device: { uuid: 'device-1', statusDevice: 'status-device', otherProperty: false }
|
60 | statusDevice: 'status-device'
|
61 | errors: ['look-an-error']
|
62 | }
|
63 | {
|
64 | device: { uuid: 'device-2', errors: ['yet-another-error'] }
|
65 | statusDevice: 'device-2'
|
66 | errors: ['yet-another-error']
|
67 | }
|
68 | ]
|
69 | @sut.getMonitoredDevices = sinon.stub().yields null, @deviceMap
|
70 |
|
71 | beforeEach (done) ->
|
72 | @firehoseServer.on 'connection', (@socket) =>
|
73 |
|
74 | {@pathname, @query} = URL.parse @socket.client.request.url, true
|
75 | @uuid = @socket.client.request.headers['x-meshblu-uuid']
|
76 | @token = @socket.client.request.headers['x-meshblu-token']
|
77 | done()
|
78 |
|
79 | @sut.connect =>
|
80 | return null
|
81 |
|
82 | afterEach (done) ->
|
83 | @sut.stop done
|
84 | return null
|
85 |
|
86 | it 'should connect', ->
|
87 | expect(@socket).to.exist
|
88 | expect(@pathname).to.equal '/socket.io/v1/inquisitor-uuid/'
|
89 |
|
90 | it 'should pass along the auth info', ->
|
91 | expect(@uuid).to.equal 'inquisitor-uuid'
|
92 | expect(@token).to.equal 'inquisitor-token'
|
93 | expect(@query.uuid).to.equal 'inquisitor-uuid'
|
94 | expect(@query.token).to.equal 'inquisitor-token'
|
95 |
|
96 | describe 'when we get a config update from the firehose', ->
|
97 | beforeEach (done) ->
|
98 | @sut.on 'status-update', (@message) => done()
|
99 | changeEvent =
|
100 | metadata:
|
101 | route: [
|
102 | {
|
103 | from: "device-2"
|
104 | to: "inquisitor-uuid"
|
105 | type: "configure.received"
|
106 | }
|
107 | {
|
108 | from: "inquisitor-uuid"
|
109 | to: "inquisitor-uuid"
|
110 | type: "configure.received"
|
111 | }
|
112 | ]
|
113 | rawData: JSON.stringify(
|
114 | uuid: "device-2"
|
115 | errors: [
|
116 | message: '#watchit'
|
117 | code: 101
|
118 | ]
|
119 | )
|
120 | @socket.emit 'message', changeEvent
|
121 |
|
122 | it 'should emit a message in the right format', ->
|
123 | expectedMessage =
|
124 | uuid: 'device-2'
|
125 | statusDevice: 'device-2'
|
126 | errors: [
|
127 | message: '#watchit'
|
128 | code: 101
|
129 | ]
|
130 | device:
|
131 | uuid: "device-2"
|
132 | errors: [
|
133 | message: '#watchit'
|
134 | code: 101
|
135 | ]
|
136 | expect(@message).to.deep.equal expectedMessage
|
137 |
|
138 |
|
139 | describe 'when we get a config update from the firehose from a status-device', ->
|
140 | beforeEach (done) ->
|
141 | @sut.on 'status-update', (@message) => done()
|
142 |
|
143 | changeEvent =
|
144 | metadata:
|
145 | route: [
|
146 | {
|
147 | from: "status-device"
|
148 | to: "inquisitor-uuid"
|
149 | type: "configure.received"
|
150 | }
|
151 | {
|
152 | from: "inquisitor-uuid"
|
153 | to: "inquisitor-uuid"
|
154 | type: "configure.received"
|
155 | }
|
156 | ]
|
157 | rawData: JSON.stringify(
|
158 | uuid: "status-device"
|
159 | errors: [
|
160 | message: '#watchit'
|
161 | code: 101
|
162 | ]
|
163 | )
|
164 | @socket.emit 'message', changeEvent
|
165 |
|
166 | it 'should emit a message in the right format', ->
|
167 | expectedMessage =
|
168 | statusDevice: 'status-device'
|
169 | uuid: 'device-1'
|
170 | errors: [
|
171 | message: '#watchit'
|
172 | code: 101
|
173 | ]
|
174 | device:
|
175 | uuid: 'device-1'
|
176 | statusDevice: 'status-device'
|
177 | otherProperty: false
|
178 |
|
179 | expect(@message).to.deep.equal expectedMessage
|