feat(editor): Add routing middleware, permission checks, RBAC store, RBAC component (#7702)

Github issue / Community forum post (link here to close automatically):

---------

Co-authored-by: Csaba Tuncsik <csaba@n8n.io>
This commit is contained in:
Alex Grozav
2023-11-23 13:22:47 +02:00
committed by GitHub
parent fdb2c18ecc
commit 67a88914f2
62 changed files with 1935 additions and 646 deletions

View File

@@ -1,9 +1,9 @@
import { afterAll, beforeAll, vi } from 'vitest';
import { afterAll, beforeAll } from 'vitest';
import { setActivePinia, createPinia } from 'pinia';
import { waitFor } from '@testing-library/vue';
import { setupServer } from '@/__tests__/server';
import VariablesView from '@/views/VariablesView.vue';
import { useSettingsStore, useUsersStore } from '@/stores';
import { useSettingsStore, useUsersStore, useRBACStore } from '@/stores';
import { createComponentRenderer } from '@/__tests__/render';
import { EnterpriseEditionFeature } from '@/constants';
@@ -12,6 +12,7 @@ describe('VariablesView', () => {
let pinia: ReturnType<typeof createPinia>;
let settingsStore: ReturnType<typeof useSettingsStore>;
let usersStore: ReturnType<typeof useUsersStore>;
let rbacStore: ReturnType<typeof useRBACStore>;
const renderComponent = createComponentRenderer(VariablesView);
@@ -25,6 +26,7 @@ describe('VariablesView', () => {
settingsStore = useSettingsStore();
usersStore = useUsersStore();
rbacStore = useRBACStore();
await settingsStore.getSettings();
await usersStore.fetchUsers();
await usersStore.loginWithCookie();
@@ -41,29 +43,12 @@ describe('VariablesView', () => {
});
describe('should render empty state', () => {
it('when feature is enabled and logged in user is owner', async () => {
settingsStore.settings.enterprise[EnterpriseEditionFeature.Variables] = true;
vi.spyOn(usersStore, 'currentUser', 'get').mockReturnValue({
isOwner: true,
});
it('when feature is disabled and logged in user is not owner', async () => {
settingsStore.settings.enterprise[EnterpriseEditionFeature.Variables] = false;
rbacStore.setGlobalScopes(['variable:read', 'variable:list']);
const { queryByTestId } = renderComponent({ pinia });
await waitFor(() => {
expect(queryByTestId('empty-resources-list')).toBeVisible();
expect(queryByTestId('unavailable-resources-list')).not.toBeInTheDocument();
expect(queryByTestId('cannot-create-variables')).not.toBeInTheDocument();
});
});
it('when feature is disabled and logged in user is owner', async () => {
settingsStore.settings.enterprise[EnterpriseEditionFeature.Variables] = false;
vi.spyOn(usersStore, 'currentUser', 'get').mockReturnValue({
isOwner: true,
});
const { queryByTestId } = renderComponent(VariablesView, { pinia });
await waitFor(() => {
expect(queryByTestId('empty-resources-list')).not.toBeInTheDocument();
expect(queryByTestId('unavailable-resources-list')).toBeVisible();
@@ -71,13 +56,49 @@ describe('VariablesView', () => {
});
});
it('when feature is eanbled and logged in user is not owner', async () => {
settingsStore.settings.enterprise[EnterpriseEditionFeature.Variables] = true;
vi.spyOn(usersStore, 'currentUser', 'get').mockReturnValue({
isDefaultUser: true,
});
it('when feature is disabled and logged in user is owner', async () => {
settingsStore.settings.enterprise[EnterpriseEditionFeature.Variables] = false;
rbacStore.setGlobalScopes([
'variable:create',
'variable:read',
'variable:update',
'variable:delete',
'variable:list',
]);
const { queryByTestId } = renderComponent(VariablesView, { pinia });
const { queryByTestId } = renderComponent({ pinia });
await waitFor(() => {
expect(queryByTestId('empty-resources-list')).not.toBeInTheDocument();
expect(queryByTestId('unavailable-resources-list')).toBeVisible();
expect(queryByTestId('cannot-create-variables')).not.toBeInTheDocument();
});
});
it('when feature is enabled and logged in user is owner', async () => {
settingsStore.settings.enterprise[EnterpriseEditionFeature.Variables] = true;
rbacStore.setGlobalScopes([
'variable:create',
'variable:read',
'variable:update',
'variable:delete',
'variable:list',
]);
const { queryByTestId } = renderComponent({ pinia });
await waitFor(() => {
expect(queryByTestId('empty-resources-list')).not.toBeInTheDocument();
expect(queryByTestId('unavailable-resources-list')).not.toBeInTheDocument();
expect(queryByTestId('cannot-create-variables')).not.toBeInTheDocument();
});
});
it('when feature is enabled and logged in user is not owner', async () => {
settingsStore.settings.enterprise[EnterpriseEditionFeature.Variables] = true;
rbacStore.setGlobalScopes(['variable:read', 'variable:list']);
const { queryByTestId } = renderComponent({ pinia });
await waitFor(() => {
expect(queryByTestId('empty-resources-list')).not.toBeInTheDocument();