2021-03-11 22:51:58 +01:00
|
|
|
import rbacMiddleware from './rbac-middleware';
|
2021-04-22 23:40:52 +02:00
|
|
|
import User from '../types/user';
|
2021-05-02 20:58:02 +02:00
|
|
|
import * as perms from '../types/permissions';
|
2021-04-22 10:07:10 +02:00
|
|
|
import { IUnleashConfig } from '../types/option';
|
2021-04-22 15:04:08 +02:00
|
|
|
import { createTestConfig } from '../../test/config/test-config';
|
2021-04-22 23:40:52 +02:00
|
|
|
import ApiUser from '../types/api-user';
|
2021-08-12 15:04:37 +02:00
|
|
|
import { IFeatureToggleStore } from '../types/stores/feature-toggle-store';
|
|
|
|
import FakeFeatureToggleStore from '../../test/fixtures/fake-feature-toggle-store';
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
let config: IUnleashConfig;
|
2021-08-12 15:04:37 +02:00
|
|
|
let featureToggleStore: IFeatureToggleStore;
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
beforeEach(() => {
|
2021-08-12 15:04:37 +02:00
|
|
|
featureToggleStore = new FakeFeatureToggleStore();
|
2021-04-22 15:04:08 +02:00
|
|
|
config = createTestConfig();
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should add checkRbac to request', () => {
|
2021-04-22 10:07:10 +02:00
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn(),
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const req = jest.fn();
|
2021-03-11 22:51:58 +01:00
|
|
|
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
// @ts-ignore
|
|
|
|
expect(req.checkRbac).toBeTruthy();
|
|
|
|
// @ts-ignore
|
|
|
|
expect(typeof req.checkRbac).toBe('function');
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should give api-user ADMIN permission', async () => {
|
2021-04-22 10:07:10 +02:00
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn(),
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-03-11 22:51:58 +01:00
|
|
|
const req: any = {
|
2021-04-22 23:40:52 +02:00
|
|
|
user: new ApiUser({
|
2021-03-11 22:51:58 +01:00
|
|
|
username: 'api',
|
|
|
|
permissions: [perms.ADMIN],
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
|
|
|
const hasAccess = await req.checkRbac(perms.ADMIN);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(hasAccess).toBe(true);
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not give api-user ADMIN permission', async () => {
|
2021-04-22 10:07:10 +02:00
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn(),
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-03-11 22:51:58 +01:00
|
|
|
const req: any = {
|
2021-04-22 23:40:52 +02:00
|
|
|
user: new ApiUser({
|
2021-03-11 22:51:58 +01:00
|
|
|
username: 'api',
|
|
|
|
permissions: [perms.CLIENT],
|
|
|
|
}),
|
|
|
|
};
|
|
|
|
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
|
|
|
const hasAccess = await req.checkRbac(perms.ADMIN);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(hasAccess).toBe(false);
|
|
|
|
expect(accessService.hasPermission).toHaveBeenCalledTimes(0);
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should not allow user to miss userId', async () => {
|
2021-04-22 10:07:10 +02:00
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn(),
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-03-11 22:51:58 +01:00
|
|
|
const req: any = {
|
2021-04-22 23:40:52 +02:00
|
|
|
user: {
|
2021-03-11 22:51:58 +01:00
|
|
|
username: 'user',
|
2021-04-22 23:40:52 +02:00
|
|
|
},
|
2021-03-11 22:51:58 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
|
|
|
const hasAccess = await req.checkRbac(perms.ADMIN);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(hasAccess).toBe(false);
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should return false for missing user', async () => {
|
2021-04-22 10:07:10 +02:00
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn(),
|
2021-04-22 10:07:10 +02:00
|
|
|
};
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-03-11 22:51:58 +01:00
|
|
|
const req: any = {};
|
|
|
|
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
|
|
|
const hasAccess = await req.checkRbac(perms.ADMIN);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(hasAccess).toBe(false);
|
|
|
|
expect(accessService.hasPermission).toHaveBeenCalledTimes(0);
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should verify permission for root resource', async () => {
|
2021-03-11 22:51:58 +01:00
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn(),
|
2021-03-11 22:51:58 +01:00
|
|
|
};
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-03-11 22:51:58 +01:00
|
|
|
const req: any = {
|
|
|
|
user: new User({
|
|
|
|
username: 'user',
|
|
|
|
id: 1,
|
|
|
|
}),
|
|
|
|
params: {},
|
|
|
|
};
|
|
|
|
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
|
|
|
await req.checkRbac(perms.ADMIN);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(accessService.hasPermission).toHaveBeenCalledTimes(1);
|
|
|
|
expect(accessService.hasPermission).toHaveBeenCalledWith(
|
|
|
|
req.user,
|
|
|
|
perms.ADMIN,
|
|
|
|
undefined,
|
|
|
|
);
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should lookup projectId from params', async () => {
|
2021-03-11 22:51:58 +01:00
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn(),
|
2021-03-11 22:51:58 +01:00
|
|
|
};
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-03-11 22:51:58 +01:00
|
|
|
const req: any = {
|
|
|
|
user: new User({
|
|
|
|
username: 'user',
|
|
|
|
id: 1,
|
|
|
|
}),
|
|
|
|
params: {
|
|
|
|
projectId: 'some-proj',
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
|
|
|
await req.checkRbac(perms.UPDATE_PROJECT);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(accessService.hasPermission).toHaveBeenCalledWith(
|
|
|
|
req.user,
|
|
|
|
perms.UPDATE_PROJECT,
|
|
|
|
req.params.projectId,
|
|
|
|
);
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should lookup projectId from feature toggle', async () => {
|
2021-03-11 22:51:58 +01:00
|
|
|
const projectId = 'some-project-33';
|
|
|
|
const featureName = 'some-feature-toggle';
|
|
|
|
|
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn(),
|
2021-03-11 22:51:58 +01:00
|
|
|
};
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
featureToggleStore.getProjectId = jest.fn().mockReturnValue(projectId);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-03-11 22:51:58 +01:00
|
|
|
const req: any = {
|
|
|
|
user: new User({
|
|
|
|
username: 'user',
|
|
|
|
id: 1,
|
|
|
|
}),
|
|
|
|
params: {
|
|
|
|
featureName,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
|
|
|
await req.checkRbac(perms.UPDATE_FEATURE);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(accessService.hasPermission).toHaveBeenCalledWith(
|
|
|
|
req.user,
|
|
|
|
perms.UPDATE_FEATURE,
|
|
|
|
projectId,
|
|
|
|
);
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('should lookup projectId from data', async () => {
|
2021-03-11 22:51:58 +01:00
|
|
|
const projectId = 'some-project-33';
|
|
|
|
const featureName = 'some-feature-toggle';
|
|
|
|
|
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn(),
|
2021-03-11 22:51:58 +01:00
|
|
|
};
|
|
|
|
|
2021-04-22 10:07:10 +02:00
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-03-11 22:51:58 +01:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-03-11 22:51:58 +01:00
|
|
|
const req: any = {
|
|
|
|
user: new User({
|
|
|
|
username: 'user',
|
|
|
|
id: 1,
|
|
|
|
}),
|
|
|
|
params: {},
|
|
|
|
body: {
|
|
|
|
featureName,
|
|
|
|
project: projectId,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
|
|
|
await req.checkRbac(perms.CREATE_FEATURE);
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(accessService.hasPermission).toHaveBeenCalledWith(
|
|
|
|
req.user,
|
|
|
|
perms.CREATE_FEATURE,
|
|
|
|
projectId,
|
|
|
|
);
|
2021-03-11 22:51:58 +01:00
|
|
|
});
|
2021-05-05 22:32:25 +02:00
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Need access to UPDATE_FEATURE on the project you change to', async () => {
|
2021-05-05 22:32:25 +02:00
|
|
|
const oldProjectId = 'some-project-34';
|
|
|
|
const newProjectId = 'some-project-35';
|
|
|
|
const featureName = 'some-feature-toggle';
|
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn().mockReturnValue(true),
|
2021-05-05 22:32:25 +02:00
|
|
|
};
|
2021-05-28 11:10:24 +02:00
|
|
|
featureToggleStore.getProjectId = jest.fn().mockReturnValue(oldProjectId);
|
2021-05-05 22:32:25 +02:00
|
|
|
|
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-05-05 22:32:25 +02:00
|
|
|
const req: any = {
|
|
|
|
user: new User({ username: 'user', id: 1 }),
|
|
|
|
params: { featureName },
|
|
|
|
body: { featureName, project: newProjectId },
|
|
|
|
};
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
|
|
|
await req.checkRbac(perms.UPDATE_FEATURE);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(accessService.hasPermission).toHaveBeenCalledTimes(2);
|
|
|
|
expect(accessService.hasPermission).toHaveBeenNthCalledWith(
|
|
|
|
1,
|
|
|
|
req.user,
|
|
|
|
perms.UPDATE_FEATURE,
|
|
|
|
oldProjectId,
|
|
|
|
);
|
|
|
|
expect(accessService.hasPermission).toHaveBeenNthCalledWith(
|
|
|
|
2,
|
|
|
|
req.user,
|
|
|
|
perms.UPDATE_FEATURE,
|
|
|
|
newProjectId,
|
|
|
|
);
|
2021-05-05 22:32:25 +02:00
|
|
|
});
|
|
|
|
|
2021-05-28 11:10:24 +02:00
|
|
|
test('Does not double check permission if not changing project when updating toggle', async () => {
|
2021-05-05 22:32:25 +02:00
|
|
|
const oldProjectId = 'some-project-34';
|
|
|
|
const featureName = 'some-feature-toggle';
|
|
|
|
const accessService = {
|
2021-05-28 11:10:24 +02:00
|
|
|
hasPermission: jest.fn().mockReturnValue(true),
|
2021-05-05 22:32:25 +02:00
|
|
|
};
|
2021-05-28 11:10:24 +02:00
|
|
|
featureToggleStore.getProjectId = jest.fn().mockReturnValue(oldProjectId);
|
2021-05-05 22:32:25 +02:00
|
|
|
|
|
|
|
const func = rbacMiddleware(config, { featureToggleStore }, accessService);
|
2021-05-28 11:10:24 +02:00
|
|
|
const cb = jest.fn();
|
2021-05-05 22:32:25 +02:00
|
|
|
const req: any = {
|
|
|
|
user: new User({ username: 'user', id: 1 }),
|
|
|
|
params: { featureName },
|
|
|
|
body: { featureName, project: oldProjectId },
|
|
|
|
};
|
|
|
|
func(req, undefined, cb);
|
|
|
|
|
|
|
|
await req.checkRbac(perms.UPDATE_FEATURE);
|
2021-05-28 11:10:24 +02:00
|
|
|
expect(accessService.hasPermission).toHaveBeenCalledTimes(1);
|
|
|
|
expect(accessService.hasPermission).toHaveBeenCalledWith(
|
|
|
|
req.user,
|
|
|
|
perms.UPDATE_FEATURE,
|
|
|
|
oldProjectId,
|
|
|
|
);
|
2021-05-05 22:32:25 +02:00
|
|
|
});
|