Skip to content

Commit 4645501

Browse files
MMT-4087: Replace crypto call to get uuid by using uuid v4 (#1416)
* MMT-4087: Replace crypto call to get uuid by v4 * MMT-4087: Running npm audit fix * MMT-4087: Do not need mock because its being called in jest.config --------- Co-authored-by: Ed Olivares <34591886+eudoroolivares2016@users.noreply.github.com>
1 parent 5664b80 commit 4645501

File tree

11 files changed

+27
-158
lines changed

11 files changed

+27
-158
lines changed

package-lock.json

Lines changed: 0 additions & 137 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

serverless/src/createTemplate/handler.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PutObjectCommand } from '@aws-sdk/client-s3'
2-
import crypto from 'crypto'
2+
import { v4 as uuidv4 } from 'uuid'
33

44
import { getApplicationConfig } from '../../../sharedUtils/getConfig'
55
import { getS3Client } from '../utils/getS3Client'
@@ -22,7 +22,7 @@ const createTemplate = async (event) => {
2222
const { TemplateName: templateName } = JSON.parse(body)
2323

2424
const hashedName = Buffer.from(templateName).toString('base64')
25-
const guid = crypto.randomUUID()
25+
const guid = uuidv4()
2626

2727
const { COLLECTION_TEMPLATES_BUCKET_NAME: collectionTemplatesBucketName } = process.env
2828
const createCommand = new PutObjectCommand({

static/src/js/components/ErrorPageNotFound/ErrorPageNotFound.jsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import React from 'react'
22

3+
import { v4 as uuidv4 } from 'uuid'
4+
35
import errorLogger from '@/js/utils/errorLogger'
46

57
import Header from '../Header/Header'
68

79
import './ErrorPageNotFound.scss'
810

911
const ErrorPageNotFound = () => {
10-
const guid = crypto.randomUUID()
12+
const guid = uuidv4()
1113

1214
errorLogger(`404 Not Found see ${guid}`, 'user attempted to access a page that does not exist')
1315

static/src/js/components/ErrorPageNotFound/__tests__/ErrorPageNotFound.test.jsx

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@ import AuthContext from '@/js/context/AuthContext'
1111
import ErrorPageNotFound from '../ErrorPageNotFound'
1212

1313
vi.mock('@/js/utils/errorLogger')
14-
vi.mock('crypto', () => ({
15-
default: {
16-
randomUUID: () => 'mock-uuid'
17-
}
18-
}))
1914

2015
const setup = () => {
2116
const context = {

static/src/js/components/MetadataForm/MetadataForm.jsx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import pluralize from 'pluralize'
99
import React, { useEffect, useState } from 'react'
1010
import Row from 'react-bootstrap/Row'
1111
import validator from '@rjsf/validator-ajv8'
12+
import { v4 as uuidv4 } from 'uuid'
1213

1314
import BoundingRectangleField from '@/js/components/BoundingRectangleField/BoundingRectangleField'
1415
import CustomArrayTemplate from '@/js/components/CustomArrayFieldTemplate/CustomArrayFieldTemplate'
@@ -164,8 +165,8 @@ const MetadataForm = () => {
164165
// Add '-draft' to the end of nativeId if it doesn't already end with it.
165166
// Can be removed after CMR-10545 is complete
166167
derivedConceptType === 'Visualization' || derivedConceptType === 'Citation'
167-
? `MMT_${crypto.randomUUID()}-draft`
168-
: `MMT_${crypto.randomUUID()}`
168+
? `MMT_${uuidv4()}-draft`
169+
: `MMT_${uuidv4()}`
169170
)
170171

171172
const schema = getUmmSchema(derivedConceptType)

static/src/js/components/PublishPreview/PublishPreview.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
FaEye,
2121
FaTrash
2222
} from 'react-icons/fa'
23+
import { v4 as uuidv4 } from 'uuid'
2324

2425
import conceptTypeQueries from '@/js//constants/conceptTypeQueries'
2526
import deleteMutationTypes from '@/js//constants/deleteMutationTypes'
@@ -138,7 +139,7 @@ const PublishPreviewHeader = () => {
138139

139140
// Calls ingestDraft mutation with a new nativeId
140141
const handleClone = () => {
141-
const cloneNativeId = `MMT_${crypto.randomUUID()}`
142+
const cloneNativeId = `MMT_${uuidv4()}`
142143
// Removes the value from the metadata that has to be unique
143144
const modifiedMetadata = removeMetadataKeys(ummMetadata, ['Name', 'LongName', 'ShortName', 'EntryTitle'])
144145

static/src/js/components/TemplateForm/TemplateForm.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Row from 'react-bootstrap/Row'
66
import Form from '@rjsf/core'
77
import validator from '@rjsf/validator-ajv8'
88
import { isEmpty, kebabCase } from 'lodash-es'
9+
import { v4 as uuidv4 } from 'uuid'
910

1011
import useAppContext from '@/js/hooks/useAppContext'
1112

@@ -217,7 +218,7 @@ const TemplateForm = () => {
217218
}
218219

219220
if (type === saveTypes.saveAndCreateDraft) {
220-
const nativeId = `MMT_${crypto.randomUUID()}`
221+
const nativeId = `MMT_${uuidv4()}`
221222

222223
delete ummMetadata.TemplateName
223224

static/src/js/components/TemplatePreview/TemplatePreview.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import validator from '@rjsf/validator-ajv8'
77
import camelcaseKeys from 'camelcase-keys'
88
import { FaCopy, FaTrash } from 'react-icons/fa'
99
import { CollectionPreview } from '@edsc/metadata-preview'
10+
import { v4 as uuidv4 } from 'uuid'
1011

1112
import collectionsTemplateConfiguration from '@/js/schemas/uiForms/collectionTemplatesConfiguration'
1213
import ummCTemplateSchema from '@/js/schemas/umm/ummCTemplateSchema'
@@ -109,7 +110,7 @@ const TemplatePreview = () => {
109110

110111
const handleCreateCollectionDraft = () => {
111112
const { ummMetadata } = draft
112-
const nativeId = `MMT_${crypto.randomUUID()}`
113+
const nativeId = `MMT_${uuidv4()}`
113114

114115
delete ummMetadata.TemplateName
115116

static/src/js/pages/DraftListPage/DraftListPage.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import React, { Suspense, useState } from 'react'
22
import { useNavigate, useParams } from 'react-router'
33
import { FaPlus, FaFileUpload } from 'react-icons/fa'
4+
import { v4 as uuidv4 } from 'uuid'
45

56
import urlValueTypeToConceptTypeStringMap from '@/js/constants/urlValueToConceptStringMap'
67

@@ -67,7 +68,7 @@ const DraftListPageHeader = () => {
6768
variables: {
6869
conceptType,
6970
metadata: removeEmpty(draftToSave),
70-
nativeId: `MMT_${crypto.randomUUID()}`,
71+
nativeId: `MMT_${uuidv4()}`,
7172
providerId,
7273
ummVersion: getUmmVersion(conceptType)
7374
},

test-setup.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ vi.mock('lodash-es', async () => ({
1616
debounce: vi.fn((fn) => fn)
1717
}))
1818

19-
vi.mock('crypto', () => ({
20-
default: {
21-
randomUUID: () => 'mock-uuid'
22-
}
19+
vi.mock('uuid', () => ({
20+
v4: () => 'mock-uuid'
2321
}))
2422

25-
Object.defineProperty(globalThis, 'crypto', {
23+
Object.defineProperty(globalThis, 'uuid', {
2624
value: {
27-
randomUUID: () => 'mock-uuid'
25+
v4: () => 'mock-uuid'
2826
}
2927
})

0 commit comments

Comments
 (0)