Files
HiveGrid-Wireframes/hivegrid-cloud/apps/web/app/routes/session-entry.host-aware.test.ts
T

111 lines
5.4 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
vi.mock("~/components/brand/Logo", () => ({ default: () => null }));
vi.mock("~/components/ui/banners/InfoBanner", () => ({ default: ({ children }: { children: React.ReactNode }) => children }));
vi.mock("~/components/ui/emptyState/EmptyState", () => ({ default: () => null }));
vi.mock("~/modules/users/components/LoginForm", () => ({ default: () => null }));
vi.mock("~/utils/data/useRootData", () => ({ useRootData: vi.fn() }));
vi.mock("~/utils/session.server", () => ({ getUserInfo: vi.fn(), createUserSession: vi.fn(), setLoggedUser: vi.fn() }));
vi.mock("~/locale/i18next.server", () => ({ getTranslations: vi.fn() }));
vi.mock("~/utils/seo/linkTags.server", () => ({ getLinkTags: vi.fn(() => []) }));
vi.mock("~/modules/users/services/AuthService", () => ({ default: { loginFromRequest: vi.fn() } }));
vi.mock("~/utils/db/appConfiguration.db.server", () => ({ getAppConfiguration: vi.fn() }));
vi.mock("~/utils/app/UserDisplayUtils", () => ({ default: { getTenantPrefix: vi.fn(() => "AC") } }));
vi.mock("~/utils/shared/colors/ColorBackgroundUtils", () => ({ default: { getBg700: vi.fn(() => "bg-primary") } }));
vi.mock("~/utils/shared/colors/ColorTextUtils", () => ({ default: { getText300: vi.fn(() => "text-primary-foreground") } }));
vi.mock("~/assets/brand/default", () => ({
iconLight: "icon-light.png",
iconDark: "icon-dark.png",
logoLight: "logo-light.png",
logoDark: "logo-dark.png",
}));
vi.mock("@toolctrl/db/auth-boundary.server", () => ({
getAuthUserWithoutPasswordById: vi.fn(),
getTenantById: vi.fn(),
getTenantsForUser: vi.fn(),
}));
vi.mock("~/utils/db/logs.db.server", () => ({ createLogLogin: vi.fn() }));
vi.mock("~/utils/db/tenants.db.server", () => ({ getTenant: vi.fn() }));
vi.mock("~/utils/db/users.db.server", () => ({
getUserByEmail: vi.fn(),
}));
vi.mock("company-email-validator", () => ({ isCompanyEmail: vi.fn() }));
vi.mock("~/utils/helpers/EmailHelper", () => ({ companyFromEmail: vi.fn() }));
vi.mock("~/utils/services/authService", () => ({ validateRegistration: vi.fn() }));
vi.mock("~/utils/app/UrlUtils", () => ({ default: { slugify: vi.fn((value: string) => value) } }));
vi.mock("~/utils/app/HostRouting.server", () => ({ getHostRoutingContextFromRequest: vi.fn(() => "unknown") }));
vi.mock("@toolctrl/config", async () => {
const actual = await vi.importActual<typeof import("@toolctrl/config")>("@toolctrl/config");
return {
...actual,
getDomainRoutingConfig: () => ({
appBaseDomain: "example.com",
webAppHost: "app.example.com",
adminAppHost: "admin.example.com",
apiHost: "api.example.com",
protocol: "https",
}),
};
});
import { sessionTenantDashboardPath } from "./app";
import { canonicalAdminLoginRedirect, canonicalAdminLoginRequestUrlForContext } from "./login";
import { loginDefaultRedirectTarget } from "./login.server";
const user = {
id: "user-1",
email: "user@example.com",
firstName: "User",
lastName: "Example",
avatar: null,
phone: null,
admin: null,
defaultTenantId: "tenant-1",
createdAt: new Date("2026-01-01T00:00:00.000Z"),
locale: null,
};
const adminUser = { ...user, admin: { userId: "user-1" } };
const tenant = { id: "tenant-1", slug: "acme" };
function expectCanonicalTenantDashboard(target: string, options: { absolute?: boolean } = {}) {
expect(target).toBe(options.absolute ? "https://app.example.com/acme/dashboard" : "/acme/dashboard");
expect(target).not.toContain("/app/acme");
expect(target).not.toContain("/admin/dashboard");
}
function expectCanonicalAdminDashboard(target: string, options: { absolute?: boolean } = {}) {
expect(target).toBe(options.absolute ? "https://admin.example.com/dashboard" : "/dashboard");
expect(target).not.toBe("/admin/dashboard");
expect(target).not.toContain("/admin/");
expect(target).not.toContain("/app/acme");
}
describe("session entry host-aware redirects", () => {
it("builds login default-tenant targets without legacy prefixes", () => {
expectCanonicalTenantDashboard(loginDefaultRedirectTarget(user, tenant), { absolute: true });
expectCanonicalAdminDashboard(loginDefaultRedirectTarget(adminUser, tenant), { absolute: true });
});
it("does not fall back to tenant IDs for default-tenant redirects", () => {
expect(loginDefaultRedirectTarget(user, { id: "tenant-1", slug: null })).toBe("https://app.example.com/");
expect(loginDefaultRedirectTarget(user, null)).toBe("https://app.example.com/");
});
it("builds tenant-picker one-tenant targets without legacy prefixes", () => {
expectCanonicalTenantDashboard(sessionTenantDashboardPath(tenant));
});
it("does not fall back to tenant IDs for tenant-picker redirects", () => {
expect(sessionTenantDashboardPath({ id: "tenant-1", slug: null } as never)).toBe("/");
});
it("canonicalizes legacy admin login redirects on the admin host", () => {
expect(canonicalAdminLoginRedirect("/admin/dashboard")).toBe("/dashboard");
expect(canonicalAdminLoginRedirect("/admin/accounts/users?filter=active")).toBe("/accounts/users?filter=active");
expect(canonicalAdminLoginRequestUrlForContext("https://admin.example.com/login?redirect=%2Fadmin%2Fdashboard", "admin")).toBe("/login?redirect=%2Fdashboard");
expect(canonicalAdminLoginRequestUrlForContext("https://admin.example.com/login?redirect=%2Fdashboard", "admin")).toBeNull();
expect(canonicalAdminLoginRequestUrlForContext("https://admin.example.com/login?redirect=%2Fadmin%2Fdashboard", "app")).toBeNull();
});
});