Skip to content
This repository has been archived by the owner on Jun 13, 2024. It is now read-only.

Commit

Permalink
jest integration, few tests (#3)
Browse files Browse the repository at this point in the history
* jest integration, few tests

* improved test coverage
  • Loading branch information
stearm authored and kandros committed Feb 21, 2017
1 parent 88e1c85 commit 9e33d7e
Show file tree
Hide file tree
Showing 3 changed files with 2,097 additions and 11 deletions.
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
"dependencies": {
"jsonwebtoken": "7.3.0"
},
"devDependencies": {
"jest": "^18.1.0"
},
"scripts": {
"test": "jest"
},
"main": "index.js",
"license": "MIT",
"repository": {
Expand Down
69 changes: 69 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'use strict'

const jwtAuth = require('../index')

test('error throwed if secret undefined', () => {
expect(
() => jwtAuth()()
).toThrow('micro-jwt-auth must me initialized passing a secret to decode incoming JWT token')
});

test('case of request has not authorization header', () => {

const request = {
headers: {}
}

const response = {
writeHead: jest.fn().mockImplementation(),
end: jest.fn().mockImplementation()
};

const result = jwtAuth('mySecret')()(request, response)

expect(result).toBeUndefined()
expect(response.writeHead).toHaveBeenCalledWith(401)
expect(response.end).toHaveBeenCalledWith('missing Authorization header')
});

test('that all works fine: no errors', () => {

const request = {
headers: {
authorization: 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IldhbHRlciBXaGl0ZSIsImFkbWluIjp0cnVlfQ.YyF_yOQsTSQghvM08WBp7VhsHRv-4Ir4eMQvsEycY1A'
}
}

const response = {
writeHead: jest.fn().mockImplementation(),
end: jest.fn().mockImplementation()
};

const result = jwtAuth('mySecret')(() => 'Good job!')(request, response)

expect(result).toEqual('Good job!')
expect(response.writeHead).toHaveBeenCalledTimes(0)
expect(response.end).toHaveBeenCalledTimes(0)

})

test('wrong bearer case', () => {

const request = {
headers: {
authorization: 'Bearer wrong.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IldhbHRlciBXaGl0ZSIsImFkbWluIjp0cnVlfQ.YyF_yOQsTSQghvM08WBp7VhsHRv-4Ir4eMQvsEycY1A'
}
}

const response = {
writeHead: jest.fn().mockImplementation(),
end: jest.fn().mockImplementation()
};

const result = jwtAuth('mySecret')(() => {})(request, response)

expect(result).toBeUndefined()
expect(response.writeHead).toHaveBeenCalledWith(401)
expect(response.end).toHaveBeenCalledWith('invalid token in Authorization header')

})
Loading

0 comments on commit 9e33d7e

Please sign in to comment.