-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(#378): jwt service middleware refactoring
- Loading branch information
Showing
2 changed files
with
68 additions
and
24 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,69 @@ | ||
const { expect, should } = require('chai'); | ||
const { setInternal, getInternal } = require('../../src/middlewares/isInternal'); | ||
const { expect } = require('chai'); | ||
const sinon = require('sinon'); | ||
const { logger } = require('../common/utils'); | ||
const { mockUser, next } = require('../mocks'); | ||
const jwtService = require('../../src/utils/jwtService') | ||
const { setInternal, getInternal } = require('../../src/middlewares/isInternal'); | ||
|
||
describe('isInternal Middleware service', function () { | ||
afterEach(() => sinon.restore()); | ||
|
||
const req = { | ||
logger, | ||
get: () => {} | ||
} | ||
|
||
should(); | ||
const res = { | ||
header: () => {}, | ||
status: () => {}, | ||
json: () => {}, | ||
send: () => {} | ||
}; | ||
|
||
describe('isInternal middleware service', function() { | ||
it.skip('rejects req.isInternal undefined values', function() { | ||
expect(getInternal.bind(this, {logger}, {}, () => {})).to.throw('Not authorized.'); | ||
}); | ||
context('getInternal', () => { | ||
it('rejects unauthorized request when no authorization header', async function () { | ||
sinon.stub(req, 'get').returns(null) | ||
try { | ||
getInternal(req, res, next); | ||
} catch (error) { | ||
expect(error.message).to.equal('Not authorized.'); | ||
} | ||
}); | ||
|
||
it('rejects unauthorized request when decoding token throws error', async function () { | ||
sinon.stub(req, 'get').returns("Bearer i2od2nlw4aeiavi2q3") | ||
sinon.stub(jwtService, 'decodeToken').throws('JsonWebTokenError: jwt malformed'); | ||
try { | ||
getInternal(req, res, next); | ||
} catch (error) { | ||
expect(error.message).to.equals('JsonWebTokenError: jwt malformed'); | ||
} | ||
}); | ||
|
||
it('authorizes request', async function () { | ||
sinon.stub(req, 'get').returns("Bearer i2od2nlw4aeiavi2q3") | ||
const nextSpy = sinon.spy(); | ||
sinon.stub(jwtService, 'decodeToken').returns(mockUser); | ||
getInternal(req, res, nextSpy); | ||
sinon.assert.calledOnce(nextSpy); | ||
}); | ||
}) | ||
|
||
it.skip('authorizes request', async function() { | ||
let req = { | ||
logger, | ||
isInternal: undefined, | ||
internal: false, | ||
get: () => ({}), | ||
}; | ||
setInternal(req, {}, () => {}); | ||
req.isInternal = req.signedToken; | ||
req.signedToken.should.exist; | ||
getInternal(req, {}, () => {}); | ||
req.internal.should.equal(true); | ||
}); | ||
}); | ||
context('setInternal', () => { | ||
|
||
it('rejects unauthorized request when decoding token throws error', async function () { | ||
sinon.stub(jwtService, 'signToken').throws('Error'); | ||
try { | ||
setInternal(req, res, next); | ||
} catch (error) { | ||
expect(error.message).to.equals('Error'); | ||
} | ||
}); | ||
|
||
it('authorizes request', async function () { | ||
sinon.stub(jwtService, 'signToken').returns({ token: 'adia4kla49qnpove'}); | ||
const result = setInternal(req, res, next); | ||
expect(result).to.have.property('token'); | ||
}); | ||
}) | ||
}); |
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