Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 51 additions & 58 deletions tests/index.js → __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,70 +1,64 @@
const fs = require('fs').promises
import fs from 'fs/promises'
import { envLoader, awsManager } from '../src/index'
const envFile = './__tests__/.env'

// eslint-disable-next-line no-unused-vars
const assert = require('chai').assert
// eslint-disable-next-line no-unused-vars
const should = require('chai').should()

const { envLoader, awsManager } = require('../src/index')
const envFile = './tests/.env'

describe('envLoader', async () => {
describe('envLoader', () => {
const params = [
{ key: 'key01', value: 'value01' },
{ key: 'key02', value: 'value02' },
{ key: 'key03', value: 'value03' }
{ key: 'key03', value: 'value03', isEncrypted: true }
]

describe('paramsToSourceFile', async () => {
describe('paramsToSourceFile', () => {
it('should save array to file', async () => {
const parmsToString = await envLoader.paramsToSourceFile(params, envFile)
const fileData = await fs.readFile(envFile, 'utf8')
fileData.should.equal(parmsToString)
expect(fileData).toEqual(parmsToString)
const aParams = fileData.split('\n')

for (let i = 0; i < aParams.length; i++) {
const aLine = aParams[i].split('=')
aLine[0].should.equal(params[i].key)
aLine[1].should.equal(params[i].value)
expect(aLine[0]).toEqual(params[i].key)
expect(aLine[1]).toEqual(params[i].value)
}
})
})

describe('readEnvFile', async () => {
describe('readEnvFile', () => {
it('should read a file and return array of values', async () => {
const fileParams = await envLoader.readEnvFile(envFile)
fileParams.should.deep.equal(params)
expect(fileParams).toEqual(params)
})
})

describe('loadParamsIntoEnv', async () => {
it('should load array into process.env ', async () => {
describe('loadParamsIntoEnv', () => {
it('should load array into process.env ', () => {
for (let i = 0; i < params.length; i++) {
process.env.should.not.have.property(params[i].key)
expect(process.env).not.toHaveProperty(params[i].key)
}

envLoader.loadParamsIntoEnv(params)

for (let i = 0; i < params.length; i++) {
process.env[params[i].key].should.equal(params[i].value)
expect(process.env[params[i].key]).toEqual(params[i].value)
}
})
})

describe('remapKeys', async () => {
it('should remap keys', async () => {
describe('remapKeys', () => {
it('should remap keys', () => {
const testParams = [
{ key: 'new_key_01', value: 'value01' },
{ key: 'new_key_02', value: 'value02' },
{ key: 'new_key_03', value: 'value03' }
]
const remapped = envLoader.remapKeys(params, 'key', 'new_key_')
remapped.should.deep.equal(testParams)
expect(remapped).toEqual(testParams)
})
})

describe('remapKeysInEnv', async () => {
it('should remap keys in the env', async () => {
describe('remapKeysInEnv', () => {
it('should remap keys in the env', () => {
const testParams = [
{ key: 'new_key_01', value: 'value01' },
{ key: 'new_key_02', value: 'value02' },
Expand All @@ -77,31 +71,31 @@ describe('envLoader', async () => {
]

const remapped = envLoader.remapKeysInEnv('key', 'new_key_', params)
remapped.should.deep.equal(testParams)
expect(remapped).toEqual(testParams)

for (let i = 0; i < testParams.length; i++) {
process.env[testParams[i].key].should.equal(testParams[i].value)
expect(process.env[testParams[i].key]).toEqual(testParams[i].value)
}

envLoader.remapKeysInEnv('new_', '')
for (let i = 0; i < testParams2.length; i++) {
testParams2[i].value.should.equal(process.env[testParams2[i].key])
expect(process.env[testParams2[i].key]).toEqual(testParams2[i].value)
}
})
})

describe('loadFileIntoEnv', async () => {
describe('loadFileIntoEnv', () => {
it('should read & load a file into process.env', async () => {
await envLoader.loadFileIntoEnv(envFile)

for (let i = 0; i < params.length; i++) {
process.env[params[i].key].should.equal(params[i].value)
expect(process.env[params[i].key]).toEqual(params[i].value)
}
})
})
})

describe('awsManager', async () => {
describe('awsManager', () => {
const env = 'test'
const service = 'config-wrapper'
const aParams = [
Expand All @@ -110,82 +104,81 @@ describe('awsManager', async () => {
{ key: 'secretParam01', value: 'secretValue01', isEncrypted: true }
]

describe('setParameter', async () => {
describe('setParameter', () => {
it('should set a parameter', async () => {
aParams.push({ key: 'testParam03', value: 'value03' })
const param = await awsManager.setParameter(aParams[3], env, service, false, true)
param.Tier.should.equal('Standard')
param.Version.should.be.at.least(1)
expect(param.Tier).toEqual('Standard')
expect(param.Version).toBeGreaterThanOrEqual(1)
})
})

describe('setParametersByService', async () => {
describe('setParametersByService', () => {
it('should set parameters by service', async () => {
const params = await awsManager.setParametersByService(aParams, env, service)
params.should.have.lengthOf(aParams.length)
expect(params).toHaveLength(aParams.length)
})
})

describe('getParameter', async () => {
describe('getParameter', () => {
it('should get a parameter from AWS', async () => {
const param = await awsManager.getParameter(env, service, aParams[0].key)
param.value.should.equal(aParams[0].value)
expect(param.value).toEqual(aParams[0].value)
})
})

describe('getParameter with secret', async () => {
describe('getParameter with secret', () => {
it('should get a secret parameter from AWS', async () => {
const param = await awsManager.getParameter(env, service, aParams[2].key, true)
param.value.should.equal(aParams[2].value)
expect(param.value).toEqual(aParams[2].value)
})
})

describe('getParametersByService', async () => {
describe('getParametersByService', () => {
it('should get all the parameters by env and service', async () => {
const params = await awsManager.getParametersByService(env, service, true)
console.log(params)
for (let i = 0; i < aParams.length; i++) {
const found = params[aParams[i].key]

should.exist(found)
found.value.should.equal(aParams[i].value)
expect(found).toBeDefined()
expect(found.value).toEqual(aParams[i].value)
}
})
})

describe('getEnvironments', async () => {
describe('getEnvironments', () => {
it('should get all the environments', async () => {
const envs = await awsManager.getEnvironments()
console.log(envs)
should.exist(envs.test)
envs.test.should.equal(4)
expect(envs.test).toBeDefined()
expect(envs.test).toEqual(4)
})
})

describe('getServicesForEnvironment', async () => {
describe('getServicesForEnvironment', () => {
it('should get all the services in an environment', async () => {
const svcs = await awsManager.getServicesForEnvironment('test')
console.log(svcs)
should.exist(svcs['config-wrapper'])
svcs['config-wrapper'].should.equal(4)
expect(svcs['config-wrapper']).toBeDefined()
expect(svcs['config-wrapper']).toEqual(4)
})
})

describe('getAllOrgParams', async () => {
describe('getAllOrgParams', () => {
it('should get all the org parameters', async () => {
const params = await awsManager.getAllOrgParams()
console.log(params)
should.exist(params.test)
should.exist(params.test['config-wrapper'])
Object.keys(params.test['config-wrapper']).should.have.lengthOf(4)
params.test['config-wrapper'].should.have.property('testParam01')
params.test['config-wrapper'].should.have.property('secretParam01')
expect(params.test).toBeDefined()
expect(params.test['config-wrapper']).toBeDefined()
expect(Object.keys(params.test['config-wrapper'])).toHaveLength(4)
expect(params.test['config-wrapper']).toHaveProperty('testParam01')
expect(params.test['config-wrapper']).toHaveProperty('secretParam01')
})
})
})


after(() => {
afterAll(() => {
console.log('afterAll called')
fs.unlink(envFile)
})
2 changes: 1 addition & 1 deletion bin/config-wrapper
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/usr/bin/env node

require('../src/cli').cli(process.argv);
require('../dist/cli').cli(process.argv);
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
};
Loading