Skip to content

Commit 4dd026b

Browse files
V3 backport for #9799 (#9807)
* Better messaging for account owned tokens * Create thin-grapes-rescue.md * fix tests * Update packages/wrangler/src/user/whoami.ts Co-authored-by: Pete Bacon Darwin <[email protected]> * further fixups --------- Co-authored-by: Pete Bacon Darwin <[email protected]> Co-authored-by: Peter Bacon Darwin <[email protected]>
1 parent 6e09672 commit 4dd026b

File tree

6 files changed

+160
-68
lines changed

6 files changed

+160
-68
lines changed

.changeset/thin-grapes-rescue.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"wrangler": patch
3+
---
4+
5+
Better messaging for account owned tokens in `wrangler whoami`

packages/wrangler/src/__tests__/cfetch-utils.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ describe("throwFetchError", () => {
7575
["message one", "message two"]
7676
)
7777
);
78+
}),
79+
http.get("*/user/tokens/verify", () => {
80+
return HttpResponse.json(createFetchResult([]));
7881
})
7982
);
8083
await expect(runWrangler("whoami")).rejects.toMatchObject({
@@ -135,6 +138,9 @@ describe("throwFetchError", () => {
135138
["message one", "message two"]
136139
)
137140
);
141+
}),
142+
http.get("*/user/tokens/verify", () => {
143+
return HttpResponse.json(createFetchResult([]));
138144
})
139145
);
140146
await expect(runWrangler("whoami")).rejects.toMatchInlineSnapshot(
@@ -182,6 +188,9 @@ describe("throwFetchError", () => {
182188
{ code: 10001, message: "error 1" },
183189
],
184190
});
191+
}),
192+
http.get("*/user/tokens/verify", () => {
193+
return HttpResponse.json(createFetchResult([]));
185194
})
186195
);
187196
await expect(runWrangler("whoami")).rejects.toMatchObject({

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,10 @@ describe("deploy", () => {
444444
})
445445
);
446446
}
447-
)
447+
),
448+
http.get("*/user/tokens/verify", () => {
449+
return HttpResponse.json(createFetchResult([]));
450+
})
448451
);
449452

450453
await expect(
@@ -463,8 +466,8 @@ describe("deploy", () => {
463466
Please ensure it has the correct permissions for this operation.
464467
465468
Getting User settings...
466-
ℹ️ The API Token is read from the CLOUDFLARE_API_TOKEN in your environment.
467-
👋 You are logged in with an API Token, associated with the email [email protected].
469+
ℹ️ The API Token is read from the CLOUDFLARE_API_TOKEN environment variable.
470+
👋 You are logged in with an User API Token, associated with the email [email protected].
468471
┌───────────────┬────────────┐
469472
│ Account Name │ Account ID │
470473
├───────────────┼────────────┤

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

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { mockAccountId, mockApiToken } from "./helpers/mock-account-id";
66
import { mockConsoleMethods } from "./helpers/mock-console";
77
import { clearDialogs, mockConfirm } from "./helpers/mock-dialogs";
88
import { useMockIsTTY } from "./helpers/mock-istty";
9-
import { msw } from "./helpers/msw";
9+
import { createFetchResult, msw } from "./helpers/msw";
1010
import { runInTempDir } from "./helpers/run-in-tmp";
1111
import { runWrangler } from "./helpers/run-wrangler";
1212

@@ -55,7 +55,10 @@ describe("sentry", () => {
5555
return HttpResponse.error();
5656
},
5757
{ once: true }
58-
)
58+
),
59+
http.get("*/user/tokens/verify", () => {
60+
return HttpResponse.json(createFetchResult([]));
61+
})
5962
);
6063
await expect(runWrangler("whoami")).rejects.toMatchInlineSnapshot(
6164
`[TypeError: Failed to fetch]`
@@ -100,7 +103,10 @@ describe("sentry", () => {
100103
return HttpResponse.error();
101104
},
102105
{ once: true }
103-
)
106+
),
107+
http.get("*/user/tokens/verify", () => {
108+
return HttpResponse.json(createFetchResult([]));
109+
})
104110
);
105111
mockConfirm({
106112
text: "Would you like to report this error to Cloudflare? Wrangler's output and the error details will be shared with the Wrangler team to help us diagnose and fix the issue.",
@@ -126,7 +132,10 @@ describe("sentry", () => {
126132
return HttpResponse.error();
127133
},
128134
{ once: true }
129-
)
135+
),
136+
http.get("*/user/tokens/verify", () => {
137+
return HttpResponse.json(createFetchResult([]));
138+
})
130139
);
131140
mockConfirm({
132141
text: "Would you like to report this error to Cloudflare? Wrangler's output and the error details will be shared with the Wrangler team to help us diagnose and fix the issue.",
@@ -259,22 +268,22 @@ describe("sentry", () => {
259268
Object {
260269
"colno": 0,
261270
"context_line": "",
262-
"filename": "/wrangler/packages/wrangler/src/core/register-yargs-command.ts",
271+
"filename": "/wrangler/packages/wrangler/src/user/commands.ts",
263272
"function": "",
264273
"in_app": false,
265274
"lineno": 0,
266-
"module": "register-yargs-command.ts",
275+
"module": "commands.ts",
267276
"post_context": Array [],
268277
"pre_context": Array [],
269278
},
270279
Object {
271280
"colno": 0,
272281
"context_line": "",
273-
"filename": "/wrangler/packages/wrangler/src/user/commands.ts",
282+
"filename": "/wrangler/packages/wrangler/src/user/whoami.ts",
274283
"function": "",
275284
"in_app": false,
276285
"lineno": 0,
277-
"module": "commands.ts",
286+
"module": "whoami.ts",
278287
"post_context": Array [],
279288
"pre_context": Array [],
280289
},
@@ -303,22 +312,22 @@ describe("sentry", () => {
303312
Object {
304313
"colno": 0,
305314
"context_line": "",
306-
"filename": "/wrangler/packages/wrangler/src/user/whoami.ts",
315+
"filename": "/wrangler/packages/wrangler/src/cfetch/index.ts",
307316
"function": "",
308317
"in_app": false,
309318
"lineno": 0,
310-
"module": "whoami.ts",
319+
"module": "index.ts",
311320
"post_context": Array [],
312321
"pre_context": Array [],
313322
},
314323
Object {
315324
"colno": 0,
316325
"context_line": "",
317-
"filename": "/wrangler/packages/wrangler/src/cfetch/index.ts",
326+
"filename": "/wrangler/packages/wrangler/src/cfetch/internal.ts",
318327
"function": "",
319328
"in_app": false,
320329
"lineno": 0,
321-
"module": "index.ts",
330+
"module": "internal.ts",
322331
"post_context": Array [],
323332
"pre_context": Array [],
324333
},
@@ -336,11 +345,11 @@ describe("sentry", () => {
336345
Object {
337346
"colno": 0,
338347
"context_line": "",
339-
"filename": "/wrangler/packages/wrangler/src/cfetch/internal.ts",
348+
"filename": "node:internal/process/task_queues",
340349
"function": "",
341350
"in_app": false,
342351
"lineno": 0,
343-
"module": "internal.ts",
352+
"module": "task_queues",
344353
"post_context": Array [],
345354
"pre_context": Array [],
346355
},

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

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ describe("getUserInfo()", () => {
5050
},
5151
{ once: true }
5252
),
53+
http.get(
54+
"*/user/tokens/verify",
55+
() => {
56+
return HttpResponse.json(createFetchResult([]));
57+
},
58+
{ once: true }
59+
),
5360
http.get(
5461
"*/accounts",
5562
({ request }) => {
@@ -71,10 +78,18 @@ describe("getUserInfo()", () => {
7178
});
7279
it("should say it's using an API token when one is set", async () => {
7380
vi.stubEnv("CLOUDFLARE_API_TOKEN", "123456789");
74-
81+
msw.use(
82+
http.get(
83+
"*/user/tokens/verify",
84+
() => {
85+
return HttpResponse.json(createFetchResult([]));
86+
},
87+
{ once: true }
88+
)
89+
);
7590
const userInfo = await getUserInfo();
7691
expect(userInfo).toEqual({
77-
authType: "API Token",
92+
authType: "User API Token",
7893
apiToken: "123456789",
7994
8095
accounts: [
@@ -147,14 +162,14 @@ describe("getUserInfo()", () => {
147162
await getUserInfo();
148163

149164
expect(std.warn).toMatchInlineSnapshot(`
150-
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mIt looks like you have used Wrangler v1's \`config\` command to login with an API token.[0m
165+
"[33m▲ [43;33m[[43;30mWARNING[43;33m][0m [1mIt looks like you have used Wrangler v1's \`config\` command to login with an API token.[0m
151166
152-
This is no longer supported in the current version of Wrangler.
153-
If you wish to authenticate via an API token then please set the \`CLOUDFLARE_API_TOKEN\`
154-
environment variable.
167+
This is no longer supported in the current version of Wrangler.
168+
If you wish to authenticate via an API token then please set the \`CLOUDFLARE_API_TOKEN\`
169+
environment variable.
155170
156-
"
157-
`);
171+
"
172+
`);
158173
});
159174
});
160175

@@ -184,22 +199,22 @@ describe("whoami", () => {
184199
);
185200
await runWrangler(`whoami --account "account-2"`);
186201
expect(std.out).toMatchInlineSnapshot(`
187-
"Getting User settings...
188-
👋 You are logged in with an OAuth Token, associated with the email [email protected].
189-
┌───────────────┬────────────┐
190-
│ Account Name │ Account ID │
191-
├───────────────┼────────────┤
192-
│ Account One │ account-1 │
193-
├───────────────┼────────────┤
194-
│ Account Two │ account-2 │
195-
├───────────────┼────────────┤
196-
│ Account Three │ account-3 │
197-
└───────────────┴────────────┘
198-
🔓 Token Permissions: If scopes are missing, you may need to logout and re-login.
199-
Scope (Access)
200-
🎢 Membership roles in \\"Account Two\\": Contact account super admin to change your permissions.
201-
- Test role"
202-
`);
202+
"Getting User settings...
203+
👋 You are logged in with an OAuth Token, associated with the email [email protected].
204+
┌───────────────┬────────────┐
205+
│ Account Name │ Account ID │
206+
├───────────────┼────────────┤
207+
│ Account One │ account-1 │
208+
├───────────────┼────────────┤
209+
│ Account Two │ account-2 │
210+
├───────────────┼────────────┤
211+
│ Account Three │ account-3 │
212+
└───────────────┴────────────┘
213+
🔓 Token Permissions: If scopes are missing, you may need to logout and re-login.
214+
Scope (Access)
215+
🎢 Membership roles in \\"Account Two\\": Contact account super admin to change your permissions.
216+
- Test role"
217+
`);
203218
});
204219

205220
it("should display membership error on authentication error 10000", async () => {
@@ -218,20 +233,20 @@ describe("whoami", () => {
218233
);
219234
await runWrangler(`whoami --account "account-2"`);
220235
expect(std.out).toMatchInlineSnapshot(`
221-
"Getting User settings...
222-
👋 You are logged in with an OAuth Token, associated with the email [email protected].
223-
┌───────────────┬────────────┐
224-
│ Account Name │ Account ID │
225-
├───────────────┼────────────┤
226-
│ Account One │ account-1 │
227-
├───────────────┼────────────┤
228-
│ Account Two │ account-2 │
229-
├───────────────┼────────────┤
230-
│ Account Three │ account-3 │
231-
└───────────────┴────────────┘
232-
🔓 Token Permissions: If scopes are missing, you may need to logout and re-login.
233-
Scope (Access)
234-
🎢 Unable to get membership roles. Make sure you have permissions to read the account. Are you missing the \`User->Memberships->Read\` permission?"
235-
`);
236+
"Getting User settings...
237+
👋 You are logged in with an OAuth Token, associated with the email [email protected].
238+
┌───────────────┬────────────┐
239+
│ Account Name │ Account ID │
240+
├───────────────┼────────────┤
241+
│ Account One │ account-1 │
242+
├───────────────┼────────────┤
243+
│ Account Two │ account-2 │
244+
├───────────────┼────────────┤
245+
│ Account Three │ account-3 │
246+
└───────────────┴────────────┘
247+
🔓 Token Permissions: If scopes are missing, you may need to logout and re-login.
248+
Scope (Access)
249+
🎢 Unable to get membership roles. Make sure you have permissions to read the account. Are you missing the \`User->Memberships->Read\` permission?"
250+
`);
236251
});
237252
});

0 commit comments

Comments
 (0)