Fix account console tests for dev (#27600)

Fixes: #27604

Signed-off-by: Hynek Mlnarik <hmlnarik@redhat.com>
This commit is contained in:
Hynek Mlnařík 2024-03-07 12:39:26 +01:00 committed by GitHub
parent 7693e2fd2d
commit 250e1137d6
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 23 additions and 18 deletions

View file

@ -5,10 +5,8 @@ import { StrictMode } from "react";
import { createRoot } from "react-dom/client"; import { createRoot } from "react-dom/client";
import { createBrowserRouter, RouterProvider } from "react-router-dom"; import { createBrowserRouter, RouterProvider } from "react-router-dom";
import { environment } from "./environment";
import { i18n } from "./i18n"; import { i18n } from "./i18n";
import { routes } from "./routes"; import { routes } from "./routes";
import { getRootPath } from "./utils/getRootPath";
// Initialize required components before rendering app. // Initialize required components before rendering app.
await i18n.init(); await i18n.init();
@ -16,8 +14,7 @@ await i18n.init();
const container = document.getElementById("app"); const container = document.getElementById("app");
const root = createRoot(container!); const root = createRoot(container!);
const basename = getRootPath(environment.realm); const router = createBrowserRouter(routes);
const router = createBrowserRouter(routes, { basename });
root.render( root.render(
<StrictMode> <StrictMode>

View file

@ -20,12 +20,14 @@ import {
useHref, useHref,
useLinkClickHandler, useLinkClickHandler,
useLocation, useLocation,
useParams,
} from "react-router-dom"; } from "react-router-dom";
import fetchContentJson from "../content/fetchContent"; import fetchContentJson from "../content/fetchContent";
import type { Feature } from "../environment"; import type { Feature } from "../environment";
import { TFuncKey } from "../i18n"; import { TFuncKey } from "../i18n";
import { usePromise } from "../utils/usePromise"; import { usePromise } from "../utils/usePromise";
import { useEnvironment } from "./KeycloakContext"; import { useEnvironment } from "./KeycloakContext";
import { getRootPath } from "../utils/getRootPath";
type RootMenuItem = { type RootMenuItem = {
label: TFuncKey; label: TFuncKey;
@ -132,8 +134,11 @@ export const NavLink = ({
isActive, isActive,
children, children,
}: PropsWithChildren<NavLinkProps>) => { }: PropsWithChildren<NavLinkProps>) => {
const href = useHref(to); const { realm } = useParams();
const handleClick = useLinkClickHandler(to);
const menuItemPath = `${getRootPath(realm)}/${to}`;
const href = useHref(menuItemPath);
const handleClick = useLinkClickHandler(menuItemPath);
return ( return (
<NavItem <NavItem

View file

@ -48,7 +48,7 @@ export type ContentComponentParams = {
}; };
export const ContentRoute: RouteObject = { export const ContentRoute: RouteObject = {
path: "/content/:componentId", path: "content/:componentId",
element: <ContentComponent />, element: <ContentComponent />,
}; };
@ -58,7 +58,7 @@ export const PersonalInfoRoute: IndexRouteObject = {
}; };
export const RootRoute: RouteObject = { export const RootRoute: RouteObject = {
path: "/", path: "/realms/:realm/account",
element: <Root />, element: <Root />,
errorElement: <ErrorPage />, errorElement: <ErrorPage />,
children: [ children: [

View file

@ -14,7 +14,7 @@ import {
inRealm, inRealm,
} from "../admin-client"; } from "../admin-client";
import groupsIdPClient from "../realms/groups-idp.json" assert { type: "json" }; import groupsIdPClient from "../realms/groups-idp.json" assert { type: "json" };
import { getBaseUrl } from "../utils"; import { getKeycloakServerUrl } from "../utils";
const realm = "groups"; const realm = "groups";
@ -32,7 +32,7 @@ test.describe("Account linking", () => {
groupIdPClientId = await createClient( groupIdPClientId = await createClient(
groupsIdPClient as ClientRepresentation, groupsIdPClient as ClientRepresentation,
); );
const baseUrl = getBaseUrl(); const baseUrl = getKeycloakServerUrl();
const idp: IdentityProviderRepresentation = { const idp: IdentityProviderRepresentation = {
alias: "master-idp", alias: "master-idp",
providerId: "oidc", providerId: "oidc",
@ -65,9 +65,7 @@ test.describe("Account linking", () => {
test("Linking", async ({ page }) => { test("Linking", async ({ page }) => {
// If refactoring this, consider introduction of helper functions for individual pages - login, update profile etc. // If refactoring this, consider introduction of helper functions for individual pages - login, update profile etc.
await page.goto( await page.goto(`/realms/${realm}/account`);
process.env.CI ? `/realms/${realm}/account` : `/?realm=${realm}`,
);
// Click the login via master-idp provider button // Click the login via master-idp provider button
await loginWithIdp(page, "master-idp"); await loginWithIdp(page, "master-idp");

View file

@ -6,10 +6,10 @@ import type { UserProfileConfig } from "@keycloak/keycloak-admin-client/lib/defs
import UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation"; import UserRepresentation from "@keycloak/keycloak-admin-client/lib/defs/userRepresentation";
import { DEFAULT_REALM } from "../src/constants"; import { DEFAULT_REALM } from "../src/constants";
import { getBaseUrl } from "./utils"; import { getKeycloakServerUrl } from "./utils";
const adminClient = new KeycloakAdminClient({ const adminClient = new KeycloakAdminClient({
baseUrl: getBaseUrl(), baseUrl: getKeycloakServerUrl(),
realmName: DEFAULT_REALM, realmName: DEFAULT_REALM,
}); });

View file

@ -1,13 +1,18 @@
import { getRootPath } from "../src/utils/getRootPath"; import { getRootPath } from "../src/utils/getRootPath";
export function getBaseUrl(): string { function getTestServerUrl(): string {
return process.env.KEYCLOAK_SERVER ?? "http://localhost:8080"; return process.env.KEYCLOAK_SERVER ?? "http://localhost:8080";
} }
export function getKeycloakServerUrl(): string {
// In CI, the Keycloak server is running in the same server as tested console, while in dev, it is running on a different port
return process.env.CI ? getTestServerUrl() : "http://localhost:8180";
}
export function getAccountUrl() { export function getAccountUrl() {
return getBaseUrl() + getRootPath(); return getTestServerUrl() + getRootPath();
} }
export function getAdminUrl() { export function getAdminUrl() {
return getBaseUrl() + "/admin/master/console/"; return getKeycloakServerUrl() + "/admin/master/console/";
} }