Skip to content

Commit

Permalink
Fixing guest being removed from other not-visible user from a channel (
Browse files Browse the repository at this point in the history
…mattermost#3914)

* Fixing guest being removed from other not-visible user from a channel

* Adding tests verifying the new behavior

* fixing lint errors

* Addressing PR review comments

* Fix linting error
  • Loading branch information
jespino committed Oct 11, 2019
1 parent 820eba2 commit 6c0634e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
9 changes: 7 additions & 2 deletions actions/websocket_actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
getMe,
getMissingProfilesByIds,
getStatusesByIds,
getUser as loadUser,
} from 'mattermost-redux/actions/users';
import {Client4} from 'mattermost-redux/client';
import {getCurrentUser, getCurrentUserId, getStatusForUserId, getUser} from 'mattermost-redux/selectors/entities/users';
Expand Down Expand Up @@ -660,7 +661,7 @@ function handleUserAddedEvent(msg) {
}
}

export function handleUserRemovedEvent(msg) {
export async function handleUserRemovedEvent(msg) {
const state = getState();
const currentChannel = getCurrentChannel(state) || {};
const currentUserId = getCurrentUserId(state);
Expand All @@ -677,7 +678,11 @@ export function handleUserRemovedEvent(msg) {
if (msg.data.remover_id === msg.broadcast.user_id) {
browserHistory.push(getCurrentRelativeTeamUrl(state));
} else {
const user = getUser(state, msg.data.remover_id) || {};
let user = getUser(state, msg.data.remover_id);
if (!user) {
await dispatch(loadUser(msg.data.remover_id));
user = getUser(state, msg.data.remover_id) || {};
}

dispatch(openModal({
modalId: ModalIdentifiers.REMOVED_FROM_CHANNEL,
Expand Down
32 changes: 32 additions & 0 deletions actions/websocket_actions.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {ChannelTypes, UserTypes} from 'mattermost-redux/action_types';
import {
getMissingProfilesByIds,
getStatusesByIds,
getUser,
} from 'mattermost-redux/actions/users';
import {General, WebsocketEvents} from 'mattermost-redux/constants';

Expand Down Expand Up @@ -47,6 +48,7 @@ jest.mock('mattermost-redux/actions/posts', () => ({
jest.mock('mattermost-redux/actions/users', () => ({
getMissingProfilesByIds: jest.fn(() => ({type: 'GET_MISSING_PROFILES_BY_IDS'})),
getStatusesByIds: jest.fn(() => ({type: 'GET_STATUSES_BY_IDS'})),
getUser: jest.fn(() => ({type: 'GET_STATUSES_BY_IDS'})),
}));

jest.mock('actions/post_actions', () => ({
Expand Down Expand Up @@ -229,6 +231,36 @@ describe('handleUserRemovedEvent', () => {
mockState.entities.roles.roles = {system_guest: {permissions: ['view_members']}};
expect(store.dispatch).toHaveBeenCalledWith(expectedAction);
});

test('should load the remover_id user if is not available in the store', async () => {
const msg = {
data: {
channel_id: 'otherChannel',
remover_id: 'otherUser',
},
broadcast: {
user_id: 'currentUserId',
},
};

handleUserRemovedEvent(msg);
expect(getUser).toHaveBeenCalledWith('otherUser');
});

test('should not load the remover_id user if is available in the store', async () => {
const msg = {
data: {
channel_id: 'otherChannel',
remover_id: 'user',
},
broadcast: {
user_id: 'currentUserId',
},
};

handleUserRemovedEvent(msg);
expect(getUser).not.toHaveBeenCalled();
});
});

describe('handleNewPostEvent', () => {
Expand Down

0 comments on commit 6c0634e

Please sign in to comment.