mirror of
https://github.com/strapi/strapi.git
synced 2025-07-29 20:10:21 +00:00
Add tests
Signed-off-by: soupette <cyril.lpz@gmail.com>
This commit is contained in:
parent
e83e25c298
commit
044c47e732
@ -166,6 +166,7 @@ const LeftMenu = forwardRef(({ version, plugins }, ref) => {
|
||||
searchable
|
||||
/>
|
||||
)}
|
||||
|
||||
<LeftMenuLinksSection
|
||||
section="plugins"
|
||||
name="plugins"
|
||||
|
@ -1,7 +1,6 @@
|
||||
const generateArrayOfLinks = object => object.map(({ links }) => links).flat();
|
||||
|
||||
const findFirstAllowedEndpoint = menuObject => {
|
||||
console.log({ menuObject });
|
||||
const arrayOfLinks = generateArrayOfLinks(menuObject);
|
||||
|
||||
const link = arrayOfLinks.find(link => link.isDisplayed === true);
|
||||
|
@ -1,10 +1,190 @@
|
||||
// TODO
|
||||
// import findFirstAllowedEndpoint, { generateArrayOfLinks } from '../findFirstAllowedEndpoint';
|
||||
import findFirstAllowedEndpoint, { generateArrayOfLinks } from '../findFirstAllowedEndpoint';
|
||||
|
||||
describe('ADMIN | SettingsPage | utils', () => {
|
||||
describe('findFirstAllowedEndpoint', () => {
|
||||
it('should', () => {
|
||||
expect(true).toBe(true);
|
||||
it('should return null if there is no sections', () => {
|
||||
expect(findFirstAllowedEndpoint([])).toBeNull();
|
||||
});
|
||||
|
||||
it('should return null if all links are hidden', () => {
|
||||
const data = [
|
||||
{
|
||||
id: 'global',
|
||||
links: [
|
||||
{
|
||||
to: 'global.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'global.test2',
|
||||
isDisplayed: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'test',
|
||||
links: [
|
||||
{
|
||||
to: 'test.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'test.test2',
|
||||
isDisplayed: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
expect(findFirstAllowedEndpoint(data)).toBeNull();
|
||||
});
|
||||
|
||||
it('should return the destination of the first displayed link', () => {
|
||||
const data = [
|
||||
{
|
||||
id: 'global',
|
||||
links: [
|
||||
{
|
||||
to: 'global.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'global.test2',
|
||||
isDisplayed: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'test',
|
||||
links: [
|
||||
{
|
||||
to: 'test.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'test.test2',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test.test3',
|
||||
isDisplayed: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'test1',
|
||||
links: [
|
||||
{
|
||||
to: 'test1.test',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test1.test2',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test1.test3',
|
||||
isDisplayed: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
|
||||
expect(findFirstAllowedEndpoint(data)).toEqual('test.test2');
|
||||
});
|
||||
});
|
||||
|
||||
describe('generateArrayOfLinks', () => {
|
||||
it('should return an empty array', () => {
|
||||
expect(generateArrayOfLinks([])).toEqual([]);
|
||||
});
|
||||
|
||||
it('should return an array containing all the links', () => {
|
||||
const data = [
|
||||
{
|
||||
id: 'global',
|
||||
links: [
|
||||
{
|
||||
to: 'global.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'global.test2',
|
||||
isDisplayed: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'test',
|
||||
links: [
|
||||
{
|
||||
to: 'test.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'test.test2',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test.test3',
|
||||
isDisplayed: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'test1',
|
||||
links: [
|
||||
{
|
||||
to: 'test1.test',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test1.test2',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test1.test3',
|
||||
isDisplayed: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
];
|
||||
const expected = [
|
||||
{
|
||||
to: 'global.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'global.test2',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'test.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'test.test2',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test.test3',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test1.test',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test1.test2',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test1.test3',
|
||||
isDisplayed: true,
|
||||
},
|
||||
];
|
||||
|
||||
expect(generateArrayOfLinks(data)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -1,5 +1,5 @@
|
||||
import produce from 'immer';
|
||||
import { set, unset } from 'lodash';
|
||||
import { set } from 'lodash';
|
||||
|
||||
const initialState = {
|
||||
menu: [],
|
||||
@ -18,11 +18,16 @@ const reducer = (state, action) =>
|
||||
['menu', ...checkedPermissions.path.split('.'), 'isDisplayed'],
|
||||
checkedPermissions.hasPermission
|
||||
);
|
||||
} else {
|
||||
unset(draftState, ['menu', ...checkedPermissions.path.split('.')]);
|
||||
}
|
||||
});
|
||||
|
||||
// Remove the not needed links in each section
|
||||
draftState.menu.forEach((section, sectionIndex) => {
|
||||
draftState.menu[sectionIndex].links = section.links.filter(
|
||||
link => link.isDisplayed === true
|
||||
);
|
||||
});
|
||||
|
||||
draftState.isLoading = false;
|
||||
break;
|
||||
}
|
||||
|
@ -1,7 +1,91 @@
|
||||
describe('ADMIN | SettingsPage | utils', () => {
|
||||
describe('findFirstAllowedEndpoint', () => {
|
||||
it('should', () => {
|
||||
expect(true).toBe(true);
|
||||
});
|
||||
import { SETTINGS_BASE_URL } from '../../../config';
|
||||
import adminPermissions from '../../../permissions';
|
||||
import init from '../init';
|
||||
|
||||
describe('ADMIN | hooks | useSettingsMenu | init', () => {
|
||||
it('should return the settings menu', () => {
|
||||
const plugins = {
|
||||
upload: {
|
||||
settings: {
|
||||
global: {
|
||||
links: [
|
||||
{
|
||||
name: 'media-library',
|
||||
permissions: [
|
||||
{
|
||||
action: 'plugins::upload.settings.read',
|
||||
subject: null,
|
||||
},
|
||||
],
|
||||
title: { id: 'upload.plugin.name', defaultMessage: 'Media Library' },
|
||||
to: '/settings/media-library',
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
const initialState = {
|
||||
isLoading: true,
|
||||
menu: [],
|
||||
};
|
||||
const expected = {
|
||||
isLoading: true,
|
||||
menu: [
|
||||
{
|
||||
id: 'global',
|
||||
links: [
|
||||
{
|
||||
isDisplayed: false,
|
||||
name: 'media-library',
|
||||
permissions: [
|
||||
{
|
||||
action: 'plugins::upload.settings.read',
|
||||
subject: null,
|
||||
},
|
||||
],
|
||||
title: { id: 'upload.plugin.name', defaultMessage: 'Media Library' },
|
||||
to: '/settings/media-library',
|
||||
},
|
||||
{
|
||||
isDisplayed: false,
|
||||
name: 'webhooks',
|
||||
permissions: [
|
||||
{ action: 'admin::webhooks.create', subject: null },
|
||||
{ action: 'admin::webhooks.read', subject: null },
|
||||
{ action: 'admin::webhooks.update', subject: null },
|
||||
{ action: 'admin::webhooks.delete', subject: null },
|
||||
],
|
||||
title: { id: 'Settings.webhooks.title' },
|
||||
to: '/settings/webhooks',
|
||||
},
|
||||
],
|
||||
title: { id: 'Settings.global' },
|
||||
},
|
||||
{
|
||||
id: 'permissions',
|
||||
title: 'Settings.permissions',
|
||||
links: [
|
||||
{
|
||||
title: { id: 'Settings.permissions.menu.link.roles.label' },
|
||||
to: `${SETTINGS_BASE_URL}/roles`,
|
||||
name: 'roles',
|
||||
isDisplayed: false,
|
||||
permissions: adminPermissions.settings.roles.main,
|
||||
},
|
||||
{
|
||||
title: { id: 'Settings.permissions.menu.link.users.label' },
|
||||
// Init the search params directly
|
||||
to: `${SETTINGS_BASE_URL}/users?pageSize=10&page=1&_sort=firstname%3AASC`,
|
||||
name: 'users',
|
||||
isDisplayed: false,
|
||||
permissions: adminPermissions.settings.users.main,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
expect(init(initialState, plugins)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
@ -1,7 +1,116 @@
|
||||
describe('ADMIN | SettingsPage | utils', () => {
|
||||
describe('findFirstAllowedEndpoint', () => {
|
||||
it('should', () => {
|
||||
expect(true).toBe(true);
|
||||
import reducer from '../reducer';
|
||||
|
||||
describe('ADMIN | hooks | useSettingsMenu | reducer', () => {
|
||||
describe('DEFAULT_ACTION', () => {
|
||||
it('should return the state', () => {
|
||||
const initialState = {
|
||||
ok: true,
|
||||
};
|
||||
const expected = {
|
||||
ok: true,
|
||||
};
|
||||
|
||||
expect(reducer(initialState, {})).toEqual(expected);
|
||||
});
|
||||
});
|
||||
|
||||
describe('CHECK_PERMISSIONS_SUCCEEDED', () => {
|
||||
it('should set the permissions correctly', () => {
|
||||
const initialState = {
|
||||
isLoading: true,
|
||||
menu: [
|
||||
{
|
||||
id: 'global',
|
||||
links: [
|
||||
{
|
||||
to: 'global.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'global.test2',
|
||||
isDisplayed: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'test',
|
||||
links: [
|
||||
{
|
||||
to: 'test.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'test.test2',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'test.test3',
|
||||
isDisplayed: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'test1',
|
||||
links: [
|
||||
{
|
||||
to: 'test1.test',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'test1.test2',
|
||||
isDisplayed: false,
|
||||
},
|
||||
{
|
||||
to: 'test1.test3',
|
||||
isDisplayed: false,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
const action = {
|
||||
type: 'CHECK_PERMISSIONS_SUCCEEDED',
|
||||
data: [
|
||||
{ hasPermission: true, path: '1.links.0' },
|
||||
{ hasPermission: true, path: '1.links.1' },
|
||||
{ hasPermission: true, path: '0.links.1' },
|
||||
{ hasPermission: undefined, path: '2.links.0' },
|
||||
],
|
||||
};
|
||||
|
||||
const expected = {
|
||||
isLoading: false,
|
||||
menu: [
|
||||
{
|
||||
id: 'global',
|
||||
links: [
|
||||
{
|
||||
to: 'global.test2',
|
||||
isDisplayed: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'test',
|
||||
links: [
|
||||
{
|
||||
to: 'test.test',
|
||||
isDisplayed: true,
|
||||
},
|
||||
{
|
||||
to: 'test.test2',
|
||||
isDisplayed: true,
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 'test1',
|
||||
links: [],
|
||||
},
|
||||
],
|
||||
};
|
||||
|
||||
expect(reducer(initialState, action)).toEqual(expected);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@ -33,6 +33,7 @@ const permissions = {
|
||||
read: [
|
||||
{ action: 'admin::users.read', subject: null },
|
||||
{ action: 'admin::users.update', subject: null },
|
||||
{ action: 'admin::users.delete', subject: null },
|
||||
],
|
||||
update: [{ action: 'admin::users.update', subject: null }],
|
||||
},
|
||||
|
@ -248,24 +248,24 @@ const data = {
|
||||
// },
|
||||
|
||||
// Admin webhooks
|
||||
{
|
||||
action: 'admin::webhooks.create',
|
||||
subject: null,
|
||||
fields: null,
|
||||
conditions: [],
|
||||
},
|
||||
{
|
||||
action: 'admin::webhooks.read',
|
||||
subject: null,
|
||||
fields: null,
|
||||
conditions: [],
|
||||
},
|
||||
{
|
||||
action: 'admin::webhooks.update',
|
||||
subject: null,
|
||||
fields: null,
|
||||
conditions: [],
|
||||
},
|
||||
// {
|
||||
// action: 'admin::webhooks.create',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions: [],
|
||||
// },
|
||||
// {
|
||||
// action: 'admin::webhooks.read',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions: [],
|
||||
// },
|
||||
// {
|
||||
// action: 'admin::webhooks.update',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions: [],
|
||||
// },
|
||||
// {
|
||||
// action: 'admin::webhooks.delete',
|
||||
// subject: null,
|
||||
@ -274,30 +274,30 @@ const data = {
|
||||
// },
|
||||
|
||||
// // Admin users
|
||||
{
|
||||
action: 'admin::users.create',
|
||||
subject: null,
|
||||
fields: null,
|
||||
conditions: [],
|
||||
},
|
||||
// {
|
||||
// action: 'admin::users.create',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions: [],
|
||||
// },
|
||||
// {
|
||||
// action: 'admin::users.read',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions: [],
|
||||
// },
|
||||
{
|
||||
action: 'admin::users.update',
|
||||
subject: null,
|
||||
fields: null,
|
||||
conditions: [],
|
||||
},
|
||||
{
|
||||
action: 'admin::users.delete',
|
||||
subject: null,
|
||||
fields: null,
|
||||
conditions: [],
|
||||
},
|
||||
// {
|
||||
// action: 'admin::users.update',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions: [],
|
||||
// },
|
||||
// {
|
||||
// action: 'admin::users.delete',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions: [],
|
||||
// },
|
||||
|
||||
// // Admin roles
|
||||
// {
|
||||
@ -306,12 +306,12 @@ const data = {
|
||||
// fields: null,
|
||||
// conditions: [],
|
||||
// },
|
||||
{
|
||||
action: 'admin::roles.read',
|
||||
subject: null,
|
||||
fields: null,
|
||||
conditions: [],
|
||||
},
|
||||
// {
|
||||
// action: 'admin::roles.read',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions: [],
|
||||
// },
|
||||
// {
|
||||
// action: 'admin::roles.update',
|
||||
// subject: null,
|
||||
@ -416,12 +416,12 @@ const data = {
|
||||
// fields: null,
|
||||
// conditions: [],
|
||||
// },
|
||||
{
|
||||
action: 'plugins::upload.settings.read',
|
||||
subject: null,
|
||||
fields: null,
|
||||
conditions: null,
|
||||
},
|
||||
// {
|
||||
// action: 'plugins::upload.settings.read',
|
||||
// subject: null,
|
||||
// fields: null,
|
||||
// conditions: null,
|
||||
// },
|
||||
|
||||
// Users-permissions
|
||||
{
|
||||
|
@ -20,7 +20,7 @@ const shouldCheckPermissions = permissions =>
|
||||
!isEmpty(permissions) && permissions.every(perm => !isEmpty(perm.conditions));
|
||||
|
||||
const hasPermissions = async (userPermissions, permissions) => {
|
||||
if (!permissions.length) {
|
||||
if (!permissions || !permissions.length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
@ -9,6 +9,7 @@ import ListWrapper from '../ListWrapper';
|
||||
import CardControl from '../CardControl';
|
||||
|
||||
const SortableList = ({
|
||||
allowedActions,
|
||||
canSelect,
|
||||
data,
|
||||
moveAsset,
|
||||
@ -54,7 +55,7 @@ const SortableList = ({
|
||||
/>
|
||||
</CardControlsWrapper>
|
||||
)}
|
||||
{!noNavigation && (
|
||||
{!noNavigation && allowedActions.canUpdate && (
|
||||
<CardControlsWrapper className="card-control-wrapper card-control-wrapper-hidden">
|
||||
<CardControl
|
||||
small
|
||||
@ -75,6 +76,9 @@ const SortableList = ({
|
||||
};
|
||||
|
||||
SortableList.defaultProps = {
|
||||
allowedActions: {
|
||||
canUpdate: false,
|
||||
},
|
||||
canSelect: true,
|
||||
data: [],
|
||||
moveAsset: () => {},
|
||||
@ -85,6 +89,7 @@ SortableList.defaultProps = {
|
||||
};
|
||||
|
||||
SortableList.propTypes = {
|
||||
allowedActions: PropTypes.object,
|
||||
canSelect: PropTypes.bool,
|
||||
data: PropTypes.array,
|
||||
moveAsset: PropTypes.func,
|
||||
|
@ -13,6 +13,7 @@ import ListWrapper from './ListWrapper';
|
||||
|
||||
const SelectedAssets = () => {
|
||||
const {
|
||||
allowedActions,
|
||||
selectedFiles,
|
||||
handleFileSelection,
|
||||
handleGoToEditFile,
|
||||
@ -39,6 +40,7 @@ const SelectedAssets = () => {
|
||||
</Flex>
|
||||
<ListWrapper>
|
||||
<SortableList
|
||||
allowedActions={allowedActions}
|
||||
data={selectedFiles}
|
||||
moveAsset={moveAsset}
|
||||
noNavigation={noNavigation}
|
||||
|
@ -50,7 +50,7 @@ export default strapi => {
|
||||
defaultMessage: 'Media Library',
|
||||
},
|
||||
name: 'media-library',
|
||||
to: `${strapi.settingsBaseURL}/upload`,
|
||||
to: `${strapi.settingsBaseURL}/media-library`,
|
||||
Component: () => (
|
||||
<CheckPagePermissions permissions={pluginPermissions.settings}>
|
||||
<SettingsPage />
|
||||
|
@ -12,24 +12,27 @@ const pluginPermissions = {
|
||||
createRole: [{ action: 'plugins::users-permissions.roles.create', subject: null }],
|
||||
readAdvancedSettings: [
|
||||
{ action: 'plugins::users-permissions.advanced-settings.read', subject: null },
|
||||
// { action: 'plugins::users-permissions.advanced-settings.update', subject: null },
|
||||
{ action: 'plugins::users-permissions.advanced-settings.update', subject: null },
|
||||
],
|
||||
updateAdvancedSettings: [
|
||||
{ action: 'plugins::users-permissions.advanced-settings.update', subject: null },
|
||||
],
|
||||
readEmailTemplates: [
|
||||
{ action: 'plugins::users-permissions.email-templates.read', subject: null },
|
||||
// { action: 'plugins::users-permissions.email-templates.update', subject: null },
|
||||
{ action: 'plugins::users-permissions.email-templates.update', subject: null },
|
||||
],
|
||||
updateEmailTemplates: [
|
||||
{ action: 'plugins::users-permissions.email-templates.update', subject: null },
|
||||
],
|
||||
readProviders: [
|
||||
{ action: 'plugins::users-permissions.providers.read', subject: null },
|
||||
// { action: 'plugins::users-permissions.providers.update', subject: null },
|
||||
{ action: 'plugins::users-permissions.providers.update', subject: null },
|
||||
],
|
||||
updateProviders: [{ action: 'plugins::users-permissions.providers.update', subject: null }],
|
||||
readRoles: [{ action: 'plugins::users-permissions.roles.read', subject: null }],
|
||||
readRoles: [
|
||||
{ action: 'plugins::users-permissions.roles.read', subject: null },
|
||||
{ action: 'plugins::users-permissions.roles.update', subject: null },
|
||||
],
|
||||
updateRole: [{ action: 'plugins::users-permissions.roles.update', subject: null }],
|
||||
deleteRole: [{ action: 'plugins::users-permissions.roles.delete', subject: null }],
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user