diff --git a/src/cdk/constructs/config/autocomplete.test.ts b/src/cdk/constructs/config/autocomplete.test.ts index 8ca0bd5..fe26264 100644 --- a/src/cdk/constructs/config/autocomplete.test.ts +++ b/src/cdk/constructs/config/autocomplete.test.ts @@ -7,3 +7,12 @@ describe('autocomplete', () => { expect(res.length > 0).toBeTruthy() }) }) + +describe('autocomplete with empty input', () => { + it('buildSearchRegions should return a promise', async () => { + const fn = buildSearchRegions() + const res = await fn({}, '') + expect(Array.isArray(res)).toBeTruthy() + expect(res.length > 0).toBeTruthy() + }) +}) diff --git a/src/cdk/constructs/config/validators.test.ts b/src/cdk/constructs/config/validators.test.ts index c9da0b1..1457272 100644 --- a/src/cdk/constructs/config/validators.test.ts +++ b/src/cdk/constructs/config/validators.test.ts @@ -1,4 +1,5 @@ import * as validators from './validators' +import { execFile } from 'child_process' describe('Validators', () => { it('isValidAwsAccountId', async () => { @@ -21,6 +22,13 @@ describe('Validators', () => { expect(validators.isValidAwsAccountIdList('1234567890123,000000000000')).toBeFalsy() }) + it('isValidAwsCliProfile', async () => { + expect(await validators.isValidAwsCliProfile('')).toBeFalsy() + expect(await validators.isValidAwsCliProfile('value')).toBeFalsy() + await execFile('aws', ['configure', 'set', 'region', 'eu-west-1', '--profile', 'user1']) + expect(await validators.isValidAwsCliProfile('user1')).toBeTruthy() + }) + it('isValidAwsHostedZoneId', async () => { expect(validators.isValidAwsHostedZoneId('ZABCDEF1234567')).toBeTruthy() @@ -51,6 +59,12 @@ describe('Validators', () => { expect(validators.isValidDomain('a.c')).toBeFalsy() }) + it('isValidGitBranchName', async () => { + expect(await validators.isValidGitBranchName('')).toBeFalsy() + expect(await validators.isValidGitBranchName(' ')).toBeFalsy() + expect(await validators.isValidGitBranchName('value')).toBeTruthy() + }) + it('isValidEnvironmentNameList', async () => { expect(validators.isValidEnvironmentNameList('ab')).toBeTruthy() expect(validators.isValidEnvironmentNameList('a1')).toBeTruthy() diff --git a/src/cdk/custom-resources/auto-delete-bucket/lambda/index.test.ts b/src/cdk/custom-resources/auto-delete-bucket/lambda/index.test.ts index 41c75e5..b46c55a 100644 --- a/src/cdk/custom-resources/auto-delete-bucket/lambda/index.test.ts +++ b/src/cdk/custom-resources/auto-delete-bucket/lambda/index.test.ts @@ -25,6 +25,26 @@ describe('auto-delete bucket lambda', () => { mockSendResponse.mockClear() }) + it('fails if ResourceProperties is not provided no matter the request type', async () => { + await handler({ + ...eventBase, + RequestType: 'Create', + ResourceProperties: undefined + }) + + expect(mockDeleteBucket).not.toHaveBeenCalled() + expect(mockSendResponse).toHaveBeenCalledTimes(1) + expect(mockSendResponse).toHaveBeenCalledWith('ResponseURL', { + status: 'FAILED', + reason: 'BucketName is required', + physicalResourceId: 'LogicalResourceId', + stackId: 'StackId', + requestId: 'RequestId', + logicalResourceId: 'LogicalResourceId', + data: undefined + }) + }) + it('fails if bucket name is not provided no matter the request type', async () => { await handler({ ...eventBase, diff --git a/src/cdk/custom-resources/auto-delete-bucket/utils/delete-bucket.test.ts b/src/cdk/custom-resources/auto-delete-bucket/utils/delete-bucket.test.ts index 8d86337..7526710 100644 --- a/src/cdk/custom-resources/auto-delete-bucket/utils/delete-bucket.test.ts +++ b/src/cdk/custom-resources/auto-delete-bucket/utils/delete-bucket.test.ts @@ -101,4 +101,39 @@ describe('delete bucket', () => { } }) }) + + it('deletes all objects from the bucket without Versions and DeleteMarkers value without Key', async () => { + mockListObjectVersionsPromise.mockResolvedValue({ + IsTruncated: false, + DeleteMarkers: [{ VersionId: 'v3', RandomKey: 'test3' }] + }) + + await deleteBucket('testBucket') + + expect(mockListObjectVersions).toBeCalledTimes(1) + expect(mockListObjectVersions).toBeCalledWith({ Bucket: 'testBucket' }) + + expect(mockDeleteObjects).toBeCalledTimes(1) + expect(mockDeleteObjects).toBeCalledWith({ + Bucket: 'testBucket', + Delete: { + Objects: [ + { Key: '', VersionId: 'v3' } + ] + } + }) + }) + + it('deletes all objects from the bucket without Versions and DeleteMarkers', async () => { + mockListObjectVersionsPromise.mockResolvedValue({ + IsTruncated: false + }) + + await deleteBucket('testBucket') + + expect(mockListObjectVersions).toBeCalledTimes(1) + expect(mockListObjectVersions).toBeCalledWith({ Bucket: 'testBucket' }) + + expect(mockDeleteObjects).toBeCalledTimes(0) + }) }) diff --git a/src/config/job-config.test.ts b/src/config/job-config.test.ts index 76f6a95..f526bbf 100644 --- a/src/config/job-config.test.ts +++ b/src/config/job-config.test.ts @@ -300,6 +300,8 @@ describe('MiraJobConfig', () => { mockConfigHandler(healthyConfig) expect(() => new MiraJobConfig('deploy')).toThrowError('Cannot deploy environment undefined') + expect(() => new MiraJobConfig('')).toThrowError('Cannot resolve deployment type') + let brokenConfig brokenConfig = _.cloneDeep(healthyConfig) diff --git a/src/config/job-config.ts b/src/config/job-config.ts index 21b95e9..4a3b9b0 100644 --- a/src/config/job-config.ts +++ b/src/config/job-config.ts @@ -135,9 +135,6 @@ export default class MiraConfig { const envData: EnvData = loadEnvironment(envKey) return massageEnvData(envData, false) }) - if (!config.get('cicd.buildspec_file')) { - throw new Error('Missing config.cicd.buildspec_file') - } const details: CICDDetails = { stackName: deploymentStackName, role: loadAWSProfile(config.get('cicd.profile')), diff --git a/src/transpiler.test.ts b/src/transpiler.test.ts index 85dd20e..a11b633 100644 --- a/src/transpiler.test.ts +++ b/src/transpiler.test.ts @@ -20,3 +20,28 @@ test('should change extension to file', () => { const res = t.changeExtension('foo') expect(res).toBe('sampleFile.foo') }) + +test('should return null when passing the wrong path', async () => { + const t = new Transpiler('sampleFile.ts') + const root = __dirname + const res = await t.findTSConfigFile(root) + expect(res).toBe(null) +}) + +test('should return null when passing the wrong path', async () => { + const t = new Transpiler('sampleFile.ts') + const root = __dirname + const res = await t.findTSConfigFile(root) + expect(res).toBe(null) +}) + +test('should throw access error when passing empty path', async () => { + console.error = jest.fn() + const t = new Transpiler('sampleFile.ts') + try { + await t.findTSConfigFile(' ') + } catch (err) { + expect(err.errno).toBe(-13) + expect(err.code).toBe('EACCES') + } +})