2022-09-28 15:53:56 +02:00
|
|
|
import getLogger from '../../test/fixtures/no-logger';
|
|
|
|
import patMiddleware from './pat-middleware';
|
|
|
|
import User from '../types/user';
|
2023-05-10 13:31:42 +02:00
|
|
|
import NotFoundError from '../error/notfound-error';
|
2024-03-18 13:58:05 +01:00
|
|
|
import type { AccountService } from '../services/account-service';
|
2022-09-28 15:53:56 +02:00
|
|
|
|
|
|
|
let config: any;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
config = {
|
|
|
|
getLogger,
|
|
|
|
flagResolver: {
|
|
|
|
isEnabled: jest.fn().mockReturnValue(true),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not set user if unknown token', async () => {
|
2024-01-18 14:36:42 +01:00
|
|
|
// @ts-expect-error wrong type
|
2023-01-18 17:08:07 +01:00
|
|
|
const accountService = {
|
|
|
|
getAccountByPersonalAccessToken: jest.fn(),
|
2023-03-10 12:12:17 +01:00
|
|
|
addPATSeen: jest.fn(),
|
2024-01-18 14:36:42 +01:00
|
|
|
} as AccountService;
|
2022-09-28 15:53:56 +02:00
|
|
|
|
2023-01-18 17:08:07 +01:00
|
|
|
const func = patMiddleware(config, { accountService });
|
2022-09-28 15:53:56 +02:00
|
|
|
|
|
|
|
const cb = jest.fn();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: jest.fn().mockReturnValue('user:some-token'),
|
|
|
|
user: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
await func(req, undefined, cb);
|
|
|
|
|
|
|
|
expect(cb).toHaveBeenCalled();
|
|
|
|
expect(req.header).toHaveBeenCalled();
|
|
|
|
expect(req.user).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not set user if token wrong format', async () => {
|
2024-01-18 14:36:42 +01:00
|
|
|
// @ts-expect-error wrong type
|
2023-01-18 17:08:07 +01:00
|
|
|
const accountService = {
|
|
|
|
getAccountByPersonalAccessToken: jest.fn(),
|
2024-01-18 14:36:42 +01:00
|
|
|
} as AccountService;
|
2022-09-28 15:53:56 +02:00
|
|
|
|
2023-01-18 17:08:07 +01:00
|
|
|
const func = patMiddleware(config, { accountService });
|
2022-09-28 15:53:56 +02:00
|
|
|
|
|
|
|
const cb = jest.fn();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: jest.fn().mockReturnValue('token-not-starting-with-user'),
|
|
|
|
user: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
await func(req, undefined, cb);
|
|
|
|
|
2023-01-18 17:08:07 +01:00
|
|
|
expect(
|
|
|
|
accountService.getAccountByPersonalAccessToken,
|
|
|
|
).not.toHaveBeenCalled();
|
2022-09-28 15:53:56 +02:00
|
|
|
expect(cb).toHaveBeenCalled();
|
|
|
|
expect(req.header).toHaveBeenCalled();
|
|
|
|
expect(req.user).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should add user if known token', async () => {
|
|
|
|
const apiUser = new User({
|
|
|
|
id: 44,
|
|
|
|
username: 'my-user',
|
|
|
|
});
|
2024-01-18 14:36:42 +01:00
|
|
|
// @ts-expect-error wrong type
|
2023-01-18 17:08:07 +01:00
|
|
|
const accountService = {
|
|
|
|
getAccountByPersonalAccessToken: jest.fn().mockReturnValue(apiUser),
|
2023-03-10 12:12:17 +01:00
|
|
|
addPATSeen: jest.fn(),
|
2024-01-18 14:36:42 +01:00
|
|
|
} as AccountService;
|
2022-09-28 15:53:56 +02:00
|
|
|
|
2023-01-18 17:08:07 +01:00
|
|
|
const func = patMiddleware(config, { accountService });
|
2022-09-28 15:53:56 +02:00
|
|
|
|
|
|
|
const cb = jest.fn();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: jest.fn().mockReturnValue('user:some-known-token'),
|
|
|
|
user: undefined,
|
|
|
|
path: '/api/client',
|
|
|
|
};
|
|
|
|
|
|
|
|
await func(req, undefined, cb);
|
|
|
|
|
|
|
|
expect(cb).toHaveBeenCalled();
|
|
|
|
expect(req.header).toHaveBeenCalled();
|
|
|
|
expect(req.user).toBe(apiUser);
|
|
|
|
});
|
|
|
|
|
2023-01-18 17:08:07 +01:00
|
|
|
test('should call next if accountService throws exception', async () => {
|
2022-09-28 15:53:56 +02:00
|
|
|
getLogger.setMuteError(true);
|
2024-01-18 14:36:42 +01:00
|
|
|
// @ts-expect-error wrong types
|
2023-01-18 17:08:07 +01:00
|
|
|
const accountService = {
|
|
|
|
getAccountByPersonalAccessToken: () => {
|
2022-09-28 15:53:56 +02:00
|
|
|
throw new Error('Error occurred');
|
|
|
|
},
|
2024-01-18 14:36:42 +01:00
|
|
|
} as AccountService;
|
2022-09-28 15:53:56 +02:00
|
|
|
|
2023-01-18 17:08:07 +01:00
|
|
|
const func = patMiddleware(config, { accountService });
|
2022-09-28 15:53:56 +02:00
|
|
|
|
|
|
|
const cb = jest.fn();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: jest.fn().mockReturnValue('user:some-token'),
|
|
|
|
user: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
await func(req, undefined, cb);
|
|
|
|
|
|
|
|
expect(cb).toHaveBeenCalled();
|
|
|
|
getLogger.setMuteError(false);
|
|
|
|
});
|
2023-05-10 13:31:42 +02:00
|
|
|
|
|
|
|
test('Should not log at error level if user not found', async () => {
|
2023-09-29 14:18:21 +02:00
|
|
|
const fakeLogger = {
|
2023-05-10 13:31:42 +02:00
|
|
|
debug: () => {},
|
|
|
|
info: () => {},
|
|
|
|
warn: jest.fn(),
|
|
|
|
error: jest.fn(),
|
|
|
|
fatal: console.error,
|
|
|
|
};
|
|
|
|
const conf = {
|
|
|
|
getLogger: () => {
|
|
|
|
return fakeLogger;
|
|
|
|
},
|
|
|
|
flagResolver: {
|
|
|
|
isEnabled: jest.fn().mockReturnValue(true),
|
|
|
|
},
|
|
|
|
};
|
2024-01-18 14:36:42 +01:00
|
|
|
// @ts-expect-error wrong type
|
2023-05-10 13:31:42 +02:00
|
|
|
const accountService = {
|
|
|
|
getAccountByPersonalAccessToken: jest.fn().mockImplementation(() => {
|
|
|
|
throw new NotFoundError('Could not find pat');
|
|
|
|
}),
|
2024-01-18 14:36:42 +01:00
|
|
|
} as AccountService;
|
2023-09-29 14:18:21 +02:00
|
|
|
const mw = patMiddleware(conf, { accountService });
|
2023-05-10 13:31:42 +02:00
|
|
|
const cb = jest.fn();
|
|
|
|
|
|
|
|
const req = {
|
|
|
|
header: jest.fn().mockReturnValue('user:some-token'),
|
|
|
|
user: undefined,
|
|
|
|
};
|
|
|
|
|
|
|
|
await mw(req, undefined, cb);
|
|
|
|
expect(fakeLogger.error).not.toHaveBeenCalled();
|
|
|
|
expect(fakeLogger.warn).toHaveBeenCalled();
|
|
|
|
});
|