UNPKG

4.01 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 /v2/devices/:subscriberUuid/subscriptions/:emitterUuid/:type', ->
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 context 'when the request is successful', ->
69 beforeEach ->
70 @jobManager.do (@request, callback) =>
71 response =
72 metadata:
73 code: 201
74 responseId: @request.metadata.responseId
75 data: {}
76
77 callback null, response
78
79 beforeEach (done) ->
80 options =
81 auth:
82 username: 'irritable-captian'
83 password: 'poop-deck'
84
85 request.post "http://localhost:#{@port}/v2/devices/irritable-captian/subscriptions/another-uuid/broadcast", options, (error, @response, @body) =>
86 done error
87
88 it 'should have the correct jobType', ->
89 expect(@request.metadata.jobType).to.equal 'CreateSubscription'
90
91 it 'should have the right toUuid', ->
92 expect(@request.metadata.toUuid).to.equal 'irritable-captian'
93
94 it 'should have the correct data', ->
95 expect(JSON.parse @request.rawData).to.deep.equal {subscriberUuid:'irritable-captian', emitterUuid: 'another-uuid', type: 'broadcast'}
96
97 it 'should return a 204', ->
98 expect(@response.statusCode).to.equal 204
99
100 context 'when the subscription already exists', ->
101 beforeEach ->
102 @jobManager.do (@request, callback) =>
103 response =
104 metadata:
105 code: 304
106 responseId: @request.metadata.responseId
107 data: {}
108
109 callback null, response
110
111 beforeEach (done) ->
112 options =
113 auth:
114 username: 'irritable-captian'
115 password: 'poop-deck'
116
117 request.post "http://localhost:#{@port}/v2/devices/irritable-captian/subscriptions/another-uuid/broadcast", options, (error, @response, @body) =>
118 done error
119
120 it 'should have the correct jobType', ->
121 expect(@request.metadata.jobType).to.equal 'CreateSubscription'
122
123 it 'should have the right toUuid', ->
124 expect(@request.metadata.toUuid).to.equal 'irritable-captian'
125
126 it 'should have the correct data', ->
127 expect(JSON.parse @request.rawData).to.deep.equal {subscriberUuid:'irritable-captian', emitterUuid: 'another-uuid', type: 'broadcast'}
128
129 it 'should return a 204', ->
130 expect(@response.statusCode).to.equal 204