Skip to content

Commit

Permalink
test: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
hideokamoto committed Nov 7, 2019
1 parent 754c8c9 commit efa03f1
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
docs/
examples/
__tests__/
74 changes: 71 additions & 3 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,76 @@

import func from '../libs/index'
import CloudFrontUpdator, {
interfaces
} from '../libs/index'

const condition: interfaces.Config = {
debugMode: true,
taskType: 'parallel',
allowSensitiveAction: false
}
class DummyClient {
// @ts-ignore
getDistributionConfig (param: any) {
return {
promise (): AWS.CloudFront.GetDistributionConfigResult {
const DistributionConfig: AWS.CloudFront.DistributionConfig = {
Enabled: true
} as AWS.CloudFront.DistributionConfig
return {
ETag: 'tag',
DistributionConfig
}
}
}
}

// @ts-ignore
updateDistribution (param: any) {
return {
promise () {
return {}
}
}
}

// @ts-ignore
listDistributions (param: any) {
return {
promise () {
return {}
}
}
}
}
describe('dummy', () => {
it('test', () => {
expect(func('John')).toEqual('Hello John !')
// @ts-ignore
const updator: interfaces.UpdatorFunction = (id, conf) => conf
const filter: interfaces.FilterCondition = () => true
let client = new CloudFrontUpdator({
updator,
filter
}, condition, {
cfClient: new DummyClient() as any as AWS.CloudFront
})
it('should work', async () => {
const dist = {} as AWS.CloudFront.Distribution
await expect(client.updateDistribution(dist)).resolves.toBe(undefined)
})
it('should throw error', async () => {
client = new CloudFrontUpdator({
updator: (id, conf) => {
console.log(id)
conf.Enabled = false
return conf
},
filter
}, condition, {
cfClient: new DummyClient() as any as AWS.CloudFront
})
const dist = {
Id: 'ETEST'
} as AWS.CloudFront.Distribution
const promise = client.updateDistribution(dist)
await expect(promise).rejects.toMatch("You can not allow the action, please set 'allowSensitiveAction' option in the constructor")
})
})
3 changes: 2 additions & 1 deletion libs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,10 @@ export class CloudFrontUpdator {
}).promise()
if (!DistributionConfig) throw new Error('No such distribution')
if (!ETag) throw new Error('no ETag')
const beforeConfig = Object.assign({}, DistributionConfig)
const config = await this.updator(distribution.Id, DistributionConfig)
if (!this.allowSensitiveAction && config) {
if (DistributionConfig.Enabled !== config.Enabled) {
if (beforeConfig.Enabled !== config.Enabled) {
const err = 'You can not allow the action, please set \'allowSensitiveAction\' option in the constructor'
this.log(err)
throw err
Expand Down

0 comments on commit efa03f1

Please sign in to comment.