mirror of
https://github.com/gethomepage/homepage.git
synced 2026-09-28 06:51:17 -07:00
Enhancement: allow OIDC auto-login (#7096)
This commit is contained in:
@@ -1,11 +1,15 @@
|
||||
// @vitest-environment jsdom
|
||||
|
||||
import { render, screen, waitFor } from "@testing-library/react";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
import { StrictMode } from "react";
|
||||
import { beforeEach, describe, expect, it, vi } from "vitest";
|
||||
|
||||
const { getSettingsMock, authOptionsMock } = vi.hoisted(() => ({
|
||||
const { getSettingsMock, authOptionsMock, signInMock, replaceMock, routerQuery } = vi.hoisted(() => ({
|
||||
getSettingsMock: vi.fn(),
|
||||
authOptionsMock: vi.fn(),
|
||||
signInMock: vi.fn(),
|
||||
replaceMock: vi.fn(),
|
||||
routerQuery: {},
|
||||
}));
|
||||
|
||||
vi.mock("utils/config/config", () => ({
|
||||
@@ -20,13 +24,25 @@ vi.mock("pages/api/auth/[...nextauth]", () => ({
|
||||
|
||||
vi.mock("next/router", () => ({
|
||||
useRouter: () => ({
|
||||
query: {},
|
||||
query: routerQuery,
|
||||
replace: replaceMock,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock("next-auth/react", () => ({ signIn: signInMock }));
|
||||
|
||||
import SignInPage, { getServerSideProps } from "pages/auth/signin";
|
||||
|
||||
const OIDC_PROVIDERS = { "homepage-oidc": { id: "homepage-oidc", name: "Homepage OIDC", type: "oauth" } };
|
||||
const SETTINGS = { theme: "dark", color: "slate", title: "Homepage" };
|
||||
|
||||
describe("pages/auth/signin", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks();
|
||||
Object.keys(routerQuery).forEach((key) => delete routerQuery[key]);
|
||||
window.sessionStorage.clear();
|
||||
});
|
||||
|
||||
it("renders an error state when no providers are configured", async () => {
|
||||
render(
|
||||
<SignInPage
|
||||
@@ -66,6 +82,67 @@ describe("pages/auth/signin", () => {
|
||||
expect(screen.getByRole("button", { name: /login via oidc/i })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("redirects to the provider when auto-login is enabled", () => {
|
||||
routerQuery.callbackUrl = "/some/page";
|
||||
|
||||
render(<SignInPage providers={OIDC_PROVIDERS} settings={SETTINGS} autoLogin />);
|
||||
|
||||
expect(signInMock).toHaveBeenCalledWith("homepage-oidc", { callbackUrl: "/some/page" });
|
||||
expect(screen.getByText(/redirecting to homepage oidc/i)).toBeInTheDocument();
|
||||
expect(screen.queryByRole("button", { name: /login via/i })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("does not auto-login when the provider returned an error", () => {
|
||||
routerQuery.error = "OAuthCallback";
|
||||
|
||||
render(<SignInPage providers={OIDC_PROVIDERS} settings={SETTINGS} autoLogin />);
|
||||
|
||||
expect(signInMock).not.toHaveBeenCalled();
|
||||
expect(screen.getByRole("button", { name: /login via homepage oidc/i })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("does not auto-login when it is explicitly disabled in the url", () => {
|
||||
routerQuery.autologin = "0";
|
||||
|
||||
render(<SignInPage providers={OIDC_PROVIDERS} settings={SETTINGS} autoLogin />);
|
||||
|
||||
expect(signInMock).not.toHaveBeenCalled();
|
||||
expect(screen.getByRole("button", { name: /login via homepage oidc/i })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it("redirects once under strict mode, rather than tripping its own loop guard", () => {
|
||||
render(
|
||||
<StrictMode>
|
||||
<SignInPage providers={OIDC_PROVIDERS} settings={SETTINGS} autoLogin />
|
||||
</StrictMode>,
|
||||
);
|
||||
|
||||
expect(signInMock).toHaveBeenCalledTimes(1);
|
||||
expect(replaceMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("stops auto-login and hands back the page when the session never sticks", () => {
|
||||
routerQuery.callbackUrl = "/some/page";
|
||||
|
||||
render(<SignInPage providers={OIDC_PROVIDERS} settings={SETTINGS} autoLogin />);
|
||||
render(<SignInPage providers={OIDC_PROVIDERS} settings={SETTINGS} autoLogin />);
|
||||
|
||||
expect(signInMock).toHaveBeenCalledTimes(1);
|
||||
expect(replaceMock).toHaveBeenCalledWith("/auth/signin?autologin=0&callbackUrl=%2Fsome%2Fpage");
|
||||
});
|
||||
|
||||
it("does not auto-login the password provider", () => {
|
||||
render(
|
||||
<SignInPage
|
||||
providers={{ credentials: { id: "credentials", name: "Password", type: "credentials" } }}
|
||||
settings={SETTINGS}
|
||||
autoLogin
|
||||
/>,
|
||||
);
|
||||
|
||||
expect(signInMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("getServerSideProps returns providers and only public sign-in settings", async () => {
|
||||
authOptionsMock.mockReturnValueOnce({ providers: [{ id: "foo", name: "Foo", type: "oauth" }] });
|
||||
getSettingsMock.mockReturnValueOnce({
|
||||
@@ -88,6 +165,7 @@ describe("pages/auth/signin", () => {
|
||||
expect(getSettingsMock).toHaveBeenCalled();
|
||||
expect(res).toEqual({
|
||||
props: {
|
||||
autoLogin: false,
|
||||
providers: { foo: { id: "foo", name: "Foo", type: "oauth" } },
|
||||
settings: {
|
||||
theme: "dark",
|
||||
@@ -102,6 +180,17 @@ describe("pages/auth/signin", () => {
|
||||
expect(res.props.settings).not.toHaveProperty("layout");
|
||||
});
|
||||
|
||||
it("getServerSideProps enables auto-login from the environment", async () => {
|
||||
authOptionsMock.mockReturnValueOnce({ providers: [] });
|
||||
getSettingsMock.mockReturnValueOnce({ theme: "dark" });
|
||||
vi.stubEnv("HOMEPAGE_OIDC_AUTO_LOGIN", "true");
|
||||
|
||||
const res = await getServerSideProps({});
|
||||
|
||||
expect(res.props.autoLogin).toBe(true);
|
||||
vi.unstubAllEnvs();
|
||||
});
|
||||
|
||||
it("getServerSideProps falls back to no providers when auth options fail to load", async () => {
|
||||
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
|
||||
authOptionsMock.mockImplementationOnce(() => {
|
||||
|
||||
Reference in New Issue
Block a user