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