Skip to content

Commit

Permalink
test: add tests for sessionauth validation failure redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
porcellus committed Apr 12, 2024
1 parent 744d04e commit f3a42ff
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 3 deletions.
3 changes: 2 additions & 1 deletion examples/for-tests-react-16/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export function getApiDomain() {
export function getWebsiteDomain() {
const websitePort = process.env.REACT_APP_WEBSITE_PORT || 3031;
const websiteUrl = process.env.REACT_APP_WEBSITE_URL || `http://localhost:${websitePort}`;
return websiteUrl;

return getQueryParams("websiteDomain") ?? websiteUrl;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion examples/for-tests/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export function getApiDomain() {
export function getWebsiteDomain() {
const websitePort = process.env.REACT_APP_WEBSITE_PORT || 3031;
const websiteUrl = process.env.REACT_APP_WEBSITE_URL || `http://localhost:${websitePort}`;
return websiteUrl;
return getQueryParams("websiteDomain") ?? websiteUrl;
}

/*
Expand Down
2 changes: 1 addition & 1 deletion lib/build/index2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/ts/recipe/session/sessionAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ const SessionAuth: React.FC<PropsWithChildren<SessionAuthProps>> = ({ children,
if (failureRedirectInfo.redirectPath !== undefined) {
if (compareRedirectionURLToCurrentURL(failureRedirectInfo.redirectPath)) {
setContext(toSetContext);
return;
} else {
return await SuperTokens.getInstanceOrThrow().redirectToUrl(
failureRedirectInfo.redirectPath,
Expand Down
135 changes: 135 additions & 0 deletions test/end-to-end/signin-rrdv6.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import {
waitForText,
waitForSTElement,
backendBeforeEach,
getInvalidClaimsJSON,
} from "../helpers";
import fetch from "isomorphic-fetch";
import { SOMETHING_WENT_WRONG_ERROR } from "../constants";
Expand Down Expand Up @@ -535,6 +536,140 @@ describe("SuperTokens SignIn with react router dom v6", function () {
assert.deepStrictEqual(redirectUrl, "/dashboard");
});

it("Should not redirect to onFailureRedirections result if it's the current url and set the context", async function () {
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

// Set correct values.
await setInputValues(page, [
{ name: "email", value: "[email protected]" },
{ name: "password", value: "Str0ngP@ssw0rd" },
]);
await Promise.all([
submitFormReturnRequestAndResponse(page, SIGN_IN_API),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

await page.evaluate(() => {
const validator = window.UserRoleClaim.validators.includes("admin");
validator.onFailureRedirection = () => window.location.href;
window.setClaimValidators([validator]);
});

await page.waitForSelector(".invalidClaims");
assert.deepStrictEqual(await getInvalidClaimsJSON(page), [
{
id: "st-role",
reason: {
actualValue: [],
expectedToInclude: "admin",
message: "wrong value",
},
},
]);
});

it("Should not redirect to onFailureRedirections result if it's the current path and set the context", async function () {
await Promise.all([
page.goto(
`${TEST_CLIENT_BASE_URL}/auth?redirectToPath=${encodeURIComponent("/dashboard?test=value#asdf")}`
),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

// Set correct values.
await setInputValues(page, [
{ name: "email", value: "[email protected]" },
{ name: "password", value: "Str0ngP@ssw0rd" },
]);
await Promise.all([
submitFormReturnRequestAndResponse(page, SIGN_IN_API),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

await page.evaluate(() => {
const validator = window.UserRoleClaim.validators.includes("admin");
validator.onFailureRedirection = () => "/dashboard?test=value#asdf";
window.setClaimValidators([validator]);
});

await page.waitForSelector(".invalidClaims");
assert.deepStrictEqual(await getInvalidClaimsJSON(page), [
{
id: "st-role",
reason: {
actualValue: [],
expectedToInclude: "admin",
message: "wrong value",
},
},
]);
});

it("Should redirect to onFailureRedirections result if it's on another domain", async function () {
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

// Set correct values.
await setInputValues(page, [
{ name: "email", value: "[email protected]" },
{ name: "password", value: "Str0ngP@ssw0rd" },
]);
await Promise.all([
submitFormReturnRequestAndResponse(page, SIGN_IN_API),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

await page.evaluate(() => {
const validator = window.UserRoleClaim.validators.includes("admin");
validator.onFailureRedirection = () => "https://supertokens.com";
window.setClaimValidators([validator]);
});

await page.waitForNavigation({ waitUntil: "networkidle0" });

let href = await page.evaluate(() => window.location.href);
assert.strictEqual(href, "https://supertokens.com/");
});

it("Should redirect to onFailureRedirections result if it's a path and we are not on the websiteDomain", async function () {
await Promise.all([
page.goto(`${TEST_CLIENT_BASE_URL}/auth`),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

// Set correct values.
await setInputValues(page, [
{ name: "email", value: "[email protected]" },
{ name: "password", value: "Str0ngP@ssw0rd" },
]);

await Promise.all([
submitFormReturnRequestAndResponse(page, SIGN_IN_API),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

await Promise.all([
page.goto(
`${TEST_CLIENT_BASE_URL}/dashboard?websiteDomain=${encodeURIComponent("https://supertokens.com")}`
),
page.waitForNavigation({ waitUntil: "networkidle0" }),
]);

await page.evaluate(() => {
const validator = window.UserRoleClaim.validators.includes("admin");
validator.onFailureRedirection = () => "/test";
window.setClaimValidators([validator]);
});
await page.waitForNavigation({ waitUntil: "networkidle0" });
let href = await page.evaluate(() => window.location.href);
assert.strictEqual(href, "https://supertokens.com/test");
});

describe("Successful Sign In with redirect to, with EmailPasswordAuth", async function () {
it("First sign in", async function () {
consoleLogs = await clearBrowserCookiesWithoutAffectingConsole(page, consoleLogs);
Expand Down

0 comments on commit f3a42ff

Please sign in to comment.