Skip to content

Commit

Permalink
Add webauthn mock with testContext and more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
deepjyoti30-st committed Jan 23, 2025
1 parent e1eab89 commit e9694cc
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
17 changes: 15 additions & 2 deletions examples/for-tests/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import UserRoles from "supertokens-auth-react/recipe/userroles";
import MultiFactorAuth from "supertokens-auth-react/recipe/multifactorauth";
import TOTP from "supertokens-auth-react/recipe/totp";
import OAuth2Provider from "supertokens-auth-react/recipe/oauth2provider";
import STGeneralError from "supertokens-web-js/lib/build/error";

import axios from "axios";
import { useSessionContext } from "supertokens-auth-react/recipe/session";
Expand Down Expand Up @@ -429,7 +430,7 @@ if (enabledRecipes.includes("passwordless") || enabledRecipes.includes("thirdpar
recipeList = [getPasswordlessConfigs(testContext), ...recipeList];
}
if (enabledRecipes.includes("webauthn")) {
recipeList = [getWebauthnConfigs(), ...recipeList];
recipeList = [getWebauthnConfigs(testContext), ...recipeList];
}

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

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

// We will throw an error if it is asked for.
if (throwWebauthnError) {
throw new STGeneralError("TEST ERROR");
}

// Return error status if the user passed that.
if (webauthnErrorStatus) {
return {
status: webauthnErrorStatus,
};
}

// We are mocking the popup since it's not possible to
// test the webauthn popup.
return {
Expand Down
2 changes: 2 additions & 0 deletions examples/for-tests/src/testContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ export function getTestContext() {
signoutOnSessionNotExists: localStorage.getItem("signoutOnSessionNotExists") === "true",
disableRedirectionAfterSuccessfulSignInUp:
localStorage.getItem("disableRedirectionAfterSuccessfulSignInUp") === "true",
throwWebauthnError: localStorage.getItem("throwWebauthnError") === "true",
webauthnErrorStatus: localStorage.getItem("webauthnErrorStatus") || undefined,
};
return ret;
}
Expand Down
30 changes: 29 additions & 1 deletion test/end-to-end/webauthn.signup.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe("SuperTokens Webauthn SignUp", () => {
assert.deepStrictEqual(headerText, "Create a passkey");
assert.strictEqual(emailText, email);
});
it("should successfully signup the user", async () => {
it("should successfully signup the user and redirect", async () => {
const email = await getTestEmail();
await tryWebauthnSignUp(page, email);

Expand All @@ -83,5 +83,33 @@ describe("SuperTokens Webauthn SignUp", () => {
"ST_LOGS SUPERTOKENS GET_REDIRECTION_URL TO_AUTH",
]);
});
it("should show recoverable error in the same view", async () => {
// Set the error to be thrown
await page.evaluateOnNewDocument(() => {
localStorage.setItem("webauthnErrorStatus", "FAILED_TO_REGISTER_USER");
});

const email = await getTestEmail();
await tryWebauthnSignUp(page, email);

// We should be in the confirmation page now.
await submitForm(page);

await waitForSTElement(page, "[data-supertokens~='passkeyRecoverableErrorContainer']");
});
it("should show something went wrong on general error", async () => {
// Set the error to be thrown
await page.evaluateOnNewDocument(() => {
localStorage.setItem("throwWebauthnError", "true");
});

const email = await getTestEmail();
await tryWebauthnSignUp(page, email);

// We should be in the confirmation page now.
await submitForm(page);

await waitForSTElement(page, "[data-supertokens~='somethingWentWrongContainer']");
});
});
});

0 comments on commit e9694cc

Please sign in to comment.