Skip to content

Commit 28a3ff7

Browse files
committed
fixup tests and devin comments
1 parent 3aa4897 commit 28a3ff7

11 files changed

Lines changed: 156 additions & 82 deletions

File tree

packages/wrangler/src/__tests__/artifacts.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
12
import { http, HttpResponse } from "msw";
23
import { afterEach, beforeEach, describe, it, vi } from "vitest";
34
import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
@@ -34,7 +35,7 @@ describe("artifacts", () => {
3435
clearDialogs();
3536
msw.resetHandlers();
3637
});
37-
38+
runInTempDir();
3839
describe("namespaces", () => {
3940
it("should show help for namespace commands", async ({ expect }) => {
4041
await runWrangler("artifacts namespaces --help");

packages/wrangler/src/__tests__/banner.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ describe("wrangler banner", () => {
1616
expect(std.out).toMatchInlineSnapshot(`
1717
"
1818
⛅️ wrangler x.x.x
19-
──────────────────
20-
Using profile: testing"
19+
──────────────────"
2120
`);
2221
});
2322

@@ -39,8 +38,7 @@ describe("wrangler banner", () => {
3938
expect(std.out).toMatchInlineSnapshot(`
4039
"
4140
⛅️ wrangler x.x.x
42-
──────────────────
43-
Using profile: testing"
41+
──────────────────"
4442
`);
4543
});
4644
});

packages/wrangler/src/__tests__/containers/registries.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { http, HttpResponse } from "msw";
2+
import { runInTempDir } from "@cloudflare/workers-utils/test-helpers";
23
import { afterEach, beforeEach, describe, it, vi } from "vitest";
34
import { mockAccount } from "../cloudchamber/utils";
45
import { mockAccountId, mockApiToken } from "../helpers/mock-account-id";
@@ -19,6 +20,7 @@ import { runWrangler } from "../helpers/run-wrangler";
1920
import type { ExpectStatic } from "vitest";
2021

2122
describe("containers registries --help", () => {
23+
runInTempDir();
2224
const std = mockConsoleMethods();
2325

2426
it("should help", async ({ expect }) => {
@@ -46,6 +48,7 @@ describe("containers registries --help", () => {
4648
});
4749

4850
describe("containers registries configure", () => {
51+
runInTempDir();
4952
const std = mockConsoleMethods();
5053
const { setIsTTY } = useMockIsTTY();
5154
const cliStd = mockCLIOutput();
@@ -569,6 +572,7 @@ describe("containers registries configure", () => {
569572
});
570573

571574
describe("containers registries list", () => {
575+
runInTempDir();
572576
const std = mockConsoleMethods();
573577
const cliStd = mockCLIOutput();
574578
mockAccountId();
@@ -601,8 +605,7 @@ describe("containers registries list", () => {
601605
expect(std.out).toMatchInlineSnapshot(`
602606
"
603607
⛅️ wrangler x.x.x
604-
──────────────────
605-
Using profile: testing"
608+
──────────────────"
606609
`);
607610
expect(cliStd.stdout).toMatchInlineSnapshot(`
608611
"╭ List configured container registries
@@ -636,6 +639,7 @@ describe("containers registries list", () => {
636639
});
637640

638641
describe("containers registries delete", () => {
642+
runInTempDir();
639643
const cliStd = mockCLIOutput();
640644
const std = mockConsoleMethods();
641645
const { setIsTTY } = useMockIsTTY();
@@ -698,7 +702,6 @@ describe("containers registries delete", () => {
698702
"
699703
⛅️ wrangler x.x.x
700704
──────────────────
701-
Using profile: testing
702705
? Are you sure you want to delete the registry credentials for 123456789012.dkr.ecr.us-west-2.amazonaws.com? This action cannot be undone.
703706
🤖 Using fallback value in non-interactive context: yes"
704707
`);
@@ -837,6 +840,7 @@ describe("containers registries delete", () => {
837840
});
838841

839842
describe("containers registries credentials", () => {
843+
runInTempDir();
840844
const { setIsTTY } = useMockIsTTY();
841845
const std = mockConsoleMethods();
842846
mockAccountId();

packages/wrangler/src/__tests__/profile.test.ts

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ import { mockConsoleMethods } from "./helpers/mock-console";
2020
import { mockConfirm } from "./helpers/mock-dialogs";
2121
import { useMockIsTTY } from "./helpers/mock-istty";
2222
import { mockOAuthFlow } from "./helpers/mock-oauth-flow";
23-
import { msw, mswSuccessOauthHandlers } from "./helpers/msw";
23+
import {
24+
msw,
25+
mswSuccessOauthHandlers,
26+
mswSuccessUserHandlers,
27+
} from "./helpers/msw";
2428
import { runWrangler } from "./helpers/run-wrangler";
2529

2630
describe("Profile", () => {
@@ -451,6 +455,65 @@ describe("Profile", () => {
451455
});
452456
});
453457

458+
describe("active profile in banner", () => {
459+
beforeEach(() => {
460+
msw.use(...mswSuccessUserHandlers);
461+
});
462+
463+
it("should print the active profile under the banner", async ({
464+
expect,
465+
}) => {
466+
writeAuthConfigFile(
467+
{
468+
oauth_token: "token",
469+
refresh_token: "refresh",
470+
expiration_time: "2030-01-01T00:00:00Z",
471+
},
472+
"work"
473+
);
474+
setActiveProfile("work");
475+
476+
await runWrangler("whoami");
477+
478+
expect(std.out).toContain("Using profile: work");
479+
});
480+
481+
it("should not print the active profile with --json", async ({
482+
expect,
483+
}) => {
484+
writeAuthConfigFile(
485+
{
486+
oauth_token: "token",
487+
refresh_token: "refresh",
488+
expiration_time: "2030-01-01T00:00:00Z",
489+
},
490+
"work"
491+
);
492+
setActiveProfile("work");
493+
494+
await runWrangler("whoami --json");
495+
496+
expect(std.out).not.toContain("Using profile");
497+
const output = JSON.parse(std.out);
498+
expect(output.loggedIn).toBe(true);
499+
expect(output.profile).toBe("work");
500+
});
501+
502+
it("should not print profile line when using default profile", async ({
503+
expect,
504+
}) => {
505+
writeAuthConfigFile({
506+
oauth_token: "token",
507+
refresh_token: "refresh",
508+
expiration_time: "2030-01-01T00:00:00Z",
509+
});
510+
511+
await runWrangler("whoami");
512+
513+
expect(std.out).not.toContain("Using profile");
514+
});
515+
});
516+
454517
describe("wrangler profiles unset", () => {
455518
it("should reset the active profile to default", async ({ expect }) => {
456519
writeAuthConfigFile(

packages/wrangler/src/__tests__/user.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -301,9 +301,9 @@ describe("User", () => {
301301
await runWrangler("login");
302302

303303
expect(std.err).toMatchInlineSnapshot(`
304-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mYou are currently using the auth profile "work[0m
304+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mYou are currently using the auth profile "work".[0m
305305
306-
". If you want to create a new auth profile, run \`wrangler profiles create <profile name>\`.
306+
If you want to create a new auth profile, run \`wrangler profiles create <profile name>\`.
307307
If you want to switch to an existing auth profile, run \`wrangler profiles set <profile name>\`.
308308
309309
@@ -321,9 +321,9 @@ describe("User", () => {
321321
await runWrangler("login");
322322

323323
expect(std.err).toMatchInlineSnapshot(`
324-
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mThis directory is bound to the auth profile "work[0m
324+
"[31mX [41;31m[[41;97mERROR[41;31m][0m [1mThis directory is bound to the auth profile "work"[0m
325325
326-
" If you want to create a new auth profile, run \`wrangler profiles create <profile name>\`.
326+
If you want to create a new auth profile, run \`wrangler profiles create <profile name>\`.
327327
If you want to switch to an existing auth profile, run \`wrangler profiles set <profile name>\`.
328328
329329

packages/wrangler/src/__tests__/vpc.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -865,6 +865,7 @@ describe("extractPortFromHostname", () => {
865865
});
866866

867867
describe("hostname validation", () => {
868+
runInTempDir();
868869
it("should accept valid hostnames", ({ expect }) => {
869870
expect(() => validateHostname("api.example.com")).not.toThrow();
870871
expect(() => validateHostname("localhost")).not.toThrow();

packages/wrangler/src/core/register-yargs-command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function createHandler(def: InternalCommandDefinition, argv: string[]) {
147147
typeof shouldPrintBanner !== "function" || bannerEnabled;
148148

149149
if (!getWranglerHideBanner()) {
150-
if ((def.behaviour?.printActiveProfile ?? true) && shouldPrintBanner) {
150+
if ((def.behaviour?.printActiveProfile ?? true) && bannerEnabled) {
151151
const activeProfile = getActiveProfileName();
152152
if (activeProfile !== "default") {
153153
logger.log(`Using profile: ${chalk.blue(activeProfile)}`);

packages/wrangler/src/user/commands.ts

Lines changed: 4 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
1-
import { CommandLineArgsError, UserError } from "@cloudflare/workers-utils";
1+
import { UserError } from "@cloudflare/workers-utils";
22
import { readConfig } from "../config";
33
import { createCommand, createNamespace } from "../core/create-command";
44
import { logger } from "../logger";
55
import * as metrics from "../metrics";
6+
import { handleScopesArgs, loginArgs } from "./login-shared";
67
import { getActiveProfileName, getProfileForDirectory } from "./profiles";
78
import {
89
getAuthFromEnv,
910
getOAuthTokenFromLocalState,
10-
listScopes,
1111
login,
1212
logout,
13-
validateScopeKeys,
1413
type Scope,
1514
} from "./user";
1615
import { whoami } from "./whoami";
@@ -23,63 +22,6 @@ export type AuthTokenInfo =
2322
| { type: "api_token"; token: string }
2423
| { type: "api_key"; key: string; email: string };
2524

26-
export const loginArgs = {
27-
browser: {
28-
default: true,
29-
type: "boolean",
30-
describe: "Automatically open the OAuth link in a browser",
31-
},
32-
scopes: {
33-
describe: "Pick the set of applicable OAuth scopes when logging in",
34-
array: true,
35-
type: "string",
36-
requiresArg: true,
37-
},
38-
"scopes-list": {
39-
describe: "List all the available OAuth scopes with descriptions",
40-
},
41-
"callback-host": {
42-
describe:
43-
"Use the ip or host address for the temporary login callback server.",
44-
type: "string",
45-
requiresArg: false,
46-
default: "localhost",
47-
},
48-
"callback-port": {
49-
describe: "Use the port for the temporary login callback server.",
50-
type: "number",
51-
requiresArg: false,
52-
default: 8976,
53-
},
54-
} as const;
55-
56-
/**
57-
* Handle `--scopes-list` and `--scopes` validation.
58-
* Returns `true` when the caller should return early (scopes were listed or empty).
59-
*/
60-
export function handleScopesArgs(args: {
61-
scopesList?: unknown;
62-
scopes?: string[];
63-
}): boolean {
64-
if (args.scopesList) {
65-
listScopes();
66-
return true;
67-
}
68-
if (args.scopes) {
69-
if (args.scopes.length === 0) {
70-
listScopes();
71-
return true;
72-
}
73-
if (!validateScopeKeys(args.scopes)) {
74-
throw new CommandLineArgsError(
75-
`One of ${args.scopes} is not a valid authentication scope. Run "wrangler login --scopes-list" to see the valid scopes.`,
76-
{ telemetryMessage: "user login invalid scope" }
77-
);
78-
}
79-
}
80-
return false;
81-
}
82-
8325
export const loginCommand = createCommand({
8426
metadata: {
8527
description: "🔓 Login to Cloudflare",
@@ -102,8 +44,8 @@ export const loginCommand = createCommand({
10244
const currentProfile = getActiveProfileName();
10345
if (currentProfile !== "default") {
10446
const message = getProfileForDirectory()
105-
? `This directory is bound to the auth profile "${currentProfile}\n"`
106-
: `You are currently using the auth profile "${currentProfile}\n".`;
47+
? `This directory is bound to the auth profile "${currentProfile}"\n`
48+
: `You are currently using the auth profile "${currentProfile}".\n`;
10749
logger.error(
10850
message,
10951
"If you want to create a new auth profile, run `wrangler profiles create <profile name>`.\n",
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { CommandLineArgsError } from "@cloudflare/workers-utils";
2+
import { listScopes, validateScopeKeys } from "./user";
3+
4+
export const loginArgs = {
5+
browser: {
6+
default: true,
7+
type: "boolean",
8+
describe: "Automatically open the OAuth link in a browser",
9+
},
10+
scopes: {
11+
describe: "Pick the set of applicable OAuth scopes when logging in",
12+
array: true,
13+
type: "string",
14+
requiresArg: true,
15+
},
16+
"scopes-list": {
17+
describe: "List all the available OAuth scopes with descriptions",
18+
},
19+
"callback-host": {
20+
describe:
21+
"Use the ip or host address for the temporary login callback server.",
22+
type: "string",
23+
requiresArg: false,
24+
default: "localhost",
25+
},
26+
"callback-port": {
27+
describe: "Use the port for the temporary login callback server.",
28+
type: "number",
29+
requiresArg: false,
30+
default: 8976,
31+
},
32+
} as const;
33+
34+
/**
35+
* Handle `--scopes-list` and `--scopes` validation.
36+
* Returns `true` when the caller should return early (scopes were listed or empty).
37+
*/
38+
export function handleScopesArgs(args: {
39+
scopesList?: unknown;
40+
scopes?: string[];
41+
}): boolean {
42+
if (args.scopesList) {
43+
listScopes();
44+
return true;
45+
}
46+
if (args.scopes) {
47+
if (args.scopes.length === 0) {
48+
listScopes();
49+
return true;
50+
}
51+
if (!validateScopeKeys(args.scopes)) {
52+
throw new CommandLineArgsError(
53+
`One of ${args.scopes} is not a valid authentication scope. Run "wrangler login --scopes-list" to see the valid scopes.`,
54+
{ telemetryMessage: "user login invalid scope" }
55+
);
56+
}
57+
}
58+
return false;
59+
}

packages/wrangler/src/user/profiles.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { createCommand, createNamespace } from "../core/create-command";
1717
import { confirm } from "../dialogs";
1818
import { logger } from "../logger";
1919
import { getWranglerProfileFromEnv } from "./auth-variables";
20-
import { handleScopesArgs, loginArgs } from "./commands";
20+
import { handleScopesArgs, loginArgs } from "./login-shared";
2121
import {
2222
getAuthConfigFilePath,
2323
login,
@@ -492,8 +492,14 @@ export const profileDeleteCommand = createCommand({
492492
}
493493
},
494494
async handler(args) {
495-
const token = readAuthConfigFile(args.name).oauth_token;
496-
await revokeToken(token);
495+
const token = readAuthConfigFile(args.name).refresh_token;
496+
if (token) {
497+
await revokeToken(token);
498+
} else {
499+
logger.warn(
500+
`No refresh token found for profile "${args.name}". The token could not be revoked and may still be valid.`
501+
);
502+
}
497503
deleteProfile(args.name);
498504
logger.log(`✅ Deleted profile "${args.name}".\n`);
499505
},

0 commit comments

Comments
 (0)