Commit d81e4fad authored by Paul Slaughter's avatar Paul Slaughter

Add default user permissions const for Web IDE

This constant is what's returned by our
getter when the project data isn't available
yet.

This way we can prevent showing an alert
based on insufficient permissions until
we've actually fetched the permissions. Using
a default value, helps us be future-proof
so we don't need to add more conditions everywhere.
parent dd82a797
......@@ -71,6 +71,7 @@ export default {
'allBlobs',
'emptyRepo',
'currentTree',
'hasCurrentProject',
'editorTheme',
'getUrlForPath',
]),
......
......@@ -16,6 +16,13 @@ export const PERMISSION_CREATE_MR = 'createMergeRequestIn';
export const PERMISSION_READ_MR = 'readMergeRequest';
export const PERMISSION_PUSH_CODE = 'pushCode';
// The default permission object to use when the project data isn't available yet.
// This helps us encapsulate checks like `canPushCode` without requiring an
// additional check like `currentProject && canPushCode`.
export const DEFAULT_PERMISSIONS = {
[PERMISSION_PUSH_CODE]: true,
};
export const viewerTypes = {
mr: 'mrdiff',
edit: 'editor',
......
......@@ -2,6 +2,7 @@ import { getChangesCountForFiles, filePathMatches } from './utils';
import {
leftSidebarViews,
packageJsonPath,
DEFAULT_PERMISSIONS,
PERMISSION_READ_MR,
PERMISSION_CREATE_MR,
PERMISSION_PUSH_CODE,
......@@ -150,7 +151,7 @@ export const getDiffInfo = (state, getters) => (path) => {
};
export const findProjectPermissions = (state, getters) => (projectId) =>
getters.findProject(projectId)?.userPermissions || {};
getters.findProject(projectId)?.userPermissions || DEFAULT_PERMISSIONS;
export const canReadMergeRequests = (state, getters) =>
Boolean(getters.findProjectPermissions(state.currentProjectId)[PERMISSION_READ_MR]);
......
......@@ -21,7 +21,7 @@ describe('WebIDE', () => {
store.state.currentProjectId = 'abcproject';
store.state.currentBranchId = 'master';
store.state.projects.abcproject = { ...projData };
store.state.projects.abcproject = projData && { ...projData };
store.state.trees['abcproject/master'] = {
tree: [],
loading: false,
......@@ -161,13 +161,13 @@ describe('WebIDE', () => {
expect(findAlert().text()).toBe(Ide.MSG_CANNOT_PUSH_CODE);
});
it('when user can push code, no alert is shown', () => {
it.each`
desc | projData
${'when user can push code'} | ${{ userPermissions: { pushCode: true } }}
${'when project is not ready'} | ${null}
`('$desc, no alert is shown', ({ projData }) => {
createComponent({
projData: {
userPermissions: {
pushCode: true,
},
},
projData,
});
expect(findAlert().exists()).toBe(false);
......
......@@ -2,6 +2,7 @@ import { TEST_HOST } from 'helpers/test_constants';
import * as getters from '~/ide/stores/getters';
import { createStore } from '~/ide/stores';
import { file } from '../helpers';
import { DEFAULT_PERMISSIONS } from '../../../../app/assets/javascripts/ide/constants';
const TEST_PROJECT_ID = 'test_project';
......@@ -386,7 +387,9 @@ describe('IDE store getters', () => {
describe('findProjectPermissions', () => {
it('returns false if project not found', () => {
expect(localStore.getters.findProjectPermissions(TEST_PROJECT_ID)).toEqual({});
expect(localStore.getters.findProjectPermissions(TEST_PROJECT_ID)).toEqual(
DEFAULT_PERMISSIONS,
);
});
it('finds permission in given project', () => {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment