|
1 | 1 | import assert from "node:assert/strict"; |
| 2 | +import { |
| 3 | + mkdir, |
| 4 | + mkdtemp, |
| 5 | + readFile, |
| 6 | + rm, |
| 7 | + stat, |
| 8 | + writeFile, |
| 9 | +} from "node:fs/promises"; |
| 10 | +import { tmpdir } from "node:os"; |
| 11 | +import { resolve } from "node:path"; |
2 | 12 | import test from "node:test"; |
3 | 13 |
|
4 | 14 | import { run } from "../src/cli.js"; |
@@ -172,3 +182,84 @@ test("supports music-video manual workflow and webhook commands", async () => { |
172 | 182 |
|
173 | 183 | assert.deepEqual(calls, ["edit", "media", "compose", "webhooks-list"]); |
174 | 184 | }); |
| 185 | + |
| 186 | +test("webhook creation stores the one-time secret instead of printing it", async () => { |
| 187 | + const output = outputCollector(); |
| 188 | + const directory = await mkdtemp(resolve(tmpdir(), "beatapi-cli-webhook-")); |
| 189 | + const inputPath = resolve(directory, "webhook.json"); |
| 190 | + await writeFile( |
| 191 | + inputPath, |
| 192 | + JSON.stringify({ url: "https://example.com/webhooks/beatapi" }), |
| 193 | + ); |
| 194 | + |
| 195 | + try { |
| 196 | + const exitCode = await run( |
| 197 | + ["webhooks", "create", "--file", inputPath], |
| 198 | + { |
| 199 | + ...output.io, |
| 200 | + apiKey: "sk_test", |
| 201 | + env: { BEATAPI_CONFIG_DIR: directory }, |
| 202 | + createClient: () => ({ |
| 203 | + createWebhook: async () => ({ |
| 204 | + id: "wh_test", |
| 205 | + url: "https://example.com/webhooks/beatapi", |
| 206 | + secret: "whsec_one_time_secret", |
| 207 | + }), |
| 208 | + deleteWebhook: async () => ({ deleted: true }), |
| 209 | + }), |
| 210 | + }, |
| 211 | + ); |
| 212 | + |
| 213 | + assert.equal(exitCode, 0); |
| 214 | + assert.doesNotMatch(output.stdout(), /whsec_one_time_secret/); |
| 215 | + const result = JSON.parse(output.stdout()) as { |
| 216 | + secret_file: string; |
| 217 | + }; |
| 218 | + assert.equal( |
| 219 | + (await readFile(result.secret_file, "utf8")).trim(), |
| 220 | + "whsec_one_time_secret", |
| 221 | + ); |
| 222 | + assert.equal((await stat(result.secret_file)).mode & 0o777, 0o600); |
| 223 | + } finally { |
| 224 | + await rm(directory, { recursive: true, force: true }); |
| 225 | + } |
| 226 | +}); |
| 227 | + |
| 228 | +test("webhook creation rolls back when secure secret storage fails", async () => { |
| 229 | + const output = outputCollector(); |
| 230 | + const directory = await mkdtemp(resolve(tmpdir(), "beatapi-cli-webhook-")); |
| 231 | + const inputPath = resolve(directory, "webhook.json"); |
| 232 | + const secretsDirectory = resolve(directory, "secrets"); |
| 233 | + await writeFile( |
| 234 | + inputPath, |
| 235 | + JSON.stringify({ url: "https://example.com/webhooks/beatapi" }), |
| 236 | + ); |
| 237 | + await mkdir(secretsDirectory, { recursive: true }); |
| 238 | + await writeFile(resolve(secretsDirectory, "wh_test.secret"), "existing\n"); |
| 239 | + let deletedWebhookId = ""; |
| 240 | + |
| 241 | + try { |
| 242 | + await assert.rejects( |
| 243 | + run(["webhooks", "create", "--file", inputPath], { |
| 244 | + ...output.io, |
| 245 | + apiKey: "sk_test", |
| 246 | + env: { BEATAPI_CONFIG_DIR: directory }, |
| 247 | + createClient: () => ({ |
| 248 | + createWebhook: async () => ({ |
| 249 | + id: "wh_test", |
| 250 | + url: "https://example.com/webhooks/beatapi", |
| 251 | + secret: "whsec_one_time_secret", |
| 252 | + }), |
| 253 | + deleteWebhook: async (id: string) => { |
| 254 | + deletedWebhookId = id; |
| 255 | + return { deleted: true }; |
| 256 | + }, |
| 257 | + }), |
| 258 | + }), |
| 259 | + /rolled back/, |
| 260 | + ); |
| 261 | + assert.equal(deletedWebhookId, "wh_test"); |
| 262 | + } finally { |
| 263 | + await rm(directory, { recursive: true, force: true }); |
| 264 | + } |
| 265 | +}); |
0 commit comments