diff --git a/packages/node/src/lib/ReadMe.ts b/packages/node/src/lib/ReadMe.ts index fb54eda9f..3089aa573 100644 --- a/packages/node/src/lib/ReadMe.ts +++ b/packages/node/src/lib/ReadMe.ts @@ -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; - byEmail: (email: string) => Promise; + byAPIKey: (apiKey: string) => Promise | undefined; + byEmail: (email: string) => Promise | undefined; manualAPIKey?: string; } diff --git a/packages/node/test/index.test.ts b/packages/node/test/index.test.ts index f7c844e85..023156db8 100644 --- a/packages/node/test/index.test.ts +++ b/packages/node/test/index.test.ts @@ -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 ( @@ -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 }); } diff --git a/packages/node/test/lib/construct-payload.test.ts b/packages/node/test/lib/construct-payload.test.ts index 5b1987e9c..96cabeab2 100644 --- a/packages/node/test/lib/construct-payload.test.ts +++ b/packages/node/test/lib/construct-payload.test.ts @@ -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'; @@ -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))); }); }; @@ -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]"}', @@ -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); }); }); diff --git a/packages/node/test/lib/get-group-id.test.ts b/packages/node/test/lib/get-group-id.test.ts index 0a8d46951..19d2bfa8e 100644 --- a/packages/node/test/lib/get-group-id.test.ts +++ b/packages/node/test/lib/get-group-id.test.ts @@ -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(); }); @@ -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); @@ -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(); }); @@ -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(); }); @@ -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', () => { @@ -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'); }); }); @@ -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'); }); }); @@ -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'); }); }); }); diff --git a/packages/node/test/lib/process-request.test.ts b/packages/node/test/lib/process-request.test.ts index 2445529df..e052a1e6e 100644 --- a/packages/node/test/lib/process-request.test.ts +++ b/packages/node/test/lib/process-request.test.ts @@ -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') diff --git a/packages/node/test/lib/verify-webhook.test.ts b/packages/node/test/lib/verify-webhook.test.ts index ff6ce54f1..c6fa24020 100644 --- a/packages/node/test/lib/verify-webhook.test.ts +++ b/packages/node/test/lib/verify-webhook.test.ts @@ -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/); }); diff --git a/packages/node/test/tsconfig.json b/packages/node/test/tsconfig.json index 25b837d8a..8a8e2ca92 100644 --- a/packages/node/test/tsconfig.json +++ b/packages/node/test/tsconfig.json @@ -3,7 +3,8 @@ "compilerOptions": { "downlevelIteration": true, "noImplicitAny": true, - "strict": false + "noEmit": true, + "strict": true }, "include": ["./index.test.ts", "./lib/*.ts", "./global.d.ts"] }