Skip to content

Commit

Permalink
test: add tests for middleware bodies (#48)
Browse files Browse the repository at this point in the history
  • Loading branch information
43081j authored May 17, 2024
1 parent 9b396aa commit 1badacd
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/__tests__/verifyKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,37 @@ describe('verify key method', () => {
),
).toBe(false);
});

it('supports array buffers', () => {
const signedRequest = signRequestWithKeyPair(
pingRequestBody,
validKeyPair.secretKey,
);
const encoder = new TextEncoder();
const bodyEncoded = encoder.encode(signedRequest.body);
expect(
verifyKey(
bodyEncoded.buffer,
signedRequest.signature,
signedRequest.timestamp,
validKeyPair.publicKey,
),
).toBe(true);
});

it('invalid body data type', () => {
const signedRequest = signRequestWithKeyPair(
pingRequestBody,
validKeyPair.secretKey,
);
const invalidBody = {} as unknown as string;
expect(
verifyKey(
invalidBody,
signedRequest.signature,
signedRequest.timestamp,
validKeyPair.publicKey,
),
).toBe(false);
});
});
59 changes: 59 additions & 0 deletions src/__tests__/verifyKeyMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ const exampleAutocompleteResponse = {
},
};

let requestBody: unknown = undefined;

expressApp.post(
'/interactions',
(req, _res, next) => {
if (requestBody) {
req.body = requestBody;
}
next();
},
verifyKeyMiddleware(Buffer.from(validKeyPair.publicKey).toString('hex')),
(req: Request, res: Response) => {
const interaction = req.body;
Expand Down Expand Up @@ -79,6 +87,11 @@ beforeAll(async () => {
});

describe('verify key middleware', () => {
afterEach(() => {
requestBody = undefined;
jest.restoreAllMocks();
});

it('valid ping', async () => {
// Sign and verify a valid ping request
const signedRequest = signRequestWithKeyPair(
Expand Down Expand Up @@ -245,6 +258,52 @@ describe('verify key middleware', () => {
);
expect(exampleRequestResponse.status).toBe(401);
});

it('missing public key', async () => {
expect(() => verifyKeyMiddleware('')).toThrow(
'You must specify a Discord client public key',
);
});

it('handles string bodies from middleware', async () => {
const signedRequest = signRequestWithKeyPair(
pingRequestBody,
validKeyPair.secretKey,
);
requestBody = signedRequest.body;
const exampleRequestResponse = await sendExampleRequest(
exampleInteractionsUrl,
{
'x-signature-ed25519': signedRequest.signature,
'x-signature-timestamp': signedRequest.timestamp,
'content-type': 'application/json',
},
'',
);
expect(exampleRequestResponse.status).toBe(200);
});

it('warns on unknown bodies from middleware', async () => {
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {
return;
});
const signedRequest = signRequestWithKeyPair(
pingRequestBody,
validKeyPair.secretKey,
);
requestBody = JSON.parse(signedRequest.body);
const exampleRequestResponse = await sendExampleRequest(
exampleInteractionsUrl,
{
'x-signature-ed25519': signedRequest.signature,
'x-signature-timestamp': signedRequest.timestamp,
'content-type': 'application/json',
},
'',
);
expect(exampleRequestResponse.status).toBe(200);
expect(warnSpy).toBeCalled();
});
});

afterAll(() => {
Expand Down

0 comments on commit 1badacd

Please sign in to comment.