1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-04-29 01:15:48 +02:00

chore: add types to pat middleware (#5951)

Add proper types
This commit is contained in:
Gastón Fournier 2024-01-18 14:36:42 +01:00 committed by GitHub
parent 105747293e
commit 80bc4e05a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 7 deletions

View File

@ -2,6 +2,7 @@ import getLogger from '../../test/fixtures/no-logger';
import patMiddleware from './pat-middleware'; import patMiddleware from './pat-middleware';
import User from '../types/user'; import User from '../types/user';
import NotFoundError from '../error/notfound-error'; import NotFoundError from '../error/notfound-error';
import { AccountService } from '../services/account-service';
let config: any; let config: any;
@ -15,10 +16,11 @@ beforeEach(() => {
}); });
test('should not set user if unknown token', async () => { test('should not set user if unknown token', async () => {
// @ts-expect-error wrong type
const accountService = { const accountService = {
getAccountByPersonalAccessToken: jest.fn(), getAccountByPersonalAccessToken: jest.fn(),
addPATSeen: jest.fn(), addPATSeen: jest.fn(),
}; } as AccountService;
const func = patMiddleware(config, { 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 () => { test('should not set user if token wrong format', async () => {
// @ts-expect-error wrong type
const accountService = { const accountService = {
getAccountByPersonalAccessToken: jest.fn(), getAccountByPersonalAccessToken: jest.fn(),
}; } as AccountService;
const func = patMiddleware(config, { accountService }); const func = patMiddleware(config, { accountService });
@ -65,10 +68,11 @@ test('should add user if known token', async () => {
id: 44, id: 44,
username: 'my-user', username: 'my-user',
}); });
// @ts-expect-error wrong type
const accountService = { const accountService = {
getAccountByPersonalAccessToken: jest.fn().mockReturnValue(apiUser), getAccountByPersonalAccessToken: jest.fn().mockReturnValue(apiUser),
addPATSeen: jest.fn(), addPATSeen: jest.fn(),
}; } as AccountService;
const func = patMiddleware(config, { 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 () => { test('should call next if accountService throws exception', async () => {
getLogger.setMuteError(true); getLogger.setMuteError(true);
// @ts-expect-error wrong types
const accountService = { const accountService = {
getAccountByPersonalAccessToken: () => { getAccountByPersonalAccessToken: () => {
throw new Error('Error occurred'); throw new Error('Error occurred');
}, },
}; } as AccountService;
const func = patMiddleware(config, { 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), isEnabled: jest.fn().mockReturnValue(true),
}, },
}; };
// @ts-expect-error wrong type
const accountService = { const accountService = {
getAccountByPersonalAccessToken: jest.fn().mockImplementation(() => { getAccountByPersonalAccessToken: jest.fn().mockImplementation(() => {
throw new NotFoundError('Could not find pat'); throw new NotFoundError('Could not find pat');
}), }),
}; } as AccountService;
const mw = patMiddleware(conf, { accountService }); const mw = patMiddleware(conf, { accountService });
const cb = jest.fn(); const cb = jest.fn();

View File

@ -1,11 +1,11 @@
import { IUnleashConfig } from '../types'; import { IUnleashConfig } from '../types';
import { IAuthRequest } from '../routes/unleash-types'; import { IAuthRequest } from '../routes/unleash-types';
import NotFoundError from '../error/notfound-error'; import NotFoundError from '../error/notfound-error';
import { AccountService } from '../services/account-service';
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
const patMiddleware = ( const patMiddleware = (
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>, { getLogger }: Pick<IUnleashConfig, 'getLogger'>,
{ accountService }: any, { accountService }: { accountService: AccountService },
): any => { ): any => {
const logger = getLogger('/middleware/pat-middleware.ts'); const logger = getLogger('/middleware/pat-middleware.ts');
logger.debug('Enabling PAT middleware'); logger.debug('Enabling PAT middleware');