-
Notifications
You must be signed in to change notification settings - Fork 735
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve login error handling #794
Conversation
🦋 Changeset detectedLatest commit: 0dd05c9 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
A wrangler prerelease is available for testing. You can install this latest build in your project with: npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/2196591553/npm-package-wrangler-794 You can reference the automatically updated head of this PR with: npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/794/npm-package-wrangler-794 Or you can use npx https://prerelease-registry.developers.workers.dev/runs/2196591553/npm-package-wrangler-794 dev path/to/script.js |
This probably deserves a test. |
That's fair 😅 |
e15fc15
to
3e62b63
Compare
At a lost on how to test this particular function. Will come back to it. |
@@ -48,6 +51,25 @@ describe("wrangler", () => { | |||
expiration_time: expect.any(String), | |||
}); | |||
}); | |||
|
|||
it.only("should confirm error message and output for failed login", async () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
forgot to remove the .only here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch
packages/wrangler/src/user.tsx
Outdated
|
||
if (tokenExhangeResErr !== undefined) { | ||
// We will throw the parsed error if it parsed correctly, otherwise we throw an unknown error. | ||
throw tokenExhangeResErr; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
throw tokenExhangeResErr; | |
throw typeof tokenExhangeResErr === 'string' ? new Error(tokenExhangeResErr): tokenExhangeResErr; |
I think you need to make this change to get the test not to hang (and to pass): diff --git a/packages/wrangler/src/__tests__/user.test.ts b/packages/wrangler/src/__tests__/user.test.ts
index b043048..3d8edb7 100644
--- a/packages/wrangler/src/__tests__/user.test.ts
+++ b/packages/wrangler/src/__tests__/user.test.ts
@@ -54,23 +54,19 @@ describe("wrangler", () => {
});
});
- it.only("should confirm error message and output for failed login", async () => {
- const accessTokenRequest = mockGrantAccessToken({ respondWith: "ok" });
+ it("should confirm error message and output for failed login", async () => {
+ mockOAuthServerCallback();
+ mockGrantAccessToken({ respondWith: "ok" });
mockGrantAuthorization({ respondWith: "failure" });
await runWrangler("login");
- expect(accessTokenRequest.actual.url).toEqual("");
- expect(accessTokenRequest.actual.method).toEqual("");
-
- expect(std.out).toMatchInlineSnapshot(``);
-
- expect(readAuthConfigFile()).toEqual<UserAuthConfig>({
- api_token: undefined,
- oauth_token: "test-access-token",
- refresh_token: "test-refresh-token",
- expiration_time: expect.any(String),
- });
+ expect(std.out).toMatchInlineSnapshot(`
+ "Attempting to login via OAuth...
+ Error: Consent denied. You must grant consent to Wrangler in order to login.
+ If you don't want to do this consider passing an API token via the \`CLOUDFLARE_API_TOKEN\` environment variable"
+ `);
+ expect(std.warn).toMatchInlineSnapshot(`""`);
});
});
|
@caass graciously helped me figure out how I caused hanging tests. It was silly workaround I was trying to make didn't play nicely with other pieces, now I am trying to mock the response for the token refresh to access function. |
c7d3051
to
ab4ef3b
Compare
respondWith: "badResponse", | ||
}); | ||
|
||
await expect(requireAuth({} as Config, false)).rejects.toThrowError(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the tip on how to get this workaround to work @petebacondarwin
ab4ef3b
to
4983812
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for driving this fix through @JacobMGEvans - not a simple part of the code base to test.
}); | ||
|
||
await expect(requireAuth({} as Config, false)).rejects.toThrowError(); | ||
expect(std.err).toContain( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use toMatchInlineSnapshot()
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it breaks in different environments, the callstack is slightly different but the overall Error is the same
04c5730
to
4af9ee0
Compare
import type { UserAuthConfig } from "../user"; | ||
|
||
describe("wrangler", () => { | ||
describe("User", () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
User
related tests
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A couple of NITs left in the tests.
…r in some situations. Added a fallback if `.json` fails to parse it will attempt `.text()` then throw result. If both attempts to parse fail it will throw an `UnknownError` with a message showing where it originated. should resolve #539
4af9ee0
to
0dd05c9
Compare
Error messaging from failed login would dump a
JSON.parse
error in some situations. Added a fallback if.json
fails to parse it will attempt.text()
then throw result. If both attempts to parse fail it will throw anUnknownError
with a message showing where it originated.resolves #539