-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from upstash/DX-1393-auth-in-failure-function
Custom auth in failure function
- Loading branch information
Showing
20 changed files
with
313 additions
and
58 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
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
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,4 @@ | ||
this directory has three tests | ||
- success: checking auth correctly | ||
- fail: auth failing | ||
- custom: define an workflow endpoint secured with custom auth (instead of receiver) and try to call it as if failure callback |
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,16 @@ | ||
import { WorkflowContext } from "@upstash/workflow"; | ||
import { serve } from "@upstash/workflow/nextjs"; | ||
import { fail } from "app/ci/upstash/redis"; | ||
import { nanoid } from "app/ci/utils"; | ||
|
||
|
||
export const { POST } = serve(async (context) => { | ||
if (context.headers.get("authorization") !== nanoid()) { | ||
return; | ||
}; | ||
}, { | ||
receiver: undefined, | ||
async failureFunction({ context }) { | ||
await fail(context as WorkflowContext) | ||
}, | ||
}) |
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,95 @@ | ||
import { serve } from "@upstash/workflow/nextjs"; | ||
import { BASE_URL, CI_RANDOM_ID_HEADER, CI_ROUTE_HEADER, TEST_ROUTE_PREFIX } from "app/ci/constants"; | ||
import { testServe, expect } from "app/ci/utils"; | ||
import { FailureFunctionPayload, WorkflowContext } from "@upstash/workflow"; | ||
import { saveResult } from "app/ci/upstash/redis"; | ||
|
||
const header = `test-header-foo` | ||
const headerValue = `header-bar` | ||
const authentication = `Bearer test-auth-super-secret` | ||
const payload = "my-payload" | ||
|
||
const thirdPartyEndpoint = `${TEST_ROUTE_PREFIX}/auth/custom/target` | ||
|
||
const makeCall = async ( | ||
context: WorkflowContext, | ||
stepName: string, | ||
method: "GET" | "POST", | ||
expectedStatus: number, | ||
expectedBody: unknown | ||
) => { | ||
const randomId = context.headers.get(CI_RANDOM_ID_HEADER) | ||
const route = context.headers.get(CI_ROUTE_HEADER) | ||
|
||
if (!randomId || !route) { | ||
throw new Error("randomId or route not found") | ||
} | ||
|
||
const { status, body } = await context.call<FailureFunctionPayload>(stepName, { | ||
url: thirdPartyEndpoint, | ||
body: | ||
{ | ||
status: 200, | ||
header: "", | ||
body: "", | ||
url: "", | ||
sourceHeader: { | ||
[CI_ROUTE_HEADER]: [route], | ||
[CI_RANDOM_ID_HEADER]: [randomId] | ||
}, | ||
sourceBody: "", | ||
workflowRunId: "", | ||
sourceMessageId: "", | ||
}, | ||
method, | ||
headers: { | ||
[ CI_RANDOM_ID_HEADER ]: randomId, | ||
[ CI_ROUTE_HEADER ]: route, | ||
"Upstash-Workflow-Is-Failure": "true" | ||
} | ||
}) | ||
|
||
expect(status, expectedStatus) | ||
|
||
expect(typeof body, typeof expectedBody) | ||
expect(JSON.stringify(body), JSON.stringify(expectedBody)) | ||
} | ||
|
||
export const { POST, GET } = testServe( | ||
serve<string>( | ||
async (context) => { | ||
|
||
expect(context.headers.get(header)!, headerValue) | ||
|
||
await makeCall( | ||
context, | ||
"regular call should fail", | ||
"POST", | ||
500, | ||
{ | ||
error: "WorkflowError", | ||
message: "Not authorized to run the failure function." | ||
} | ||
) | ||
|
||
const input = context.requestPayload; | ||
expect(input, payload); | ||
|
||
await saveResult( | ||
context, | ||
"not authorized for failure" | ||
) | ||
}, { | ||
baseUrl: BASE_URL, | ||
retries: 0, | ||
} | ||
), { | ||
expectedCallCount: 4, | ||
expectedResult: "not authorized for failure", | ||
payload, | ||
headers: { | ||
[ header ]: headerValue, | ||
"authentication": authentication | ||
} | ||
} | ||
) |
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
File renamed without changes.
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
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
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
Oops, something went wrong.