Skip to content

Commit

Permalink
chore: add types to pat middleware (#5951)
Browse files Browse the repository at this point in the history
Add proper types
  • Loading branch information
gastonfournier committed Jan 18, 2024
1 parent 1057472 commit 80bc4e0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 11 additions & 5 deletions src/lib/middleware/pat-middleware.test.ts
Expand Up @@ -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;

Expand All @@ -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 });

Expand All @@ -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 });

Expand All @@ -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 });

Expand All @@ -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 });

Expand Down Expand Up @@ -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();

Expand Down
4 changes: 2 additions & 2 deletions 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<IUnleashConfig, 'getLogger'>,
{ accountService }: any,
{ accountService }: { accountService: AccountService },
): any => {
const logger = getLogger('/middleware/pat-middleware.ts');
logger.debug('Enabling PAT middleware');
Expand Down

0 comments on commit 80bc4e0

Please sign in to comment.