From d10c23fdec5db7bb94fbe61fbf22795fac62f09b Mon Sep 17 00:00:00 2001 From: John Brunton Date: Fri, 13 Jan 2023 16:03:05 +0000 Subject: [PATCH] test: improve domain coverage --- __tests__/entities/policies.test.ts | 19 +++++++++++++++++++ .../usecases/checks/stack-age-check.test.ts | 10 ++++++++++ __tests__/usecases/parse-policies.test.ts | 4 ++-- 3 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 __tests__/entities/policies.test.ts diff --git a/__tests__/entities/policies.test.ts b/__tests__/entities/policies.test.ts new file mode 100644 index 0000000..2698e03 --- /dev/null +++ b/__tests__/entities/policies.test.ts @@ -0,0 +1,19 @@ +import {isValidTTL} from '@entities/policies' + +describe('isValidTTL', () => { + it('returns true for valid TTL policies', () => { + expect(isValidTTL({days: 1})).toEqual(true) + expect(isValidTTL({hours: 1})).toEqual(true) + expect(isValidTTL({minutes: 1})).toEqual(true) + }) + + it('treats 0 time units as valid', () => { + expect(isValidTTL({days: 0})).toEqual(true) + expect(isValidTTL({hours: 0})).toEqual(true) + expect(isValidTTL({minutes: 0})).toEqual(true) + }) + + it('returns false for invalid TTL policies', () => { + expect(isValidTTL({})).toEqual(false) + }) +}) diff --git a/__tests__/usecases/checks/stack-age-check.test.ts b/__tests__/usecases/checks/stack-age-check.test.ts index 68042d8..89b860c 100644 --- a/__tests__/usecases/checks/stack-age-check.test.ts +++ b/__tests__/usecases/checks/stack-age-check.test.ts @@ -35,6 +35,16 @@ describe('StackAgeCheck', () => { }) }) + it('passes when the stack is exactly the age of the ttl policy', async () => { + const threeHoursAgo = sub(now, {hours: 3, minutes: 30}) + const result = await check(stack(threeHoursAgo)) + expect(result).toEqual({ + isLegacy: false, + description: + 'checked stack age [2021-01-01T08:30:00.000Z] against ttl [3 hours 30 minutes]' + }) + }) + it('fails when the stack is older than the ttl policy', async () => { const fourHoursAgo = sub(now, {hours: 4}) const result = await check(stack(fourHoursAgo)) diff --git a/__tests__/usecases/parse-policies.test.ts b/__tests__/usecases/parse-policies.test.ts index b3a95d9..039ff29 100644 --- a/__tests__/usecases/parse-policies.test.ts +++ b/__tests__/usecases/parse-policies.test.ts @@ -5,7 +5,7 @@ describe('parsePolicies', () => { it('parses valid yaml', () => { const yaml = ` policies: - clean-staging: + clean-dev: match: tags: environment: 'dev*' @@ -20,7 +20,7 @@ describe('parsePolicies', () => { const policy = parsePolicies(yaml) expect(policy).toEqual([ { - name: 'clean-staging', + name: 'clean-dev', match: { tags: [{pattern: 'dev*', tag: 'environment'}] },