forked from bcgov/common-hosted-form-service
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: FORMS-1488 new middleware for reminder route (bcgov#1482)
* fix: FORMS-1448 new middleware for reminder route * fix test comments * fixed a comment that could cause confusion
- Loading branch information
1 parent
6c9b36d
commit d7f9780
Showing
4 changed files
with
115 additions
and
64 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
const Problem = require('api-problem'); | ||
|
||
/** | ||
* Checks that the API Key in the request headers matches the API Key in the | ||
* process environment variables. | ||
* | ||
* @param {*} req the Express object representing the HTTP request. | ||
* @param {*} _res the Express object representing the HTTP response - unused. | ||
* @param {*} next the Express chaining function. | ||
*/ | ||
const checkApiKey = async (req, _res, next) => { | ||
try { | ||
const requestApikey = req.headers.apikey; | ||
if (requestApikey === undefined || requestApikey === '') { | ||
throw new Problem(401, { | ||
detail: 'No API key provided', | ||
}); | ||
} | ||
|
||
const systemApikey = process.env.APITOKEN; | ||
if (requestApikey !== systemApikey) { | ||
throw new Problem(401, { | ||
detail: 'Invalid API key', | ||
}); | ||
} | ||
|
||
next(); | ||
} catch (error) { | ||
next(error); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
checkApiKey, | ||
}; |
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,66 @@ | ||
const { getMockReq, getMockRes } = require('@jest-mock/express'); | ||
const uuid = require('uuid'); | ||
|
||
const apiAccess = require('../../../../../src/forms/public/middleware/apiAccess'); | ||
|
||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
describe('checkApiKey', () => { | ||
process.env.APITOKEN = uuid.v4(); | ||
|
||
describe('401 response when', () => { | ||
const expectedStatus = { status: 401 }; | ||
|
||
test('there is no APITOKEN or apikey', async () => { | ||
const req = getMockReq(); | ||
const { res, next } = getMockRes(); | ||
|
||
await apiAccess.checkApiKey(req, res, next); | ||
|
||
expect(next).toBeCalledTimes(1); | ||
expect(next).toBeCalledWith(expect.objectContaining(expectedStatus)); | ||
}); | ||
|
||
test('the apikey is empty', async () => { | ||
const req = getMockReq({ | ||
headers: { apikey: '' }, | ||
}); | ||
const { res, next } = getMockRes(); | ||
|
||
await apiAccess.checkApiKey(req, res, next); | ||
|
||
expect(next).toBeCalledTimes(1); | ||
expect(next).toBeCalledWith(expect.objectContaining(expectedStatus)); | ||
}); | ||
|
||
test('the apikey exists but does not match', async () => { | ||
const req = getMockReq({ | ||
headers: { apikey: uuid.v4() }, | ||
}); | ||
const { res, next } = getMockRes(); | ||
|
||
await apiAccess.checkApiKey(req, res, next); | ||
|
||
expect(next).toBeCalledTimes(1); | ||
expect(next).toBeCalledWith(expect.objectContaining(expectedStatus)); | ||
}); | ||
}); | ||
|
||
describe('allows', () => { | ||
test('matching APITOKEN and apikey', async () => { | ||
const req = getMockReq({ | ||
headers: { | ||
apikey: process.env.APITOKEN, | ||
}, | ||
}); | ||
const { res, next } = getMockRes(); | ||
|
||
await apiAccess.checkApiKey(req, res, next); | ||
|
||
expect(next).toBeCalledTimes(1); | ||
expect(next).toBeCalledWith(); | ||
}); | ||
}); | ||
}); |
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