This repository has been archived by the owner on Jan 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: do not delete inbound edi file when source is core artifact bucket
when using Core's remote sftp poller, the poller adds the document to Core's artifact bucket and sets this as the input when sending transaction processed events. In these cases the edi-inbound function should not attempt to delete the input since Core handles the work and the input is read-only. Fix tests which use the artifact bucket as the source input by default.
- Loading branch information
1 parent
27c1459
commit dc13bdb
Showing
5 changed files
with
152 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/functions/edi/inbound/__tests__/handler.source-is-artifact-bucket.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
import test from "ava"; | ||
import { handler } from "../handler.js"; | ||
import nock from "nock"; | ||
import { sampleTransactionProcessedEvent } from "../__fixtures__/events.js"; | ||
import { sdkStreamMixin } from "@aws-sdk/util-stream-node"; | ||
import { | ||
mockBucketClient, | ||
mockExecutionTracking, | ||
mockGuideClient, | ||
mockStashClient, | ||
mockTranslateClient, | ||
} from "../../../../lib/testing/testHelpers.js"; | ||
import { | ||
DeleteObjectCommand, | ||
GetObjectCommand, | ||
} from "@stedi/sdk-client-buckets"; | ||
import { Readable } from "stream"; | ||
import { GetValueCommand } from "@stedi/sdk-client-stash"; | ||
import guideJSON855 from "../__fixtures__/855-guide.json" assert { type: "json" }; | ||
|
||
const buckets = mockBucketClient(); | ||
const translate = mockTranslateClient(); | ||
const stash = mockStashClient(); | ||
const guides = mockGuideClient(); | ||
|
||
const partnershipId = "this-is-me_another-merchant"; | ||
|
||
test.beforeEach(() => { | ||
nock.disableNetConnect(); | ||
mockExecutionTracking(buckets); | ||
}); | ||
|
||
test.afterEach.always(() => { | ||
buckets.reset(); | ||
guides.reset(); | ||
stash.reset(); | ||
translate.reset(); | ||
}); | ||
|
||
test.serial("deletes input file", async (t) => { | ||
// loading incoming EDI file from S3 | ||
buckets.on(GetObjectCommand, {}).resolves({ | ||
body: sdkStreamMixin( | ||
Readable.from([new TextEncoder().encode(JSON.stringify(guideJSON855))]) | ||
), | ||
}); | ||
|
||
stash | ||
.on(GetValueCommand, { | ||
key: `destinations|${partnershipId}|855`, | ||
}) // mock destinations lookup | ||
.resolvesOnce({ | ||
value: { | ||
description: | ||
"Purchase Order Acknowledgments received from ANOTHERMERCH", | ||
destinations: [ | ||
{ | ||
destination: { | ||
type: "webhook", | ||
url: "https://webhook.site/TESTING", | ||
verb: "POST", | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
// mock destination webhook delivery | ||
const webhookRequest = nock("https://webhook.site") | ||
.post("/TESTING", (body) => t.deepEqual(body, guideJSON855)) | ||
.reply(200, { thank: "you" }); | ||
|
||
const result = await handler(sampleTransactionProcessedEvent); | ||
|
||
t.assert( | ||
webhookRequest.isDone(), | ||
"delivered guide JSON to destination webhook" | ||
); | ||
|
||
const bucketDestinationCall = buckets.commandCalls(DeleteObjectCommand, { | ||
bucketName: "account_id-sftp", | ||
}); | ||
|
||
t.is(bucketDestinationCall.length, 1); | ||
|
||
t.deepEqual(result, {}); | ||
}); | ||
|
||
test.serial( | ||
"does not delete input when source is artifact bucket", | ||
async (t) => { | ||
// loading incoming EDI file from S3 | ||
buckets.on(GetObjectCommand, {}).resolves({ | ||
body: sdkStreamMixin( | ||
Readable.from([new TextEncoder().encode(JSON.stringify(guideJSON855))]) | ||
), | ||
}); | ||
|
||
stash | ||
.on(GetValueCommand, { | ||
key: `destinations|${partnershipId}|855`, | ||
}) // mock destinations lookup | ||
.resolvesOnce({ | ||
value: { | ||
description: | ||
"Purchase Order Acknowledgments received from ANOTHERMERCH", | ||
destinations: [ | ||
{ | ||
destination: { | ||
type: "webhook", | ||
url: "https://webhook.site/TESTING", | ||
verb: "POST", | ||
}, | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
// mock destination webhook delivery | ||
const webhookRequest = nock("https://webhook.site") | ||
.post("/TESTING", (body) => t.deepEqual(body, guideJSON855)) | ||
.reply(200, { thank: "you" }); | ||
|
||
const transactionWithInputasAritfactBucket = structuredClone( | ||
sampleTransactionProcessedEvent | ||
); | ||
|
||
const inputBucketName = "stedi-default-core-artifacts-1234"; | ||
sampleTransactionProcessedEvent.detail.input.bucketName = inputBucketName; | ||
const result = await handler(transactionWithInputasAritfactBucket); | ||
|
||
t.assert( | ||
webhookRequest.isDone(), | ||
"delivered guide JSON to destination webhook" | ||
); | ||
|
||
const bucketDestinationCall = buckets.commandCalls(DeleteObjectCommand, { | ||
bucketName: inputBucketName, | ||
}); | ||
|
||
t.is(bucketDestinationCall.length, 0); | ||
|
||
t.deepEqual(result, {}); | ||
} | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters