Skip to content

Commit

Permalink
🔫 add tests for handler
Browse files Browse the repository at this point in the history
  • Loading branch information
builtinnya committed Jan 31, 2019
1 parent 75248d9 commit 7ee849c
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 8 deletions.
1 change: 1 addition & 0 deletions build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ set -o xtrace
__dir="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

docker-compose build dev
docker-compose run --rm dev npm test
docker-compose run --rm dev npm run build
2 changes: 1 addition & 1 deletion module/functions/basic-auth.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"scripts": {
"build": "npx gulp",
"clean": "npx gulp clean",
"test": "echo \"Error: no test specified\" && exit 1"
"test": "npx mocha"
},
"repository": {
"type": "git",
Expand All @@ -20,11 +20,14 @@
"devDependencies": {
"@babel/core": "^7.2.2",
"@babel/preset-env": "^7.3.1",
"chai": "^4.2.0",
"gulp": "^4.0.0",
"gulp-babel": "^8.0.0",
"gulp-cli": "^2.0.1",
"gulp-concat": "^2.6.1",
"gulp-uglify": "^3.0.1",
"rimraf": "^2.6.3"
"mocha": "^5.2.0",
"rimraf": "^2.6.3",
"sinon": "^7.2.3"
}
}
3 changes: 2 additions & 1 deletion src/basic-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ exports.handler = (event, context, callback) => {
'www-authenticate': [
{
key: 'WWW-Authenticate',
value:'Basic',
value: 'Basic',
}
]
},
}

callback(null, response)
return
}

// Continue request processing if authentication passed
Expand Down
121 changes: 121 additions & 0 deletions test/basic-auth.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
const expect = require('chai').expect
const sinon = require('sinon')
const { handler } = require('../src/basic-auth')

describe('Basic Auth', function() {
describe('handler', function () {
it('should return 401 and WWW-Authenticate: Basic header without Authorization header', function() {
const event = {
Records: [
{
cf: {
request: {
headers: {
},
},
},
},
],
}

const callback = sinon.fake()

handler(event, null, callback)

expect(callback.calledOnce).to.be.true

const [ err, response ] = callback.args[0]

expect(err).to.be.null
expect(response).to.have.property('status', '401')
expect(response).to.have.property('headers')

const { headers } = response

expect(headers).to.have.property('www-authenticate')

expect(headers['www-authenticate']).to.deep.equal([
{
key: 'WWW-Authenticate',
value: 'Basic',
},
])
})

it('should return 401 and WWW-Authenticate: Basic header if authentication failed', function() {
const event = {
Records: [
{
cf: {
request: {
headers: {
authorization: [
{
// new Buffer('impossible:impossible').toString('base64')
value: 'Basic aW1wb3NzaWJsZTppbXBvc3NpYmxl',
},
],
},
},
},
},
],
}

const callback = sinon.fake()

handler(event, null, callback)

expect(callback.calledOnce).to.be.true

const [ err, response ] = callback.args[0]

expect(err).to.be.null
expect(response).to.have.property('status', '401')
expect(response).to.have.property('headers')

const { headers } = response

expect(headers).to.have.property('www-authenticate')

expect(headers['www-authenticate']).to.deep.equal([
{
key: 'WWW-Authenticate',
value: 'Basic',
},
])
})

it('should return request if authentication succeeded', function() {
const event = {
Records: [
{
cf: {
request: {
headers: {
authorization: [
{
// new Buffer('${user}:${password}').toString('base64')
value: 'Basic JHt1c2VyfToke3Bhc3N3b3JkfQ==',
},
],
},
},
},
},
],
}

const callback = sinon.fake()

handler(event, null, callback)

expect(callback.calledOnce).to.be.true

const [ err, response ] = callback.args[0]

expect(err).to.be.null
expect(response).to.deep.equal(event.Records[0].cf.request)
})
})
})
Loading

0 comments on commit 7ee849c

Please sign in to comment.