-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
75248d9
commit 7ee849c
Showing
6 changed files
with
306 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.