Skip to content

Commit

Permalink
Ensure function outputs are encrypted
Browse files Browse the repository at this point in the history
  • Loading branch information
jpwilliams committed Jul 22, 2024
1 parent 9d41a10 commit d91a730
Showing 1 changed file with 41 additions and 11 deletions.
52 changes: 41 additions & 11 deletions packages/middleware-encryption/src/middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,18 @@ describe("encryptionMiddleware", () => {
}

test(spec.name, async () => {
if (!spec.result && !spec.rawOutput) {
throw new Error("Missing result or rawOutput in spec");
}

const result = await runFn({ spec });
expect(result).toMatchObject(spec.result);
if (spec.result) {
expect(result.execResult).toMatchObject(spec.result);
}

if (spec.rawOutput) {
expect(result.rawOutput).toEqual(spec.rawOutput);
}
});
});
};
Expand Down Expand Up @@ -160,15 +170,17 @@ describe("encryptionMiddleware", () => {
},
},
{
name: "returns decrypted data",
name: "returns encrypted data",
fn,
result: {
type: "function-resolved",
data: {
foo: { foo: "foo" },
bar: { foowas: { foo: "foo" }, bar: "bar" },
},
data: partialEncryptedValue,
},
rawOutput: {
foo: { foo: "foo" },
bar: { foowas: { foo: "foo" }, bar: "bar" },
},

steps: {
[stepIds.foo]: {
id: stepIds.foo,
Expand Down Expand Up @@ -199,23 +211,41 @@ type Specification = {
steps?: InngestExecutionOptions["stepState"];
events?: [EventPayload, ...EventPayload[]];
fn: (ctx: Context) => unknown;
result: ExecutionResult;

/**
* The result of the execution as it will be sent back to Inngest.
*/
result?: ExecutionResult;

/**
* The raw output from the user's function, before any potential encryption.
*/
rawOutput?: unknown;
};

const runFn = async ({
spec: {
fn: testFn,
fn: specFn,
steps = {},
events = [{ name: "my-event", data: { foo: "bar" } }],
},
}: {
spec: Specification;
}): Promise<ExecutionResult> => {
}): Promise<{ execResult: ExecutionResult; rawOutput: unknown }> => {
const inngest = new Inngest({
id: "test-client",
middleware: [encryptionMiddleware({ key })],
});

let rawOutput: unknown;

// wrap the given fn so we can get the raw output without encryption for
// testing
const testFn = async (ctx: Context) => {
rawOutput = await specFn(ctx);
return rawOutput;
};

const fn = inngest.createFunction(
{ id: "my-fn" },
{ event: "my-event" },
Expand All @@ -241,7 +271,7 @@ const runFn = async ({
},
});

const result = await execution.start();
const execResult = await execution.start();

return result;
return { execResult, rawOutput };
};

0 comments on commit d91a730

Please sign in to comment.