Skip to content

Commit

Permalink
feat: extract out auth config to separate file (#30)
Browse files Browse the repository at this point in the history
* feat: extract out auth config to separate file

* bump: version

* fix: linting

* feat: add tests
  • Loading branch information
shivanshuit914 authored Aug 10, 2022
1 parent a5c1e9a commit bf73fee
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
25 changes: 25 additions & 0 deletions app/config/auth.js
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
18 changes: 4 additions & 14 deletions app/config/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const Joi = require('joi')
const authConfig = require('./auth')

const schema = Joi.object({
cache: {
expiresIn: Joi.number().default(1000 * 3600 * 24 * 3), // 3 days
Expand Down Expand Up @@ -28,13 +30,6 @@ const schema = Joi.object({
path: Joi.string().default('/'),
ttl: Joi.number().default(1000 * 60 * 60 * 24 * 365) // 1 year
},
auth: {
enabled: Joi.boolean().default(false),
clientSecret: Joi.string().allow(''),
clientId: Joi.string().allow(''),
authority: Joi.string().allow(''),
redirectUrl: Joi.string().default('http://localhost:3002/authenticate')
},
env: Joi.string().valid('development', 'test', 'production').default('development'),
isDev: Joi.boolean().default(false),
isProd: Joi.boolean().default(false),
Expand Down Expand Up @@ -72,13 +67,6 @@ const config = {
isSecure: process.env.NODE_ENV === 'production',
password: process.env.COOKIE_PASSWORD
},
auth: {
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
},
env: process.env.NODE_ENV,
isDev: process.env.NODE_ENV === 'development',
isProd: process.env.NODE_ENV === 'production',
Expand All @@ -95,4 +83,6 @@ if (error) {
throw new Error(`The server config is invalid. ${error.message}`)
}

value.auth = authConfig

module.exports = value
27 changes: 27 additions & 0 deletions test/unit/config/auth.test.js
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')
}
})
})

0 comments on commit bf73fee

Please sign in to comment.