endo-core
Version:
Put some description here
148 lines (120 loc) • 4.25 kB
text/coffeescript
{afterEach, beforeEach, describe, it} = global
{expect} = require 'chai'
sinon = require 'sinon'
fs = require 'fs'
Encryption = require 'meshblu-encryption'
request = require 'request'
enableDestroy = require 'server-destroy'
shmock = require 'shmock'
MockStrategy = require '../mock-strategy'
Server = require '../..'
describe 'response schema', ->
beforeEach (done) ->
= fs.readFileSync "#{__dirname}/../data/private-key.pem", 'utf8'
= Encryption.fromPem
= .key.exportKey 'public'
encrypted =
secrets:
credentials:
secret: 'this is secret'
= .encrypt encrypted
= shmock 0xd00d
enableDestroy
= new MockStrategy name: 'api'
= new MockStrategy name: 'octoblu'
= responseSchema: sinon.stub()
.get '/v2/whoami'
.set 'Authorization', "Basic cGV0ZXI6aS1jb3VsZC1lYXQ="
.reply 200, {
options:
imageUrl: "http://this-is-an-image.exe"
}
.get '/publickey'
.reply 200, {}
serverOptions =
logFn: ->
port: undefined,
disableLogging: true
apiStrategy:
octobluStrategy:
messageHandler:
serviceUrl: 'http://octoblu.xxx'
deviceType: 'endo-endor'
meshbluConfig:
hostname: 'localhost'
protocol: 'http'
port: 0xd00d
uuid: 'peter'
token: 'i-could-eat'
privateKey:
appOctobluHost: 'app.octoblu.guru'
userDeviceManagerUrl: 'http://manage-my.endo'
meshbluPublicKeyUri: 'http://localhost:53261/publickey'
= new Server serverOptions
.run (error) =>
return done error if error?
= .address().port
done()
afterEach (done) ->
.stop done
afterEach (done) ->
.destroy done
describe 'On GET /v1/response-schema', ->
describe 'when the message-handler yields an empty object', ->
beforeEach (done) ->
.responseSchema.yields null, {}
options =
baseUrl: "http://localhost:#{@serverPort}"
json: true
request.get '/v1/response-schema', options, (error, , ) =>
done error
it 'should return a 200', ->
expect(.statusCode).to.equal 200, JSON.stringify
it 'should return the empty object', ->
expect().to.deep.equal {}
describe 'when the message-handler yields a larger schema', ->
beforeEach (done) ->
.responseSchema.yields null, {
doSomething:
type: 'object'
required: ['name', 'color']
properties:
name:
type: 'string'
color:
type: 'string'
}
options =
baseUrl: "http://localhost:#{@serverPort}"
json: true
request.get '/v1/response-schema', options, (error, , ) =>
done error
it 'should return a 200', ->
expect(.statusCode).to.equal 200, JSON.stringify
it 'should return the schema', ->
expect().to.deep.equal {
doSomething:
type: 'object'
required: ['name', 'color']
properties:
name:
type: 'string'
color:
type: 'string'
}
describe 'when the message-handler yields an error', ->
beforeEach (done) ->
error = new Error 'Something is awry'
error.code = 418
.responseSchema.yields error
options =
baseUrl: "http://localhost:#{@serverPort}"
json: true
request.get '/v1/response-schema', options, (error, , ) =>
done error
it 'should return a 418', ->
expect(.statusCode).to.equal 418, JSON.stringify
it 'should return the schema', ->
expect().to.deep.equal error: 'Something is awry'