Skip to content

Commit

Permalink
check variable types before trying to delete. fixes #96
Browse files Browse the repository at this point in the history
  • Loading branch information
justinedelson committed Apr 10, 2020
1 parent 08cfb1c commit f248f11
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 15 deletions.
6 changes: 3 additions & 3 deletions src/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ class Client {
debug(text)
if (res.ok) resolve(res)
else {
const error = new Error(`${message}: ${res.url} (${res.status} ${res.statusText})`)
error.res = res
reject(error)
const error = new Error(`${message}: ${res.url} (${res.status} ${res.statusText})`)
error.res = res
reject(error)
}
})
}
Expand Down
31 changes: 23 additions & 8 deletions src/commands/cloudmanager/set-environment-variables.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class SetEnvironmentVariablesCommand extends BaseEnvironmentVariablesCommand {

const programId = await getProgramId(flags)

const currentVariablesList = await this.getEnvironmentVariables(programId, args.environmentId, flags.passphrase)
const currentVariableTypes = {}
currentVariablesList.forEach(variable => currentVariableTypes[variable.name] = variable.type)

const variables = []
if (flags.variable) {
// each --param flag expects two values ( a key and a value ). Multiple --parm flags can be passed
Expand All @@ -56,20 +60,31 @@ class SetEnvironmentVariablesCommand extends BaseEnvironmentVariablesCommand {
}
}
if (flags.delete) {
flags.delete.forEach(key => variables.push({
name: key,
value: ''
}))
flags.delete.forEach(key => {
if (currentVariableTypes[key]) {
variables.push({
name: key,
type: currentVariableTypes[key],
value: ''
})
} else {
this.warn(`Variable ${key} not found. Will not try to delete.`)
}
})
}

cli.action.start('setting variables')
await this.setEnvironmentVariables(programId, args.environmentId, variables, flags.passphrase)
cli.action.stop()
if (variables.length > 0) {
cli.action.start('setting variables')
await this.setEnvironmentVariables(programId, args.environmentId, variables, flags.passphrase)
cli.action.stop()
} else {
this.log("No variables to set or delete.")
}

let result

try {
result = await this.getEnvironmentVariables(programId, args.environmentId, variables, flags.passphrase)
result = await this.getEnvironmentVariables(programId, args.environmentId, flags.passphrase)
} catch (error) {
this.error(error.message)
}
Expand Down
53 changes: 49 additions & 4 deletions test/commands/set-environment-variables.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ test('set-environment-variables - bad variable flag', async () => {
iss: "good"
}
}),
'cloudmanager_programid': "6"
'cloudmanager_programid': "4"
})

expect.assertions(2)
Expand All @@ -71,7 +71,7 @@ test('set-environment-variables - bad secret flag', async () => {
iss: "good"
}
}),
'cloudmanager_programid': "6"
'cloudmanager_programid': "4"
})

expect.assertions(2)
Expand Down Expand Up @@ -272,12 +272,57 @@ test('set-environment-variables - delete', async () => {

expect.assertions(3)

let runResult = SetEnvironmentVariablesCommand.run(["1", "--delete", "foo"])
let runResult = SetEnvironmentVariablesCommand.run(["1", "--delete", "KEY"])
await expect(runResult instanceof Promise).toBeTruthy()
await expect(runResult).resolves.toBeTruthy()
const patchCall = fetchMock.calls().find(call => call[0] === 'https://cloudmanager.adobe.io/api/program/4/environment/1/variables' && call[1].method === 'PATCH')
await expect(JSON.parse(patchCall[1].body)).toMatchObject([{
"name" : "foo",
"name" : "KEY",
"type": "string",
"value" : ""
}])
})

test('set-environment-variables - delete secret', async () => {
setStore({
'jwt-auth': JSON.stringify({
client_id: '1234',
jwt_payload: {
iss: "good"
}
}),
'cloudmanager_programid': "4"
})

expect.assertions(3)

let runResult = SetEnvironmentVariablesCommand.run(["1", "--delete", "I_AM_A_SECRET"])
await expect(runResult instanceof Promise).toBeTruthy()
await expect(runResult).resolves.toBeTruthy()
const patchCall = fetchMock.calls().find(call => call[0] === 'https://cloudmanager.adobe.io/api/program/4/environment/1/variables' && call[1].method === 'PATCH')
await expect(JSON.parse(patchCall[1].body)).toMatchObject([{
"name" : "I_AM_A_SECRET",
"type": "secretString",
"value" : ""
}])
})

test('set-environment-variables - delete not found', async () => {
setStore({
'jwt-auth': JSON.stringify({
client_id: '1234',
jwt_payload: {
iss: "good"
}
}),
'cloudmanager_programid': "4"
})

expect.assertions(3)

let runResult = SetEnvironmentVariablesCommand.run(["1", "--delete", "foo"])
await expect(runResult instanceof Promise).toBeTruthy()
await expect(runResult).resolves.toBeTruthy()
const patchCall = fetchMock.calls().find(call => call[0] === 'https://cloudmanager.adobe.io/api/program/4/environment/1/variables' && call[1].method === 'PATCH')
await expect(patchCall).toBeFalsy()
})

0 comments on commit f248f11

Please sign in to comment.