-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
12 changed files
with
798 additions
and
15 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
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,75 @@ | ||
import { INestApplication } from '@nestjs/common'; | ||
import { Parameter } from '@aws-sdk/client-ssm'; | ||
import { PSConfigModule } from './config.module'; | ||
import { PSConfigParameters, PSConfigOptions } from './interfaces'; | ||
import { PS_CONFIG_PARAMETERS, PS_CONFIG_OPTIONS } from './constants'; | ||
import { createTestHarness } from '../tests/utils'; | ||
import { mockData } from '../tests/mockdata'; | ||
|
||
describe('PSConfigModule', () => { | ||
let app: INestApplication; | ||
const moduleOptions: PSConfigOptions = { | ||
ssmParamStorePath: '/', | ||
ssmDecryptParams: true, | ||
ssmRecursive: true, | ||
ssmClientOptions: { | ||
region: 'us-east-1', | ||
}, | ||
}; | ||
|
||
describe('register()', () => { | ||
beforeEach(async () => { | ||
const testHarness = await createTestHarness( | ||
PSConfigModule.register(moduleOptions), | ||
); | ||
app = testHarness.app; | ||
}); | ||
|
||
it('should fetch all parameters when loading', () => { | ||
const parameters = app.get< | ||
typeof PS_CONFIG_PARAMETERS, | ||
PSConfigParameters | ||
>(PS_CONFIG_PARAMETERS); | ||
expect(parameters).toBeInstanceOf(Array<Parameter>); | ||
expect(parameters).toHaveLength(mockData.length); | ||
}); | ||
|
||
it('should load the module with options', () => { | ||
expect(app.get<PSConfigOptions>(PS_CONFIG_OPTIONS)).toMatchObject( | ||
moduleOptions, | ||
); | ||
}); | ||
}); | ||
|
||
describe('registerAsync()', () => { | ||
beforeEach(async () => { | ||
const testHarness = await createTestHarness( | ||
PSConfigModule.registerAsync({ | ||
useFactory: () => moduleOptions, | ||
}), | ||
); | ||
app = testHarness.app; | ||
}); | ||
|
||
it('should fetch all parameters when loading', () => { | ||
const parameters = app.get< | ||
typeof PS_CONFIG_PARAMETERS, | ||
PSConfigParameters | ||
>(PS_CONFIG_PARAMETERS); | ||
expect(parameters).toBeInstanceOf(Array<Parameter>); | ||
expect(parameters).toHaveLength(mockData.length); | ||
}); | ||
|
||
it('should load the module with options', () => { | ||
expect(app.get<PSConfigOptions>(PS_CONFIG_OPTIONS)).toMatchObject( | ||
moduleOptions, | ||
); | ||
}); | ||
}); | ||
|
||
afterEach(async () => { | ||
if (app) { | ||
await app.close(); | ||
} | ||
}); | ||
}); |
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,40 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
import { configParametersProvider } from './config-parameters.provider'; | ||
import { createOptionsProvider } from './options.provider'; | ||
import { ParameterStoreService } from '../services'; | ||
import { PSConfigParameters } from '../interfaces'; | ||
import { PS_CONFIG_PARAMETERS } from '../constants'; | ||
import { mockData } from '../../tests/mockdata'; | ||
|
||
describe('configParametersProvider#PS_CONFIG_PARAMETERS', () => { | ||
let moduleRef: TestingModule; | ||
let parameterStoreService: ParameterStoreService; | ||
|
||
beforeEach(async () => { | ||
moduleRef = await Test.createTestingModule({ | ||
providers: [ | ||
createOptionsProvider({ ssmParamStorePath: '/' }), | ||
{ | ||
provide: ParameterStoreService, | ||
useFactory: () => ({ | ||
getParametersByPath: jest | ||
.spyOn(ParameterStoreService.prototype, 'getParametersByPath') | ||
.mockImplementation(() => Promise.resolve(mockData)), | ||
}), | ||
}, | ||
configParametersProvider, | ||
], | ||
}).compile(); | ||
|
||
parameterStoreService = moduleRef.get<ParameterStoreService>( | ||
ParameterStoreService, | ||
); | ||
}); | ||
|
||
it('should hold all parameters', () => { | ||
const parameters = moduleRef.get<PSConfigParameters>(PS_CONFIG_PARAMETERS); | ||
expect(parameters).toBeInstanceOf(Array); | ||
expect(parameters).toHaveLength(mockData.length); | ||
expect(parameterStoreService.getParametersByPath).toHaveBeenCalled(); | ||
}); | ||
}); |
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,67 @@ | ||
import { Test } from '@nestjs/testing'; | ||
import { PSConfigService } from './config.service'; | ||
import { PS_CONFIG_PARAMETERS } from '../constants'; | ||
import { mockData } from '../../tests/mockdata'; | ||
|
||
describe('PSConfigService', () => { | ||
let psConfigService: PSConfigService; | ||
|
||
beforeEach(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
providers: [ | ||
PSConfigService, | ||
{ | ||
provide: PS_CONFIG_PARAMETERS, | ||
useValue: mockData, | ||
}, | ||
], | ||
}).compile(); | ||
|
||
psConfigService = moduleRef.get(PSConfigService); | ||
}); | ||
|
||
describe('get()', () => { | ||
it('should return value for existing parameter', () => { | ||
const value = psConfigService.get('post-table', 'fake'); | ||
expect(value).toBe('ProductionPostTable'); | ||
expect(typeof value).toBe('string'); | ||
}); | ||
|
||
it('should return a default value for not found parameter', () => { | ||
expect(psConfigService.get('unknown', null)).toBeNull(); | ||
expect(psConfigService.get('unknown', '')).toBe(''); | ||
}); | ||
}); | ||
|
||
describe('getBool()', () => { | ||
it('should return true for truthy values', () => { | ||
const value = psConfigService.getBool('signup-with-facebook', false); | ||
expect(value).toBeTruthy(); | ||
expect(typeof value).toBe('boolean'); | ||
}); | ||
|
||
it('should return false for falsy values', () => { | ||
const value = psConfigService.getBool('signup-with-google', true); | ||
expect(value).toBeFalsy(); | ||
expect(typeof value).toBe('boolean'); | ||
}); | ||
|
||
it('should return a default value for not found parameter', () => { | ||
expect(psConfigService.getBool('unknown', true)).toBeTruthy(); | ||
expect(psConfigService.getBool('unknown', false)).toBeFalsy(); | ||
}); | ||
}); | ||
|
||
describe('getNumber()', () => { | ||
it('should return a number', () => { | ||
const value = psConfigService.getNumber('port', 0); | ||
expect(value).toBe(3000); | ||
expect(typeof value).toBe('number'); | ||
}); | ||
|
||
it('should return a default value for not found parameter', () => { | ||
const value = psConfigService.getNumber('unknown', 5000); | ||
expect(value).toBe(5000); | ||
}); | ||
}); | ||
}); |
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,52 @@ | ||
import 'aws-sdk-client-mock-jest'; | ||
import { | ||
SSMClient, | ||
Parameter, | ||
GetParametersByPathCommand, | ||
} from '@aws-sdk/client-ssm'; | ||
import { AwsStub } from 'aws-sdk-client-mock'; | ||
import { Test } from '@nestjs/testing'; | ||
import { ParameterStoreService } from './parameter-store.service'; | ||
import { SSM_PS_CLIENT } from '../constants'; | ||
import { createSSMClientWithTestData } from '../../tests/utils'; | ||
import { mockData } from '../../tests/mockdata'; | ||
|
||
describe('ParameterStoreService', () => { | ||
let parameterStoreService: ParameterStoreService; | ||
let ssmClient: AwsStub<SSMClient, unknown, unknown>; | ||
|
||
beforeEach(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
providers: [ | ||
ParameterStoreService, | ||
{ | ||
provide: SSM_PS_CLIENT, | ||
useFactory: () => createSSMClientWithTestData(), | ||
}, | ||
], | ||
}).compile(); | ||
|
||
parameterStoreService = moduleRef.get(ParameterStoreService); | ||
ssmClient = moduleRef.get(SSM_PS_CLIENT); | ||
}); | ||
|
||
describe('getParametersByPath()', () => { | ||
it('should fetch all parameters with pagination', async () => { | ||
const totalItems = mockData.length; | ||
const totalPages = Math.ceil(totalItems / 10); | ||
|
||
const results = await parameterStoreService.getParametersByPath( | ||
'/', | ||
true, | ||
true, | ||
); | ||
|
||
expect(results).toBeInstanceOf(Array<Parameter>); | ||
expect(results).toHaveLength(totalItems); | ||
expect(ssmClient).toHaveReceivedCommandTimes( | ||
GetParametersByPathCommand, | ||
totalPages, | ||
); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.