This repository has been archived by the owner on Jun 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* jest integration, few tests * improved test coverage
- Loading branch information
Showing
3 changed files
with
2,097 additions
and
11 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
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,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') | ||
|
||
}) |
Oops, something went wrong.