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 'Authenticate', ->
|
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 'POST /authenticate', ->
|
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 | @request = request
|
39 |
|
40 | response =
|
41 | metadata:
|
42 | code: 204
|
43 | responseId: request.metadata.responseId
|
44 |
|
45 | @jobManager.createResponse 'response', response, (error) =>
|
46 | throw error if error?
|
47 |
|
48 | beforeEach (done) ->
|
49 | options =
|
50 | auth:
|
51 | username: 'irritable-captian'
|
52 | password: 'poop-deck'
|
53 |
|
54 | request.post "http://localhost:#{@port}/authenticate", options, (error, @response) =>
|
55 | done error
|
56 |
|
57 | it 'should have jobType Authenticate', ->
|
58 | expect(@request.metadata.jobType).to.equal 'Authenticate'
|
59 |
|
60 | it 'should have auth correct', ->
|
61 | expect(@request.metadata.auth).to.deep.equal uuid: 'irritable-captian', token: 'poop-deck'
|
62 |
|
63 | it 'should return a 204', ->
|
64 | expect(@response.statusCode).to.equal 204
|
65 |
|
66 | describe 'GET /authenticate/:uuid', ->
|
67 | context 'when the request is successful', ->
|
68 | beforeEach ->
|
69 | async.forever (next) =>
|
70 | @jobManager.getRequest ['request'], (error, request) =>
|
71 | next request
|
72 | return unless request?
|
73 | @request = request
|
74 |
|
75 | response =
|
76 | metadata:
|
77 | code: 204
|
78 | responseId: @request.metadata.responseId
|
79 |
|
80 | @jobManager.createResponse 'response', response, (error) =>
|
81 | throw error if error?
|
82 |
|
83 | beforeEach (done) ->
|
84 | options =
|
85 | auth:
|
86 | username: 'irritable-captian'
|
87 | password: 'poop-deck'
|
88 | qs:
|
89 | token: 'some-token'
|
90 |
|
91 | request.get "http://localhost:#{@port}/authenticate/some-uuid", options, (error, @response) =>
|
92 | done error
|
93 |
|
94 | it 'should have jobType Authenticate', ->
|
95 | expect(@request.metadata.jobType).to.equal 'Authenticate'
|
96 |
|
97 | it 'should have auth correct', ->
|
98 | expect(@request.metadata.auth).to.deep.equal uuid: 'some-uuid', token: 'some-token'
|
99 |
|
100 | it 'should return a 200', ->
|
101 | expect(@response.statusCode).to.equal 200
|