balena-supervisor/test/lib/mocked-balena-api.coffee
Pablo Carranza Velez 8298487a88 Rename most of the documentation and variable names from resin to balena
Change-type: minor
Signed-off-by: Pablo Carranza Velez <pablo@balena.io>
2018-11-01 17:00:13 -07:00

36 lines
983 B
CoffeeScript

express = require 'express'
_ = require 'lodash'
api = express()
api.use(require('body-parser').json())
api.balenaBackend = {
currentId: 1
devices: {}
registerHandler: (req, res) ->
console.log('/device/register called with ', req.body)
device = req.body
device.id = api.balenaBackend.currentId++
api.balenaBackend.devices[device.id] = device
res.status(201).json(device)
getDeviceHandler: (req, res) ->
uuid = req.query['$filter']?.match(/uuid eq '(.*)'/)?[1]
if uuid?
res.json({ d: _.filter(api.balenaBackend.devices, (dev) -> dev.uuid is uuid ) })
else
res.json({ d: [] })
deviceKeyHandler: (req, res) ->
res.status(200).send(req.body.apiKey)
}
api.post '/device/register', (req, res) ->
api.balenaBackend.registerHandler(req, res)
api.get '/v5/device', (req, res) ->
api.balenaBackend.getDeviceHandler(req, res)
api.post '/api-key/device/:deviceId/device-key', (req, res) ->
api.balenaBackend.deviceKeyHandler(req, res)
module.exports = api