Skip to content

Commit

Permalink
fix: type errors in tests (#1056)
Browse files Browse the repository at this point in the history
## 🧰 Changes

Noticed a bunch of type errors when running `npx tsc -p test/` from the
`packages/node` directory. Not sure if we should do this typechecking in
CI?

## 🧬 QA & Testing

Do you notice any errors when running `npx tsc -p test/` from the
`packages/node` directory?
  • Loading branch information
kanadgupta authored Jul 29, 2024
1 parent 3875633 commit be9a8e2
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 18 deletions.
4 changes: 2 additions & 2 deletions packages/node/src/lib/ReadMe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ export interface GroupingObject {
// Typing the return as unknown to make it easier to format the user to our format in the middleware
// This way these functions can just return from their database
interface GetUserParams {
byAPIKey: (apiKey: string) => Promise<GroupingObject | void>;
byEmail: (email: string) => Promise<GroupingObject | void>;
byAPIKey: (apiKey: string) => Promise<GroupingObject | undefined | void> | undefined;
byEmail: (email: string) => Promise<GroupingObject | undefined | void> | undefined;
manualAPIKey?: string;
}

Expand Down
4 changes: 2 additions & 2 deletions packages/node/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const server = setupServer(

function doMetricsHeadersMatch(headers: Headers) {
const auth = headers.get('authorization');
const decodedAuth = Buffer.from(auth.replace(/^Basic /, ''), 'base64').toString('ascii');
const decodedAuth = Buffer.from((auth || '').replace(/^Basic /, ''), 'base64').toString('ascii');
const contentType = headers.get('content-type');
const userAgent = headers.get('user-agent');
return (
Expand Down Expand Up @@ -625,7 +625,7 @@ describe('#metrics', function () {
http.post(`${config.host}/v1/request`, async ({ request: req }) => {
const body = (await req.json()) as OutgoingLogBody[];
if (doMetricsHeadersMatch(req.headers)) {
expect(body[0].request.log.entries[0].request.postData[checkLocation]).toStrictEqual(requestBody);
expect(body[0].request.log.entries[0].request.postData?.[checkLocation]).toStrictEqual(requestBody);
return new HttpResponse(null, { status: 200 });
}

Expand Down
6 changes: 2 additions & 4 deletions packages/node/test/lib/construct-payload.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as qs from 'querystring';

import { isValidUUIDV4 } from 'is-valid-uuid-v4';
import request from 'supertest';
import { describe, expect, expectTypeOf, it } from 'vitest';
import { describe, expect, it } from 'vitest';

import pkg from '../../package.json';
import { constructPayload, mask } from '../../src/lib/construct-payload';
Expand All @@ -33,6 +33,7 @@ function createApp(options?: LogOptions, payloadData?: PayloadData) {
parsedBody = qs.parse(body);
}

// @ts-expect-error deliberately passing in potentially bad data
res.end(JSON.stringify(constructPayload(req, res, { ...payloadData, requestBody: parsedBody }, options)));
});
};
Expand Down Expand Up @@ -81,8 +82,6 @@ describe('constructPayload()', function () {
.send({ password: '123456' })
.expect(({ body }) => {
expect(isValidUUIDV4(body._id)).toBe(true);
expectTypeOf(body.request.log.entries[0].request).toBeObject();
expectTypeOf(body.request.log.entries[0].response).toBeObject();
expect(body.request.log.entries[0].request.postData).toStrictEqual({
mimeType: 'application/json',
text: '{"password":"[REDACTED 6]"}',
Expand Down Expand Up @@ -140,7 +139,6 @@ describe('constructPayload()', function () {
return request(createApp(undefined, group))
.post('/')
.expect(({ body }) => {
expectTypeOf(body.request.log.entries[0].time).toBeNumber();
expect(body.request.log.entries[0].time).toBe(20000);
});
});
Expand Down
22 changes: 13 additions & 9 deletions packages/node/test/lib/get-group-id.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ describe('getGroupId', () => {
});

it('returns undefined when no user is passed', () => {
// @ts-expect-error deliberately passing in bad data
const groupId = getGroupIdByOperation(undefined, operation as Operation);
expect(groupId).toBeUndefined();
});
Expand Down Expand Up @@ -174,6 +175,7 @@ describe('getGroupId', () => {
});

it('does not error if the keys array is null', () => {
// @ts-expect-error deliberately passing in bad data
const user = mockUser(null);
const groupId = getGroupIdByOperation(user, operation as Operation);

Expand All @@ -184,6 +186,7 @@ describe('getGroupId', () => {

describe('byApiKey', () => {
it('returns undefined when no user is passed', () => {
// @ts-expect-error deliberately passing in bad data
const groupId = getGroupByApiKey(undefined, 'requestApiKey');
expect(groupId).toBeUndefined();
});
Expand All @@ -194,6 +197,7 @@ describe('getGroupId', () => {
});

it('returns undefined for a user with a null keys array', () => {
// @ts-expect-error deliberately passing in bad data
const groupId = getGroupByApiKey(mockUser(null), 'requestApiKey');
expect(groupId).toBeUndefined();
});
Expand All @@ -220,14 +224,14 @@ describe('getGroupId', () => {
]);
const groupId = getGroupByApiKey(user, 'requestApiKey');

expect(groupId.id).toBe('key-2-id');
expect(groupId?.id).toBe('key-2-id');
});

it('returns the id of the key as first priority', () => {
const user = mockUser([{ id: 'key-1-id', name: 'key-1-name', otherField: 'requestApiKey' }]);
const groupId = getGroupByApiKey(user, 'requestApiKey');

expect(groupId.id).toBe('key-1-id');
expect(groupId?.id).toBe('key-1-id');
});

it('returns the apiKey of the key as second priority', () => {
Expand All @@ -240,35 +244,35 @@ describe('getGroupId', () => {
]);
const groupId = getGroupByApiKey(user, 'requestApiKey');

expect(groupId.id).toBe('key-1-apiKey');
expect(groupId?.id).toBe('key-1-apiKey');
});

it('returns the value of the matching apiKey as the third priority', () => {
const user = mockUser([{ otherField: 'requestApiKey' }]);
const groupId = getGroupByApiKey(user, 'requestApiKey');

expect(groupId.id).toBe('requestApiKey');
expect(groupId?.id).toBe('requestApiKey');
});

it('returns the basic user as the fourth priority', () => {
const user = mockUser([{ user: 'basic-user', pass: 'basic-pass' }]);
const groupId = getGroupByApiKey(user, 'requestApiKey');

expect(groupId.id).toBe('basic-user');
expect(groupId?.id).toBe('basic-user');
});

it('returns the name of the key as fifth priority', () => {
const user = mockUser([{ name: 'key-1-name' }]);
const groupId = getGroupByApiKey(user, 'requestApiKey');

expect(groupId.id).toBe('key-1-name');
expect(groupId?.id).toBe('key-1-name');
});

it('supports having nested basic auth', () => {
const user = mockUser([{ notRelevant: 'foo' }, { basic: { user: 'basic-user', pass: 'basic-pass' } }]);
const groupId = getGroupByApiKey(user, 'basic-user');

expect(groupId.id).toBe('basic-user');
expect(groupId?.id).toBe('basic-user');
});
});

Expand All @@ -283,7 +287,7 @@ describe('getGroupId', () => {
]);
const groupId = getGroupByApiKey(user, 'requestApiKey');

expect(groupId.id).toBe('key-1-id');
expect(groupId?.id).toBe('key-1-id');
});
});

Expand All @@ -298,7 +302,7 @@ describe('getGroupId', () => {
]);
const groupId = getGroupByApiKey(user, 'requestApiKey');

expect(groupId.label).toBe('key-1-name');
expect(groupId?.label).toBe('key-1-name');
});
});
});
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/lib/process-request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ describe('process-request', function () {
});

it('should not error with no body', function () {
// @ts-expect-error deliberately passing in bad data
return request(createApp({}, false, null))
.post('/')
.set('content-type', 'application/x-www-form-urlencoded')
Expand Down
1 change: 1 addition & 0 deletions packages/node/test/lib/verify-webhook.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('verifyWebhook', function () {
const secret = 'docs4dayz';

expect(() => {
// @ts-expect-error deliberately passing in bad data
verifyWebhook(body, undefined, secret);
}).toThrow(/Missing Signature/);
});
Expand Down
3 changes: 2 additions & 1 deletion packages/node/test/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"downlevelIteration": true,
"noImplicitAny": true,
"strict": false
"noEmit": true,
"strict": true
},
"include": ["./index.test.ts", "./lib/*.ts", "./global.d.ts"]
}

0 comments on commit be9a8e2

Please sign in to comment.