Skip to content

Commit

Permalink
Replace random uuid function
Browse files Browse the repository at this point in the history
  • Loading branch information
matborowczyk committed Apr 29, 2024
1 parent 5d4830c commit 97e6a8b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 18 deletions.
6 changes: 6 additions & 0 deletions changelogs/unreleased/5706-replace-random.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
description: "replace randomUUID function with adequate cryptographic substitute"
issue-nr: 5706
change-type: patch
destination-branches: [master, iso7]
sections:
minor-improvement: "{{description}}"
15 changes: 1 addition & 14 deletions src/Slices/EditInstance/Data/CommandManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,6 @@ export function TriggerInstanceUpdateCommandManager(apiHelper: ApiHelper) {
);
}

export const create_UUID = () => {
let dt = new Date().getTime();
const uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
/[xy]/g,
function (c) {
const r = (dt + Math.random() * 16) % 16 | 0;
dt = Math.floor(dt / 16);
return (c == "x" ? r : (r & 0x3) | 0x8).toString(16);
},
);
return uuid;
};

export const getBodyV1 = (
fields: Field[],
currentAttributes: InstanceAttributeModel | null,
Expand Down Expand Up @@ -95,5 +82,5 @@ export const getBodyV2 = (
},
];

return { edit: patchData, patch_id: create_UUID() };
return { edit: patchData, patch_id: crypto.randomUUID() };
};
6 changes: 4 additions & 2 deletions src/UI/Components/Diagram/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
a as InstanceAttributesA,
b as InstanceAttributesB,
} from "@/Test/Data/ServiceInstance/Attributes";
import * as uuidApi from "../../../Slices/EditInstance/Data/CommandManager";
import {
childModel,
containerModel,
Expand Down Expand Up @@ -40,7 +39,10 @@ import {
TypeEnum,
} from "./interfaces";
import { Link, ServiceEntityBlock } from "./shapes";
jest.spyOn(uuidApi, "create_UUID").mockReturnValue("1");

jest.mock("crypto", () => ({
randomUUID: jest.fn(() => "1"),
}));

describe("extractRelationsIds", () => {
const serviceInstanceForThirdTest = {
Expand Down
4 changes: 2 additions & 2 deletions src/UI/Components/Diagram/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import crypto from "crypto";
import { dia, g, highlighters } from "@inmanta/rappid";
import {
EmbeddedEntity,
InstanceAttributeModel,
ServiceInstanceModel,
ServiceModel,
} from "@/Core";
import { create_UUID } from "@/Slices/EditInstance/Data";
import {
ConnectionRules,
InstanceForApi,
Expand Down Expand Up @@ -354,7 +354,7 @@ export const shapesDataTransform = (
if (!!instance.attributes && !instance.edits) {
instance.edits = [
{
edit_id: `${instance.instance_id}_order_update-${create_UUID()}`,
edit_id: `${instance.instance_id}_order_update-${crypto.randomUUID()}`,
operation: "replace",
target: ".",
value: instance.attributes,
Expand Down

0 comments on commit 97e6a8b

Please sign in to comment.