1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-25 00:07:47 +01:00
unleash.unleash/src/lib/middleware/api-token-middleware.test.ts

158 lines
3.7 KiB
TypeScript
Raw Normal View History

2021-03-29 19:58:11 +02:00
import apiTokenMiddleware from './api-token-middleware';
import getLogger from '../../test/fixtures/no-logger';
2021-05-02 20:58:02 +02:00
import { CLIENT } from '../types/permissions';
import { createTestConfig } from '../../test/config/test-config';
import ApiUser from '../types/api-user';
2021-03-29 19:58:11 +02:00
let config: any;
beforeEach(() => {
2021-03-29 19:58:11 +02:00
config = {
getLogger,
authentication: {
enableApiToken: true,
},
};
});
test('should not do anything if request does not contain a authorization', async () => {
2021-03-29 19:58:11 +02:00
const apiTokenService = {
getUserForToken: jest.fn(),
2021-03-29 19:58:11 +02:00
};
const func = apiTokenMiddleware(config, { apiTokenService });
const cb = jest.fn();
2021-03-29 19:58:11 +02:00
const req = {
header: jest.fn(),
2021-03-29 19:58:11 +02:00
};
await func(req, undefined, cb);
expect(req.header).toHaveBeenCalledTimes(1);
expect(cb).toHaveBeenCalledTimes(1);
2021-03-29 19:58:11 +02:00
});
test('should not add user if unknown token', async () => {
2021-03-29 19:58:11 +02:00
const apiTokenService = {
getUserForToken: jest.fn(),
2021-03-29 19:58:11 +02:00
};
const func = apiTokenMiddleware(config, { apiTokenService });
const cb = jest.fn();
2021-03-29 19:58:11 +02:00
const req = {
header: jest.fn().mockReturnValue('some-token'),
2021-03-29 19:58:11 +02:00
user: undefined,
};
await func(req, undefined, cb);
expect(cb).toHaveBeenCalled();
expect(req.header).toHaveBeenCalled();
expect(req.user).toBeFalsy();
2021-03-29 19:58:11 +02:00
});
test('should add user if unknown token', async () => {
const apiUser = new ApiUser({
2021-03-29 19:58:11 +02:00
username: 'default',
permissions: [CLIENT],
});
const apiTokenService = {
getUserForToken: jest.fn().mockReturnValue(apiUser),
2021-03-29 19:58:11 +02:00
};
const func = apiTokenMiddleware(config, { apiTokenService });
const cb = jest.fn();
2021-03-29 19:58:11 +02:00
const req = {
header: jest.fn().mockReturnValue('some-known-token'),
2021-03-29 19:58:11 +02:00
user: undefined,
};
await func(req, undefined, cb);
expect(cb).toHaveBeenCalled();
expect(req.header).toHaveBeenCalled();
expect(req.user).toBe(apiUser);
2021-03-29 19:58:11 +02:00
});
test('should not add user if disabled', async () => {
const apiUser = new ApiUser({
2021-03-29 19:58:11 +02:00
username: 'default',
permissions: [CLIENT],
});
const apiTokenService = {
getUserForToken: jest.fn().mockReturnValue(apiUser),
2021-03-29 19:58:11 +02:00
};
const disabledConfig = createTestConfig({
2021-03-29 19:58:11 +02:00
getLogger,
authentication: {
enableApiToken: false,
createAdminUser: false,
2021-03-29 19:58:11 +02:00
},
});
2021-03-29 19:58:11 +02:00
const func = apiTokenMiddleware(disabledConfig, { apiTokenService });
const cb = jest.fn();
2021-03-29 19:58:11 +02:00
const req = {
header: jest.fn().mockReturnValue('some-known-token'),
2021-03-29 19:58:11 +02:00
user: undefined,
};
await func(req, undefined, cb);
expect(cb).toHaveBeenCalled();
expect(req.user).toBeFalsy();
2021-03-29 19:58:11 +02:00
});
test('should call next if apiTokenService throws', async () => {
2021-03-29 19:58:11 +02:00
getLogger.setMuteError(true);
const apiTokenService = {
getUserForToken: () => {
throw new Error('hi there, i am stupid');
},
};
const func = apiTokenMiddleware(config, { apiTokenService });
const cb = jest.fn();
2021-03-29 19:58:11 +02:00
const req = {
header: jest.fn().mockReturnValue('some-token'),
2021-03-29 19:58:11 +02:00
user: undefined,
};
await func(req, undefined, cb);
expect(cb).toHaveBeenCalled();
2021-03-29 19:58:11 +02:00
getLogger.setMuteError(false);
});
test('should call next if apiTokenService throws x2', async () => {
2021-03-29 19:58:11 +02:00
const apiTokenService = {
getUserForToken: () => {
throw new Error('hi there, i am stupid');
},
};
const func = apiTokenMiddleware(config, { apiTokenService });
const cb = jest.fn();
2021-03-29 19:58:11 +02:00
const req = {
header: jest.fn().mockReturnValue('some-token'),
2021-03-29 19:58:11 +02:00
user: undefined,
};
await func(req, undefined, cb);
expect(cb).toHaveBeenCalled();
2021-03-29 19:58:11 +02:00
});