Merged PR 2317: Auth stabilisation: add reason-coded guards to MyPortal loaders
Adds incremental auth/session hardening across myportal loader paths (loadMyPortalAppealPage, searchresults, addresssearchresults) with explicit guard ordering and reason-coded diagnostics for missing session, missing session identity, missing cookie identity, contact/account lookup failures, and upstream dependency failures. Includes targeted Phase22 loader guard tests and keeps redirect behaviour policy unchanged. Related work items: #23020
This commit is contained in:
@@ -44,12 +44,27 @@ export async function loadMyPortalAppealPage(ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const session = await getSession(ctx);
|
const session = await getSession(ctx);
|
||||||
if (!session || !session?.user?.id || !session?.user?.email) {
|
if (!session) {
|
||||||
consoleLogger({
|
consoleLogger({
|
||||||
name: "MyPortalAppealLoaderMissingSession",
|
name: "MyPortalAppealLoaderAuthGuard",
|
||||||
|
reasonCode: "NO_SESSION",
|
||||||
message:
|
message:
|
||||||
"loadMyPortalAppealPage missing session identity; redirecting to signin",
|
"loadMyPortalAppealPage missing session; redirecting to signin"
|
||||||
hasSession: !!session,
|
});
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: "/auth/signin",
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!session?.user?.id || !session?.user?.email) {
|
||||||
|
consoleLogger({
|
||||||
|
name: "MyPortalAppealLoaderAuthGuard",
|
||||||
|
reasonCode: "NO_SESSION_USER",
|
||||||
|
message:
|
||||||
|
"loadMyPortalAppealPage missing session user identity; redirecting to signin",
|
||||||
hasUserId: !!session?.user?.id,
|
hasUserId: !!session?.user?.id,
|
||||||
hasUserEmail: !!session?.user?.email
|
hasUserEmail: !!session?.user?.email
|
||||||
});
|
});
|
||||||
@@ -68,7 +83,8 @@ export async function loadMyPortalAppealPage(ctx) {
|
|||||||
|
|
||||||
if (!loggedInUser) {
|
if (!loggedInUser) {
|
||||||
consoleLogger({
|
consoleLogger({
|
||||||
name: "MyPortalAppealLoaderMissingPinsUserCookie",
|
name: "MyPortalAppealLoaderAuthGuard",
|
||||||
|
reasonCode: "NO_PINSUSER_COOKIE",
|
||||||
message:
|
message:
|
||||||
"loadMyPortalAppealPage missing pinsUser cookie; redirecting to signin"
|
"loadMyPortalAppealPage missing pinsUser cookie; redirecting to signin"
|
||||||
});
|
});
|
||||||
@@ -107,6 +123,7 @@ export async function loadMyPortalAppealPage(ctx) {
|
|||||||
} catch (_error) {
|
} catch (_error) {
|
||||||
consoleLogger({
|
consoleLogger({
|
||||||
name: "MyPortalAppealLoaderDependencyFailure",
|
name: "MyPortalAppealLoaderDependencyFailure",
|
||||||
|
reasonCode: "UPSTREAM_FAILURE",
|
||||||
message:
|
message:
|
||||||
"loadMyPortalAppealPage dependency fetch failed; redirecting to /myportal",
|
"loadMyPortalAppealPage dependency fetch failed; redirecting to /myportal",
|
||||||
appealType: query?.appealtypes,
|
appealType: query?.appealtypes,
|
||||||
@@ -121,6 +138,23 @@ export async function loadMyPortalAppealPage(ctx) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!accountDetails || accountDetails?.errorCode) {
|
||||||
|
consoleLogger({
|
||||||
|
name: "MyPortalAppealLoaderContactGuard",
|
||||||
|
reasonCode: "CONTACT_LOOKUP_FAILED",
|
||||||
|
message:
|
||||||
|
"loadMyPortalAppealPage account contact lookup failed; redirecting to signin",
|
||||||
|
hasAccountDetails: !!accountDetails,
|
||||||
|
hasErrorCode: !!accountDetails?.errorCode
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: "/auth/signin",
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Optional: awaiting-submission view setup when query.key exists
|
// Optional: awaiting-submission view setup when query.key exists
|
||||||
let awaitingSubmissionFromBlob = null;
|
let awaitingSubmissionFromBlob = null;
|
||||||
if (Object.prototype.hasOwnProperty.call(query, "key")) {
|
if (Object.prototype.hasOwnProperty.call(query, "key")) {
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import useTranslation from "next-translate/useTranslation";
|
|||||||
import Head from "next/head";
|
import Head from "next/head";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { connect } from "react-redux";
|
import { connect } from "react-redux";
|
||||||
import { getIP } from "../../actions/core/logger";
|
import { consoleLogger, getIP } from "../../actions/core/logger";
|
||||||
import { getPersonalAccount } from "../../actions/services/accountService";
|
import { getPersonalAccount } from "../../actions/services/accountService";
|
||||||
import { getPortalModuleDetails } from "../../actions/services/caseService";
|
import { getPortalModuleDetails } from "../../actions/services/caseService";
|
||||||
import {
|
import {
|
||||||
@@ -142,8 +142,6 @@ export const getServerSideProps = wrapper.getServerSideProps(
|
|||||||
(store) => async (ctx) => {
|
(store) => async (ctx) => {
|
||||||
const { query, req } = ctx;
|
const { query, req } = ctx;
|
||||||
getIP(req);
|
getIP(req);
|
||||||
const searchResultsObj = await getAddressSearch(query);
|
|
||||||
const searchDetailsObj = searchResultsObj;
|
|
||||||
const showLoginCheck = process.env.SHOWLOGIN || false;
|
const showLoginCheck = process.env.SHOWLOGIN || false;
|
||||||
const showReps = process.env.SHOWREPRESENTATIONS || false;
|
const showReps = process.env.SHOWREPRESENTATIONS || false;
|
||||||
store.dispatch(setShowReps(showReps, showLoginCheck));
|
store.dispatch(setShowReps(showReps, showLoginCheck));
|
||||||
@@ -155,7 +153,12 @@ export const getServerSideProps = wrapper.getServerSideProps(
|
|||||||
let thisSession = await getSession(ctx);
|
let thisSession = await getSession(ctx);
|
||||||
|
|
||||||
if (!thisSession) {
|
if (!thisSession) {
|
||||||
console.log("not has sesssion.......");
|
consoleLogger({
|
||||||
|
name: "MyPortalAddressSearchResultsAuthGuard",
|
||||||
|
reasonCode: "NO_SESSION",
|
||||||
|
message:
|
||||||
|
"myportal addresssearchresults loader missing session; redirecting to signin"
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
destination: "/auth/signin",
|
destination: "/auth/signin",
|
||||||
@@ -163,20 +166,91 @@ export const getServerSideProps = wrapper.getServerSideProps(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
|
if (!thisSession?.user?.id || !thisSession?.user?.email) {
|
||||||
|
consoleLogger({
|
||||||
|
name: "MyPortalAddressSearchResultsAuthGuard",
|
||||||
|
reasonCode: "NO_SESSION_USER",
|
||||||
|
message:
|
||||||
|
"myportal addresssearchresults loader missing session user identity; redirecting to signin",
|
||||||
|
hasSessionUserId: !!thisSession?.user?.id,
|
||||||
|
hasSessionUserEmail: !!thisSession?.user?.email
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: "/auth/signin",
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!loggedInUser) {
|
||||||
|
consoleLogger({
|
||||||
|
name: "MyPortalAddressSearchResultsAuthGuard",
|
||||||
|
reasonCode: "NO_PINSUSER_COOKIE",
|
||||||
|
message:
|
||||||
|
"myportal addresssearchresults loader missing pinsUser cookie; redirecting to signin"
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: "/auth/signin",
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
thisSession != false &&
|
thisSession != false &&
|
||||||
store.dispatch(setContainerID(thisSession.user.id));
|
store.dispatch(setContainerID(thisSession.user.id));
|
||||||
|
|
||||||
const [accountDetails, searchResultsObj, watchedCases] =
|
let accountDetails;
|
||||||
await Promise.all([
|
let searchResultsObj;
|
||||||
getPersonalAccount(loggedInUser),
|
let watchedCases;
|
||||||
getAdvancedSearch(query),
|
let searchDetailsObj;
|
||||||
getWatchedCases(loggedInUser)
|
let watchedCasesDetails;
|
||||||
]);
|
|
||||||
|
|
||||||
const [searchDetailsObj, watchedCasesDetails] = await Promise.all([
|
try {
|
||||||
getSearchDetails(searchResultsObj),
|
[accountDetails, searchResultsObj, watchedCases] =
|
||||||
getDetails(watchedCases, "myWatchedCases")
|
await Promise.all([
|
||||||
]);
|
getPersonalAccount(loggedInUser),
|
||||||
|
getAdvancedSearch(query),
|
||||||
|
getWatchedCases(loggedInUser)
|
||||||
|
]);
|
||||||
|
|
||||||
|
[searchDetailsObj, watchedCasesDetails] = await Promise.all([
|
||||||
|
getSearchDetails(searchResultsObj),
|
||||||
|
getDetails(watchedCases, "myWatchedCases")
|
||||||
|
]);
|
||||||
|
} catch (error) {
|
||||||
|
consoleLogger({
|
||||||
|
name: "MyPortalAddressSearchResultsAuthGuard",
|
||||||
|
reasonCode: "UPSTREAM_FAILURE",
|
||||||
|
message:
|
||||||
|
"myportal addresssearchresults loader dependency call failed; redirecting to signin",
|
||||||
|
error: error?.message
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: "/auth/signin",
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!accountDetails || accountDetails?.errorCode) {
|
||||||
|
consoleLogger({
|
||||||
|
name: "MyPortalAddressSearchResultsAuthGuard",
|
||||||
|
reasonCode: "CONTACT_LOOKUP_FAILED",
|
||||||
|
message:
|
||||||
|
"myportal addresssearchresults loader account lookup failed; redirecting to signin",
|
||||||
|
hasAccountDetails: !!accountDetails,
|
||||||
|
hasErrorCode: !!accountDetails?.errorCode
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: "/auth/signin",
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
store.dispatch(setAccountDetails(accountDetails));
|
store.dispatch(setAccountDetails(accountDetails));
|
||||||
store.dispatch(setSearchResults(searchResultsObj));
|
store.dispatch(setSearchResults(searchResultsObj));
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import useTranslation from "next-translate/useTranslation";
|
|||||||
import Head from "next/head";
|
import Head from "next/head";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import { connect } from "react-redux";
|
import { connect } from "react-redux";
|
||||||
import { getIP } from "../../actions/core/logger";
|
import { consoleLogger, getIP } from "../../actions/core/logger";
|
||||||
import {
|
import {
|
||||||
getPersonalAccount,
|
getPersonalAccount,
|
||||||
getPortalLogin
|
getPortalLogin
|
||||||
@@ -90,7 +90,12 @@ export const getServerSideProps = wrapper.getServerSideProps(
|
|||||||
store.dispatch(setShowReps(showReps, showLoginCheck));
|
store.dispatch(setShowReps(showReps, showLoginCheck));
|
||||||
|
|
||||||
if (!thisSession) {
|
if (!thisSession) {
|
||||||
console.log("not has sesssion.......");
|
consoleLogger({
|
||||||
|
name: "MyPortalSearchResultsAuthGuard",
|
||||||
|
reasonCode: "NO_SESSION",
|
||||||
|
message:
|
||||||
|
"myportal searchresults loader missing session; redirecting to signin"
|
||||||
|
});
|
||||||
return {
|
return {
|
||||||
redirect: {
|
redirect: {
|
||||||
destination: "/auth/signin",
|
destination: "/auth/signin",
|
||||||
@@ -98,12 +103,77 @@ export const getServerSideProps = wrapper.getServerSideProps(
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
let loggedInUser = await getPortalLogin(thisSession.user.email);
|
if (!thisSession?.user?.id || !thisSession?.user?.email) {
|
||||||
|
consoleLogger({
|
||||||
|
name: "MyPortalSearchResultsAuthGuard",
|
||||||
|
reasonCode: "NO_SESSION_USER",
|
||||||
|
message:
|
||||||
|
"myportal searchresults loader missing session user identity; redirecting to signin",
|
||||||
|
hasSessionUserId: !!thisSession?.user?.id,
|
||||||
|
hasSessionUserEmail: !!thisSession?.user?.email
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: "/auth/signin",
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!cookies?.pinsUser) {
|
||||||
|
consoleLogger({
|
||||||
|
name: "MyPortalSearchResultsAuthGuard",
|
||||||
|
reasonCode: "NO_PINSUSER_COOKIE",
|
||||||
|
message:
|
||||||
|
"myportal searchresults loader missing pinsUser cookie; redirecting to signin"
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: "/auth/signin",
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
let loggedInUser;
|
||||||
|
try {
|
||||||
|
const portalLogin = await getPortalLogin(
|
||||||
|
thisSession.user.email
|
||||||
|
);
|
||||||
|
loggedInUser = portalLogin?.value?.[0]?.contactid;
|
||||||
|
} catch (error) {
|
||||||
|
consoleLogger({
|
||||||
|
name: "MyPortalSearchResultsAuthGuard",
|
||||||
|
reasonCode: "UPSTREAM_FAILURE",
|
||||||
|
message:
|
||||||
|
"myportal searchresults loader portal login lookup failed; redirecting to signin",
|
||||||
|
error: error?.message
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: "/auth/signin",
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!loggedInUser) {
|
||||||
|
consoleLogger({
|
||||||
|
name: "MyPortalSearchResultsAuthGuard",
|
||||||
|
reasonCode: "CONTACT_LOOKUP_FAILED",
|
||||||
|
message:
|
||||||
|
"myportal searchresults loader missing CRM contact id; redirecting to signin"
|
||||||
|
});
|
||||||
|
return {
|
||||||
|
redirect: {
|
||||||
|
destination: "/auth/signin",
|
||||||
|
permanent: false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
//console.log("sssss", searchResultsObj);
|
//console.log("sssss", searchResultsObj);
|
||||||
|
|
||||||
loggedInUser = loggedInUser.value[0].contactid;
|
|
||||||
|
|
||||||
const watchedCases = await getWatchedCases(loggedInUser);
|
const watchedCases = await getWatchedCases(loggedInUser);
|
||||||
|
|
||||||
//console.log("sssss", loggedInUser);
|
//console.log("sssss", loggedInUser);
|
||||||
|
|||||||
@@ -13,6 +13,8 @@ const runRepresentationBuildRepsArrRulesTests = require("./representation-build-
|
|||||||
const runSessionClientTests = require("./session-client-behaviour.test.cjs");
|
const runSessionClientTests = require("./session-client-behaviour.test.cjs");
|
||||||
const runNewAppealLoaderGuardTests = require("./newappeal-loader-guards.test.cjs");
|
const runNewAppealLoaderGuardTests = require("./newappeal-loader-guards.test.cjs");
|
||||||
const runMyPortalLoaderGuardTests = require("./myportal-loader-guards.test.cjs");
|
const runMyPortalLoaderGuardTests = require("./myportal-loader-guards.test.cjs");
|
||||||
|
const runMyPortalSearchResultsLoaderGuardTests = require("./myportal-searchresults-loader-guards.test.cjs");
|
||||||
|
const runMyPortalAddressSearchResultsLoaderGuardTests = require("./myportal-addresssearchresults-loader-guards.test.cjs");
|
||||||
const runRepresentationLoaderGuardTests = require("./representation-loader-guards.test.cjs");
|
const runRepresentationLoaderGuardTests = require("./representation-loader-guards.test.cjs");
|
||||||
|
|
||||||
const run = async () => {
|
const run = async () => {
|
||||||
@@ -31,6 +33,8 @@ const run = async () => {
|
|||||||
await runSessionClientTests();
|
await runSessionClientTests();
|
||||||
await runNewAppealLoaderGuardTests();
|
await runNewAppealLoaderGuardTests();
|
||||||
await runMyPortalLoaderGuardTests();
|
await runMyPortalLoaderGuardTests();
|
||||||
|
await runMyPortalSearchResultsLoaderGuardTests();
|
||||||
|
await runMyPortalAddressSearchResultsLoaderGuardTests();
|
||||||
await runRepresentationLoaderGuardTests();
|
await runRepresentationLoaderGuardTests();
|
||||||
console.log("Phase 22 combined suite passed.");
|
console.log("Phase 22 combined suite passed.");
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -0,0 +1,149 @@
|
|||||||
|
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 loadMyPortalAddressSearchResultsPage = (overrides = {}) => {
|
||||||
|
const filePath = path.join(
|
||||||
|
__dirname,
|
||||||
|
"..",
|
||||||
|
"..",
|
||||||
|
"pages",
|
||||||
|
"myportal",
|
||||||
|
"addresssearchresults.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: "x@y.z" } }),
|
||||||
|
getPersonalAccount: async () => ({}),
|
||||||
|
getAddressSearch: async () => ({ value: [] }),
|
||||||
|
getAdvancedSearch: async () => ({ value: [] }),
|
||||||
|
getWatchedCases: async () => ({ value: [] }),
|
||||||
|
getSearchDetails: async () => ({ value: [] }),
|
||||||
|
getPortalModuleDetails: async () => ({}),
|
||||||
|
getFormCollectionByID: () => ({ LogicalCollectionName: "x" }),
|
||||||
|
setShowReps: () => ({}),
|
||||||
|
setSearch: () => ({}),
|
||||||
|
setContainerID: () => ({}),
|
||||||
|
setAccountDetails: () => ({}),
|
||||||
|
setSearchResults: () => ({}),
|
||||||
|
setSearchDetails: () => ({}),
|
||||||
|
setWatchedCases: () => ({}),
|
||||||
|
setWatchedCasesDetails: () => ({}),
|
||||||
|
setLoggedInUserId: () => ({}),
|
||||||
|
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("addresssearchresults redirects to signin when session is missing", async () => {
|
||||||
|
const mod = loadMyPortalAddressSearchResultsPage({
|
||||||
|
getSession: async () => null
|
||||||
|
});
|
||||||
|
const result = await mod.getServerSideProps({
|
||||||
|
query: { q: "CAS" },
|
||||||
|
req: { cookies: { pinsUser: "contact-1" } }
|
||||||
|
});
|
||||||
|
assert.strictEqual(result.redirect.destination, "/auth/signin");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("addresssearchresults redirects to signin when session user identity is missing", async () => {
|
||||||
|
const mod = loadMyPortalAddressSearchResultsPage({
|
||||||
|
getSession: async () => ({ user: { id: "u-1" } })
|
||||||
|
});
|
||||||
|
const result = await mod.getServerSideProps({
|
||||||
|
query: { q: "CAS" },
|
||||||
|
req: { cookies: { pinsUser: "contact-1" } }
|
||||||
|
});
|
||||||
|
assert.strictEqual(result.redirect.destination, "/auth/signin");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("addresssearchresults redirects to signin when pinsUser cookie is missing", async () => {
|
||||||
|
const mod = loadMyPortalAddressSearchResultsPage();
|
||||||
|
const result = await mod.getServerSideProps({
|
||||||
|
query: { q: "CAS" },
|
||||||
|
req: { cookies: {} }
|
||||||
|
});
|
||||||
|
assert.strictEqual(result.redirect.destination, "/auth/signin");
|
||||||
|
});
|
||||||
|
|
||||||
|
test("addresssearchresults classifies contact lookup failure and redirects to signin", async () => {
|
||||||
|
let capturedLog = null;
|
||||||
|
const mod = loadMyPortalAddressSearchResultsPage({
|
||||||
|
getPersonalAccount: async () => ({ errorCode: "CONTACT_MISSING" }),
|
||||||
|
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("addresssearchresults classifies upstream failure and redirects to signin", async () => {
|
||||||
|
let capturedLog = null;
|
||||||
|
const mod = loadMyPortalAddressSearchResultsPage({
|
||||||
|
getAdvancedSearch: async () => {
|
||||||
|
throw new Error("search 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-addresssearchresults-loader-guards tests passed (${passed}/${tests.length}).`
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports = run;
|
||||||
|
|
||||||
|
if (require.main === module) {
|
||||||
|
run().catch((error) => {
|
||||||
|
console.error(error);
|
||||||
|
process.exit(1);
|
||||||
|
});
|
||||||
|
}
|
||||||
@@ -57,6 +57,19 @@ test("myportal loader redirects to signin when session is missing", async () =>
|
|||||||
assert.strictEqual(result.redirect.destination, "/auth/signin");
|
assert.strictEqual(result.redirect.destination, "/auth/signin");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("myportal loader redirects to signin when session user identity is missing", async () => {
|
||||||
|
const mod = loadMyPortalLoader({
|
||||||
|
getSession: async () => ({ user: { id: "u-1" } })
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await mod.loadMyPortalAppealPage({
|
||||||
|
query: { appealtypes: "s78", apt: "1", casereference: "CAS-1" },
|
||||||
|
req: { cookies: { pinsUser: "contact-1" } }
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.strictEqual(result.redirect.destination, "/auth/signin");
|
||||||
|
});
|
||||||
|
|
||||||
test("myportal loader redirects to signin when pinsUser cookie is missing", async () => {
|
test("myportal loader redirects to signin when pinsUser cookie is missing", async () => {
|
||||||
const mod = loadMyPortalLoader();
|
const mod = loadMyPortalLoader();
|
||||||
|
|
||||||
@@ -99,6 +112,24 @@ test("myportal loader redirects to myportal when dependency fetch fails", async
|
|||||||
assert.strictEqual(result.redirect.destination, "/myportal");
|
assert.strictEqual(result.redirect.destination, "/myportal");
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("myportal loader classifies account lookup failure and redirects to signin", async () => {
|
||||||
|
let capturedLog = null;
|
||||||
|
const mod = loadMyPortalLoader({
|
||||||
|
getPersonalAccount: async () => ({ errorCode: "CRM_CONTACT_MISSING" }),
|
||||||
|
consoleLogger: (entry) => {
|
||||||
|
capturedLog = entry;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const result = await mod.loadMyPortalAppealPage({
|
||||||
|
query: { appealtypes: "s78", apt: "1", casereference: "CAS-1" },
|
||||||
|
req: { cookies: { pinsUser: "contact-1" } }
|
||||||
|
});
|
||||||
|
|
||||||
|
assert.strictEqual(result.redirect.destination, "/auth/signin");
|
||||||
|
assert.strictEqual(capturedLog.reasonCode, "CONTACT_LOOKUP_FAILED");
|
||||||
|
});
|
||||||
|
|
||||||
const run = async () => {
|
const run = async () => {
|
||||||
let passed = 0;
|
let passed = 0;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,146 @@
|
|||||||
|
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: () => ({}),
|
||||||
|
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({ getSession: async () => null });
|
||||||
|
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({
|
||||||
|
getSession: async () => ({ user: { id: "u-1" } })
|
||||||
|
});
|
||||||
|
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();
|
||||||
|
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);
|
||||||
|
});
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user