Skip to content

Commit

Permalink
Add tests for default signin feature
Browse files Browse the repository at this point in the history
  • Loading branch information
amitbadala committed Nov 1, 2023
1 parent 0333dbf commit fa3b6f7
Showing 1 changed file with 88 additions and 0 deletions.
88 changes: 88 additions & 0 deletions test/end-to-end/signin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "[email protected]",
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: "[email protected]" },
{ 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 () {
Expand Down

0 comments on commit fa3b6f7

Please sign in to comment.