Skip to content

Commit

Permalink
Check canPost permissions for v5.22+ (mattermost#4193)
Browse files Browse the repository at this point in the history
  • Loading branch information
migbot authored Apr 22, 2020
1 parent bf3fbbc commit 9a97f91
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 17 deletions.
20 changes: 11 additions & 9 deletions app/components/post_textbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import PostTextbox from './post_textbox';

const MAX_MESSAGE_LENGTH = 4000;

function mapStateToProps(state, ownProps) {
export function mapStateToProps(state, ownProps) {
const currentDraft = ownProps.rootId ? getThreadDraft(state, ownProps.rootId) : getCurrentChannelDraft(state);
const config = getConfig(state);

Expand All @@ -50,17 +50,19 @@ function mapStateToProps(state, ownProps) {
const currentChannelStats = getCurrentChannelStats(state);
const currentChannelMembersCount = currentChannelStats?.member_count || 0; // eslint-disable-line camelcase
const isTimezoneEnabled = config?.ExperimentalTimezone === 'true';
const canPost = haveIChannelPermission(
state,
{
channel: currentChannel.id,
team: currentChannel.team_id,
permission: Permissions.CREATE_POST,
},
);

let canPost = true;
let useChannelMentions = true;
if (isMinimumServerVersion(state.entities.general.serverVersion, 5, 22)) {
canPost = haveIChannelPermission(
state,
{
channel: currentChannel.id,
team: currentChannel.team_id,
permission: Permissions.CREATE_POST,
},
);

useChannelMentions = haveIChannelPermission(
state,
{
Expand Down
100 changes: 100 additions & 0 deletions app/components/post_textbox/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.

import {Permissions} from '@mm-redux/constants';
import * as channelSelectors from '@mm-redux/selectors/entities/channels';
import * as userSelectors from '@mm-redux/selectors/entities/users';
import * as generalSelectors from '@mm-redux/selectors/entities/general';
import * as preferenceSelectors from '@mm-redux/selectors/entities/preferences';
import * as roleSelectors from '@mm-redux/selectors/entities/roles';
import * as deviceSelectors from '@selectors/device';

import {isMinimumServerVersion} from '@mm-redux/utils/helpers';

import {mapStateToProps} from './index';

jest.mock('./post_textbox', () => ({
__esModule: true,
default: jest.fn(),
}));

channelSelectors.getCurrentChannel = jest.fn().mockReturnValue({});
channelSelectors.isCurrentChannelReadOnly = jest.fn();
channelSelectors.getCurrentChannelStats = jest.fn();
userSelectors.getStatusForUserId = jest.fn();
generalSelectors.canUploadFilesOnMobile = jest.fn();
preferenceSelectors.getTheme = jest.fn();
roleSelectors.haveIChannelPermission = jest.fn();
deviceSelectors.isLandscape = jest.fn();

describe('mapStateToProps', () => {
const baseState = {
entities: {
general: {
config: {},
serverVersion: '',
},
users: {
currentUserId: '',
},
channels: {
currentChannelId: '',
},
preferences: {
myPreferences: {},
},
},
views: {
channel: {
drafts: {},
},
},
requests: {
files: {
uploadFiles: {
status: '',
},
},
},
};

const baseOwnProps = {};

test('haveIChannelPermission is not called when isMinimumServerVersion is not 5.22v', () => {
const state = {...baseState};
state.entities.general.serverVersion = '5.21';

mapStateToProps(state, baseOwnProps);
expect(isMinimumServerVersion(state.entities.general.serverVersion, 5, 22)).toBe(false);

expect(roleSelectors.haveIChannelPermission).not.toHaveBeenCalledWith(state, {
channel: undefined,
team: undefined,
permission: Permissions.CREATE_POST,
});

expect(roleSelectors.haveIChannelPermission).not.toHaveBeenCalledWith(state, {
channel: undefined,
permission: Permissions.USE_CHANNEL_MENTIONS,
});
});

test('haveIChannelPermission is called when isMinimumServerVersion is 5.22v', () => {
const state = {...baseState};
state.entities.general.serverVersion = '5.22';

mapStateToProps(state, baseOwnProps);
expect(isMinimumServerVersion(state.entities.general.serverVersion, 5, 22)).toBe(true);

expect(roleSelectors.haveIChannelPermission).toHaveBeenCalledWith(state, {
channel: undefined,
team: undefined,
permission: Permissions.CREATE_POST,
});

expect(roleSelectors.haveIChannelPermission).toHaveBeenCalledWith(state, {
channel: undefined,
permission: Permissions.USE_CHANNEL_MENTIONS,
});
});
});
20 changes: 12 additions & 8 deletions app/screens/post_options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,18 @@ export function makeMapStateToProps() {
let {canDelete} = ownProps;
let canFlag = true;
let canPin = true;
const canPost = haveIChannelPermission(
state,
{
channel: post.channel_id,
team: channel.team_id,
permission: Permissions.CREATE_POST,
},
);

let canPost = true;
if (isMinimumServerVersion(serverVersion, 5, 22)) {
canPost = haveIChannelPermission(
state,
{
channel: post.channel_id,
team: channel.team_id,
permission: Permissions.CREATE_POST,
},
);
}

if (hasNewPermissions(state)) {
canAddReaction = haveIChannelPermission(state, {
Expand Down
43 changes: 43 additions & 0 deletions app/screens/post_options/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
// See LICENSE.txt for license information.
import {makeMapStateToProps} from './index';

import {Permissions} from '@mm-redux/constants';
import * as channelSelectors from '@mm-redux/selectors/entities/channels';
import * as generalSelectors from '@mm-redux/selectors/entities/general';
import * as userSelectors from '@mm-redux/selectors/entities/users';
import * as commonSelectors from '@mm-redux/selectors/entities/common';
import * as teamSelectors from '@mm-redux/selectors/entities/teams';
import * as roleSelectors from '@mm-redux/selectors/entities/roles';
import * as deviceSelectors from 'app/selectors/device';
import * as preferencesSelectors from '@mm-redux/selectors/entities/preferences';
import {isMinimumServerVersion} from '@mm-redux/utils/helpers';
Expand All @@ -26,6 +28,7 @@ teamSelectors.getCurrentTeamUrl = jest.fn();
deviceSelectors.getDimensions = jest.fn();
deviceSelectors.isLandscape = jest.fn();
preferencesSelectors.getTheme = jest.fn();
roleSelectors.haveIChannelPermission = jest.fn();

describe('makeMapStateToProps', () => {
const baseState = {
Expand Down Expand Up @@ -135,4 +138,44 @@ describe('makeMapStateToProps', () => {
expect(isMinimumServerVersion(state.entities.general.serverVersion, 5, 18)).toBe(false);
expect(props.canMarkAsUnread).toBe(false);
});

test('haveIChannelPermission for canPost is not called when isMinimumServerVersion is not 5.22v', () => {
const state = {
entities: {
...baseState.entities,
general: {
serverVersion: '5.21',
},
},
};

const mapStateToProps = makeMapStateToProps();
mapStateToProps(state, baseOwnProps);
expect(isMinimumServerVersion(state.entities.general.serverVersion, 5, 22)).toBe(false);
expect(roleSelectors.haveIChannelPermission).not.toHaveBeenCalledWith(state, {
channel: undefined,
team: undefined,
permission: Permissions.CREATE_POST,
});
});

test('haveIChannelPermission for canPost is called when isMinimumServerVersion is 5.22v', () => {
const state = {
entities: {
...baseState.entities,
general: {
serverVersion: '5.22',
},
},
};

const mapStateToProps = makeMapStateToProps();
mapStateToProps(state, baseOwnProps);
expect(isMinimumServerVersion(state.entities.general.serverVersion, 5, 22)).toBe(true);
expect(roleSelectors.haveIChannelPermission).toHaveBeenCalledWith(state, {
channel: undefined,
team: undefined,
permission: Permissions.CREATE_POST,
});
});
});

0 comments on commit 9a97f91

Please sign in to comment.