Use crypto.randomUUID() for random ID generation (#4323)

This commit is contained in:
Jon Koops 2023-02-06 12:41:11 +01:00 committed by GitHub
parent 2f6601febf
commit b667cfc452
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 36 additions and 45 deletions

View file

@ -26,7 +26,7 @@ export const AlertContext = createContext<AlertProps | undefined>(undefined);
export const useAlerts = () => useContext(AlertContext)!; export const useAlerts = () => useContext(AlertContext)!;
export type AlertType = { export type AlertType = {
id: number; id: string;
message: string; message: string;
variant: AlertVariant; variant: AlertVariant;
description?: string; description?: string;
@ -36,7 +36,7 @@ export const AlertProvider = ({ children }: PropsWithChildren<unknown>) => {
const { t } = useTranslation(); const { t } = useTranslation();
const [alerts, setAlerts] = useState<AlertType[]>([]); const [alerts, setAlerts] = useState<AlertType[]>([]);
const hideAlert = (id: number) => { const hideAlert = (id: string) => {
setAlerts((alerts) => alerts.filter((alert) => alert.id !== id)); setAlerts((alerts) => alerts.filter((alert) => alert.id !== id));
}; };
@ -47,7 +47,7 @@ export const AlertProvider = ({ children }: PropsWithChildren<unknown>) => {
) => { ) => {
setAlerts([ setAlerts([
{ {
id: Math.random() * 100, id: crypto.randomUUID(),
//@ts-ignore //@ts-ignore
message: t(message), message: t(message),
variant, variant,

View file

@ -20,8 +20,7 @@ describe("Client authentication subtab", () => {
const clientDetailsPage = new ClientDetailsPage(); const clientDetailsPage = new ClientDetailsPage();
const policiesSubTab = new PoliciesTab(); const policiesSubTab = new PoliciesTab();
const permissionsSubTab = new PermissionsTab(); const permissionsSubTab = new PermissionsTab();
const clientId = const clientId = "client-authentication-" + crypto.randomUUID();
"client-authentication-" + (Math.random() + 1).toString(36).substring(7);
before(() => { before(() => {
keycloakBefore(); keycloakBefore();

View file

@ -299,7 +299,7 @@ describe("Client Scopes test", () => {
}); });
it("Client scope CRUD test", () => { it("Client scope CRUD test", () => {
itemId += "_" + (Math.random() + 1).toString(36).substring(7); itemId += "_" + crypto.randomUUID();
// Create // Create
listingPage.itemExist(itemId, false).goToCreateItem(); listingPage.itemExist(itemId, false).goToCreateItem();

View file

@ -316,7 +316,7 @@ describe("Clients test", () => {
}); });
it("Client CRUD test", () => { it("Client CRUD test", () => {
itemId += "_" + (Math.random() + 1).toString(36).substring(7); itemId += "_" + crypto.randomUUID();
// Create // Create
commonPage.tableUtils().checkRowItemExists(itemId, false); commonPage.tableUtils().checkRowItemExists(itemId, false);
@ -491,7 +491,7 @@ describe("Clients test", () => {
loginPage.logIn(); loginPage.logIn();
commonPage.sidebar().goToClients(); commonPage.sidebar().goToClients();
client = "client_" + (Math.random() + 1).toString(36).substring(7); client = "client_" + crypto.randomUUID();
commonPage.tableToolbarUtils().createClient(); commonPage.tableToolbarUtils().createClient();
@ -703,7 +703,7 @@ describe("Clients test", () => {
beforeEach(() => { beforeEach(() => {
commonPage.sidebar().goToClients(); commonPage.sidebar().goToClients();
client = "client_" + (Math.random() + 1).toString(36).substring(7); client = "client_" + crypto.randomUUID();
commonPage.tableToolbarUtils().createClient(); commonPage.tableToolbarUtils().createClient();
createClientPage createClientPage
.selectClientType("openid-connect") .selectClientType("openid-connect")

View file

@ -55,7 +55,7 @@ describe("Group test", () => {
keycloakBefore(); keycloakBefore();
loginPage.logIn(); loginPage.logIn();
sidebarPage.goToGroups(); sidebarPage.goToGroups();
groupName = groupNamePrefix + (Math.random() + 1).toString(36).substring(7); groupName = groupNamePrefix + crypto.randomUUID();
groupNames.push(groupName); groupNames.push(groupName);
}); });

View file

@ -46,7 +46,7 @@ describe("Realm roles test", () => {
}); });
it("Realm role CRUD test", () => { it("Realm role CRUD test", () => {
itemId += "_" + (Math.random() + 1).toString(36).substring(7); itemId += "_" + crypto.randomUUID();
// Create // Create
listingPage.itemExist(itemId, false).goToCreateItem(); listingPage.itemExist(itemId, false).goToCreateItem();
@ -69,7 +69,7 @@ describe("Realm roles test", () => {
}); });
it("should delete role from details action", () => { it("should delete role from details action", () => {
itemId += "_" + (Math.random() + 1).toString(36).substring(7); itemId += "_" + crypto.randomUUID();
listingPage.goToCreateItem(); listingPage.goToCreateItem();
createRealmRolePage.fillRealmRoleData(itemId).save(); createRealmRolePage.fillRealmRoleData(itemId).save();
masthead.checkNotificationMessage("Role created", true); masthead.checkNotificationMessage("Role created", true);
@ -89,7 +89,7 @@ describe("Realm roles test", () => {
}); });
it("Add associated roles test", () => { it("Add associated roles test", () => {
itemId += "_" + (Math.random() + 1).toString(36).substring(7); itemId += "_" + crypto.randomUUID();
// Create // Create
listingPage.itemExist(itemId, false).goToCreateItem(); listingPage.itemExist(itemId, false).goToCreateItem();
@ -183,7 +183,7 @@ describe("Realm roles test", () => {
it("Should delete associated roles from list test", () => { it("Should delete associated roles from list test", () => {
itemId = "realm_role_crud"; itemId = "realm_role_crud";
itemId += "_" + (Math.random() + 1).toString(36).substring(7); itemId += "_" + crypto.randomUUID();
// Create // Create
listingPage.itemExist(itemId, false).goToCreateItem(); listingPage.itemExist(itemId, false).goToCreateItem();

View file

@ -12,7 +12,7 @@ const modalUtils = new ModalUtils();
const masthead = new Masthead(); const masthead = new Masthead();
describe("Realm settings client policies tab tests", () => { describe("Realm settings client policies tab tests", () => {
const realmName = "Realm_" + (Math.random() + 1).toString(36).substring(7); const realmName = "Realm_" + crypto.randomUUID();
const realmSettingsPage = new RealmSettingsPage(realmName); const realmSettingsPage = new RealmSettingsPage(realmName);
beforeEach(() => { beforeEach(() => {

View file

@ -14,7 +14,7 @@ const masthead = new Masthead();
describe("Realm settings client profiles tab tests", () => { describe("Realm settings client profiles tab tests", () => {
const profileName = "Test"; const profileName = "Test";
const editedProfileName = "Edit"; const editedProfileName = "Edit";
const realmName = "Realm_" + (Math.random() + 1).toString(36).substring(7); const realmName = "Realm_" + crypto.randomUUID();
const realmSettingsPage = new RealmSettingsPage(realmName); const realmSettingsPage = new RealmSettingsPage(realmName);
beforeEach(() => { beforeEach(() => {

View file

@ -14,7 +14,7 @@ const modalUtils = new ModalUtils();
const realmSettingsPage = new RealmSettingsPage(); const realmSettingsPage = new RealmSettingsPage();
describe("Realm settings events tab tests", () => { describe("Realm settings events tab tests", () => {
const realmName = "Realm_" + (Math.random() + 1).toString(36).substring(7); const realmName = "Realm_" + crypto.randomUUID();
const listingPage = new ListingPage(); const listingPage = new ListingPage();
beforeEach(() => { beforeEach(() => {
@ -88,8 +88,8 @@ describe("Realm settings events tab tests", () => {
const addBundle = () => { const addBundle = () => {
realmSettingsPage.addKeyValuePair( realmSettingsPage.addKeyValuePair(
"key_" + (Math.random() + 1).toString(36).substring(7), "key_" + crypto.randomUUID(),
"value_" + (Math.random() + 1).toString(36).substring(7) "value_" + crypto.randomUUID()
); );
return this; return this;

View file

@ -11,7 +11,7 @@ const masthead = new Masthead();
const realmSettingsPage = new RealmSettingsPage(); const realmSettingsPage = new RealmSettingsPage();
describe("Realm settings general tab tests", () => { describe("Realm settings general tab tests", () => {
const realmName = "Realm_" + (Math.random() + 1).toString(36).substring(7); const realmName = "Realm_" + crypto.randomUUID();
beforeEach(() => { beforeEach(() => {
keycloakBefore(); keycloakBefore();

View file

@ -11,7 +11,7 @@ const masthead = new Masthead();
const realmSettingsPage = new RealmSettingsPage(); const realmSettingsPage = new RealmSettingsPage();
describe("Realm settings tabs tests", () => { describe("Realm settings tabs tests", () => {
const realmName = "Realm_" + (Math.random() + 1).toString(36).substring(7); const realmName = "Realm_" + crypto.randomUUID();
beforeEach(() => { beforeEach(() => {
keycloakBefore(); keycloakBefore();

View file

@ -23,7 +23,7 @@ const clickCreateAttributeButton = () =>
userProfileTab.createAttributeButtonClick(); userProfileTab.createAttributeButtonClick();
describe("User profile tabs", () => { describe("User profile tabs", () => {
const realmName = "Realm_" + (Math.random() + 1).toString(36).substring(7); const realmName = "Realm_" + crypto.randomUUID();
const attributeName = "Test"; const attributeName = "Test";
before(() => { before(() => {

View file

@ -14,12 +14,9 @@ const createRealmPage = new CreateRealmPage();
const realmSettings = new RealmSettings(); const realmSettings = new RealmSettings();
const modalUtils = new ModalUtils(); const modalUtils = new ModalUtils();
const testRealmName = const testRealmName = "Test-realm-" + crypto.randomUUID();
"Test-realm-" + (Math.random() + 1).toString(36).substring(7); const newRealmName = "New-Test-realm-" + crypto.randomUUID();
const newRealmName = const editedRealmName = "Edited-Test-realm-" + crypto.randomUUID();
"New-Test-realm-" + (Math.random() + 1).toString(36).substring(7);
const editedRealmName =
"Edited-Test-realm-" + (Math.random() + 1).toString(36).substring(7);
const testDisabledName = "Test-Disabled"; const testDisabledName = "Test-Disabled";
describe("Realm tests", () => { describe("Realm tests", () => {

View file

@ -37,7 +37,7 @@ describe("User creation", () => {
before(() => { before(() => {
for (let i = 0; i <= 2; i++) { for (let i = 0; i <= 2; i++) {
groupName += "_" + (Math.random() + 1).toString(36).substring(7); groupName += "_" + crypto.randomUUID();
adminClient.createGroup(groupName); adminClient.createGroup(groupName);
groupsList = [...groupsList, groupName]; groupsList = [...groupsList, groupName];
} }
@ -63,7 +63,7 @@ describe("User creation", () => {
}); });
it("Create user test", () => { it("Create user test", () => {
itemId += "_" + (Math.random() + 1).toString(36).substring(7); itemId += "_" + crypto.randomUUID();
// Create // Create
createUserPage.goToCreateUser(); createUserPage.goToCreateUser();
@ -75,7 +75,7 @@ describe("User creation", () => {
}); });
it("Create user with groups test", () => { it("Create user with groups test", () => {
itemIdWithGroups += (Math.random() + 1).toString(36).substring(7); itemIdWithGroups += crypto.randomUUID();
// Add user from search bar // Add user from search bar
createUserPage.goToCreateUser(); createUserPage.goToCreateUser();
@ -97,7 +97,7 @@ describe("User creation", () => {
}); });
it("Create user with credentials test", () => { it("Create user with credentials test", () => {
itemIdWithCred += "_" + (Math.random() + 1).toString(36).substring(7); itemIdWithCred += "_" + crypto.randomUUID();
// Add user from search bar // Add user from search bar
createUserPage.goToCreateUser(); createUserPage.goToCreateUser();

View file

@ -19,10 +19,7 @@ export default class UserDetailsPage extends PageObject {
this.cancelBtn = "cancel-create-user"; this.cancelBtn = "cancel-create-user";
this.emailInput = "email-input"; this.emailInput = "email-input";
this.emailValue = () => this.emailValue = () =>
"example" + "example" + "_" + crypto.randomUUID() + "@example.com";
"_" +
(Math.random() + 1).toString(36).substring(7) +
"@example.com";
this.firstNameInput = "firstName-input"; this.firstNameInput = "firstName-input";
this.firstNameValue = "firstname"; this.firstNameValue = "firstname";
this.lastNameInput = "lastName-input"; this.lastNameInput = "lastName-input";

View file

@ -8,7 +8,7 @@ import type { AlertEntry } from "./Alerts";
type AlertPanelProps = { type AlertPanelProps = {
alerts: AlertEntry[]; alerts: AlertEntry[];
onCloseAlert: (id: number) => void; onCloseAlert: (id: string) => void;
}; };
export function AlertPanel({ alerts, onCloseAlert }: AlertPanelProps) { export function AlertPanel({ alerts, onCloseAlert }: AlertPanelProps) {

View file

@ -31,7 +31,7 @@ export const AlertContext = createNamedContext<AlertProps | undefined>(
export const useAlerts = () => useRequiredContext(AlertContext); export const useAlerts = () => useRequiredContext(AlertContext);
export type AlertEntry = { export type AlertEntry = {
id: number; id: string;
message: string; message: string;
variant: AlertVariant; variant: AlertVariant;
description?: string; description?: string;
@ -42,13 +42,13 @@ export const AlertProvider = ({ children }: PropsWithChildren<unknown>) => {
const setTimeout = useSetTimeout(); const setTimeout = useSetTimeout();
const [alerts, setAlerts] = useState<AlertEntry[]>([]); const [alerts, setAlerts] = useState<AlertEntry[]>([]);
const removeAlert = (id: number) => const removeAlert = (id: string) =>
setAlerts((alerts) => alerts.filter((alert) => alert.id !== id)); setAlerts((alerts) => alerts.filter((alert) => alert.id !== id));
const addAlert = useCallback<AddAlertFunction>( const addAlert = useCallback<AddAlertFunction>(
(message, variant = AlertVariant.success, description) => { (message, variant = AlertVariant.success, description) => {
const alert: AlertEntry = { const alert: AlertEntry = {
id: Math.random(), id: crypto.randomUUID(),
message, message,
variant, variant,
description, description,

View file

@ -18,11 +18,9 @@ import type { ComponentProps } from "./components";
import { convertToName } from "./DynamicComponents"; import { convertToName } from "./DynamicComponents";
type IdKeyValueType = KeyValueType & { type IdKeyValueType = KeyValueType & {
id: number; id: string;
}; };
const generateId = () => Math.floor(Math.random() * 100);
export const MapComponent = ({ name, label, helpText }: ComponentProps) => { export const MapComponent = ({ name, label, helpText }: ComponentProps) => {
const { t } = useTranslation("dynamic"); const { t } = useTranslation("dynamic");
@ -36,7 +34,7 @@ export const MapComponent = ({ name, label, helpText }: ComponentProps) => {
if (!values.length) { if (!values.length) {
values.push({ key: "", value: "" }); values.push({ key: "", value: "" });
} }
setMap(values.map((value) => ({ ...value, id: generateId() }))); setMap(values.map((value) => ({ ...value, id: crypto.randomUUID() })));
}, [register, getValues]); }, [register, getValues]);
const update = (val = map) => { const update = (val = map) => {
@ -130,7 +128,7 @@ export const MapComponent = ({ name, label, helpText }: ComponentProps) => {
variant="link" variant="link"
icon={<PlusCircleIcon />} icon={<PlusCircleIcon />}
onClick={() => onClick={() =>
setMap([...map, { key: "", value: "", id: generateId() }]) setMap([...map, { key: "", value: "", id: crypto.randomUUID() }])
} }
> >
{t("common:addAttribute")} {t("common:addAttribute")}