Refactored and introduced test for client url (#3139)

This commit is contained in:
Erik Jan de Wit 2022-08-22 17:09:41 +02:00 committed by GitHub
parent 01cc7d7d20
commit e4882d31d6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 119 additions and 36 deletions

View file

@ -24,7 +24,8 @@ import {
import { ViewHeader } from "../components/view-header/ViewHeader"; import { ViewHeader } from "../components/view-header/ViewHeader";
import { useAdminClient } from "../context/auth/AdminClient"; import { useAdminClient } from "../context/auth/AdminClient";
import { useRealm } from "../context/realm-context/RealmContext"; import { useRealm } from "../context/realm-context/RealmContext";
import { addTrailingSlash, emptyFormatter, exportClient } from "../util"; import { emptyFormatter, exportClient } from "../util";
import { convertClientToUrl } from "../utils/client-url";
import { InitialAccessTokenList } from "./initial-access/InitialAccessTokenList"; import { InitialAccessTokenList } from "./initial-access/InitialAccessTokenList";
import { toAddClient } from "./routes/AddClient"; import { toAddClient } from "./routes/AddClient";
import { toClient } from "./routes/Client"; import { toClient } from "./routes/Client";
@ -103,40 +104,6 @@ export default function ClientsSection() {
</TableText> </TableText>
); );
const ClientHomePage = (client: ClientRepresentation) => {
const rootUrl = client.rootUrl;
const baseUrl = client.baseUrl;
// absolute base url configured, use base url is
if (baseUrl?.startsWith("http")) {
return baseUrl;
}
if (rootUrl === "${authBaseUrl}" && baseUrl) {
// eslint-disable-next-line prettier/prettier
return rootUrl.replace("${authBaseUrl}", addTrailingSlash(adminClient.baseUrl)) + baseUrl.substring(1);
}
if (rootUrl === "${authAdminUrl}" && baseUrl) {
// eslint-disable-next-line prettier/prettier
return rootUrl.replace("${authAdminUrl}", addTrailingSlash(adminClient.baseUrl)) + baseUrl.substring(1);
}
if (!rootUrl) {
return baseUrl;
}
if (rootUrl.startsWith("http")) {
let targetUrl = rootUrl;
if (baseUrl) {
targetUrl = addTrailingSlash(targetUrl) + baseUrl.substring(1);
}
return targetUrl;
}
return baseUrl;
};
const ToolbarItems = () => { const ToolbarItems = () => {
if (!isManager) return <span />; if (!isManager) return <span />;
@ -249,7 +216,8 @@ export default function ClientsSection() {
name: "baseUrl", name: "baseUrl",
displayKey: "clients:homeURL", displayKey: "clients:homeURL",
cellFormatters: [formattedLinkTableCell(), emptyFormatter()], cellFormatters: [formattedLinkTableCell(), emptyFormatter()],
cellRenderer: ClientHomePage, cellRenderer: (c) =>
convertClientToUrl(c, adminClient.baseUrl),
}, },
]} ]}
/> />

View file

@ -0,0 +1,84 @@
import { describe, expect, it } from "vitest";
import { convertClientToUrl } from "./client-url";
describe("convertClientToUrl", () => {
it("returns base url when base url starts with http", () => {
//given
const baseUrl = "http://something";
//when
const result = convertClientToUrl({ baseUrl }, "");
//then
expect(result).toBe(baseUrl);
});
it("when root url constrains ${authAdminUrl}", () => {
//given
const rootUrl = "${authAdminUrl}";
const baseUrl = "/else";
//when
const result = convertClientToUrl({ rootUrl, baseUrl }, "/admin");
//then
expect(result).toBe("/admin/else");
});
it("when root url constrains ${authBaseUrl}", () => {
//given
const rootUrl = "${authBaseUrl}";
const baseUrl = "/something";
//when
const result = convertClientToUrl({ rootUrl, baseUrl }, "/admin");
//then
expect(result).toBe("/admin/something");
});
it("when baseUrl when rootUrl is not set", () => {
//given
const baseUrl = "/another";
//when
const result = convertClientToUrl({ rootUrl: undefined, baseUrl }, "");
//then
expect(result).toBe("/another");
});
it("when rootUrl starts with http and baseUrl is set", () => {
//given
const baseUrl = "/another";
const rootUrl = "http://test.nl";
//when
const result = convertClientToUrl({ rootUrl, baseUrl }, "");
//then
expect(result).toBe("http://test.nl/another");
});
it("when rootUrl starts with http and baseUrl not set return it", () => {
//given
const rootUrl = "http://test.nl";
//when
const result = convertClientToUrl({ rootUrl, baseUrl: undefined }, "");
//then
expect(result).toBe("http://test.nl");
});
it("should it return ${authBaseUrl} when baseUrl is not set?", () => {
//given
const rootUrl = "${authBaseUrl}";
//when
const result = convertClientToUrl({ rootUrl, baseUrl: undefined }, "");
//then
expect(result).toBeUndefined();
});
});

31
src/utils/client-url.ts Normal file
View file

@ -0,0 +1,31 @@
import ClientRepresentation from "@keycloak/keycloak-admin-client/lib/defs/clientRepresentation";
import { joinPath } from "./joinPath";
export const convertClientToUrl = (
{ rootUrl, baseUrl }: ClientRepresentation,
adminClientBaseUrl: string
) => {
// absolute base url configured, use base url is
if (baseUrl?.startsWith("http")) {
return baseUrl;
}
if (
(rootUrl === "${authBaseUrl}" || rootUrl === "${authAdminUrl}") &&
baseUrl
) {
return rootUrl.replace(
/\$\{(authAdminUrl|authBaseUrl)\}/,
joinPath(adminClientBaseUrl, baseUrl)
);
}
if (rootUrl?.startsWith("http")) {
if (baseUrl) {
return joinPath(rootUrl, baseUrl);
}
return rootUrl;
}
return baseUrl;
};