-
Notifications
You must be signed in to change notification settings - Fork 791
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR implements `wrangler secret` for `--local` mode. The implementation is simply a no-op, since we don't want to actually write secret values to disk (I think?). I also got the messaging for remote mode right by copying from wrangler 1. Further, I added tests for all the `wrangler secret commands`.
- Loading branch information
1 parent
71b0fab
commit 55a087e
Showing
3 changed files
with
263 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"wrangler": patch | ||
--- | ||
|
||
feat: `wrangler secret * --local` | ||
|
||
This PR implements `wrangler secret` for `--local` mode. The implementation is simply a no-op, since we don't want to actually write secret values to disk (I think?). I also got the messaging for remote mode right by copying from wrangler 1. Further, I added tests for all the `wrangler secret commands`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
import { setMockResponse, unsetAllMocks } from "./mock-cfetch"; | ||
import { runWrangler } from "./run-wrangler"; | ||
import { runInTempDir } from "./run-in-tmp"; | ||
import { mockConfirm, mockPrompt } from "./mock-dialogs"; | ||
|
||
describe("wrangler secret", () => { | ||
runInTempDir(); | ||
afterEach(() => { | ||
unsetAllMocks(); | ||
}); | ||
|
||
describe("put", () => { | ||
function mockPutRequest(input: { name: string; text: string }) { | ||
setMockResponse( | ||
"/accounts/:accountId/workers/scripts/:scriptName/secrets", | ||
"PUT", | ||
([_url, accountId], { body }) => { | ||
expect(accountId).toEqual("some-account-id"); | ||
const { name, text, type } = JSON.parse(body as string); | ||
expect(type).toEqual("secret_text"); | ||
expect(name).toEqual(input.name); | ||
expect(text).toEqual(input.text); | ||
|
||
return { name, type }; | ||
} | ||
); | ||
} | ||
|
||
it("should create a secret", async () => { | ||
mockPrompt({ | ||
text: "Enter a secret value:", | ||
type: "password", | ||
result: "the-secret", | ||
}); | ||
|
||
mockPutRequest({ name: "the-secret-name", text: "the-secret" }); | ||
const { stdout, stderr, error } = await runWrangler( | ||
"secret put the-key --name script-name" | ||
); | ||
|
||
expect(stdout).toMatchInlineSnapshot(` | ||
"🌀 Creating the secret for script script-name | ||
✨ Success! Uploaded secret the-key" | ||
`); | ||
expect(stderr).toMatchInlineSnapshot(`""`); | ||
expect(error).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
|
||
it("should error without a script name", async () => { | ||
const { stdout, stderr, error } = await runWrangler("secret put the-key"); | ||
expect(stdout).toMatchInlineSnapshot(`""`); | ||
expect(stderr).toMatchInlineSnapshot(` | ||
"Missing script name | ||
[32m%s[0m | ||
If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new." | ||
`); | ||
expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`); | ||
}); | ||
|
||
it("warns about being a no-op in local mode", async () => { | ||
mockPrompt({ | ||
text: "Enter a secret value:", | ||
type: "password", | ||
result: "the-secret", | ||
}); | ||
|
||
mockPutRequest({ name: "the-secret-name", text: "the-secret" }); | ||
const { stdout, stderr, warnings, error } = await runWrangler( | ||
"secret put the-key --name script-name --local" | ||
); | ||
expect(stdout).toMatchInlineSnapshot(`""`); | ||
expect(stderr).toMatchInlineSnapshot(`""`); | ||
expect(warnings).toMatchInlineSnapshot( | ||
`"\`wrangler secret put\` is a no-op in --local mode"` | ||
); | ||
expect(error).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
}); | ||
|
||
describe("delete", () => { | ||
function mockDeleteRequest(input: { | ||
scriptName: string; | ||
secretName: string; | ||
}) { | ||
setMockResponse( | ||
"/accounts/:accountId/workers/scripts/:scriptName/secrets/:secretName", | ||
"DELETE", | ||
([_url, accountId, scriptName, secretName]) => { | ||
expect(accountId).toEqual("some-account-id"); | ||
expect(scriptName).toEqual(input.scriptName); | ||
expect(secretName).toEqual(input.secretName); | ||
|
||
return null; | ||
} | ||
); | ||
} | ||
it("should delete a secret", async () => { | ||
mockDeleteRequest({ scriptName: "script-name", secretName: "the-key" }); | ||
mockConfirm({ | ||
text: "Are you sure you want to permanently delete the variable the-key on the script script-name?", | ||
result: true, | ||
}); | ||
const { stdout, stderr, error } = await runWrangler( | ||
"secret delete the-key --name script-name" | ||
); | ||
expect(stdout).toMatchInlineSnapshot(` | ||
"🌀 Deleting the secret the-key on script script-name. | ||
✨ Success! Deleted secret the-key" | ||
`); | ||
expect(stderr).toMatchInlineSnapshot(`""`); | ||
expect(error).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
|
||
it("should error without a script name", async () => { | ||
const { stdout, stderr, error } = await runWrangler( | ||
"secret delete the-key" | ||
); | ||
expect(stdout).toMatchInlineSnapshot(`""`); | ||
expect(stderr).toMatchInlineSnapshot(` | ||
"Missing script name | ||
[32m%s[0m | ||
If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new." | ||
`); | ||
expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`); | ||
}); | ||
|
||
it("warns about being a no-op in local mode", async () => { | ||
mockConfirm({ | ||
text: "Are you sure you want to permanently delete the variable the-key on the script script-name?", | ||
result: true, | ||
}); | ||
const { stdout, stderr, warnings, error } = await runWrangler( | ||
"secret delete the-key --name script-name --local" | ||
); | ||
expect(stdout).toMatchInlineSnapshot( | ||
`"🌀 Deleting the secret the-key on script script-name."` | ||
); | ||
expect(stderr).toMatchInlineSnapshot(`""`); | ||
expect(warnings).toMatchInlineSnapshot( | ||
`"\`wrangler secret delete\` is a no-op in --local mode"` | ||
); | ||
expect(error).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
}); | ||
|
||
describe("list", () => { | ||
function mockListRequest(input: { scriptName: string }) { | ||
setMockResponse( | ||
"/accounts/:accountId/workers/scripts/:scriptName/secrets", | ||
"GET", | ||
([_url, accountId, scriptName]) => { | ||
expect(accountId).toEqual("some-account-id"); | ||
expect(scriptName).toEqual(input.scriptName); | ||
|
||
return [ | ||
{ | ||
name: "the-secret-name", | ||
type: "secret_text", | ||
}, | ||
]; | ||
} | ||
); | ||
} | ||
|
||
it("should list secrets", async () => { | ||
mockListRequest({ scriptName: "script-name" }); | ||
const { stdout, stderr, error } = await runWrangler( | ||
"secret list --name script-name" | ||
); | ||
expect(stdout).toMatchInlineSnapshot(` | ||
"[ | ||
{ | ||
\\"name\\": \\"the-secret-name\\", | ||
\\"type\\": \\"secret_text\\" | ||
} | ||
]" | ||
`); | ||
expect(stderr).toMatchInlineSnapshot(`""`); | ||
expect(error).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
|
||
it("should error without a script name", async () => { | ||
const { stdout, stderr, error } = await runWrangler("secret list"); | ||
expect(stdout).toMatchInlineSnapshot(`""`); | ||
expect(stderr).toMatchInlineSnapshot(` | ||
"Missing script name | ||
[32m%s[0m | ||
If you think this is a bug then please create an issue at https://github.com/cloudflare/wrangler2/issues/new." | ||
`); | ||
expect(error).toMatchInlineSnapshot(`[Error: Missing script name]`); | ||
}); | ||
|
||
it("warns about being a no-op in local mode", async () => { | ||
const { stdout, stderr, warnings, error } = await runWrangler( | ||
"secret list --name script-name --local" | ||
); | ||
expect(stdout).toMatchInlineSnapshot(`""`); | ||
expect(stderr).toMatchInlineSnapshot(`""`); | ||
expect(warnings).toMatchInlineSnapshot( | ||
`"\`wrangler secret list\` is a no-op in --local mode"` | ||
); | ||
expect(error).toMatchInlineSnapshot(`undefined`); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters