Files
pedwfrontend/tests/phase22/myportal-searchresults-loader-guards.test.cjs
Robert Bond c2ae6964f9 Merged PR 2318: auth stabilisation: extract shared myportal auth guard helper
auth stabilisation: extract shared myportal auth guard helper

ntroduces a small shared SSR helper (resolveMyPortalAuthContext) to standardize common myportal auth/session guards (session presence, session user identity, and pinsUser cookie) with preserved reason-coded diagnostics and signin redirect behavior. Migrates exactly two loaders (pages/myportal/searchresults.js, pages/myportal/addresssearchresults.js) to use the helper while keeping loader-specific UPSTREAM_FAILURE and CONTACT_LOOKUP_FAILED logic unchanged.

Related work items: #23020
2026-05-14 10:38:45 +00:00

165 lines
5.3 KiB
JavaScript

const assert = require("assert");
const fs = require("fs");
const path = require("path");
const vm = require("vm");
const tests = [];
const test = (name, fn) => tests.push({ name, fn });
const loadMyPortalSearchResultsPage = (overrides = {}) => {
const filePath = path.join(
__dirname,
"..",
"..",
"pages",
"myportal",
"searchresults.js"
);
let source = fs.readFileSync(filePath, "utf8");
source = source.replace(/import[\s\S]*?from\s+"[^"]+";\n?/g, "");
source = source.slice(source.indexOf("export const getServerSideProps"));
source = source.replace(
/export\s+const\s+getServerSideProps\s*=\s*/,
"const getServerSideProps = "
);
source = source.replace(/const mapStateToProps[\s\S]*$/, "");
source += "\nmodule.exports = { getServerSideProps };\n";
const context = {
module: { exports: {} },
exports: {},
getIP: () => {},
consoleLogger: () => {},
getSession: async () => ({
user: { id: "u-1", email: "test@example.com" }
}),
getPortalLogin: async () => ({ value: [{ contactid: "contact-1" }] }),
getPersonalAccount: async () => ({}),
getWatchedCases: async () => ({ value: [] }),
getPortalModuleDetails: async () => ({}),
getFormCollectionByID: () => ({ LogicalCollectionName: "x" }),
setShowReps: () => ({}),
setAccountDetails: () => ({}),
setSearch: () => ({}),
setWatchedCases: () => ({}),
setWatchedCasesDetails: () => ({}),
setLoggedInUserId: () => ({}),
setContainerID: () => ({}),
resolveMyPortalAuthContext: async (ctx) => ({
ok: true,
session: { user: { id: "u-1", email: "test@example.com" } },
pinsUser: ctx?.req?.cookies?.pinsUser
}),
wrapper: {
getServerSideProps: (factory) => async (ctx) => {
const store = { dispatch: () => {} };
return factory(store)(ctx);
}
},
process: { env: {} },
console,
...overrides
};
vm.runInNewContext(source, context, { filename: filePath });
return context.module.exports;
};
test("myportal searchresults redirects to signin when session is missing", async () => {
const mod = loadMyPortalSearchResultsPage({
resolveMyPortalAuthContext: async () => ({
ok: false,
redirect: { destination: "/auth/signin", permanent: false }
})
});
const result = await mod.getServerSideProps({
query: { q: "CAS" },
req: { cookies: { pinsUser: "contact-1" } }
});
assert.strictEqual(result.redirect.destination, "/auth/signin");
});
test("myportal searchresults redirects to signin when session user identity is missing", async () => {
const mod = loadMyPortalSearchResultsPage({
resolveMyPortalAuthContext: async () => ({
ok: false,
redirect: { destination: "/auth/signin", permanent: false }
})
});
const result = await mod.getServerSideProps({
query: { q: "CAS" },
req: { cookies: { pinsUser: "contact-1" } }
});
assert.strictEqual(result.redirect.destination, "/auth/signin");
});
test("myportal searchresults redirects to signin when pinsUser cookie is missing", async () => {
const mod = loadMyPortalSearchResultsPage({
resolveMyPortalAuthContext: async () => ({
ok: false,
redirect: { destination: "/auth/signin", permanent: false }
})
});
const result = await mod.getServerSideProps({
query: { q: "CAS" },
req: { cookies: {} }
});
assert.strictEqual(result.redirect.destination, "/auth/signin");
});
test("myportal searchresults classifies contact lookup failure and redirects to signin", async () => {
let capturedLog = null;
const mod = loadMyPortalSearchResultsPage({
getPortalLogin: async () => ({ value: [] }),
consoleLogger: (entry) => {
capturedLog = entry;
}
});
const result = await mod.getServerSideProps({
query: { q: "CAS" },
req: { cookies: { pinsUser: "contact-1" } }
});
assert.strictEqual(result.redirect.destination, "/auth/signin");
assert.strictEqual(capturedLog.reasonCode, "CONTACT_LOOKUP_FAILED");
});
test("myportal searchresults classifies upstream failure and redirects to signin", async () => {
let capturedLog = null;
const mod = loadMyPortalSearchResultsPage({
getPortalLogin: async () => {
throw new Error("crm unavailable");
},
consoleLogger: (entry) => {
capturedLog = entry;
}
});
const result = await mod.getServerSideProps({
query: { q: "CAS" },
req: { cookies: { pinsUser: "contact-1" } }
});
assert.strictEqual(result.redirect.destination, "/auth/signin");
assert.strictEqual(capturedLog.reasonCode, "UPSTREAM_FAILURE");
});
const run = async () => {
let passed = 0;
for (const currentTest of tests) {
await currentTest.fn();
passed += 1;
}
console.log(
`Phase 22 myportal-searchresults-loader-guards tests passed (${passed}/${tests.length}).`
);
};
module.exports = run;
if (require.main === module) {
run().catch((error) => {
console.error(error);
process.exit(1);
});
}