Skip to content

Commit

Permalink
feat: hook for deprecating a note version
Browse files Browse the repository at this point in the history
  • Loading branch information
espy committed May 17, 2018
1 parent 0d9f5cc commit a382295
Show file tree
Hide file tree
Showing 3 changed files with 219 additions and 0 deletions.
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ require('./lib/rollbar')
env,
channel
}
}, {
register: require('./lib/deprecate-node-event'),
options: {
env,
channel
}
}, {
register: require('good'),
options: {
Expand Down
62 changes: 62 additions & 0 deletions lib/deprecate-node-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
const _ = require('lodash')
const Boom = require('boom')

const rollbar = require('./rollbar')

module.exports = deprecateNodeEvent
module.exports.attributes = {
name: 'deprecateNode'
}

function deprecateNodeEvent (server, {env, channel}, next) {
server.route({
method: 'POST',
path: '/deprecate-node',
config: {
pre: [{method: (request, reply) => {
if (request.headers['bearer-token'] === env.BEARER_TOKEN) {
return reply.continue()
}
return reply(Boom.unauthorized())
}}]
},
handler
})

async function handler (request, reply) {
if (!_.get(request, 'payload.repositoryFullName')) return reply({error: 'repositoryFullName missing'}).code(400)
if (!_.get(request, 'payload.nodeVersion')) return reply({error: 'nodeVersion missing'}).code(400)
if (!_.get(request, 'payload.codeName')) return reply({error: 'codeName missing'}).code(400)
if (!_.get(request, 'payload.newLowestVersion')) return reply({error: 'newLowestVersion missing'}).code(400)
if (!_.get(request, 'payload.newLowestCodeName')) return reply({error: 'newLowestCodeName missing'}).code(400)
// `request.payload.announcementURL` is optional

const job = {
name: 'deprecate-nodejs-version',
repositoryFullName: request.payload.repositoryFullName,
nodeVersion: request.payload.nodeVersion,
codeName: request.payload.codeName,
newLowestVersion: request.payload.newLowestVersion,
newLowestCodeName: request.payload.newLowestCodeName,
announcementURL: request.payload.announcementURL ? request.payload.announcementURL : undefined
}

try {
await channel.sendToQueue(env.QUEUE_NAME, Buffer.from(JSON.stringify(job)), {priority: 3})
} catch (err) {
rollbar.error(err, _.assign({}, request.raw.req, {
socket: {
encrypted: request.server.info.protocol === 'https'
},
connection: {
remoteAddress: request.info.remoteAddress
}
}))
return reply({error: true}).code(500)
}

reply({ok: true}).code(202)
}

next()
}
151 changes: 151 additions & 0 deletions test/deprecate-node-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
const amqp = require('amqplib')
const hapi = require('hapi')
const tap = require('tap')

const env = require('../lib/env')
const register = require('../lib/deprecate-node-event')

;(async () => {
const conn = await amqp.connect(env.AMQP_URL)
const channel = await conn.createChannel()
await channel.assertQueue(env.QUEUE_NAME, {
maxPriority: 5
})

let server
tap.beforeEach(done => {
server = new hapi.Server()
server.connection()
done()
})

// w/o `.then` everthing blows up ¯\_(ツ)_/¯
tap.afterEach(() => channel.purgeQueue().then(() => {}))

tap.tearDown(async () => {
channel.reply = null // not sure why there is an outstanding reply
await conn.close()
})

tap.test('rejects without payload', async (t) => {
server.register({
register,
options: {env}
})

const {statusCode} = await server.inject({
method: 'POST',
url: '/deprecate-node',
headers: {
'Bearer-Token': env.BEARER_TOKEN,
'Content-Type': 'application/json'
},
payload: JSON.stringify({id: '12'})
})
t.is(statusCode, 400, 'statusCode')
t.end()
})

tap.test('rejects without authentification', async (t) => {
const payload = JSON.stringify({
repositoryFullName: 'finnp/abc',
nodeVersion: 4,
codeName: 'Argon',
newLowestVersion: 6,
newLowestCodeName: 'Boron'
})
server.register({
register,
options: {env, channel}
})

const {statusCode} = await server.inject({
method: 'POST',
url: '/deprecate-node',
headers: {
'Content-Type': 'application/json'
},
payload
})
t.is(statusCode, 401, 'statusCode')
t.end()
})

tap.test('stores event in queue', async (t) => {
server.register({
register,
options: {env, channel}
})

const reqPayload = JSON.stringify({
repositoryFullName: 'finnp/abc',
nodeVersion: 4,
codeName: 'Argon',
newLowestVersion: 6,
newLowestCodeName: 'Boron'
})

const {statusCode, payload} = await server.inject({
method: 'POST',
url: '/deprecate-node',
headers: {
'Bearer-Token': env.BEARER_TOKEN,
'Content-Type': 'application/json'
},
payload: reqPayload
})
t.is(statusCode, 202, 'statusCode')
t.true(JSON.parse(payload).ok, 'payload')
const job = await channel.get(env.QUEUE_NAME)
t.same(JSON.parse(job.content.toString()), {
name: 'deprecate-nodejs-version',
repositoryFullName: 'finnp/abc',
nodeVersion: 4,
codeName: 'Argon',
newLowestVersion: 6,
newLowestCodeName: 'Boron'
})
t.same(job.properties.priority, 3, 'job priority')
t.end()
})

tap.test('stores event with announcementURL in queue', async (t) => {
server.register({
register,
options: {env, channel}
})

const reqPayload = JSON.stringify({
repositoryFullName: 'finnp/abc',
nodeVersion: 4,
codeName: 'Argon',
newLowestVersion: 6,
newLowestCodeName: 'Boron',
announcementURL: 'http://zeppelin.club/zesty'
})

const {statusCode, payload} = await server.inject({
method: 'POST',
url: '/deprecate-node',
headers: {
'Bearer-Token': env.BEARER_TOKEN,
'Content-Type': 'application/json'
},
payload: reqPayload
})
t.is(statusCode, 202, 'statusCode')
t.true(JSON.parse(payload).ok, 'payload')
const job = await channel.get(env.QUEUE_NAME)
t.same(JSON.parse(job.content.toString()), {
name: 'deprecate-nodejs-version',
repositoryFullName: 'finnp/abc',
nodeVersion: 4,
codeName: 'Argon',
newLowestVersion: 6,
newLowestCodeName: 'Boron',
announcementURL: 'http://zeppelin.club/zesty'
})
t.same(job.properties.priority, 3, 'job priority')
t.end()
})
})()

0 comments on commit a382295

Please sign in to comment.