|
| 1 | +/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved. |
| 2 | + * |
| 3 | + * This software is licensed under the Apache License, Version 2.0 (the |
| 4 | + * "License") as published by the Apache Software Foundation. |
| 5 | + * |
| 6 | + * You may not use this file except in compliance with the License. You may |
| 7 | + * obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software |
| 10 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | + * License for the specific language governing permissions and limitations |
| 13 | + * under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +/* |
| 17 | + * Imports |
| 18 | + */ |
| 19 | + |
| 20 | +const assert = require("assert"); |
| 21 | +const puppeteer = require("puppeteer"); |
| 22 | +const { |
| 23 | + getTestEmail, |
| 24 | + getTestPhoneNumber, |
| 25 | + setInputValues, |
| 26 | + submitForm, |
| 27 | + toggleSignInSignUp, |
| 28 | + waitForSTElement, |
| 29 | +} = require("../../../test/exampleTestHelpers"); |
| 30 | + |
| 31 | +const SuperTokensNode = require("../backend/node_modules/supertokens-node"); |
| 32 | +const Session = require("../backend/node_modules/supertokens-node/recipe/session"); |
| 33 | +const EmailVerification = require("../backend/node_modules/supertokens-node/recipe/emailverification"); |
| 34 | +const EmailPassword = require("../backend/node_modules/supertokens-node/recipe/emailpassword"); |
| 35 | +const Passwordless = require("../backend/node_modules/supertokens-node/recipe/passwordless"); |
| 36 | + |
| 37 | +// Run the tests in a DOM environment. |
| 38 | +require("jsdom-global")(); |
| 39 | + |
| 40 | +const apiDomain = "http://localhost:3001"; |
| 41 | +const websiteDomain = "http://localhost:3000"; |
| 42 | +SuperTokensNode.init({ |
| 43 | + supertokens: { |
| 44 | + // We are running these tests without running a local ST instance |
| 45 | + connectionURI: "https://try.supertokens.com", |
| 46 | + }, |
| 47 | + appInfo: { |
| 48 | + // These largely shouldn't matter except for creating links which we can change anyway |
| 49 | + apiDomain: apiDomain, |
| 50 | + websiteDomain: websiteDomain, |
| 51 | + appName: "testNode", |
| 52 | + }, |
| 53 | + recipeList: [ |
| 54 | + EmailVerification.init({ mode: "OPTIONAL" }), |
| 55 | + EmailPassword.init(), |
| 56 | + Session.init(), |
| 57 | + Passwordless.init({ |
| 58 | + contactMethod: "EMAIL_OR_PHONE", |
| 59 | + flowType: "USER_INPUT_CODE_AND_MAGIC_LINK", |
| 60 | + }), |
| 61 | + ], |
| 62 | +}); |
| 63 | + |
| 64 | +describe("SuperTokens Example Basic tests", function () { |
| 65 | + let browser; |
| 66 | + let page; |
| 67 | + const email = getTestEmail(); |
| 68 | + const phoneNumber = getTestPhoneNumber(); |
| 69 | + const testOTP = "1234"; |
| 70 | + const testPW = "Str0ngP@ssw0rd"; |
| 71 | + |
| 72 | + before(async function () { |
| 73 | + browser = await puppeteer.launch({ |
| 74 | + args: ["--no-sandbox", "--disable-setuid-sandbox"], |
| 75 | + headless: true, |
| 76 | + }); |
| 77 | + page = await browser.newPage(); |
| 78 | + }); |
| 79 | + |
| 80 | + after(async function () { |
| 81 | + await browser.close(); |
| 82 | + }); |
| 83 | + |
| 84 | + describe("Email Password test", function () { |
| 85 | + it("Successful signup with multiple credentials", async function () { |
| 86 | + await Promise.all([page.goto(websiteDomain), page.waitForNavigation({ waitUntil: "networkidle0" })]); |
| 87 | + |
| 88 | + // redirected to /auth |
| 89 | + await toggleSignInSignUp(page); |
| 90 | + await setInputValues(page, [ |
| 91 | + { name: "email", value: email }, |
| 92 | + { name: "password", value: testPW }, |
| 93 | + ]); |
| 94 | + await submitForm(page); |
| 95 | + |
| 96 | + // Redirected to email verification screen |
| 97 | + await waitForSTElement(page, "[data-supertokens~='sendVerifyEmailIcon']"); |
| 98 | + const userId = await page.evaluate(() => window.__supertokensSessionRecipe.getUserId()); |
| 99 | + |
| 100 | + // Attempt reloading Home |
| 101 | + await Promise.all([page.goto(websiteDomain), page.waitForNavigation({ waitUntil: "networkidle0" })]); |
| 102 | + await waitForSTElement(page, "[data-supertokens~='sendVerifyEmailIcon']"); |
| 103 | + |
| 104 | + // Create a new token and use it (we don't have access to the originally sent one) |
| 105 | + const tokenInfo = await EmailVerification.createEmailVerificationToken( |
| 106 | + "public", |
| 107 | + SuperTokensNode.convertToRecipeUserId(userId), |
| 108 | + email |
| 109 | + ); |
| 110 | + await page.goto(`${websiteDomain}/auth/verify-email?token=${tokenInfo.token}`); |
| 111 | + |
| 112 | + await submitForm(page); |
| 113 | + |
| 114 | + await page.waitForSelector(".sessionButton"); |
| 115 | + |
| 116 | + await page.goto(`${websiteDomain}/link`); |
| 117 | + |
| 118 | + await page.waitForSelector(".emailpassword.login-method"); |
| 119 | + await checkLoginMethods(page, [{ loginMethod: "emailpassword", email }]); |
| 120 | + |
| 121 | + const input = await page.waitForSelector("[type=tel]"); |
| 122 | + await input.type(phoneNumber); |
| 123 | + |
| 124 | + await page.click("[type=tel]+button"); |
| 125 | + await page.waitForSelector(".passwordless.login-method"); |
| 126 | + |
| 127 | + await checkLoginMethods(page, [ |
| 128 | + { loginMethod: "emailpassword", email }, |
| 129 | + { loginMethod: "passwordless", phoneNumber }, |
| 130 | + ]); |
| 131 | + |
| 132 | + await page.evaluate(() => __supertokensSessionRecipe.signOut({})); |
| 133 | + await new Promise((res) => setTimeout(res, 200)); |
| 134 | + |
| 135 | + await page.goto(`${websiteDomain}/auth?rid=passwordless`); |
| 136 | + |
| 137 | + await setInputValues(page, [{ name: "emailOrPhone", value: email }]); |
| 138 | + await submitForm(page); |
| 139 | + |
| 140 | + await waitForSTElement(page, "[name=userInputCode]"); |
| 141 | + |
| 142 | + const loginAttemptInfo = JSON.parse( |
| 143 | + await page.evaluate(() => localStorage.getItem("supertokens-passwordless-loginAttemptInfo")) |
| 144 | + ); |
| 145 | + |
| 146 | + await Passwordless.createNewCodeForDevice({ |
| 147 | + tenantId: "public", |
| 148 | + deviceId: loginAttemptInfo.deviceId, |
| 149 | + userInputCode: testOTP, |
| 150 | + }); |
| 151 | + |
| 152 | + await setInputValues(page, [{ name: "userInputCode", value: testOTP }]); |
| 153 | + await submitForm(page); |
| 154 | + const callApiBtn = await page.waitForSelector(".sessionButton"); |
| 155 | + let setAlertContent; |
| 156 | + let alertContent = new Promise((res) => (setAlertContent = res)); |
| 157 | + page.on("dialog", async (dialog) => { |
| 158 | + setAlertContent(dialog.message()); |
| 159 | + await dialog.dismiss(); |
| 160 | + }); |
| 161 | + await callApiBtn.click(); |
| 162 | + |
| 163 | + const alertText = await alertContent; |
| 164 | + assert(alertText.startsWith("Session Information:")); |
| 165 | + const sessionInfo = JSON.parse(alertText.replace(/^Session Information:/, "")); |
| 166 | + assert.strictEqual(sessionInfo.userId, userId); |
| 167 | + |
| 168 | + await page.goto(`${websiteDomain}/link`); |
| 169 | + |
| 170 | + await page.waitForSelector(".emailpassword.login-method"); |
| 171 | + await checkLoginMethods(page, [ |
| 172 | + { loginMethod: "emailpassword", email }, |
| 173 | + { loginMethod: "passwordless", phoneNumber }, |
| 174 | + { loginMethod: "passwordless", email }, |
| 175 | + ]); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
| 179 | + |
| 180 | +async function checkLoginMethods(page, expectedLoginMethods) { |
| 181 | + assert.strictEqual(await page.url(), `${websiteDomain}/link`); |
| 182 | + const methodDivs = await page.$$(".login-method"); |
| 183 | + |
| 184 | + for (const div of methodDivs) { |
| 185 | + const classNameProp = await div.getProperty("className"); |
| 186 | + const className = await classNameProp.jsonValue(); |
| 187 | + const method = className.split(" ")[0]; |
| 188 | + const contactInfo = (await (await div.$(".contactInfo")).evaluate((el) => el.textContent)).trim(); |
| 189 | + |
| 190 | + assert( |
| 191 | + expectedLoginMethods.some( |
| 192 | + (m) => |
| 193 | + m.loginMethod === method && |
| 194 | + (m.email === undefined || contactInfo === `Email: ${m.email}`) && |
| 195 | + (m.phoneNumber === undefined || contactInfo === `Phone number: ${m.phoneNumber}`) |
| 196 | + ) |
| 197 | + ); |
| 198 | + } |
| 199 | + assert.strictEqual(methodDivs.length, expectedLoginMethods.length); |
| 200 | +} |
0 commit comments