-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: FORMS-1448 expand jwt service tests (#1479)
- Loading branch information
1 parent
cb022d7
commit 09cafe0
Showing
8 changed files
with
334 additions
and
21 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
Large diffs are not rendered by default.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
const request = require('supertest'); | ||
|
||
const { expressHelper } = require('../../../common/helper'); | ||
|
||
const jwtService = require('../../../../src/components/jwtService'); | ||
const userAccess = require('../../../../src/forms/auth/middleware/userAccess'); | ||
const controller = require('../../../../src/forms/permission/controller'); | ||
|
||
// | ||
// Mock out all the middleware - we're testing that the routes are set up | ||
// correctly, not the functionality of the middleware. | ||
// | ||
|
||
const mockJwtServiceProtect = jest.fn((_req, _res, next) => { | ||
next(); | ||
}); | ||
jwtService.protect = jest.fn(() => { | ||
return mockJwtServiceProtect; | ||
}); | ||
|
||
userAccess.currentUser = jest.fn((_req, _res, next) => { | ||
next(); | ||
}); | ||
|
||
// | ||
// Create the router and a simple Express server. | ||
// | ||
|
||
const router = require('../../../../src/forms/permission/routes'); | ||
const basePath = '/permission'; | ||
const app = expressHelper(basePath, router); | ||
const appRequest = request(app); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
// jwtService.protect is tricky to test. This test is fragile as the file could | ||
// change and have routes that need admin and others that don't. This test only | ||
// works when we use(protect) at the file level, not individually in the routes. | ||
// However, this does test that we don't accidentally turn off the protection. | ||
describe('jwtService.protect', () => { | ||
it('should be called with admin', () => { | ||
jest.resetModules(); | ||
const jwtService = require('../../../../src/components/jwtService'); | ||
jwtService.protect = jest.fn(() => { | ||
return jest.fn((_req, _res, next) => { | ||
next(); | ||
}); | ||
}); | ||
require('../../../../src/forms/permission/routes'); | ||
|
||
expect(jwtService.protect).toBeCalledWith('admin'); | ||
}); | ||
}); | ||
|
||
describe(`${basePath}`, () => { | ||
const path = `${basePath}`; | ||
|
||
it('should have correct middleware for GET', async () => { | ||
controller.list = jest.fn((_req, res) => { | ||
res.sendStatus(200); | ||
}); | ||
|
||
await appRequest.get(path); | ||
|
||
expect(controller.list).toBeCalledTimes(1); | ||
expect(mockJwtServiceProtect).toBeCalledTimes(1); | ||
expect(userAccess.currentUser).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should have correct middleware for POST', async () => { | ||
controller.create = jest.fn((_req, res) => { | ||
res.sendStatus(200); | ||
}); | ||
|
||
await appRequest.post(path); | ||
|
||
expect(controller.create).toBeCalledTimes(1); | ||
expect(mockJwtServiceProtect).toBeCalledTimes(1); | ||
expect(userAccess.currentUser).toBeCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe(`${basePath}/:code`, () => { | ||
const path = `${basePath}/code`; | ||
|
||
it('should have correct middleware for GET', async () => { | ||
controller.read = jest.fn((_req, res) => { | ||
res.sendStatus(200); | ||
}); | ||
|
||
await appRequest.get(path); | ||
|
||
expect(controller.read).toBeCalledTimes(1); | ||
expect(mockJwtServiceProtect).toBeCalledTimes(1); | ||
expect(userAccess.currentUser).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should have correct middleware for PUT', async () => { | ||
controller.update = jest.fn((_req, res) => { | ||
res.sendStatus(200); | ||
}); | ||
|
||
await appRequest.put(path); | ||
|
||
expect(controller.update).toBeCalledTimes(1); | ||
expect(mockJwtServiceProtect).toBeCalledTimes(1); | ||
expect(userAccess.currentUser).toBeCalledTimes(1); | ||
}); | ||
}); |
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,111 @@ | ||
const request = require('supertest'); | ||
|
||
const { expressHelper } = require('../../../common/helper'); | ||
|
||
const jwtService = require('../../../../src/components/jwtService'); | ||
const userAccess = require('../../../../src/forms/auth/middleware/userAccess'); | ||
const controller = require('../../../../src/forms/role/controller'); | ||
|
||
// | ||
// Mock out all the middleware - we're testing that the routes are set up | ||
// correctly, not the functionality of the middleware. | ||
// | ||
|
||
const mockJwtServiceProtect = jest.fn((_req, _res, next) => { | ||
next(); | ||
}); | ||
jwtService.protect = jest.fn(() => { | ||
return mockJwtServiceProtect; | ||
}); | ||
|
||
userAccess.currentUser = jest.fn((_req, _res, next) => { | ||
next(); | ||
}); | ||
|
||
// | ||
// Create the router and a simple Express server. | ||
// | ||
|
||
const router = require('../../../../src/forms/role/routes'); | ||
const basePath = '/role'; | ||
const app = expressHelper(basePath, router); | ||
const appRequest = request(app); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
// jwtService.protect is tricky to test. This test is fragile as the file could | ||
// change and have routes that need admin and others that don't. This test only | ||
// works when we use(protect) at the file level, not individually in the routes. | ||
// However, this does test that we don't accidentally turn off the protection. | ||
describe('jwtService.protect', () => { | ||
it('should be called with admin', () => { | ||
jest.resetModules(); | ||
const jwtService = require('../../../../src/components/jwtService'); | ||
jwtService.protect = jest.fn(() => { | ||
return jest.fn((_req, _res, next) => { | ||
next(); | ||
}); | ||
}); | ||
require('../../../../src/forms/role/routes'); | ||
|
||
expect(jwtService.protect).toBeCalledWith('admin'); | ||
}); | ||
}); | ||
|
||
describe(`${basePath}`, () => { | ||
const path = `${basePath}`; | ||
|
||
it('should have correct middleware for GET', async () => { | ||
controller.list = jest.fn((_req, res) => { | ||
res.sendStatus(200); | ||
}); | ||
|
||
await appRequest.get(path); | ||
|
||
expect(controller.list).toBeCalledTimes(1); | ||
expect(mockJwtServiceProtect).toBeCalledTimes(1); | ||
expect(userAccess.currentUser).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should have correct middleware for POST', async () => { | ||
controller.create = jest.fn((_req, res) => { | ||
res.sendStatus(200); | ||
}); | ||
|
||
await appRequest.post(path); | ||
|
||
expect(controller.create).toBeCalledTimes(1); | ||
expect(mockJwtServiceProtect).toBeCalledTimes(1); | ||
expect(userAccess.currentUser).toBeCalledTimes(1); | ||
}); | ||
}); | ||
|
||
describe(`${basePath}/:code`, () => { | ||
const path = `${basePath}/code`; | ||
|
||
it('should have correct middleware for GET', async () => { | ||
controller.read = jest.fn((_req, res) => { | ||
res.sendStatus(200); | ||
}); | ||
|
||
await appRequest.get(path); | ||
|
||
expect(controller.read).toBeCalledTimes(1); | ||
expect(mockJwtServiceProtect).toBeCalledTimes(1); | ||
expect(userAccess.currentUser).toBeCalledTimes(1); | ||
}); | ||
|
||
it('should have correct middleware for PUT', async () => { | ||
controller.update = jest.fn((_req, res) => { | ||
res.sendStatus(200); | ||
}); | ||
|
||
await appRequest.put(path); | ||
|
||
expect(controller.update).toBeCalledTimes(1); | ||
expect(mockJwtServiceProtect).toBeCalledTimes(1); | ||
expect(userAccess.currentUser).toBeCalledTimes(1); | ||
}); | ||
}); |
Oops, something went wrong.