From 80bc4e05a20c872e6ac2e42757da8b00b9a6e5c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gast=C3=B3n=20Fournier?= Date: Thu, 18 Jan 2024 14:36:42 +0100 Subject: [PATCH] chore: add types to pat middleware (#5951) Add proper types --- src/lib/middleware/pat-middleware.test.ts | 16 +++++++++++----- src/lib/middleware/pat-middleware.ts | 4 ++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/lib/middleware/pat-middleware.test.ts b/src/lib/middleware/pat-middleware.test.ts index b23c71b4704..d6167f7a554 100644 --- a/src/lib/middleware/pat-middleware.test.ts +++ b/src/lib/middleware/pat-middleware.test.ts @@ -2,6 +2,7 @@ import getLogger from '../../test/fixtures/no-logger'; import patMiddleware from './pat-middleware'; import User from '../types/user'; import NotFoundError from '../error/notfound-error'; +import { AccountService } from '../services/account-service'; let config: any; @@ -15,10 +16,11 @@ beforeEach(() => { }); test('should not set user if unknown token', async () => { + // @ts-expect-error wrong type const accountService = { getAccountByPersonalAccessToken: jest.fn(), addPATSeen: jest.fn(), - }; + } as AccountService; const func = patMiddleware(config, { accountService }); @@ -37,9 +39,10 @@ test('should not set user if unknown token', async () => { }); test('should not set user if token wrong format', async () => { + // @ts-expect-error wrong type const accountService = { getAccountByPersonalAccessToken: jest.fn(), - }; + } as AccountService; const func = patMiddleware(config, { accountService }); @@ -65,10 +68,11 @@ test('should add user if known token', async () => { id: 44, username: 'my-user', }); + // @ts-expect-error wrong type const accountService = { getAccountByPersonalAccessToken: jest.fn().mockReturnValue(apiUser), addPATSeen: jest.fn(), - }; + } as AccountService; const func = patMiddleware(config, { accountService }); @@ -89,11 +93,12 @@ test('should add user if known token', async () => { test('should call next if accountService throws exception', async () => { getLogger.setMuteError(true); + // @ts-expect-error wrong types const accountService = { getAccountByPersonalAccessToken: () => { throw new Error('Error occurred'); }, - }; + } as AccountService; const func = patMiddleware(config, { accountService }); @@ -126,11 +131,12 @@ test('Should not log at error level if user not found', async () => { isEnabled: jest.fn().mockReturnValue(true), }, }; + // @ts-expect-error wrong type const accountService = { getAccountByPersonalAccessToken: jest.fn().mockImplementation(() => { throw new NotFoundError('Could not find pat'); }), - }; + } as AccountService; const mw = patMiddleware(conf, { accountService }); const cb = jest.fn(); diff --git a/src/lib/middleware/pat-middleware.ts b/src/lib/middleware/pat-middleware.ts index a3ffc520dad..519daae9c42 100644 --- a/src/lib/middleware/pat-middleware.ts +++ b/src/lib/middleware/pat-middleware.ts @@ -1,11 +1,11 @@ import { IUnleashConfig } from '../types'; import { IAuthRequest } from '../routes/unleash-types'; import NotFoundError from '../error/notfound-error'; +import { AccountService } from '../services/account-service'; -/* eslint-disable @typescript-eslint/explicit-module-boundary-types */ const patMiddleware = ( { getLogger }: Pick, - { accountService }: any, + { accountService }: { accountService: AccountService }, ): any => { const logger = getLogger('/middleware/pat-middleware.ts'); logger.debug('Enabling PAT middleware');