endo-core
Version:
Put some description here
185 lines (157 loc) • 5.5 kB
text/coffeescript
{afterEach, beforeEach, describe, it} = global
{expect} = require 'chai'
sinon = require 'sinon'
fs = require 'fs'
request = require 'request'
URL = require 'url'
Encryption = require 'meshblu-encryption'
enableDestroy = require 'server-destroy'
shmock = require 'shmock'
SocketIO = require 'socket.io'
MockStrategy = require '../mock-strategy'
Endo = require '../..'
_ = require 'lodash'
describe 'firehose', ->
beforeEach 'setup socket.io', ->
= new SocketIO 0xcaf1
afterEach ->
.close()
beforeEach 'setup meshblu http', ->
= new Buffer('peter:i-could-eat').toString 'base64'
= fs.readFileSync "#{__dirname}/../data/private-key.pem", 'utf8'
= Encryption.fromPem
= .key.exportKey 'public'
= shmock 0xd00d
enableDestroy
class MessageHandler
_onMessage: sinon.stub()
onMessage: (data, callback) =>
data, callback
if ?
= new MessageHandler()
.get '/v2/whoami'
.set 'Authorization', "Basic #{@serviceAuth}"
.reply 200, {
options:
imageUrl: "http://this-is-an-image.exe"
}
.get '/publickey'
.reply 200, {}
afterEach (done) ->
.destroy done
beforeEach 'setup sut', ->
options =
logFn: ->
messageHandler:
serviceUrl: 'http://octoblu.xxx'
deviceType: 'endo-endor'
skipExpress: true
useFirehose: true
meshbluConfig:
hostname: 'localhost'
protocol: 'http'
port: 0xd00d
uuid: 'peter'
token: 'i-could-eat'
privateKey:
firehoseMeshbluConfig:
hostname: 'localhost'
protocol: 'http'
port: 0xcaf1
uuid: 'peter'
token: 'i-could-eat'
privateKey:
= new Endo options
afterEach (done) ->
.stop done
describe '->run', ->
beforeEach (done) ->
.on 'connection', () =>
{, } = URL.parse .client.request.url, true
= .client.request.headers['x-meshblu-uuid']
= .client.request.headers['x-meshblu-token']
done()
.run =>
it 'should connect', ->
expect().to.exist
expect().to.equal '/socket.io/v1/peter/'
it 'should pass along the auth info', ->
expect().to.equal 'peter'
expect().to.equal 'i-could-eat'
expect(.uuid).to.equal 'peter'
expect(.token).to.equal 'i-could-eat'
describe 'when the credentials device has an encrypted token', ->
beforeEach 'message', ->
=
metadata:
route: [
{"from": "flow-uuid", "to": "user-uuid", "type": "message.sent"}
{"from": "user-uuid", "to": "cred-uuid", "type": "message.received"}
{"from": "cred-uuid", "to": "cred-uuid", "type": "message.received"}
{"from": "cred-uuid", "to": "peter", "type": "message.received"}
]
rawData: JSON.stringify(
metadata:
jobType: 'hello'
respondTo: foo: 'bar'
data:
greeting: 'hola'
)
beforeEach 'credentials-device', ->
unencrypted =
secrets:
credentialsDeviceToken: 'cred-token'
credentials:
secret: 'this is secret'
endo =
authorizedKey: 'some-uuid'
credentialsDeviceUuid: 'cred-uuid'
encrypted: .encrypt unencrypted
endoSignature = .sign {
authorizedKey: 'some-uuid'
credentialsDeviceUuid: 'cred-uuid'
encrypted: unencrypted
}
.post '/search/devices'
.set 'Authorization', "Basic #{@serviceAuth}"
.set 'x-meshblu-as', 'cred-uuid'
.send uuid: 'cred-uuid'
.reply 200, [
uuid: 'cred-uuid'
endoSignature: endoSignature
endo: endo
]
beforeEach (done) ->
credentialsDeviceAuth = new Buffer('cred-uuid:cred-token').toString 'base64'
=
.post '/messages'
.set 'Authorization', "Basic #{credentialsDeviceAuth}"
.set 'x-meshblu-as', 'user-uuid'
.send
devices: ['flow-uuid']
metadata:
code: 200
to: { foo: 'bar' }
data:
whatever: 'this is a response'
.reply 201
.emit 'message',
.done = _.once done
._onMessage.yields null, metadata: {code: 200}, data: {whatever: 'this is a response'}
it 'should respond to the message via meshblu', (done)->
.wait(1000, done)
it 'should call the hello messageHandler with the message and auth', ->
expect(._onMessage).to.have.been.calledWith {
encrypted:
secrets:
credentials:
secret: 'this is secret'
metadata:
jobType: 'hello'
respondTo: foo: 'bar'
data:
greeting: 'hola'
}