From fa3b6f7a8488fa2c84c3a838f1bd0fa22cdbaba3 Mon Sep 17 00:00:00 2001 From: amitbadala Date: Wed, 1 Nov 2023 18:26:09 +0530 Subject: [PATCH] Add tests for default signin feature --- test/end-to-end/signin.test.js | 88 ++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/test/end-to-end/signin.test.js b/test/end-to-end/signin.test.js index 86a204645..e9c4721f1 100644 --- a/test/end-to-end/signin.test.js +++ b/test/end-to-end/signin.test.js @@ -669,6 +669,94 @@ describe("SuperTokens SignIn", function () { }); }); }); + + describe("SignIn default field tests", function () { + it("Should contain email and password fields prefilled", async function () { + await page.evaluate(() => window.localStorage.setItem("SHOW_SIGNIN_DEFAULT_FIELDS", "YES")); + + await page.reload({ + waitUntil: "domcontentloaded", + }); + + const expectedDefaultValues = { + email: "abc@xyz.com", + password: "fakepassword123", + }; + + const emailInput = await getInputField(page, "email"); + const defaultEmail = await emailInput.evaluate((f) => f.value); + assert.strictEqual(defaultEmail, expectedDefaultValues["email"]); + + const passwordInput = await getInputField(page, "password"); + const defaultPassword = await passwordInput.evaluate((f) => f.value); + assert.strictEqual(defaultPassword, expectedDefaultValues["password"]); + }); + + it("Should have default values in the signin request payload", async function () { + await page.evaluate(() => window.localStorage.setItem("SHOW_SIGNIN_DEFAULT_FIELDS", "YES")); + + await page.reload({ + waitUntil: "domcontentloaded", + }); + + const expectedDefautlValues = [ + { id: "email", value: "abc@xyz.com" }, + { id: "password", value: "fakepassword123" }, + ]; + + let assertionError = null; + let interceptionPassed = false; + + const requestHandler = async (request) => { + if (request.url().includes(SIGN_IN_API) && request.method() === "POST") { + try { + const postData = JSON.parse(request.postData()); + expectedDefautlValues.forEach(({ id, value }) => { + let findFormData = postData.formFields.find((inputData) => inputData.id === id); + if (findFormData) { + assert.strictEqual(findFormData["value"], value, `Mismatch in payload for key: ${id}`); + } else { + throw new Error("Field not found in req.data"); + } + }); + interceptionPassed = true; + return request.respond({ + status: 200, + headers: { + "access-control-allow-origin": TEST_CLIENT_BASE_URL, + "access-control-allow-credentials": "true", + }, + body: JSON.stringify({ + status: "OK", + }), + }); + } catch (error) { + assertionError = error; // Store the error + } + } + return request.continue(); + }; + + await page.setRequestInterception(true); + page.on("request", requestHandler); + + try { + // Perform the button click and wait for all network activity to finish + await Promise.all([page.waitForNetworkIdle(), submitForm(page)]); + } finally { + page.off("request", requestHandler); + await page.setRequestInterception(false); + } + + if (assertionError) { + throw assertionError; + } + + if (!interceptionPassed) { + throw new Error("test failed"); + } + }); + }); }); describe("SuperTokens SignIn => Server Error", function () {