From 49ef163e5d91ac5123cd6ccc29b5f98e0c92d7df Mon Sep 17 00:00:00 2001 From: Daniel Walsh Date: Mon, 4 Nov 2024 15:32:33 +0000 Subject: [PATCH] Disable warnings for invalid config in wrangler login & logout (#6756) --- .changeset/selfish-readers-exercise.md | 5 +++ .../wrangler/src/__tests__/logout.test.ts | 31 +++++++++++++ packages/wrangler/src/__tests__/user.test.ts | 44 +++++++++++++++++++ packages/wrangler/src/index.ts | 4 +- 4 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 .changeset/selfish-readers-exercise.md diff --git a/.changeset/selfish-readers-exercise.md b/.changeset/selfish-readers-exercise.md new file mode 100644 index 000000000000..f8ef28c1b808 --- /dev/null +++ b/.changeset/selfish-readers-exercise.md @@ -0,0 +1,5 @@ +--- +"wrangler": patch +--- + +chore: disable wrangler.toml warnings when doing `wrangler login` & `wrangler logout` diff --git a/packages/wrangler/src/__tests__/logout.test.ts b/packages/wrangler/src/__tests__/logout.test.ts index 5ab0da8dac9b..7d63394eccbf 100644 --- a/packages/wrangler/src/__tests__/logout.test.ts +++ b/packages/wrangler/src/__tests__/logout.test.ts @@ -5,6 +5,7 @@ import { mockConsoleMethods } from "./helpers/mock-console"; import { msw } from "./helpers/msw"; import { runInTempDir } from "./helpers/run-in-tmp"; import { runWrangler } from "./helpers/run-wrangler"; +import { writeWranglerToml } from "./helpers/write-wrangler-toml"; describe("logout", () => { runInTempDir(); @@ -50,4 +51,34 @@ describe("logout", () => { expect(fs.existsSync(config)).toBeFalsy(); expect(counter).toBe(1); }); + + it("should not warn on invalid wrangler.toml when logging out", async () => { + writeAuthConfigFile({ + oauth_token: "some-oauth-tok", + refresh_token: "some-refresh-tok", + }); + const config = getAuthConfigFilePath(); + + msw.use( + http.post( + "*/oauth2/revoke", + async () => { + return HttpResponse.text(""); + }, + { once: true } + ) + ); + + expect(fs.existsSync(config)).toBeTruthy(); + + // @ts-expect-error - intentionally invalid + writeWranglerToml({ invalid: true }); + + await runWrangler("logout"); + + expect(std.out).toMatchInlineSnapshot(`"Successfully logged out."`); + expect(std.warn).toMatchInlineSnapshot(`""`); + expect(std.err).toMatchInlineSnapshot(`""`); + expect(fs.existsSync(config)).toBeFalsy(); + }); }); diff --git a/packages/wrangler/src/__tests__/user.test.ts b/packages/wrangler/src/__tests__/user.test.ts index 24d9be6ab064..f8d299b574c3 100644 --- a/packages/wrangler/src/__tests__/user.test.ts +++ b/packages/wrangler/src/__tests__/user.test.ts @@ -23,6 +23,7 @@ import { import { normalizeString } from "./helpers/normalize"; import { runInTempDir } from "./helpers/run-in-tmp"; import { runWrangler } from "./helpers/run-wrangler"; +import { writeWranglerToml } from "./helpers/write-wrangler-toml"; import type { Config } from "../config"; import type { UserAuthConfig } from "../user"; import type { MockInstance } from "vitest"; @@ -179,4 +180,47 @@ describe("User", () => { normalizeString(`${getGlobalWranglerConfigPath()}/config/staging.toml`) ); }); + + it("should not warn on invalid wrangler.toml when logging in", async () => { + mockOAuthServerCallback("success"); + + let counter = 0; + msw.use( + http.post( + "*/oauth2/token", + async () => { + counter += 1; + + return HttpResponse.json({ + access_token: "test-access-token", + expires_in: 100000, + refresh_token: "test-refresh-token", + scope: "account:read", + }); + }, + { once: true } + ) + ); + + // @ts-expect-error - intentionally invalid + writeWranglerToml({ invalid: true }); + + await runWrangler("login"); + + expect(counter).toBe(1); + expect(std.out).toMatchInlineSnapshot(` + "Attempting to login via OAuth... + Opening a link in your default browser: https://dash.cloudflare.com/oauth2/auth?response_type=code&client_id=54d11594-84e4-41aa-b438-e81b8fa78ee7&redirect_uri=http%3A%2F%2Flocalhost%3A8976%2Foauth%2Fcallback&scope=account%3Aread%20user%3Aread%20workers%3Awrite%20workers_kv%3Awrite%20workers_routes%3Awrite%20workers_scripts%3Awrite%20workers_tail%3Aread%20d1%3Awrite%20pages%3Awrite%20zone%3Aread%20ssl_certs%3Awrite%20ai%3Awrite%20queues%3Awrite%20pipelines%3Awrite%20offline_access&state=MOCK_STATE_PARAM&code_challenge=MOCK_CODE_CHALLENGE&code_challenge_method=S256 + Successfully logged in." + `); + expect(std.warn).toMatchInlineSnapshot(`""`); + expect(std.err).toMatchInlineSnapshot(`""`); + expect(readAuthConfigFile()).toEqual({ + api_token: undefined, + oauth_token: "test-access-token", + refresh_token: "test-refresh-token", + expiration_time: expect.any(String), + scopes: ["account:read"], + }); + }); }); diff --git a/packages/wrangler/src/index.ts b/packages/wrangler/src/index.ts index 71a4654a7cb0..b81d82dafb8e 100644 --- a/packages/wrangler/src/index.ts +++ b/packages/wrangler/src/index.ts @@ -642,7 +642,7 @@ export function createCLIParser(argv: string[]) { return; } await login({ browser: args.browser }); - const config = readConfig(args.config, args); + const config = readConfig(args.config, args, undefined, true); await metrics.sendMetricsEvent("login user", { sendMetrics: config.send_metrics, }); @@ -662,7 +662,7 @@ export function createCLIParser(argv: string[]) { async (args) => { await printWranglerBanner(); await logout(); - const config = readConfig(undefined, args); + const config = readConfig(undefined, args, undefined, true); await metrics.sendMetricsEvent("logout user", { sendMetrics: config.send_metrics, });