/
user_settings_security.test.tsx
234 lines (185 loc) · 8.22 KB
/
user_settings_security.test.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import {shallow} from 'enzyme';
import React from 'react';
import type {OAuthApp} from '@mattermost/types/integrations';
import type {UserProfile} from '@mattermost/types/users';
import type {MockIntl} from 'tests/helpers/intl-test-helper';
import Constants from 'utils/constants';
import type * as Utils from 'utils/utils';
import {SecurityTab} from './user_settings_security';
jest.mock('utils/utils', () => {
const original = jest.requireActual('utils/utils');
return {...original, isValidPassword: () => ({valid: true})};
});
describe('components/user_settings/display/UserSettingsDisplay', () => {
const user = {
id: 'user_id',
};
const requiredProps = {
user: user as UserProfile,
closeModal: jest.fn(),
collapseModal: jest.fn(),
setRequireConfirm: jest.fn(),
updateSection: jest.fn(),
authorizedApps: jest.fn(),
actions: {
getMe: jest.fn(),
updateUserPassword: jest.fn(() => Promise.resolve({error: true})),
getAuthorizedOAuthApps: jest.fn().mockResolvedValue({data: []}),
deauthorizeOAuthApp: jest.fn().mockResolvedValue({data: true}),
},
canUseAccessTokens: true,
enableOAuthServiceProvider: false,
allowedToSwitchToEmail: true,
enableSignUpWithGitLab: false,
enableSignUpWithGoogle: true,
enableSignUpWithOpenId: false,
enableLdap: false,
enableSaml: true,
enableSignUpWithOffice365: false,
experimentalEnableAuthenticationTransfer: true,
passwordConfig: {} as ReturnType<typeof Utils.getPasswordConfig>,
militaryTime: false,
intl: {
formatMessage: jest.fn(({id, defaultMessage}) => defaultMessage || id),
} as MockIntl,
};
test('should match snapshot, enable google', () => {
const props = {...requiredProps, enableSaml: false};
const wrapper = shallow<SecurityTab>(<SecurityTab {...props}/>);
expect(wrapper).toMatchSnapshot();
});
test('should match snapshot, enable gitlab', () => {
const props = {...requiredProps, enableSignUpWithGoogle: false, enableSaml: false, enableSignUpWithGitLab: true};
const wrapper = shallow<SecurityTab>(<SecurityTab {...props}/>);
expect(wrapper).toMatchSnapshot();
});
test('should match snapshot, enable office365', () => {
const props = {...requiredProps, enableSignUpWithGoogle: false, enableSaml: false, enableSignUpWithOffice365: true};
const wrapper = shallow<SecurityTab>(<SecurityTab {...props}/>);
expect(wrapper).toMatchSnapshot();
});
test('should match snapshot, enable openID', () => {
const props = {...requiredProps, enableSignUpWithGoogle: false, enableSaml: false, enableSignUpWithOpenId: true};
const wrapper = shallow<SecurityTab>(<SecurityTab {...props}/>);
expect(wrapper).toMatchSnapshot();
});
test('should match snapshot, to email', () => {
const user = {
id: 'user_id',
auth_service: Constants.OPENID_SERVICE,
};
const props = {...requiredProps, user: user as UserProfile};
const wrapper = shallow<SecurityTab>(<SecurityTab {...props}/>);
expect(wrapper).toMatchSnapshot();
});
test('componentDidMount() should have called getAuthorizedOAuthApps', () => {
const props = {...requiredProps, enableOAuthServiceProvider: true};
shallow<SecurityTab>(<SecurityTab {...props}/>);
expect(requiredProps.actions.getAuthorizedOAuthApps).toHaveBeenCalled();
});
test('componentDidMount() should have updated state.authorizedApps', async () => {
const apps = [{name: 'app1'}];
const promise = Promise.resolve({data: apps});
const getAuthorizedOAuthApps = () => promise;
const props = {
...requiredProps,
actions: {...requiredProps.actions, getAuthorizedOAuthApps},
enableOAuthServiceProvider: true,
};
const wrapper = shallow<SecurityTab>(<SecurityTab {...props}/>);
await promise;
expect(wrapper.state().authorizedApps).toEqual(apps);
});
test('componentDidMount() should have updated state.serverError', async () => {
const error = {message: 'error'};
const promise = Promise.resolve({error});
const getAuthorizedOAuthApps = () => promise;
const props = {
...requiredProps,
actions: {...requiredProps.actions, getAuthorizedOAuthApps},
enableOAuthServiceProvider: true,
};
const wrapper = shallow<SecurityTab>(<SecurityTab {...props}/>);
await promise;
expect(wrapper.state('serverError')).toEqual(error.message);
});
test('submitPassword() should not have called updateUserPassword', async () => {
const wrapper = shallow<SecurityTab>(<SecurityTab {...requiredProps}/>);
await wrapper.instance().submitPassword();
expect(requiredProps.actions.updateUserPassword).toHaveBeenCalledTimes(0);
});
test('submitPassword() should have called updateUserPassword', async () => {
const updateUserPassword = jest.fn(() => Promise.resolve({data: true}));
const props = {
...requiredProps,
actions: {...requiredProps.actions, updateUserPassword},
};
const wrapper = shallow<SecurityTab>(<SecurityTab {...props}/>);
const password = 'psw';
const state = {
currentPassword: 'currentPassword',
newPassword: password,
confirmPassword: password,
};
wrapper.setState(state);
await wrapper.instance().submitPassword();
expect(updateUserPassword).toHaveBeenCalled();
expect(updateUserPassword).toHaveBeenCalledWith(
user.id,
state.currentPassword,
state.newPassword,
);
expect(requiredProps.updateSection).toHaveBeenCalled();
expect(requiredProps.updateSection).toHaveBeenCalledWith('');
});
test('deauthorizeApp() should have called deauthorizeOAuthApp', () => {
const appId = 'appId';
const event: any = {
currentTarget: {getAttribute: jest.fn().mockReturnValue(appId)},
preventDefault: jest.fn(),
};
const wrapper = shallow<SecurityTab>(<SecurityTab {...requiredProps}/>);
wrapper.setState({authorizedApps: []});
wrapper.instance().deauthorizeApp(event);
expect(requiredProps.actions.deauthorizeOAuthApp).toHaveBeenCalled();
expect(requiredProps.actions.deauthorizeOAuthApp).toHaveBeenCalledWith(
appId,
);
});
test('deauthorizeApp() should have updated state.authorizedApps', async () => {
const promise = Promise.resolve({data: true});
const props = {
...requiredProps,
actions: {...requiredProps.actions, deauthorizeOAuthApp: () => promise},
};
const wrapper = shallow<SecurityTab>(<SecurityTab {...props}/>);
const appId = 'appId';
const apps = [{id: appId}, {id: '2'}] as OAuthApp[];
const event: any = {
currentTarget: {getAttribute: jest.fn().mockReturnValue(appId)},
preventDefault: jest.fn(),
};
wrapper.setState({authorizedApps: apps});
wrapper.instance().deauthorizeApp(event);
await promise;
expect(wrapper.state().authorizedApps).toEqual(apps.slice(1));
});
test('deauthorizeApp() should have updated state.serverError', async () => {
const error = {message: 'error'};
const promise = Promise.resolve({error});
const props = {
...requiredProps,
actions: {...requiredProps.actions, deauthorizeOAuthApp: () => promise},
};
const wrapper = shallow<SecurityTab>(<SecurityTab {...props}/>);
const event: any = {
currentTarget: {getAttribute: jest.fn().mockReturnValue('appId')},
preventDefault: jest.fn(),
};
wrapper.instance().deauthorizeApp(event);
await promise;
expect(wrapper.state('serverError')).toEqual(error.message);
});
});