Skip to content

Commit e9694cc

Browse files
Add webauthn mock with testContext and more tests
1 parent e1eab89 commit e9694cc

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

examples/for-tests/src/App.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import UserRoles from "supertokens-auth-react/recipe/userroles";
1616
import MultiFactorAuth from "supertokens-auth-react/recipe/multifactorauth";
1717
import TOTP from "supertokens-auth-react/recipe/totp";
1818
import OAuth2Provider from "supertokens-auth-react/recipe/oauth2provider";
19+
import STGeneralError from "supertokens-web-js/lib/build/error";
1920

2021
import axios from "axios";
2122
import { useSessionContext } from "supertokens-auth-react/recipe/session";
@@ -429,7 +430,7 @@ if (enabledRecipes.includes("passwordless") || enabledRecipes.includes("thirdpar
429430
recipeList = [getPasswordlessConfigs(testContext), ...recipeList];
430431
}
431432
if (enabledRecipes.includes("webauthn")) {
432-
recipeList = [getWebauthnConfigs(), ...recipeList];
433+
recipeList = [getWebauthnConfigs(testContext), ...recipeList];
433434
}
434435

435436
if (emailVerificationMode !== "OFF") {
@@ -1151,7 +1152,7 @@ function setIsNewUserToStorage(recipeName, isNewRecipeUser) {
11511152
localStorage.setItem("isNewUserCheck", `${recipeName}-${isNewRecipeUser}`);
11521153
}
11531154

1154-
function getWebauthnConfigs() {
1155+
function getWebauthnConfigs({ throwWebauthnError, webauthnErrorStatus }) {
11551156
return Webauthn.init({
11561157
style: `
11571158
[data-supertokens~=container] {
@@ -1171,6 +1172,18 @@ function getWebauthnConfigs() {
11711172
registerCredentialWithSignUp(...args) {
11721173
log(`GET REGISTER OPTIONS WITH SIGN UP`);
11731174

1175+
// We will throw an error if it is asked for.
1176+
if (throwWebauthnError) {
1177+
throw new STGeneralError("TEST ERROR");
1178+
}
1179+
1180+
// Return error status if the user passed that.
1181+
if (webauthnErrorStatus) {
1182+
return {
1183+
status: webauthnErrorStatus,
1184+
};
1185+
}
1186+
11741187
// We are mocking the popup since it's not possible to
11751188
// test the webauthn popup.
11761189
return {

examples/for-tests/src/testContext.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export function getTestContext() {
2525
signoutOnSessionNotExists: localStorage.getItem("signoutOnSessionNotExists") === "true",
2626
disableRedirectionAfterSuccessfulSignInUp:
2727
localStorage.getItem("disableRedirectionAfterSuccessfulSignInUp") === "true",
28+
throwWebauthnError: localStorage.getItem("throwWebauthnError") === "true",
29+
webauthnErrorStatus: localStorage.getItem("webauthnErrorStatus") || undefined,
2830
};
2931
return ret;
3032
}

test/end-to-end/webauthn.signup.test.js

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe("SuperTokens Webauthn SignUp", () => {
7070
assert.deepStrictEqual(headerText, "Create a passkey");
7171
assert.strictEqual(emailText, email);
7272
});
73-
it("should successfully signup the user", async () => {
73+
it("should successfully signup the user and redirect", async () => {
7474
const email = await getTestEmail();
7575
await tryWebauthnSignUp(page, email);
7676

@@ -83,5 +83,33 @@ describe("SuperTokens Webauthn SignUp", () => {
8383
"ST_LOGS SUPERTOKENS GET_REDIRECTION_URL TO_AUTH",
8484
]);
8585
});
86+
it("should show recoverable error in the same view", async () => {
87+
// Set the error to be thrown
88+
await page.evaluateOnNewDocument(() => {
89+
localStorage.setItem("webauthnErrorStatus", "FAILED_TO_REGISTER_USER");
90+
});
91+
92+
const email = await getTestEmail();
93+
await tryWebauthnSignUp(page, email);
94+
95+
// We should be in the confirmation page now.
96+
await submitForm(page);
97+
98+
await waitForSTElement(page, "[data-supertokens~='passkeyRecoverableErrorContainer']");
99+
});
100+
it("should show something went wrong on general error", async () => {
101+
// Set the error to be thrown
102+
await page.evaluateOnNewDocument(() => {
103+
localStorage.setItem("throwWebauthnError", "true");
104+
});
105+
106+
const email = await getTestEmail();
107+
await tryWebauthnSignUp(page, email);
108+
109+
// We should be in the confirmation page now.
110+
await submitForm(page);
111+
112+
await waitForSTElement(page, "[data-supertokens~='somethingWentWrongContainer']");
113+
});
86114
});
87115
});

0 commit comments

Comments
 (0)