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';
|
|
|
|
|
|
|
|
let config: any;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
config = {
|
|
|
|
getLogger,
|
|
|
|
flagResolver: {
|
|
|
|
isEnabled: jest.fn().mockReturnValue(true),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
test('should not set user if unknown token', async () => {
|
2023-01-18 17:08:07 +01:00
|
|
|
const accountService = {
|
|
|
|
getAccountByPersonalAccessToken: jest.fn(),
|
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 () => {
|
2023-01-18 17:08:07 +01:00
|
|
|
const accountService = {
|
|
|
|
getAccountByPersonalAccessToken: jest.fn(),
|
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',
|
|
|
|
});
|
2023-01-18 17:08:07 +01:00
|
|
|
const accountService = {
|
|
|
|
getAccountByPersonalAccessToken: jest.fn().mockReturnValue(apiUser),
|
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);
|
2023-01-18 17:08:07 +01:00
|
|
|
const accountService = {
|
|
|
|
getAccountByPersonalAccessToken: () => {
|
2022-09-28 15:53:56 +02:00
|
|
|
throw new Error('Error occurred');
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
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);
|
|
|
|
});
|