generated from DEFRA/ffc-template-node
-
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.
feat: extract out auth config to separate file (#30)
* feat: extract out auth config to separate file * bump: version * fix: linting * feat: add tests
- Loading branch information
1 parent
a5c1e9a
commit bf73fee
Showing
3 changed files
with
56 additions
and
14 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,25 @@ | ||
const Joi = require('joi') | ||
|
||
const schema = Joi.object({ | ||
enabled: Joi.boolean().default(false), | ||
clientSecret: Joi.string().allow(''), | ||
clientId: Joi.string().allow(''), | ||
authority: Joi.string().allow(''), | ||
redirectUrl: Joi.string() | ||
}) | ||
|
||
const config = { | ||
enabled: process.env.AADAR_ENABLED, | ||
clientSecret: process.env.AADAR_CLIENT_SECRET, | ||
clientId: process.env.AADAR_CLIENT_ID, | ||
authority: `https://login.microsoftonline.com/${process.env.AADAR_TENANT_ID}`, | ||
redirectUrl: process.env.AADAR_REDIRECT_URL | ||
} | ||
|
||
const { error, value } = schema.validate(config, { abortEarly: false }) | ||
|
||
if (error) { | ||
throw new Error(`The auth config is invalid. ${error.message}`) | ||
} | ||
|
||
module.exports = value |
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,27 @@ | ||
const authConfig = require('../../../app/config/auth') | ||
|
||
describe('cache Config Test', () => { | ||
const OLD_ENV = process.env | ||
|
||
beforeEach(() => { | ||
jest.resetModules() | ||
process.env = { ...OLD_ENV } | ||
}) | ||
|
||
afterAll(() => { | ||
process.env = OLD_ENV | ||
}) | ||
test('Should pass validation for all fields populated', async () => { | ||
expect(authConfig).toBeDefined() | ||
}) | ||
|
||
test('Invalid env var throws error', () => { | ||
try { | ||
process.env.AADAR_CLIENT_SECRET = null | ||
process.env.AADAR_CLIENT_ID = null | ||
require('../../../app/config/auth') | ||
} catch (err) { | ||
expect(err.message).toBe('The auth config is invalid. "clientSecret" must be a string. "clientId" must be a string') | ||
} | ||
}) | ||
}) |