Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add types to pat middleware #5951

Merged
merged 2 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions src/lib/middleware/pat-middleware.test.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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