Skip to content

Commit d81b685

Browse files
Add test for checking recovery with token
1 parent 706a671 commit d81b685

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,18 @@ export async function signUpAndSendRecoveryEmail(page, email) {
6363
await openRecoveryAccountPage(page, email, true);
6464
}
6565

66-
export async function getTokenFromEmail(page, email) {
66+
export async function getTokenFromEmail(email) {
6767
// Make an API call to get the token from the email
6868
// Since the email can contain special characters, we need to encode it
6969
const encodedEmail = encodeURIComponent(email);
7070
const response = await fetch(`${TEST_SERVER_BASE_URL}/test/webauthn/get-token?email=${encodedEmail}`);
7171
const data = await response.json();
7272
return data.token;
7373
}
74+
75+
export async function openRecoveryWithToken(page, token) {
76+
await Promise.all([
77+
page.goto(`${TEST_CLIENT_BASE_URL}/auth/webauthn/recover?token=${token}`),
78+
page.waitForNavigation({ waitUntil: "networkidle0" }),
79+
]);
80+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import fetch from "isomorphic-fetch";
2+
import { TEST_SERVER_BASE_URL } from "../constants";
3+
import {
4+
backendBeforeEach,
5+
setupBrowser,
6+
screenshotOnFailure,
7+
clearBrowserCookiesWithoutAffectingConsole,
8+
getTestEmail,
9+
waitForSTElement,
10+
} from "../helpers";
11+
import { openRecoveryWithToken, signUpAndSendRecoveryEmail, getTokenFromEmail } from "./webauthn.helpers";
12+
import assert from "assert";
13+
14+
describe("SuperTokens Webauthn Recover Account", () => {
15+
let browser;
16+
let page;
17+
let consoleLogs = [];
18+
let email;
19+
20+
before(async function () {
21+
await backendBeforeEach();
22+
23+
await fetch(`${TEST_SERVER_BASE_URL}/startst`, {
24+
method: "POST",
25+
}).catch(console.error);
26+
27+
browser = await setupBrowser();
28+
page = await browser.newPage();
29+
page.on("console", (consoleObj) => {
30+
const log = consoleObj.text();
31+
if (log.startsWith("ST_LOGS")) {
32+
consoleLogs.push(log);
33+
}
34+
});
35+
});
36+
37+
after(async function () {
38+
await browser.close();
39+
await fetch(`${TEST_SERVER_BASE_URL}/after`, {
40+
method: "POST",
41+
}).catch(console.error);
42+
43+
await fetch(`${TEST_SERVER_BASE_URL}/stopst`, {
44+
method: "POST",
45+
}).catch(console.error);
46+
});
47+
48+
afterEach(function () {
49+
return screenshotOnFailure(this, browser);
50+
});
51+
52+
beforeEach(async function () {
53+
consoleLogs = [];
54+
consoleLogs = await clearBrowserCookiesWithoutAffectingConsole(page, consoleLogs);
55+
56+
// Signup and send the recovery email
57+
email = await getTestEmail();
58+
await signUpAndSendRecoveryEmail(page, email);
59+
await new Promise((res) => setTimeout(res, 1000));
60+
});
61+
62+
describe("Recover Account Test", () => {
63+
it("should show the recovery token page", async () => {
64+
// Get the token from the email
65+
const token = await getTokenFromEmail(email);
66+
assert.ok(token);
67+
assert.strictEqual(token.length, 128);
68+
69+
// Use the token to recover the account
70+
await openRecoveryWithToken(page, token);
71+
72+
const continueWithPasskeyContainer = await waitForSTElement(page, "[data-supertokens~='headerTitle']");
73+
const headerText = await continueWithPasskeyContainer.evaluate((el) => el.textContent);
74+
75+
// Assert the text contains "Create a passkey"
76+
assert.deepStrictEqual(headerText, "Create a passkey");
77+
78+
assert.deepStrictEqual(consoleLogs, [
79+
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
80+
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
81+
"ST_LOGS WEBAUTHN OVERRIDE GET REGISTER OPTIONS WITH SIGN UP",
82+
"ST_LOGS WEBAUTHN OVERRIDE GET REGISTER OPTIONS",
83+
"ST_LOGS WEBAUTHN PRE_API_HOOKS REGISTER_OPTIONS",
84+
"ST_LOGS WEBAUTHN OVERRIDE REGISTER CREDENTIAL",
85+
"ST_LOGS WEBAUTHN OVERRIDE SIGN UP",
86+
"ST_LOGS WEBAUTHN PRE_API_HOOKS SIGN_UP",
87+
"ST_LOGS SESSION ON_HANDLE_EVENT SESSION_CREATED",
88+
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
89+
"ST_LOGS SUPERTOKENS GET_REDIRECTION_URL SUCCESS undefined",
90+
"ST_LOGS SESSION OVERRIDE GET_USER_ID",
91+
"ST_LOGS SESSION OVERRIDE SIGN_OUT",
92+
"ST_LOGS SESSION PRE_API_HOOKS SIGN_OUT",
93+
"ST_LOGS SESSION ON_HANDLE_EVENT SIGN_OUT",
94+
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
95+
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
96+
"ST_LOGS WEBAUTHN GET_REDIRECTION_URL SEND_RECOVERY_EMAIL",
97+
"ST_LOGS WEBAUTHN OVERRIDE GENERATE RECOVER ACCOUNT TOKEN",
98+
"ST_LOGS WEBAUTHN PRE_API_HOOKS GENERATE_RECOVER_ACCOUNT_TOKEN",
99+
"ST_LOGS SESSION OVERRIDE ADD_FETCH_INTERCEPTORS_AND_RETURN_MODIFIED_FETCH",
100+
"ST_LOGS SESSION OVERRIDE ADD_AXIOS_INTERCEPTORS",
101+
"ST_LOGS WEBAUTHN OVERRIDE GET REGISTER OPTIONS",
102+
"ST_LOGS WEBAUTHN OVERRIDE GET REGISTER OPTIONS",
103+
"ST_LOGS WEBAUTHN PRE_API_HOOKS REGISTER_OPTIONS",
104+
"ST_LOGS WEBAUTHN PRE_API_HOOKS REGISTER_OPTIONS",
105+
]);
106+
});
107+
});
108+
});

0 commit comments

Comments
 (0)