Skip to content

Commit

Permalink
GQL-75: Encodes native id before sending it to CMR. (#134)
Browse files Browse the repository at this point in the history
* GQL-75: Encodes native id before sending it to CMR.
  • Loading branch information
cgokey authored and eudoroolivares2016 committed Oct 11, 2024
1 parent a09c3fe commit 2d5ce05
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/cmr/concepts/concept.js
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,7 @@ export default class Concept {
data: preparedParameters,
headers: preparedHeaders,
options: {
path: `ingest/providers/${providerId}/${this.getConceptType()}/${nativeId}`,
path: `ingest/providers/${providerId}/${this.getConceptType()}/${encodeURIComponent(nativeId)}`,
...options
}
})
Expand Down
2 changes: 1 addition & 1 deletion src/cmr/concepts/draft.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ export default class Draft extends Concept {
data: prepDataForCmr,
headers: permittedHeaders,
options: {
path: `ingest/publish/${draftConceptId}/${nativeId}`
path: `ingest/publish/${draftConceptId}/${encodeURIComponent(nativeId)}`
}
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/cmr/concepts/subscription.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default class Subscription extends Concept {
const { nativeId = uuidv4() } = params

super.ingest(data, requestedKeys, providedHeaders, {
path: `ingest/subscriptions/${nativeId}`
path: `ingest/subscriptions/${encodeURIComponent(nativeId)}`
})
}

Expand All @@ -134,6 +134,6 @@ export default class Subscription extends Concept {

const { nativeId } = params

super.delete(data, requestedKeys, providedHeaders, { path: `ingest/subscriptions/${nativeId}` })
super.delete(data, requestedKeys, providedHeaders, { path: `ingest/subscriptions/${encodeURIComponent(nativeId)}` })
}
}
109 changes: 109 additions & 0 deletions src/datasources/__tests__/draft.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,57 @@ describe('draft#ingest', () => {
})
})

test('handles native ids with special characters', async () => {
nock(/example-cmr/)
.defaultReplyHeaders({
'CMR-Request-Id': 'abcd-1234-efgh-5678'
})
.put(/ingest\/providers\/EDSC\/tool-drafts\/test-guid%2Ftest-slash/, JSON.stringify({
Name: 'mock name',
URL: {
URLContentType: 'DistributionURL',
Type: 'GOTO WEB TOOL',
Description: 'Landing Page',
URLValue: 'https://example.com/'
},
MetadataSpecification: {
URL: 'https://cdn.earthdata.nasa.gov/umm/tool/v1.0.0',
Name: 'UMM-T',
Version: '1.0.0'
}
}))
.reply(201, {
'concept-id': 'TD100000-EDSC',
'revision-id': '1'
})

const response = await draftSourceIngest({
conceptType: 'Tool',
metadata: {
Name: 'mock name',
URL: {
URLContentType: 'DistributionURL',
Type: 'GOTO WEB TOOL',
Description: 'Landing Page',
URLValue: 'https://example.com/'
}
},
nativeId: 'test-guid/test-slash',
providerId: 'EDSC',
ummVersion: '1.0.0'
}, {
headers: {
'Client-Id': 'eed-test-graphql',
'CMR-Request-Id': 'abcd-1234-efgh-5678'
}
}, requestInfo)

expect(response).toEqual({
conceptId: 'TD100000-EDSC',
revisionId: '1'
})
})

test('catches errors received from ingestCmr', async () => {
nock(/example-cmr/)
.put(/ingest\/providers\/EDSC\/tool-drafts\/test-guid/)
Expand Down Expand Up @@ -536,6 +587,34 @@ describe('draft#delete', () => {
})
})

test('handles native ids with special characters', async () => {
nock(/example-cmr/)
.defaultReplyHeaders({
'CMR-Request-Id': 'abcd-1234-efgh-5678'
})
.delete(/ingest\/providers\/EDSC\/tool-drafts\/test-guid%2Ftest-slash/)
.reply(201, {
'concept-id': 'TD100000-EDSC',
'revision-id': '1'
})

const response = await draftSourceDelete({
conceptType: 'Tool',
nativeId: 'test-guid/test-slash',
providerId: 'EDSC'
}, {
headers: {
'Client-Id': 'eed-test-graphql',
'CMR-Request-Id': 'abcd-1234-efgh-5678'
}
}, requestInfo)

expect(response).toEqual({
conceptId: 'TD100000-EDSC',
revisionId: '1'
})
})

test('catches errors received from cmrDelete', async () => {
nock(/example-cmr/)
.delete(/ingest\/providers\/EDSC\/tool-drafts\/test-guid/)
Expand Down Expand Up @@ -633,6 +712,36 @@ describe('draft#publish', () => {
})
})

test('handles native ids with special characters', async () => {
nock(/example/)
.defaultReplyHeaders({
'CMR-Request-Id': 'abcd-1234-efgh-5678'
})
.put(/ingest\/publish\/CD100000-EDSC\/mock-native-id%2Ftest-slash/)
.reply(201, {
'concept-id': 'C100000-EDSC',
'revision-id': '1'
})

// Native id includes a / so ensure it gets encoded before sending to CMR.
const response = await draftSourcePublish({
draftConceptId: 'CD100000-EDSC',
nativeId: 'mock-native-id/test-slash',
providerId: 'EDSC',
ummVersion: '1.0.0'
}, {
headers: {
'Client-Id': 'eed-test-graphql',
'CMR-Request-Id': 'abcd-1234-efgh-5678'
}
}, requestInfo)

expect(response).toEqual({
conceptId: 'C100000-EDSC',
revisionId: '1'
})
})

test('catches errors received from ingestCmr', async () => {
nock(/example/)
.put(/ingest\/publish\/CD100000-EDSC\/mock-native-id/, JSON.stringify({}))
Expand Down
73 changes: 73 additions & 0 deletions src/datasources/__tests__/subscription.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,50 @@ describe('subscription#ingest', () => {
})
})

test('handles native ids with special characters', async () => {
nock(/example-cmr/)
.defaultReplyHeaders({
'CMR-Request-Id': 'abcd-1234-efgh-5678'
})
.put(/ingest\/subscriptions\/test-guid%2Ftest-slash/, JSON.stringify({
CollectionConceptId: 'C100000-EDSC',
EmailAddress: 'test@example.com',
Name: 'Test Subscription',
Query: 'polygon=-18,-78,-13,-74,-16,-73,-22,-77,-18,-78',
SubscriberId: 'testuser',
MetadataSpecification: {
URL: 'https://cdn.earthdata.nasa.gov/umm/subscription/v1.0.0',
Name: 'UMM-Sub',
Version: '1.0.0'
}
}))
.reply(201, {
'concept-id': 'SUB100000-EDSC',
'revision-id': '1'
})

const response = await subscriptionSourceIngest({
params: {
collectionConceptId: 'C100000-EDSC',
emailAddress: 'test@example.com',
name: 'Test Subscription',
nativeId: 'test-guid/test-slash',
query: 'polygon=-18,-78,-13,-74,-16,-73,-22,-77,-18,-78',
subscriberId: 'testuser'
}
}, {
headers: {
'Client-Id': 'eed-test-graphql',
'CMR-Request-Id': 'abcd-1234-efgh-5678'
}
}, requestInfo)

expect(response).toEqual({
conceptId: 'SUB100000-EDSC',
revisionId: '1'
})
})

test('catches errors received from ingestCmr', async () => {
nock(/example-cmr/)
.put(/ingest\/subscriptions\/1b9d6bcd-bbfd-4b2d-9b5d-ab8dfbbd4bed/)
Expand Down Expand Up @@ -571,6 +615,35 @@ describe('subscription#delete', () => {
})
})

test('handles native ids with special characters', async () => {
nock(/example-cmr/)
.defaultReplyHeaders({
'CMR-Request-Id': 'abcd-1234-efgh-5678'
})
.delete(/ingest\/subscriptions\/test-guid%2Ftest-slash/)
.reply(201, {
'concept-id': 'SUB100000-EDSC',
'revision-id': '1'
})

const response = await subscriptionSourceDelete({
params: {
conceptId: 'SUB100000-EDSC',
nativeId: 'test-guid/test-slash'
}
}, {
headers: {
'Client-Id': 'eed-test-graphql',
'CMR-Request-Id': 'abcd-1234-efgh-5678'
}
}, requestInfo)

expect(response).toEqual({
conceptId: 'SUB100000-EDSC',
revisionId: '1'
})
})

test('catches errors received from cmrDelete', async () => {
nock(/example-cmr/)
.delete(/ingest\/subscriptions\/test-guid/)
Expand Down
2 changes: 1 addition & 1 deletion src/utils/cmrDelete.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const cmrDelete = async ({

// Default options
const {
path = `ingest/providers/${providerId}/${conceptType}/${nativeId}`
path = `ingest/providers/${providerId}/${conceptType}/${encodeURIComponent(nativeId)}`
} = options

// Merge default headers into the provided headers and then pick out only permitted values
Expand Down

0 comments on commit 2d5ce05

Please sign in to comment.