endo-core
Version:
Put some description here
344 lines (299 loc) • 12.1 kB
text/coffeescript
{afterEach, beforeEach, describe, it} = global
{expect} = require 'chai'
sinon = require 'sinon'
fs = require 'fs'
request = require 'request'
Encryption = require 'meshblu-encryption'
enableDestroy = require 'server-destroy'
shmock = require '@octoblu/shmock'
MockStrategy = require '../mock-strategy'
Server = require '../../src/server'
describe 'messages', ->
beforeEach (done) ->
= fs.readFileSync "#{__dirname}/../data/private-key.pem", 'utf8'
= Encryption.fromPem
encrypted =
secrets:
credentials:
secret: 'this is secret'
= .encrypt encrypted
= shmock 0xd00d
enableDestroy
= new MockStrategy name: 'api'
= new MockStrategy name: 'octoblu'
= onMessage: sinon.stub()
.get '/v2/whoami'
.set 'Authorization', "Basic cGV0ZXI6aS1jb3VsZC1lYXQ="
.reply 200, {
options:
imageUrl: "http://this-is-an-image.exe"
}
serverOptions =
logFn: ->
port: undefined,
disableLogging: true
apiStrategy:
octobluStrategy:
messageHandler:
serviceUrl: 'http://octoblu.xxx'
deviceType: 'endo-endor'
meshbluConfig:
server: 'localhost'
port: 0xd00d
uuid: 'peter'
token: 'i-could-eat'
privateKey:
appOctobluHost: 'http://app.octoblu.mom'
userDeviceManagerUrl: 'http://manage-my.endo'
= new Server serverOptions
.run (error) =>
return done error if error?
= .address().port
done()
afterEach (done) ->
.stop done
afterEach (done) ->
.destroy done
describe 'On POST /v1/messages', ->
describe 'when authorized', ->
beforeEach ->
= new Buffer('cred-uuid:cred-token').toString 'base64'
.post '/authenticate'
.set 'Authorization', "Basic #{@credentialsDeviceAuth}"
.reply 204
describe 'when we get some weird device instead of a credentials device', ->
beforeEach ->
serviceAuth = new Buffer('peter:i-could-eat').toString 'base64'
.get '/v2/devices/cred-uuid'
.set 'Authorization', "Basic #{serviceAuth}"
.reply 200,
uuid: 'cred-uuid'
banana: 'pudding'
describe 'when called with a valid message', ->
beforeEach (done) ->
options =
baseUrl: "http://localhost:#{@serverPort}"
headers:
'x-meshblu-route': JSON.stringify [
{"from": "flow-uuid", "to": "user-device", "type": "message.sent"}
{"from": "user-device", "to": "cred-uuid", "type": "message.received"}
]
json:
metadata:
jobType: 'hello'
data:
greeting: 'hola'
auth:
username: 'cred-uuid'
password: 'cred-token'
request.post '/v1/messages', options, (error, , ) =>
done error
it 'should return a 400', ->
expect(.statusCode).to.equal 400, JSON.stringify
describe 'when we get an invalid credentials device', ->
beforeEach ->
serviceAuth = new Buffer('peter:i-could-eat').toString 'base64'
.get '/v2/devices/cred-uuid'
.set 'Authorization', "Basic #{serviceAuth}"
.reply 200,
uuid: 'cred-uuid'
endoSignature: 'John Hancock. Definitely, definitely John Hancock'
endo:
credentialsDeviceUuid: 'cred-uuid'
encrypted:
describe 'when called with a valid message', ->
beforeEach (done) ->
options =
baseUrl: "http://localhost:#{@serverPort}"
headers:
'x-meshblu-route': JSON.stringify [
{"from": "flow-uuid", "to": "user-device", "type": "message.sent"}
{"from": "user-device", "to": "cred-uuid", "type": "message.received"}
]
json:
metadata:
jobType: 'hello'
data:
greeting: 'hola'
auth:
username: 'cred-uuid'
password: 'cred-token'
request.post '/v1/messages', options, (error, , ) =>
done error
it 'should return a 400', ->
expect(.statusCode).to.equal 400, JSON.stringify
describe 'when we have a real credentials device', ->
beforeEach ->
serviceAuth = new Buffer('peter:i-could-eat').toString 'base64'
.get '/v2/devices/cred-uuid'
.set 'Authorization', "Basic #{serviceAuth}"
.reply 200,
uuid: 'cred-uuid'
endoSignature: 'LebOB6aPRQJC7HuLqVqwBeZOFITW+S+jTExlXKrnhvcbzgn6b82fwyh0Qin8ccMym9y4ymIWcKunfa9bZj2YsA=='
endo:
authorizedKey: 'some-uuid'
credentialsDeviceUuid: 'cred-uuid'
encrypted:
describe 'when called with a message without metadata', ->
beforeEach (done) ->
options =
baseUrl: "http://localhost:#{@serverPort}"
json:
data:
greeting: 'hola'
auth:
username: 'cred-uuid'
password: 'cred-token'
request.post '/v1/messages', options, (error, , ) =>
done error
it 'should return a 422', ->
expect(.statusCode).to.equal 422, JSON.stringify()
describe 'when called with a valid message', ->
beforeEach (done) ->
.onMessage.yields null, metadata: {code: 200}, data: {whatever: 'this is a response'}
=
.post '/messages'
.set 'Authorization', "Basic #{@credentialsDeviceAuth}"
.set 'x-meshblu-as', 'user-device'
.send
devices: ['flow-uuid']
metadata:
code: 200
to: { foo: 'bar' }
data:
whatever: 'this is a response'
.reply 201
options =
baseUrl: "http://localhost:#{@serverPort}"
headers:
'x-meshblu-route': JSON.stringify [
{"from": "flow-uuid", "to": "user-device", "type": "message.sent"}
{"from": "user-device", "to": "cred-uuid", "type": "message.received"}
]
json:
metadata:
jobType: 'hello'
respondTo: { foo: 'bar' }
data:
greeting: 'hola'
auth:
username: 'cred-uuid'
password: 'cred-token'
request.post '/v1/messages', options, (error, , ) =>
done error
it 'should return a 201', ->
expect(.statusCode).to.equal 201, JSON.stringify
it 'should respond to the message via meshblu', ->
.done()
it 'should call the hello messageHandler with the message and auth', ->
expect(.onMessage).to.have.been.calledWith sinon.match {
encrypted:
secrets:
credentials:
secret: 'this is secret'
}, {
metadata:
jobType: 'hello'
data:
greeting: 'hola'
}
describe 'when called with a valid message, but theres an error', ->
beforeEach (done) ->
.onMessage.yields new Error 'Something very bad happened'
=
.post '/messages'
.set 'Authorization', "Basic #{@credentialsDeviceAuth}"
.set 'x-meshblu-as', 'user-device'
.send
devices: ['flow-uuid']
metadata:
code: 500
to: 'food'
error:
message: 'Something very bad happened'
.reply 201
options =
baseUrl: "http://localhost:#{@serverPort}"
headers:
'x-meshblu-route': JSON.stringify [
{"from": "flow-uuid", "to": "user-device", "type": "message.sent"}
{"from": "user-device", "to": "cred-uuid", "type": "message.received"}
]
json:
metadata:
jobType: 'hello'
respondTo: 'food'
data:
greeting: 'hola'
auth:
username: 'cred-uuid'
password: 'cred-token'
request.post '/v1/messages', options, (error, , ) =>
done error
it 'should call the onMessage messageHandler with the message and auth', ->
expect(.onMessage).to.have.been.calledWith sinon.match {
encrypted:
secrets:
credentials:
secret: 'this is secret'
}, {
metadata:
jobType: 'hello'
data:
greeting: 'hola'
}
it 'should return a 500', ->
expect(.statusCode).to.equal 500, JSON.stringify
it 'should respond to the message with the error via meshblu', ->
.done()
describe 'when called with a valid message, but the the endo is invalid', ->
beforeEach (done) ->
.onMessage.yields new Error 'Something very bad happened'
=
.post '/messages'
.set 'Authorization', "Basic #{@credentialsDeviceAuth}"
.set 'x-meshblu-as', 'user-device'
.send
devices: ['flow-uuid']
metadata:
code: 500
error:
message: 'Something very bad happened'
.reply 201
options =
baseUrl: "http://localhost:#{@serverPort}"
headers:
'x-meshblu-route': JSON.stringify [
{"from": "flow-uuid", "to": "user-device", "type": "message.sent"}
{"from": "user-device", "to": "cred-uuid", "type": "message.received"}
]
json:
metadata:
jobType: 'hello'
data:
greeting: 'hola'
auth:
username: 'cred-uuid'
password: 'cred-token'
request.post '/v1/messages', options, (error, , ) =>
done error
it 'should call the hello messageHandler with the message and auth', ->
expect(.onMessage).to.have.been.calledWith sinon.match {
metadata:
jobType: 'hello'
data:
greeting: 'hola'
encrypted:
secrets:
credentials:
secret: 'this is secret'
}
it 'should return a 500', ->
expect(.statusCode).to.equal 500, JSON.stringify
it 'should respond to the message with the error via meshblu', ->
.done()