Skip to content

Commit

Permalink
test: improve domain coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jbrunton committed Jan 13, 2023
1 parent e8109ce commit 41772fd
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 3 deletions.
39 changes: 39 additions & 0 deletions __tests__/entities/policies.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import {
isValidMatchPolicy,
isValidTTL,
StackNamePolicy,
TagPolicy
} 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)
})
})

describe('isValidMatchPolicy', () => {
const namePolicy: StackNamePolicy = {pattern: 'dev'}
const tagPolicy: TagPolicy = {tag: 'env', pattern: 'dev'}

it('returns true for valid match policies', () => {
expect(isValidMatchPolicy({name: namePolicy})).toEqual(true)
expect(isValidMatchPolicy({tags: [tagPolicy]})).toEqual(true)
})

it('returns false for invalid match policies', () => {
expect(isValidMatchPolicy({})).toEqual(false)
expect(isValidMatchPolicy({tags: []})).toEqual(false)
})
})
10 changes: 10 additions & 0 deletions __tests__/usecases/checks/stack-age-check.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
35 changes: 33 additions & 2 deletions __tests__/usecases/parse-policies.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('parsePolicies', () => {
it('parses valid yaml', () => {
const yaml = `
policies:
clean-staging:
clean-dev:
match:
tags:
environment: 'dev*'
Expand All @@ -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'}]
},
Expand Down Expand Up @@ -151,5 +151,36 @@ describe('parsePolicies', () => {
])
)
})

it('requires a valid tags policy', () => {
const yaml = `
policies:
invalid-match:
ttl:
hours: 3
match: {
tags: {}
}
`
expect(() => parsePolicies(yaml)).toThrowError(
new z.ZodError([
{
code: 'custom',
message: 'Policy must match on at least one tag',
path: ['policies', 'invalid-match', 'match', 'tags']
},
{
code: 'custom',
message: 'Policy must match on either name or tags',
path: ['policies', 'invalid-match', 'match']
},
{
code: 'custom',
message: 'At least one policy must be defined',
path: []
}
])
)
})
})
})
2 changes: 1 addition & 1 deletion src/domain/entities/policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ export type StackPolicy = {
}

export const isValidMatchPolicy = ({name, tags}: MatchPolicy): boolean =>
!(name === undefined && tags === undefined)
name !== undefined || (tags !== undefined && tags.length > 0)

export type PolicyParser = (input: string) => StackPolicy[]
3 changes: 3 additions & 0 deletions src/domain/usecases/parse-policies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ const TagsPolicyParser = z
.transform(arg =>
Object.entries(arg).map(([tag, pattern]) => ({tag, pattern}))
)
.refine(policies => policies.length > 0, {
message: 'Policy must match on at least one tag'
})

const MatchPolicy = z
.object({
Expand Down

0 comments on commit 41772fd

Please sign in to comment.