Skip to content

Commit 3bfcc78

Browse files
Merge branch 'scottybollinger/as-role-mappings-2' into scottybollinger/yolo-as-role-mappings
# Conflicts: # x-pack/plugins/enterprise_search/public/applications/app_search/components/role_mappings/constants.ts
2 parents a531fa7 + eb1298a commit 3bfcc78

3 files changed

Lines changed: 102 additions & 90 deletions

File tree

x-pack/plugins/enterprise_search/public/applications/app_search/components/role_mappings/constants.ts

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@ export const EMPTY_ROLE_MAPPINGS_BODY = i18n.translate(
1515
}
1616
);
1717

18-
export const DELETE_ROLE_MAPPINGS_MESSAGE = i18n.translate(
19-
'xpack.enterpriseSearch.appSearch.deleteRoleMappingMessage',
20-
{
21-
defaultMessage:
22-
'Are you sure you want to permanently delete this mapping? This action is not reversible and some users might lose access.',
23-
}
24-
);
25-
2618
export const ROLE_MAPPINGS_ENGINE_ACCESS_HEADING = i18n.translate(
2719
'xpack.enterpriseSearch.appSearch.roleMappingsEngineAccessHeading',
2820
{

x-pack/plugins/enterprise_search/public/applications/app_search/components/role_mappings/role_mappings_logic.test.ts

Lines changed: 95 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('RoleMappingsLogic', () => {
2222
const { navigateToUrl } = mockKibanaValues;
2323
const { clearFlashMessages, flashAPIErrors, setSuccessMessage } = mockFlashMessageHelpers;
2424
const { mount } = new LogicMounter(RoleMappingsLogic);
25-
const defaultValues = {
25+
const DEFAULT_VALUES = {
2626
attributes: [],
2727
availableAuthProviders: [],
2828
elasticsearchRoles: [],
@@ -42,8 +42,8 @@ describe('RoleMappingsLogic', () => {
4242

4343
const mappingsServerProps = { multipleAuthProvidersConfig: true, roleMappings: [asRoleMapping] };
4444
const mappingServerProps = {
45-
attributes: [],
46-
authProviders: [],
45+
attributes: ['email', 'metadata', 'username', 'role'],
46+
authProviders: [ANY_AUTH_PROVIDER],
4747
availableEngines: engines,
4848
elasticsearchRoles: [],
4949
hasAdvancedRoles: false,
@@ -57,35 +57,37 @@ describe('RoleMappingsLogic', () => {
5757
});
5858

5959
it('has expected default values', () => {
60-
expect(RoleMappingsLogic.values).toEqual(defaultValues);
60+
expect(RoleMappingsLogic.values).toEqual(DEFAULT_VALUES);
6161
});
6262

6363
describe('actions', () => {
64-
it('setRoleMappingsData', () => {
65-
RoleMappingsLogic.actions.setRoleMappingsData(mappingsServerProps);
64+
describe('setRoleMappingsData', () => {
65+
it('it sets data based on server response from the `mappings` (plural) endpoint', () => {
66+
RoleMappingsLogic.actions.setRoleMappingsData(mappingsServerProps);
6667

67-
expect(RoleMappingsLogic.values.roleMappings).toEqual([asRoleMapping]);
68-
expect(RoleMappingsLogic.values.dataLoading).toEqual(false);
69-
expect(RoleMappingsLogic.values.multipleAuthProvidersConfig).toEqual(true);
68+
expect(RoleMappingsLogic.values.roleMappings).toEqual([asRoleMapping]);
69+
expect(RoleMappingsLogic.values.dataLoading).toEqual(false);
70+
expect(RoleMappingsLogic.values.multipleAuthProvidersConfig).toEqual(true);
71+
});
7072
});
7173

7274
describe('setRoleMappingData', () => {
7375
it('sets state based on server response from the `mapping` (singular) endpoint', () => {
7476
RoleMappingsLogic.actions.setRoleMappingData(mappingServerProps);
7577

76-
expect(RoleMappingsLogic.values.roleMapping).toEqual(asRoleMapping);
77-
expect(RoleMappingsLogic.values.dataLoading).toEqual(false);
78-
expect(RoleMappingsLogic.values.attributes).toEqual(mappingServerProps.attributes);
79-
expect(RoleMappingsLogic.values.availableEngines).toEqual(
80-
mappingServerProps.availableEngines
81-
);
82-
expect(RoleMappingsLogic.values.accessAllEngines).toEqual(true);
83-
expect(RoleMappingsLogic.values.elasticsearchRoles).toEqual(
84-
mappingServerProps.elasticsearchRoles
85-
);
86-
expect(RoleMappingsLogic.values.selectedEngines).toEqual(
87-
new Set(engines.map((e) => e.name))
88-
);
78+
expect(RoleMappingsLogic.values).toEqual({
79+
...DEFAULT_VALUES,
80+
roleMapping: asRoleMapping,
81+
dataLoading: false,
82+
attributes: mappingServerProps.attributes,
83+
availableAuthProviders: mappingServerProps.authProviders,
84+
availableEngines: mappingServerProps.availableEngines,
85+
accessAllEngines: true,
86+
attributeName: 'role',
87+
attributeValue: 'superuser',
88+
elasticsearchRoles: mappingServerProps.elasticsearchRoles,
89+
selectedEngines: new Set(engines.map((e) => e.name)),
90+
});
8991
});
9092

9193
it('will remove all selected engines if no roleMapping was returned from the server', () => {
@@ -94,72 +96,111 @@ describe('RoleMappingsLogic', () => {
9496
roleMapping: undefined,
9597
});
9698

97-
expect(RoleMappingsLogic.values.selectedEngines).toEqual(new Set());
99+
expect(RoleMappingsLogic.values).toEqual({
100+
...DEFAULT_VALUES,
101+
dataLoading: false,
102+
selectedEngines: new Set(),
103+
attributes: mappingServerProps.attributes,
104+
availableAuthProviders: mappingServerProps.authProviders,
105+
availableEngines: mappingServerProps.availableEngines,
106+
});
98107
});
99108
});
100109

101110
it('handleRoleChange', () => {
102111
RoleMappingsLogic.actions.handleRoleChange('dev');
103112

104-
expect(RoleMappingsLogic.values.roleType).toEqual('dev');
113+
expect(RoleMappingsLogic.values).toEqual({
114+
...DEFAULT_VALUES,
115+
roleType: 'dev',
116+
accessAllEngines: false,
117+
});
105118
});
106119

107-
it('handleEngineSelectionChange', () => {
120+
describe('handleEngineSelectionChange', () => {
108121
const engine = engines[0];
109122
const otherEngine = engines[1];
110-
RoleMappingsLogic.actions.setRoleMappingData({
123+
const mountedValues = {
111124
...mappingServerProps,
112125
roleMapping: {
113126
...asRoleMapping,
114127
engines: [engine, otherEngine],
115128
},
129+
selectedEngines: new Set([engine.name]),
130+
};
131+
132+
beforeEach(() => {
133+
mount(mountedValues);
116134
});
117135

118-
RoleMappingsLogic.actions.handleEngineSelectionChange(otherEngine.name, true);
119-
expect(RoleMappingsLogic.values.selectedEngines).toEqual(
120-
new Set([engine.name, otherEngine.name])
121-
);
136+
it('handles adding an engine to selected engines', () => {
137+
RoleMappingsLogic.actions.handleEngineSelectionChange(otherEngine.name, true);
138+
139+
expect(RoleMappingsLogic.values.selectedEngines).toEqual(
140+
new Set([engine.name, otherEngine.name])
141+
);
142+
});
143+
it('handles removing an engine from selected engines', () => {
144+
RoleMappingsLogic.actions.handleEngineSelectionChange(otherEngine.name, false);
122145

123-
RoleMappingsLogic.actions.handleEngineSelectionChange(otherEngine.name, false);
124-
expect(RoleMappingsLogic.values.selectedEngines).toEqual(new Set([engine.name]));
146+
expect(RoleMappingsLogic.values.selectedEngines).toEqual(new Set([engine.name]));
147+
});
125148
});
126149

127150
it('handleAccessAllEnginesChange', () => {
128151
RoleMappingsLogic.actions.handleAccessAllEnginesChange();
129152

130-
expect(RoleMappingsLogic.values.accessAllEngines).toEqual(false);
153+
expect(RoleMappingsLogic.values).toEqual({
154+
...DEFAULT_VALUES,
155+
accessAllEngines: false,
156+
});
131157
});
132158

133159
describe('handleAttributeSelectorChange', () => {
134160
const elasticsearchRoles = ['foo', 'bar'];
135161

136162
it('sets values correctly', () => {
137-
RoleMappingsLogic.actions.setRoleMappingData({
163+
mount({
138164
...mappingServerProps,
139165
elasticsearchRoles,
140166
});
141167
RoleMappingsLogic.actions.handleAttributeSelectorChange('role', elasticsearchRoles[0]);
142168

143-
expect(RoleMappingsLogic.values.attributeValue).toEqual(elasticsearchRoles[0]);
144-
expect(RoleMappingsLogic.values.attributeName).toEqual('role');
169+
expect(RoleMappingsLogic.values).toEqual({
170+
...DEFAULT_VALUES,
171+
attributeValue: elasticsearchRoles[0],
172+
roleMapping: asRoleMapping,
173+
attributes: mappingServerProps.attributes,
174+
availableEngines: mappingServerProps.availableEngines,
175+
accessAllEngines: true,
176+
attributeName: 'role',
177+
elasticsearchRoles,
178+
selectedEngines: new Set(),
179+
});
145180
});
146181

147182
it('correctly handles "role" fallback', () => {
148183
RoleMappingsLogic.actions.handleAttributeSelectorChange('username', elasticsearchRoles[0]);
149184

150-
expect(RoleMappingsLogic.values.attributeValue).toEqual('');
185+
expect(RoleMappingsLogic.values).toEqual({
186+
...DEFAULT_VALUES,
187+
attributeValue: '',
188+
});
151189
});
152190
});
153191

154192
it('handleAttributeValueChange', () => {
155193
RoleMappingsLogic.actions.handleAttributeValueChange('changed_value');
156194

157-
expect(RoleMappingsLogic.values.attributeValue).toEqual('changed_value');
195+
expect(RoleMappingsLogic.values).toEqual({
196+
...DEFAULT_VALUES,
197+
attributeValue: 'changed_value',
198+
});
158199
});
159200

160201
describe('handleAuthProviderChange', () => {
161202
beforeEach(() => {
162-
RoleMappingsLogic.actions.setRoleMappingData({
203+
mount({
163204
...mappingServerProps,
164205
roleMapping: {
165206
...asRoleMapping,
@@ -188,7 +229,7 @@ describe('RoleMappingsLogic', () => {
188229
});
189230

190231
it('handles "any" auth in previous state', () => {
191-
RoleMappingsLogic.actions.setRoleMappingData({
232+
mount({
192233
...mappingServerProps,
193234
roleMapping: {
194235
...asRoleMapping,
@@ -199,25 +240,14 @@ describe('RoleMappingsLogic', () => {
199240

200241
expect(RoleMappingsLogic.values.selectedAuthProviders).toEqual([providers[1]]);
201242
});
202-
203-
it('handles catch-all state', () => {
204-
RoleMappingsLogic.actions.handleAuthProviderChange(providerWithAny);
205-
206-
expect(RoleMappingsLogic.values.selectedAuthProviders).toEqual([ANY_AUTH_PROVIDER]);
207-
});
208243
});
209244

210245
it('resetState', () => {
211-
RoleMappingsLogic.actions.setRoleMappingsData(mappingsServerProps);
212-
RoleMappingsLogic.actions.setRoleMappingData(mappingServerProps);
246+
mount(mappingsServerProps);
247+
mount(mappingServerProps);
213248
RoleMappingsLogic.actions.resetState();
214249

215-
expect(RoleMappingsLogic.values.dataLoading).toEqual(true);
216-
expect(RoleMappingsLogic.values.roleMappings).toEqual([]);
217-
expect(RoleMappingsLogic.values.roleMapping).toEqual(null);
218-
expect(RoleMappingsLogic.values.attributeValue).toEqual('');
219-
expect(RoleMappingsLogic.values.attributeName).toEqual('username');
220-
expect(clearFlashMessages).toHaveBeenCalled();
250+
expect(RoleMappingsLogic.values).toEqual(DEFAULT_VALUES);
221251
});
222252
});
223253

@@ -313,7 +343,7 @@ describe('RoleMappingsLogic', () => {
313343
};
314344

315345
it('calls API and navigates when new mapping', async () => {
316-
RoleMappingsLogic.actions.setRoleMappingsData(mappingsServerProps);
346+
mount(mappingsServerProps);
317347

318348
http.post.mockReturnValue(Promise.resolve(mappingServerProps));
319349
RoleMappingsLogic.actions.handleSaveMapping();
@@ -327,18 +357,13 @@ describe('RoleMappingsLogic', () => {
327357
});
328358

329359
it('calls API and navigates when existing mapping', async () => {
330-
RoleMappingsLogic.actions.setRoleMappingData(mappingServerProps);
360+
mount(mappingServerProps);
331361

332362
http.put.mockReturnValue(Promise.resolve(mappingServerProps));
333363
RoleMappingsLogic.actions.handleSaveMapping();
334364

335365
expect(http.put).toHaveBeenCalledWith(`/api/app_search/role_mappings/${asRoleMapping.id}`, {
336-
body: JSON.stringify({
337-
...body,
338-
rules: {
339-
role: 'superuser',
340-
},
341-
}),
366+
body: JSON.stringify(body),
342367
});
343368
await nextTick();
344369

@@ -348,30 +373,21 @@ describe('RoleMappingsLogic', () => {
348373

349374
it('sends array when "accessAllEngines" is false', () => {
350375
const engine = engines[0];
351-
const otherEngine = engines[1];
352-
RoleMappingsLogic.actions.setRoleMappingData({
376+
377+
mount({
353378
...mappingServerProps,
354-
roleMapping: {
355-
...asRoleMapping,
356-
engines: [engine, otherEngine],
357-
},
379+
accessAllEngines: false,
380+
selectedEngines: new Set([engine.name]),
358381
});
359382

360-
RoleMappingsLogic.actions.setRoleMappingData(mappingServerProps);
361-
RoleMappingsLogic.actions.handleAccessAllEnginesChange();
362-
RoleMappingsLogic.actions.handleEngineSelectionChange(otherEngine.name, true);
363-
364383
http.put.mockReturnValue(Promise.resolve(mappingServerProps));
365384
RoleMappingsLogic.actions.handleSaveMapping();
366385

367386
expect(http.put).toHaveBeenCalledWith(`/api/app_search/role_mappings/${asRoleMapping.id}`, {
368387
body: JSON.stringify({
369388
...body,
370389
accessAllEngines: false,
371-
rules: {
372-
role: 'superuser',
373-
},
374-
engines: [otherEngine.name],
390+
engines: [engine.name],
375391
}),
376392
});
377393
});
@@ -404,7 +420,7 @@ describe('RoleMappingsLogic', () => {
404420
});
405421

406422
it('calls API and navigates', async () => {
407-
RoleMappingsLogic.actions.setRoleMappingData(mappingServerProps);
423+
mount(mappingServerProps);
408424
http.delete.mockReturnValue(Promise.resolve({}));
409425
RoleMappingsLogic.actions.handleDeleteMapping();
410426

@@ -418,7 +434,7 @@ describe('RoleMappingsLogic', () => {
418434
});
419435

420436
it('handles error', async () => {
421-
RoleMappingsLogic.actions.setRoleMappingData(mappingServerProps);
437+
mount(mappingServerProps);
422438
http.delete.mockReturnValue(Promise.reject('this is an error'));
423439
RoleMappingsLogic.actions.handleDeleteMapping();
424440
await nextTick();
@@ -427,7 +443,7 @@ describe('RoleMappingsLogic', () => {
427443
});
428444

429445
it('will do nothing if not confirmed', async () => {
430-
RoleMappingsLogic.actions.setRoleMappingData(mappingServerProps);
446+
mount(mappingServerProps);
431447
window.confirm = () => false;
432448
RoleMappingsLogic.actions.handleDeleteMapping();
433449

x-pack/plugins/enterprise_search/public/applications/app_search/components/role_mappings/role_mappings_logic.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ export interface RoleMappingsValues {
9393
}
9494

9595
export const RoleMappingsLogic = kea<MakeLogicType<RoleMappingsValues, RoleMappingsActions>>({
96+
path: ['enterprise_search', 'app_search', 'role_mappings'],
9697
actions: {
9798
setRoleMappingsData: (data: RoleMappingsServerDetails) => data,
9899
setRoleMappingData: (data: RoleMappingServerDetails) => data,
@@ -149,12 +150,14 @@ export const RoleMappingsLogic = kea<MakeLogicType<RoleMappingsValues, RoleMappi
149150
[],
150151
{
151152
setRoleMappingData: (_, { availableEngines }) => availableEngines,
153+
resetState: () => [],
152154
},
153155
],
154156
attributes: [
155157
[],
156158
{
157159
setRoleMappingData: (_, { attributes }) => attributes,
160+
resetState: () => [],
158161
},
159162
],
160163
elasticsearchRoles: [
@@ -237,11 +240,12 @@ export const RoleMappingsLogic = kea<MakeLogicType<RoleMappingsValues, RoleMappi
237240
handleAuthProviderChange: (previous, { value }) => {
238241
const previouslyContainedAny = previous.includes(ANY_AUTH_PROVIDER);
239242
const newSelectionsContainAny = value.includes(ANY_AUTH_PROVIDER);
243+
const hasItems = value.length > 0;
240244

241-
if (value.length < 1) return [ANY_AUTH_PROVIDER];
242245
if (value.length === 1) return value;
243-
if (!newSelectionsContainAny) return value;
244-
if (previouslyContainedAny) return value.filter((v) => v !== ANY_AUTH_PROVIDER);
246+
if (!newSelectionsContainAny && hasItems) return value;
247+
if (previouslyContainedAny && hasItems)
248+
return value.filter((v) => v !== ANY_AUTH_PROVIDER);
245249
return [ANY_AUTH_PROVIDER];
246250
},
247251
setRoleMappingData: (_, { roleMapping }) =>

0 commit comments

Comments
 (0)