UNPKG

9.27 kBtext/coffeescriptView Raw
1_ = require 'lodash'
2UUID = require 'uuid'
3request = require 'request'
4Server = require '../../src/server'
5Redis = require 'ioredis'
6RedisNS = require '@octoblu/redis-ns'
7{ JobManagerResponder } = require 'meshblu-core-job-manager'
8
9describe 'POST /search/devices, GET /v2/devices, GET /devices', ->
10 beforeEach (done) ->
11 @responseQueueId = UUID.v4()
12 @requestQueueName = "request:queue:#{@responseQueueId}"
13 @responseQueueName = "response:queue:#{@responseQueueId}"
14 @namespace = 'test:meshblu-http'
15 @jobLogQueue = 'test:meshblu:job-log'
16 @redisUri = 'redis://localhost'
17 @port = 0xd00d
18 @sut = new Server {
19 @port
20 disableLogging: true
21 jobTimeoutSeconds: 1
22 @namespace
23 @jobLogQueue
24 jobLogRedisUri: @redisUri
25 jobLogSampleRate: 1
26 redisUri: @redisUri
27 cacheRedisUri: @redisUri
28 @requestQueueName
29 @responseQueueName
30 }
31
32 @sut.run done
33
34 afterEach ->
35 @sut.stop()
36
37 beforeEach (done) ->
38 @redis = new RedisNS @namespace, new Redis @redisUri, dropBufferSupport: true
39 @redis.on 'ready', done
40
41 afterEach (done) ->
42 @redis.del @requestQueueName, @responseQueueName, done
43 return # avoid returning redis
44
45 beforeEach (done) ->
46 @workerFunc = (@request, callback=_.noop) =>
47 @jobManagerDo @request, callback
48
49 @jobManager = new JobManagerResponder {
50 @redisUri
51 @namespace
52 @workerFunc
53 maxConnections: 1
54 queueTimeoutSeconds: 1
55 jobTimeoutSeconds: 1
56 jobLogSampleRate: 1
57 requestQueueName: @requestQueueName
58 responseQueueName: @responseQueueName
59 }
60 @jobManager.start done
61
62 beforeEach ->
63 @jobManager.do = (@jobManagerDo) =>
64
65 afterEach ->
66 @jobManager.stop()
67
68 describe '->v3', ->
69 context 'when the request is successful', ->
70 beforeEach ->
71 @jobManager.do (@request, callback) =>
72 response =
73 metadata:
74 code: 200
75 responseId: @request.metadata.responseId
76 name: 'dinosaur-getter'
77 data: [
78 {uuid: 't-rex'}
79 {uuid: 'megalodon'}
80 {uuid: 'killasaurus'}
81 ]
82
83 callback null, response
84
85 beforeEach (done) ->
86 options =
87 auth:
88 username: 'irritable-captian'
89 password: 'poop-deck'
90 json: type: 'dinosaur'
91
92 headers:
93 'x-meshblu-as': 'treasure-map'
94 'x-meshblu-erik-feature': 'custom-headers'
95
96 request.post "http://localhost:#{@port}/search/devices", options, (error, @response, @body) =>
97 done error
98
99 it 'should return a 200', ->
100 expect(@response.statusCode).to.equal 200
101
102 it 'should dispatch the correct metadata', ->
103 expect(@request).to.containSubset
104 metadata:
105 fromUuid: 'treasure-map'
106 erikFeature: 'custom-headers'
107 auth:
108 uuid: 'irritable-captian'
109 token: 'poop-deck'
110
111 it 'should send the search body as the data of the job', ->
112 data = JSON.parse @request.rawData
113 expect(data).to.containSubset type: 'dinosaur'
114
115 it 'should have a devices array in the response', ->
116 expect(@body).to.be.an('array')
117 expect(@body.length).to.equal 3
118
119
120 describe '->v2', ->
121 context 'when the request is successful', ->
122 beforeEach ->
123 @jobManager.do (@request, callback) =>
124 response =
125 metadata:
126 code: 200
127 responseId: @request.metadata.responseId
128 data: [
129 {uuid: 't-rex'}
130 {uuid: 'megalodon'}
131 {uuid: 'killasaurus'}
132 ]
133
134 callback null, response
135
136 beforeEach (done) ->
137 options =
138 auth:
139 username: 'irritable-captian'
140 password: 'poop-deck'
141 json: true
142 qs:
143 type: 'dinosaur'
144 headers:
145 'x-meshblu-as': 'treasure-map'
146
147 request.get "http://localhost:#{@port}/v2/devices", options, (error, @response, @body) =>
148 done error
149
150 it 'should return a 200', ->
151 expect(@response.statusCode).to.equal 200
152
153 it 'should dispatch the correct metadata', ->
154 expect(@request).to.containSubset
155 metadata:
156 fromUuid: 'treasure-map'
157 auth:
158 uuid: 'irritable-captian'
159 token: 'poop-deck'
160
161 it 'should send the search body as the data of the job', ->
162 data = JSON.parse @request.rawData
163 expect(data).to.deep.equal type: 'dinosaur'
164
165 it 'should have a devices array in the response', ->
166 expect(@body).to.be.an 'array'
167 expect(@body.length).to.equal 3
168
169 describe '->mydevices', ->
170 context 'when the request is successful', ->
171 beforeEach ->
172 @jobManager.do (@request, callback) =>
173 response =
174 metadata:
175 code: 200
176 responseId: @request.metadata.responseId
177 data: [
178 {uuid: 't-rex'}
179 {uuid: 'megalodon'}
180 {uuid: 'killasaurus'}
181 ]
182
183 callback null, response
184
185 beforeEach (done) ->
186 options =
187 auth:
188 username: 'irritable-captian'
189 password: 'poop-deck'
190 json: true
191 qs:
192 type: 'dinosaur'
193 headers:
194 'x-meshblu-as': 'treasure-map'
195
196 request.get "http://localhost:#{@port}/mydevices", options, (error, @response, @body) =>
197 done error
198
199 it 'should return a 200', ->
200 expect(@response.statusCode).to.equal 200
201
202 it 'should dispatch the correct metadata', ->
203 expect(@request).to.containSubset
204 metadata:
205 fromUuid: 'treasure-map'
206 auth:
207 uuid: 'irritable-captian'
208 token: 'poop-deck'
209
210 it 'should send the search body as the data of the job', ->
211 data = JSON.parse @request.rawData
212 expect(data).to.deep.equal type: 'dinosaur', owner: 'treasure-map'
213
214 it 'should have a devices array in the response', ->
215 expect(@body.devices).to.be.an 'array'
216 expect(@body.devices.length).to.equal 3
217
218 describe '->v1', ->
219 context 'when the request is successful', ->
220 beforeEach ->
221 @jobManager.do (@request, callback) =>
222 response =
223 metadata:
224 code: 200
225 responseId: @request.metadata.responseId
226 data: [
227 {uuid: 't-rex'}
228 {uuid: 'megalodon'}
229 {uuid: 'killasaurus'}
230 ]
231
232 callback null, response
233
234 beforeEach (done) ->
235 options =
236 auth:
237 username: 'irritable-captian'
238 password: 'poop-deck'
239 json: true
240 qs:
241 type: 'dinosaur'
242 headers:
243 'x-meshblu-as': 'treasure-map'
244
245 request.get "http://localhost:#{@port}/devices", options, (error, @response, @body) =>
246 done error
247
248 it 'should return a 200', ->
249 expect(@response.statusCode).to.equal 200
250
251 it 'should dispatch the correct metadata', ->
252 expect(@request).to.containSubset
253 metadata:
254 fromUuid: 'treasure-map'
255 auth:
256 uuid: 'irritable-captian'
257 token: 'poop-deck'
258
259 it 'should send the search body as the data of the job', ->
260 data = JSON.parse @request.rawData
261 expect(data).to.deep.equal type: 'dinosaur'
262
263 it 'should have a devices array in the response', ->
264 expect(@body.devices.length).to.equal 3
265
266 context 'when the request is successful and a uuid and token is in the query', ->
267 beforeEach ->
268 @jobManager.do (@request, callback) =>
269 response =
270 metadata:
271 code: 200
272 responseId: @request.metadata.responseId
273 data: [
274 {uuid: 't-rex'}
275 {uuid: 'megalodon'}
276 {uuid: 'killasaurus'}
277 ]
278
279 callback null, response
280
281 beforeEach (done) ->
282 options =
283 auth:
284 username: 'irritable-captian'
285 password: 'poop-deck'
286 json: true
287 qs:
288 type: 'dinosaur'
289 uuid: 'use-this-uuid'
290 token: 'use-this-token'
291 headers:
292 'x-meshblu-as': 'treasure-map'
293
294 request.get "http://localhost:#{@port}/devices", options, (error, @response, @body) =>
295 done error
296
297 it 'should return a 200', ->
298 expect(@response.statusCode).to.equal 200
299
300 it 'should dispatch the correct metadata', ->
301 expect(@request).to.containSubset
302 metadata:
303 fromUuid: 'treasure-map'
304 auth:
305 uuid: 'use-this-uuid'
306 token: 'use-this-token'
307
308 it 'should send the search body as the data of the job', ->
309 data = JSON.parse @request.rawData
310 expect(data).to.deep.equal uuid: 'use-this-uuid', type: 'dinosaur'
311
312 it 'should have a devices array in the response', ->
313 expect(@body.devices.length).to.equal 3