Skip to content

Commit

Permalink
added the warning message when a user with bot account gets deleted (m…
Browse files Browse the repository at this point in the history
  • Loading branch information
pradeepmurugesan authored and hanzei committed Jun 19, 2019
1 parent 2e6c815 commit 8dbde42
Show file tree
Hide file tree
Showing 5 changed files with 200 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`components/admin_console/system_users/system_users_dropdown/system_users_dropdown.jsx renderDeactivateMemberModal should not render the bot accounts warning in case the user do not have any bot accounts 1`] = `
<div>
<InjectIntl(FormattedMarkdownMessage)
defaultMessage="This action deactivates {username}. They will be logged out and not have access to any teams or channels on this system.\\\\n"
id="deactivate_member_modal.desc"
values={
Object {
"username": "some-user",
}
}
/>
<InjectIntl(FormattedMarkdownMessage)
defaultMessage="Are you sure you want to deactivate {username}?"
id="deactivate_member_modal.desc.confirm"
values={
Object {
"username": "some-user",
}
}
/>
<strong>
<br />
<br />
<FormattedMessage
defaultMessage="You must also deactivate this user in the SSO provider or they will be reactivated on next login or sync."
id="deactivate_member_modal.sso_warning"
values={Object {}}
/>
</strong>
</div>
`;

exports[`components/admin_console/system_users/system_users_dropdown/system_users_dropdown.jsx renderDeactivateMemberModal should render the bot accounts warning in case the user do has any bot accounts 1`] = `
<div>
<InjectIntl(FormattedMarkdownMessage)
defaultMessage="This action deactivates {username}.\\\\n \\\\n * They will be logged out and not have access to any teams or channels on this system.\\\\n * Bot accounts they manage will be disabled along with their integrations. To enable them again, go to Integrations > Bot Accounts. [Learn more about bot accounts](!https://mattermost.com/pl/default-bot-accounts).\\\\n \\\\n \\\\n"
id="deactivate_member_modal.desc.for_users_with_bot_accounts"
values={
Object {
"username": "some-user",
}
}
/>
<InjectIntl(FormattedMarkdownMessage)
defaultMessage="Are you sure you want to deactivate {username}?"
id="deactivate_member_modal.desc.confirm"
values={
Object {
"username": "some-user",
}
}
/>
<strong>
<br />
<br />
<FormattedMessage
defaultMessage="You must also deactivate this user in the SSO provider or they will be reactivated on next login or sync."
id="deactivate_member_modal.sso_warning"
values={Object {}}
/>
</strong>
</div>
`;
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,19 @@ import {bindActionCreators} from 'redux';

import {updateUserActive, revokeAllSessionsForUser} from 'mattermost-redux/actions/users';
import {getCurrentUser} from 'mattermost-redux/selectors/entities/users';
import {getBotAccounts} from 'mattermost-redux/selectors/entities/bots';
import {loadBots} from 'mattermost-redux/actions/bots';

import * as Selectors from 'mattermost-redux/selectors/entities/admin';

import SystemUsersDropdown from './system_users_dropdown.jsx';

function mapStateToProps(state) {
const bots = getBotAccounts(state);
return {
config: Selectors.getConfig(state),
currentUser: getCurrentUser(state),
bots,
};
}

Expand All @@ -20,6 +27,7 @@ function mapDispatchToProps(dispatch) {
actions: bindActionCreators({
updateUserActive,
revokeAllSessionsForUser,
loadBots,
}, dispatch),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as UserUtils from 'mattermost-redux/utils/user_utils';
import {Permissions} from 'mattermost-redux/constants';

import {adminResetMfa} from 'actions/admin_actions.jsx';
import FormattedMarkdownMessage from 'components/formatted_markdown_message.jsx';
import {Constants} from 'utils/constants.jsx';
import * as Utils from 'utils/utils.jsx';
import {t} from 'utils/i18n';
Expand Down Expand Up @@ -79,7 +80,11 @@ export default class SystemUsersDropdown extends React.PureComponent {
actions: PropTypes.shape({
updateUserActive: PropTypes.func.isRequired,
revokeAllSessionsForUser: PropTypes.func.isRequired,
loadBots: PropTypes.func.isRequired,
}).isRequired,
config: PropTypes.object.isRequired,
bots: PropTypes.object.isRequired,

};

constructor(props) {
Expand Down Expand Up @@ -132,9 +137,17 @@ export default class SystemUsersDropdown extends React.PureComponent {
adminResetMfa(this.props.user.id, null, this.props.onError);
}

handleShowDeactivateMemberModal = (e) => {
handleShowDeactivateMemberModal = async (e) => {
e.preventDefault();
this.setState({showDeactivateMemberModal: true});
if (this.shouldDisableBotsWhenOwnerIsDeactivated()) {
const {data} = await this.props.actions.loadBots(
Constants.Integrations.START_PAGE_NUM,
Constants.Integrations.PAGE_SIZE
);
if (data) {
this.setState({loading: false, showDeactivateMemberModal: true});
}
}
}

handleDeactivateMember = () => {
Expand All @@ -155,7 +168,6 @@ export default class SystemUsersDropdown extends React.PureComponent {

renderDeactivateMemberModal = () => {
const user = this.props.user;

const title = (
<FormattedMessage
id='deactivate_member_modal.title'
Expand All @@ -166,6 +178,15 @@ export default class SystemUsersDropdown extends React.PureComponent {
/>
);

const defaultMessage = (
<FormattedMarkdownMessage
id='deactivate_member_modal.desc'
defaultMessage='This action deactivates {username}. They will be logged out and not have access to any teams or channels on this system.\n'
values={{
username: user.username,
}}
/>);

let warning;
if (user.auth_service !== '' && user.auth_service !== Constants.EMAIL_SERVICE) {
warning = (
Expand All @@ -180,15 +201,35 @@ export default class SystemUsersDropdown extends React.PureComponent {
);
}

const confirmationMessage = (
<FormattedMarkdownMessage
id='deactivate_member_modal.desc.confirm'
defaultMessage='Are you sure you want to deactivate {username}?'
values={{
username: user.username,
}}
/>);
let messageForUsersWithBotAccounts;
if (this.shouldDisableBotsWhenOwnerIsDeactivated()) {
for (const bot of Object.values(this.props.bots)) {
if (bot.owner_id === user.id) {
messageForUsersWithBotAccounts = (
<FormattedMarkdownMessage
id='deactivate_member_modal.desc.for_users_with_bot_accounts'
defaultMessage='This action deactivates {username}.\n \n * They will be logged out and not have access to any teams or channels on this system.\n * Bot accounts they manage will be disabled along with their integrations. To enable them again, go to Integrations > Bot Accounts. [Learn more about bot accounts](!https://mattermost.com/pl/default-bot-accounts).\n \n \n'
values={{
username: user.username,
}}
/>);
break;
}
}
}

const message = (
<div>
<FormattedMessage
id='deactivate_member_modal.desc'
defaultMessage='This action deactivates {username}. They will be logged out and not have access to any teams or channels on this system. Are you sure you want to deactivate {username}?'
values={{
username: user.username,
}}
/>
{messageForUsersWithBotAccounts || defaultMessage}
{confirmationMessage}
{warning}
</div>
);
Expand All @@ -214,6 +255,12 @@ export default class SystemUsersDropdown extends React.PureComponent {
);
}

shouldDisableBotsWhenOwnerIsDeactivated() {
return this.props.config &&
this.props.config.ServiceSettings &&
this.props.config.ServiceSettings.DisableBotsWhenOwnerIsDeactivated;
}

handleShowRevokeSessionsModal = (e) => {
e.preventDefault();
this.setState({showRevokeSessionsModal: true});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ describe('components/admin_console/system_users/system_users_dropdown/system_use
const user = {
id: 'user_id',
roles: '',
username: 'some-user',
};

const requiredProps = {
Expand All @@ -30,7 +31,12 @@ describe('components/admin_console/system_users/system_users_dropdown/system_use
actions: {
updateUserActive: jest.fn().mockResolvedValue({data: true}),
revokeAllSessionsForUser: jest.fn().mockResolvedValue({data: true}),
loadBots: jest.fn(() => Promise.resolve({})),
},
config: {
ServiceSettings: {},
},
bots: {},
};

test('handleMakeActive() should have called updateUserActive', async () => {
Expand Down Expand Up @@ -96,4 +102,65 @@ describe('components/admin_console/system_users/system_users_dropdown/system_use

expect(onError).toHaveBeenCalled();
});

test('handleShowDeactivateMemberModal should not call the loadBots if the setting is not true', async () => {
const wrapper = shallow(<SystemUsersDropdown {...requiredProps}/>);

const event = {preventDefault: jest.fn()};
await wrapper.instance().handleShowDeactivateMemberModal(event);

expect(requiredProps.actions.loadBots).toHaveBeenCalledTimes(0);
});

test('handleShowDeactivateMemberModal should call the loadBots only if the setting is true', async () => {
const overrideConfig = {
ServiceSettings: {
DisableBotsWhenOwnerIsDeactivated: true,
},
};
const wrapper = shallow(<SystemUsersDropdown {...{...requiredProps, config: overrideConfig, bots: { }}}/>);

const event = {preventDefault: jest.fn()};
await wrapper.instance().handleShowDeactivateMemberModal(event);

expect(requiredProps.actions.loadBots).toHaveBeenCalledTimes(1);
});

test('renderDeactivateMemberModal should not render the bot accounts warning in case the user do not have any bot accounts', async () => {
const overrideProps = {
config: {
ServiceSettings: {
DisableBotsWhenOwnerIsDeactivated: true,
},
},
bots: {
1: {owner_id: '1'},
2: {owner_id: '1'},
3: {owner_id: '2'},
},
};
const wrapper = shallow(<SystemUsersDropdown {...{...requiredProps, ...overrideProps}}/>);

const modal = wrapper.wrap(wrapper.instance().renderDeactivateMemberModal());
expect(modal.prop('message')).toMatchSnapshot();
});

test('renderDeactivateMemberModal should render the bot accounts warning in case the user do has any bot accounts', async () => {
const overrideProps = {
config: {
ServiceSettings: {
DisableBotsWhenOwnerIsDeactivated: true,
},
},
bots: {
1: {owner_id: '1'},
2: {owner_id: '1'},
3: {owner_id: 'user_id'},
},
};
const wrapper = shallow(<SystemUsersDropdown {...{...requiredProps, ...overrideProps}}/>);

const modal = wrapper.wrap(wrapper.instance().renderDeactivateMemberModal());
expect(modal.prop('message')).toMatchSnapshot();
});
});
4 changes: 3 additions & 1 deletion i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -1884,7 +1884,9 @@
"create_team.team_url.webAddress": "Choose the web address of your new team:",
"custom_emoji.header": "Custom Emoji",
"deactivate_member_modal.deactivate": "Deactivate",
"deactivate_member_modal.desc": "This action deactivates {username}. They will be logged out and not have access to any teams or channels on this system. Are you sure you want to deactivate {username}?",
"deactivate_member_modal.desc": "This action deactivates {username}. They will be logged out and not have access to any teams or channels on this system.\n",
"deactivate_member_modal.desc.confirm": " Are you sure you want to deactivate {username}?",
"deactivate_member_modal.desc.for_users_with_bot_accounts": "This action deactivates {username}.\n \n * They will be logged out and not have access to any teams or channels on this system.\n * Bot accounts they manage will be disabled along with their integrations. To enable them again, go to Integrations > Bot Accounts. [Learn more about bot accounts](!https://mattermost.com/pl/default-bot-accounts).\n \n \n",
"deactivate_member_modal.sso_warning": "You must also deactivate this user in the SSO provider or they will be reactivated on next login or sync.",
"deactivate_member_modal.title": "Deactivate {username}",
"default_channel.purpose": "Post messages here that you want everyone to see. Everyone automatically becomes a permanent member of this channel when they join the team.",
Expand Down

0 comments on commit 8dbde42

Please sign in to comment.