Skip to content

Commit

Permalink
Merge pull request #13 from envoy/job-attchment
Browse files Browse the repository at this point in the history
added job_attach method for attachments shown in visitor view
  • Loading branch information
dboskovic authored Sep 14, 2018
2 parents e81c36c + 929991d commit aeef68b
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 20 deletions.
15 changes: 15 additions & 0 deletions lib/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Response = (function () {
this._platform = platform
this._store = {}
this._headers = {}
this._attachments = []
this._http_headers = []
this._context = context
}
Expand All @@ -43,6 +44,20 @@ Response = (function () {
this._respond(data)
}
}
Response.prototype.job_attach = function (...attachments) {
const requiredLabels = [ 'type', 'label', 'value' ]
const allowedTypes = [ 'link', 'password' ]
for (const attachment of attachments) {
if (!~requiredLabels.every(l => ~Object.keys(attachment).indexOf(l))) {
throw new Error(`Attachment requires mandatory properties ${requiredLabels.join(', ')}`)
}
if (!~allowedTypes.indexOf(attachment.type)) {
throw new Error(`Attachment must be of one of the following types ${allowedTypes.join(', ')}`)
}
}
this._attachments.push(...attachments)
this.meta('set_job_attachments', JSON.stringify(this._attachments))
}
Response.prototype.job_ignore = function (status, data) {
this.meta('set_job_status', 'ignored')
this.meta('set_job_status_message', status)
Expand Down
88 changes: 68 additions & 20 deletions test/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,36 +161,56 @@ describe('index', function () {
}
}, 500)
})
it('job should call .job_failed in case of unhandled asynchronous promise error', function (done) {
})
describe('worker', function () {
it('worker should add attachments on job_success', function () {
let context = {
succeed: sinon.spy(),
fail: sinon.spy(),
job_failed: sinon.spy(),
awsRequestId: 'LAMBDA_INVOKE',
logStreamName: 'LAMBDA_INVOKE'
}
let routeEvent = { name: 'route', request_meta: { route: 'welcome', job: { id: 1 } } }
let workerEvent = { name: 'event', request_meta: { event: 'welcome' } }
let Sdk = proxyquire('../index', {})
let platformInstance = new Sdk({})
platformInstance.registerRoute('welcome', (req, res) => { return Promise.reject(new Error('no')) })
platformInstance.registerWorker('welcome', function (req, res) {
res.job_attach({
type: 'link',
label: 'NDA',
value: 'some dropbox url'
})
res.job_attach({
type: 'link',
label: 'NDA2',
value: 'some dropbox url'
}, {
type: 'link',
label: 'NDA3',
value: 'some dropbox url'
})
res.job_complete('Uploaded NDA', {})
})
let handler = platformInstance.getHandler()
handler(routeEvent, context)
setTimeout(() => {
try {
expect(context.succeed).to.have.been.called()
let args = context.succeed.args[0][0]
expect(args.meta.set_job_status).to.equal('failed')
expect(args.meta.set_job_status_message).to.equal('Unhandled Exception')
expect(args.meta.set_job_failure_message).to.equal('no')
expect(args.body.message).to.equal('no')
done()
} catch (e) {
done(e)
}
}, 500)
handler(workerEvent, context)
expect(context.succeed).to.have.been.called()
let args = context.succeed.args[0][0]
expect(args.meta.set_job_status).to.equal('done')
expect(args.meta.set_job_status_message).to.equal('Uploaded NDA')
expect(JSON.parse(args.meta.set_job_attachments)).to.deep.equal([{
type: 'link',
label: 'NDA',
value: 'some dropbox url'
}, {
type: 'link',
label: 'NDA2',
value: 'some dropbox url'
}, {
type: 'link',
label: 'NDA3',
value: 'some dropbox url'
}])
expect(args.body).to.deep.equal({})
})
})
describe('worker', function () {
it('worker should call .job_failed in case of unhandled synchronous error', function () {
let context = {
succeed: sinon.spy(),
Expand All @@ -211,5 +231,33 @@ describe('index', function () {
expect(args.meta.set_job_failure_message).to.equal('no')
expect(args.body.message).to.equal('no')
})
it('job should call .job_failed in case of unhandled asynchronous promise error', function (done) {
let context = {
succeed: sinon.spy(),
fail: sinon.spy(),
job_failed: sinon.spy(),
awsRequestId: 'LAMBDA_INVOKE',
logStreamName: 'LAMBDA_INVOKE'
}
let routeEvent = { name: 'route', request_meta: { route: 'welcome', job: { id: 1 } } }
let Sdk = proxyquire('../index', {})
let platformInstance = new Sdk({})
platformInstance.registerRoute('welcome', (req, res) => { return Promise.reject(new Error('no')) })
let handler = platformInstance.getHandler()
handler(routeEvent, context)
setTimeout(() => {
try {
expect(context.succeed).to.have.been.called()
let args = context.succeed.args[0][0]
expect(args.meta.set_job_status).to.equal('failed')
expect(args.meta.set_job_status_message).to.equal('Unhandled Exception')
expect(args.meta.set_job_failure_message).to.equal('no')
expect(args.body.message).to.equal('no')
done()
} catch (e) {
done(e)
}
}, 500)
})
})
})

0 comments on commit aeef68b

Please sign in to comment.