Skip to content

Commit

Permalink
Disable warnings for invalid config in wrangler login & logout (#6756)
Browse files Browse the repository at this point in the history
  • Loading branch information
WalshyDev authored Nov 4, 2024
1 parent 89f6274 commit 49ef163
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/selfish-readers-exercise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wrangler": patch
---

chore: disable wrangler.toml warnings when doing `wrangler login` & `wrangler logout`
31 changes: 31 additions & 0 deletions packages/wrangler/src/__tests__/logout.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
});
});
44 changes: 44 additions & 0 deletions packages/wrangler/src/__tests__/user.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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<UserAuthConfig>({
api_token: undefined,
oauth_token: "test-access-token",
refresh_token: "test-refresh-token",
expiration_time: expect.any(String),
scopes: ["account:read"],
});
});
});
4 changes: 2 additions & 2 deletions packages/wrangler/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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,
});
Expand Down

0 comments on commit 49ef163

Please sign in to comment.