foreman_puppet 11.0.0 → 11.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/foreman_puppet/locale/ca/foreman_puppet.js +181 -1
  3. data/app/assets/javascripts/foreman_puppet/locale/cs_CZ/foreman_puppet.js +181 -1
  4. data/app/assets/javascripts/foreman_puppet/locale/de/foreman_puppet.js +181 -1
  5. data/app/assets/javascripts/foreman_puppet/locale/en/foreman_puppet.js +181 -1
  6. data/app/assets/javascripts/foreman_puppet/locale/en_GB/foreman_puppet.js +181 -1
  7. data/app/assets/javascripts/foreman_puppet/locale/es/foreman_puppet.js +181 -1
  8. data/app/assets/javascripts/foreman_puppet/locale/fr/foreman_puppet.js +181 -1
  9. data/app/assets/javascripts/foreman_puppet/locale/gl/foreman_puppet.js +181 -1
  10. data/app/assets/javascripts/foreman_puppet/locale/it/foreman_puppet.js +181 -1
  11. data/app/assets/javascripts/foreman_puppet/locale/ja/foreman_puppet.js +181 -1
  12. data/app/assets/javascripts/foreman_puppet/locale/ka/foreman_puppet.js +192 -12
  13. data/app/assets/javascripts/foreman_puppet/locale/ko/foreman_puppet.js +181 -1
  14. data/app/assets/javascripts/foreman_puppet/locale/nl_NL/foreman_puppet.js +181 -1
  15. data/app/assets/javascripts/foreman_puppet/locale/pl/foreman_puppet.js +181 -1
  16. data/app/assets/javascripts/foreman_puppet/locale/pt_BR/foreman_puppet.js +181 -1
  17. data/app/assets/javascripts/foreman_puppet/locale/ru/foreman_puppet.js +181 -1
  18. data/app/assets/javascripts/foreman_puppet/locale/sv_SE/foreman_puppet.js +181 -1
  19. data/app/assets/javascripts/foreman_puppet/locale/zh_CN/foreman_puppet.js +181 -1
  20. data/app/assets/javascripts/foreman_puppet/locale/zh_TW/foreman_puppet.js +181 -1
  21. data/app/controllers/concerns/foreman_puppet/extensions/hosts_controller_extensions.rb +1 -1
  22. data/app/controllers/foreman_puppet/api/v2/hosts_bulk_actions_controller.rb +75 -1
  23. data/app/models/foreman_puppet/config_group.rb +3 -1
  24. data/app/models/foreman_puppet/host_config_group.rb +12 -0
  25. data/app/models/foreman_puppet/puppetclass.rb +14 -3
  26. data/app/services/concerns/foreman_puppet/extensions/bulk_hosts_manager.rb +18 -0
  27. data/config/api_routes.rb +1 -0
  28. data/db/seeds.d/111_puppet_proxy_feature.rb +1 -1
  29. data/lib/foreman_puppet/register.rb +1 -0
  30. data/lib/foreman_puppet/version.rb +1 -1
  31. data/locale/ca/LC_MESSAGES/foreman_puppet.mo +0 -0
  32. data/locale/ca/foreman_puppet.po +187 -1
  33. data/locale/cs_CZ/LC_MESSAGES/foreman_puppet.mo +0 -0
  34. data/locale/cs_CZ/foreman_puppet.po +187 -1
  35. data/locale/de/LC_MESSAGES/foreman_puppet.mo +0 -0
  36. data/locale/de/foreman_puppet.po +188 -2
  37. data/locale/en/LC_MESSAGES/foreman_puppet.mo +0 -0
  38. data/locale/en/foreman_puppet.po +187 -1
  39. data/locale/en_GB/LC_MESSAGES/foreman_puppet.mo +0 -0
  40. data/locale/en_GB/foreman_puppet.po +187 -1
  41. data/locale/es/LC_MESSAGES/foreman_puppet.mo +0 -0
  42. data/locale/es/foreman_puppet.po +187 -1
  43. data/locale/foreman_puppet.pot +312 -13
  44. data/locale/fr/LC_MESSAGES/foreman_puppet.mo +0 -0
  45. data/locale/fr/foreman_puppet.po +187 -1
  46. data/locale/gl/LC_MESSAGES/foreman_puppet.mo +0 -0
  47. data/locale/gl/foreman_puppet.po +187 -1
  48. data/locale/it/LC_MESSAGES/foreman_puppet.mo +0 -0
  49. data/locale/it/foreman_puppet.po +187 -1
  50. data/locale/ja/LC_MESSAGES/foreman_puppet.mo +0 -0
  51. data/locale/ja/foreman_puppet.po +187 -1
  52. data/locale/ka/LC_MESSAGES/foreman_puppet.mo +0 -0
  53. data/locale/ka/foreman_puppet.po +199 -13
  54. data/locale/ko/LC_MESSAGES/foreman_puppet.mo +0 -0
  55. data/locale/ko/foreman_puppet.po +187 -1
  56. data/locale/nl_NL/LC_MESSAGES/foreman_puppet.mo +0 -0
  57. data/locale/nl_NL/foreman_puppet.po +187 -1
  58. data/locale/pl/LC_MESSAGES/foreman_puppet.mo +0 -0
  59. data/locale/pl/foreman_puppet.po +187 -1
  60. data/locale/pt_BR/LC_MESSAGES/foreman_puppet.mo +0 -0
  61. data/locale/pt_BR/foreman_puppet.po +187 -1
  62. data/locale/ru/LC_MESSAGES/foreman_puppet.mo +0 -0
  63. data/locale/ru/foreman_puppet.po +187 -1
  64. data/locale/sv_SE/LC_MESSAGES/foreman_puppet.mo +0 -0
  65. data/locale/sv_SE/foreman_puppet.po +187 -1
  66. data/locale/zh_CN/LC_MESSAGES/foreman_puppet.mo +0 -0
  67. data/locale/zh_CN/foreman_puppet.po +187 -1
  68. data/locale/zh_TW/LC_MESSAGES/foreman_puppet.mo +0 -0
  69. data/locale/zh_TW/foreman_puppet.po +187 -1
  70. data/test/controllers/foreman_puppet/api/v2/hosts_bulk_actions_controller_test.rb +93 -0
  71. data/test/models/foreman_puppet/host_test.rb +50 -0
  72. data/test/models/foreman_puppet/hostgroup_test.rb +50 -0
  73. data/test/services/foreman_puppet/bulk_hosts_manager_test.rb +45 -0
  74. data/webpack/__mocks__/foremanReact/redux/API/index.js +8 -0
  75. data/webpack/global_index.js +18 -1
  76. data/webpack/src/Extends/Hosts/ActionsBar/index.js +20 -0
  77. data/webpack/src/Extends/Hosts/BulkActions/BulkChangeProxyCommon/__tests__/actions.test.js +0 -7
  78. data/webpack/src/Extends/Hosts/BulkActions/BulkChangePuppetEnvironment/BulkChangePuppetEnvironmentModal.js +239 -0
  79. data/webpack/src/Extends/Hosts/BulkActions/BulkChangePuppetEnvironment/__tests__/BulkChangePuppetEnvironmentModal.test.js +155 -0
  80. data/webpack/src/Extends/Hosts/BulkActions/BulkChangePuppetEnvironment/__tests__/actions.test.js +49 -0
  81. data/webpack/src/Extends/Hosts/BulkActions/BulkChangePuppetEnvironment/__tests__/index.test.js +66 -0
  82. data/webpack/src/Extends/Hosts/BulkActions/BulkChangePuppetEnvironment/actions.js +37 -0
  83. data/webpack/src/Extends/Hosts/BulkActions/BulkChangePuppetEnvironment/index.js +30 -0
  84. data/webpack/src/Extends/Hosts/BulkActions/BulkChangePuppetProxy/__tests__/index.test.js +160 -47
  85. data/webpack/src/Extends/Hosts/BulkActions/BulkRemoveProxyCommon/__tests__/actions.test.js +0 -6
  86. data/webpack/src/Extends/Hosts/BulkActions/BulkRemovePuppetCAProxy/__tests__/index.test.js +136 -39
  87. data/webpack/src/Extends/Hosts/BulkActions/BulkRemovePuppetEnvironment/BulkRemovePuppetEnvironmentModal.js +148 -0
  88. data/webpack/src/Extends/Hosts/BulkActions/BulkRemovePuppetEnvironment/__tests__/BulkRemovePuppetEnvironmentModal.test.js +80 -0
  89. data/webpack/src/Extends/Hosts/BulkActions/BulkRemovePuppetEnvironment/__tests__/index.test.js +66 -0
  90. data/webpack/src/Extends/Hosts/BulkActions/BulkRemovePuppetEnvironment/index.js +30 -0
  91. data/webpack/src/Extends/Hosts/BulkActions/BulkRemovePuppetProxy/__tests__/index.test.js +123 -34
  92. data/webpack/src/Router/routes.fixtures.js +13 -0
  93. data/webpack/src/Router/routes.test.js +68 -8
  94. data/webpack/src/foreman_puppet_host_form.test.js +3 -1
  95. data/webpack/test_setup.js +1 -0
  96. metadata +14 -3
  97. data/webpack/src/Router/__snapshots__/routes.test.js.snap +0 -3
@@ -1,69 +1,182 @@
1
1
  import React from 'react';
2
- import { mount } from '@theforeman/test';
2
+ import { act, screen, waitFor } from '@testing-library/react';
3
+ import userEvent from '@testing-library/user-event';
4
+ import '@testing-library/jest-dom';
3
5
 
4
6
  import { openBulkModal } from 'foremanReact/common/BulkModalStateHelper';
5
7
  import { ForemanActionsBarContext } from 'foremanReact/components/HostDetails/ActionsBar';
8
+ import { rtlHelpers } from 'foremanReact/common/rtlTestHelpers';
9
+ import { APIActions } from 'foremanReact/redux/API';
10
+ import API from 'foremanReact/redux/API/API';
6
11
 
7
12
  import BulkChangePuppetProxyScene from '../index';
8
- import BulkChangeProxyCommon from '../../BulkChangeProxyCommon';
13
+ import { BULK_CHANGE_PUPPET_PROXY_KEY } from '../../BulkChangeProxyCommon/actions';
9
14
 
10
- jest.mock('foremanReact/components/HostDetails/ActionsBar', () => ({
11
- ForemanActionsBarContext: jest.requireActual('react').createContext(),
12
- }));
15
+ jest.mock('foremanReact/redux/API', () => {
16
+ const actual = jest.requireActual('foremanReact/redux/API');
13
17
 
14
- jest.mock('../../BulkChangeProxyCommon', () => ({
15
- __esModule: true,
16
- default: jest.fn(() => null),
17
- }));
18
-
19
- describe('BulkChangePuppetProxyScene', () => {
20
- const fetchBulkParams = jest.fn();
21
- const refreshTableData = jest.fn();
22
- const contextValue = {
23
- selectAllHostsMode: false,
24
- selectedCount: 2,
25
- selectedResults: [1, 2],
26
- fetchBulkParams,
27
- refreshTableData,
18
+ return {
19
+ ...actual,
20
+ APIActions: {
21
+ ...actual.APIActions,
22
+ put: jest.fn(params => ({ type: 'MOCK_API_PUT', payload: params })),
23
+ },
28
24
  };
25
+ });
26
+
27
+ jest.mock('foremanReact/redux/API/APISelectors', () =>
28
+ jest.requireActual('foremanReact/redux/API/APISelectors')
29
+ );
30
+
31
+ const { renderWithStoreAndI18n } = rtlHelpers;
32
+
33
+ const MODAL_ID = 'bulk-change-puppet-proxy';
34
+
35
+ const fetchBulkParams = jest.fn(() => 'id ^ (1,2)');
36
+ const refreshTableData = jest.fn();
37
+
38
+ const defaultContextValue = {
39
+ selectAllHostsMode: false,
40
+ selectedCount: 2,
41
+ selectedResults: [1, 2],
42
+ fetchBulkParams,
43
+ refreshTableData,
44
+ };
45
+
46
+ const smartProxiesResponse = {
47
+ data: {
48
+ results: [{ id: 1, name: 'proxy1.example.com' }],
49
+ },
50
+ };
29
51
 
52
+ const renderScene = ({ contextValue = defaultContextValue } = {}) =>
53
+ renderWithStoreAndI18n(
54
+ <ForemanActionsBarContext.Provider value={contextValue}>
55
+ <BulkChangePuppetProxyScene />
56
+ </ForemanActionsBarContext.Provider>
57
+ );
58
+
59
+ const selectPuppetProxy = async () => {
60
+ await act(async () => {
61
+ await userEvent.click(await screen.findByText('Select a Puppet Proxy'));
62
+ });
63
+ await act(async () => {
64
+ await userEvent.click(screen.getByText('proxy1.example.com'));
65
+ });
66
+ };
67
+
68
+ describe('BulkChangePuppetProxyScene', () => {
30
69
  beforeEach(() => {
70
+ openBulkModal(MODAL_ID, false);
71
+ API.get.mockImplementation(() => Promise.resolve(smartProxiesResponse));
72
+ });
73
+
74
+ afterEach(() => {
31
75
  jest.clearAllMocks();
32
- openBulkModal('bulk-change-puppet-proxy', false);
33
76
  });
34
77
 
35
- it('opens with bulk modal state and passes expected props', () => {
36
- openBulkModal('bulk-change-puppet-proxy', true);
37
- const wrapper = mount(
38
- <ForemanActionsBarContext.Provider value={contextValue}>
39
- <BulkChangePuppetProxyScene />
40
- </ForemanActionsBarContext.Provider>
41
- );
78
+ it('does not show the modal when bulk modal state is closed', () => {
79
+ renderScene();
42
80
 
43
- const componentType =
44
- BulkChangeProxyCommon.default || BulkChangeProxyCommon;
45
- const props = wrapper.find(componentType).props();
81
+ expect(
82
+ screen.queryByRole('dialog', { name: 'Change Puppet Proxy' })
83
+ ).not.toBeInTheDocument();
84
+ });
46
85
 
47
- expect(props).toEqual(
86
+ it('opens the modal with Puppet Proxy content when bulk modal is open', async () => {
87
+ openBulkModal(MODAL_ID, true);
88
+ renderScene();
89
+
90
+ expect(
91
+ await screen.findByRole('dialog', { name: 'Change Puppet Proxy' })
92
+ ).toBeInTheDocument();
93
+ expect(
94
+ screen.getByText(/Changing the Puppet proxy will affect/)
95
+ ).toBeInTheDocument();
96
+ expect(screen.getByText('2')).toBeInTheDocument();
97
+ expect(
98
+ await screen.findByText('Select a Puppet Proxy')
99
+ ).toBeInTheDocument();
100
+ expect(screen.queryByText('Select a Puppet CA Proxy')).not.toBeInTheDocument();
101
+ expect(screen.queryByText('Change Puppet CA Proxy')).not.toBeInTheDocument();
102
+ expect(
103
+ await screen.findByRole('button', { name: 'Change Puppet Proxy' })
104
+ ).toBeDisabled();
105
+ expect(screen.getByRole('button', { name: 'Cancel' })).toBeInTheDocument();
106
+ });
107
+
108
+ it('shows the all-hosts warning when select all hosts mode is enabled', async () => {
109
+ openBulkModal(MODAL_ID, true);
110
+ renderScene({
111
+ contextValue: { ...defaultContextValue, selectAllHostsMode: true },
112
+ });
113
+
114
+ await screen.findByRole('dialog', { name: 'Change Puppet Proxy' });
115
+ expect(screen.getByText('All')).toBeInTheDocument();
116
+ });
117
+
118
+ it('closes the modal when Cancel is clicked', async () => {
119
+ openBulkModal(MODAL_ID, true);
120
+ renderScene();
121
+
122
+ await screen.findByRole('dialog', { name: 'Change Puppet Proxy' });
123
+ await userEvent.click(screen.getByRole('button', { name: 'Cancel' }));
124
+
125
+ await waitFor(() => {
126
+ expect(
127
+ screen.queryByRole('dialog', { name: 'Change Puppet Proxy' })
128
+ ).not.toBeInTheDocument();
129
+ });
130
+ });
131
+
132
+ it('submits puppet proxy change request when confirm is clicked', async () => {
133
+ openBulkModal(MODAL_ID, true);
134
+ renderScene();
135
+
136
+ await screen.findByRole('dialog', { name: 'Change Puppet Proxy' });
137
+ await selectPuppetProxy();
138
+ await act(async () => {
139
+ await userEvent.click(
140
+ screen.getByRole('button', { name: 'Change Puppet Proxy' })
141
+ );
142
+ });
143
+
144
+ expect(fetchBulkParams).toHaveBeenCalledTimes(1);
145
+ expect(APIActions.put).toHaveBeenCalledWith(
48
146
  expect.objectContaining({
49
- isCAProxy: false,
50
- fetchBulkParams,
51
- selectedCount: 2,
52
- selectedResults: [1, 2],
53
- selectAllHostsMode: false,
54
- isOpen: true,
55
- closeModal: expect.any(Function),
56
- onSuccess: refreshTableData,
57
- selectMessage: 'Select a Puppet Proxy',
58
- handleErrorMessage: 'Failed to change Puppet Proxy',
59
- changeMessage: 'Change Puppet Proxy',
60
- allHostsMessage:
61
- 'Changing the Puppet proxy will affect {boldCount} selected hosts. Some hosts may already have been associated with the selected Puppet proxy.',
62
- someHostsMessage:
63
- 'Changing the Puppet proxy will affect {boldCount} selected {count, plural, one {host} other {hosts}}. Some hosts may already have been associated with the selected Puppet proxy.',
147
+ key: BULK_CHANGE_PUPPET_PROXY_KEY,
148
+ params: {
149
+ included: {
150
+ search: 'id ^ (1,2)',
151
+ },
152
+ proxy_id: '1',
153
+ ca_proxy: false,
154
+ },
64
155
  })
65
156
  );
157
+ });
158
+
159
+ it('refreshes table data after successful change', async () => {
160
+ openBulkModal(MODAL_ID, true);
161
+ renderScene();
162
+
163
+ await screen.findByRole('dialog', { name: 'Change Puppet Proxy' });
164
+ await selectPuppetProxy();
165
+ await act(async () => {
166
+ await userEvent.click(
167
+ screen.getByRole('button', { name: 'Change Puppet Proxy' })
168
+ );
169
+ });
170
+
171
+ const { handleSuccess } = APIActions.put.mock.calls[0][0];
172
+
173
+ act(() => {
174
+ handleSuccess({ data: { message: 'Change started' } });
175
+ });
66
176
 
67
- wrapper.unmount();
177
+ expect(refreshTableData).toHaveBeenCalledTimes(1);
178
+ expect(
179
+ screen.queryByRole('dialog', { name: 'Change Puppet Proxy' })
180
+ ).not.toBeInTheDocument();
68
181
  });
69
182
  });
@@ -7,12 +7,6 @@ import {
7
7
  BULK_REMOVE_PUPPET_CA_PROXY_KEY,
8
8
  } from '../actions';
9
9
 
10
- jest.mock('foremanReact/redux/API', () => ({
11
- APIActions: {
12
- put: jest.fn(),
13
- },
14
- }));
15
-
16
10
  describe('BulkRemoveProxyCommon actions', () => {
17
11
  const url = foremanUrl('/api/v2/hosts/bulk/remove_puppet_proxy');
18
12
 
@@ -1,68 +1,165 @@
1
1
  import React from 'react';
2
- import { mount } from '@theforeman/test';
2
+ import { act, screen, within, waitFor } from '@testing-library/react';
3
+ import userEvent from '@testing-library/user-event';
4
+ import '@testing-library/jest-dom';
3
5
 
6
+ import { rtlHelpers } from 'foremanReact/common/rtlTestHelpers';
4
7
  import { openBulkModal } from 'foremanReact/common/BulkModalStateHelper';
5
8
  import { ForemanActionsBarContext } from 'foremanReact/components/HostDetails/ActionsBar';
9
+ import { APIActions } from 'foremanReact/redux/API';
10
+ import API from 'foremanReact/redux/API/API';
6
11
 
7
12
  import BulkRemovePuppetCAProxyScene from '../index';
8
- import BulkRemoveProxyCommon from '../../BulkRemoveProxyCommon';
13
+ import { BULK_REMOVE_PUPPET_CA_PROXY_KEY } from '../../BulkRemoveProxyCommon/actions';
9
14
 
10
- jest.mock('foremanReact/components/HostDetails/ActionsBar', () => ({
11
- ForemanActionsBarContext: jest.requireActual('react').createContext(),
12
- }));
13
-
14
- jest.mock('../../BulkRemoveProxyCommon', () => ({
15
- __esModule: true,
16
- default: jest.fn(() => null),
17
- }));
15
+ const { renderWithStoreAndI18n } = rtlHelpers;
18
16
 
19
17
  describe('BulkRemovePuppetCAProxyScene', () => {
20
- const fetchBulkParams = jest.fn();
18
+ const fetchBulkParams = jest.fn(() => 'id ^ (1,2)');
21
19
  const refreshTableData = jest.fn();
22
- const contextValue = {
20
+ const defaultContextValue = {
23
21
  selectAllHostsMode: false,
24
22
  selectedCount: 2,
25
23
  selectedResults: [1, 2],
26
24
  fetchBulkParams,
27
25
  refreshTableData,
28
26
  };
27
+ let renderResult;
29
28
 
30
- beforeEach(() => {
31
- jest.clearAllMocks();
32
- openBulkModal('bulk-remove-puppet-ca-proxy', false);
33
- });
29
+ const renderScene = (contextOverrides = {}, isOpen = true) => {
30
+ openBulkModal('bulk-remove-puppet-ca-proxy', isOpen);
34
31
 
35
- it('opens with bulk modal state and passes expected CA proxy props', () => {
36
- openBulkModal('bulk-remove-puppet-ca-proxy', true);
37
- const wrapper = mount(
38
- <ForemanActionsBarContext.Provider value={contextValue}>
32
+ renderResult = renderWithStoreAndI18n(
33
+ <ForemanActionsBarContext.Provider
34
+ value={{ ...defaultContextValue, ...contextOverrides }}
35
+ >
39
36
  <BulkRemovePuppetCAProxyScene />
40
37
  </ForemanActionsBarContext.Provider>
41
38
  );
42
39
 
43
- const componentType =
44
- BulkRemoveProxyCommon.default || BulkRemoveProxyCommon;
45
- const props = wrapper.find(componentType).props();
40
+ return renderResult;
41
+ };
42
+
43
+ beforeEach(() => {
44
+ openBulkModal('bulk-remove-puppet-ca-proxy', false);
45
+ APIActions.put.mockImplementation(params => ({
46
+ type: 'API_PUT',
47
+ payload: params,
48
+ }));
49
+ });
50
+
51
+ afterEach(() => {
52
+ renderResult?.unmount();
53
+ jest.clearAllMocks();
54
+ });
55
+
56
+ it('does not show the remove modal when bulk modal is closed', () => {
57
+ renderScene({}, false);
58
+
59
+ expect(
60
+ screen.queryByRole('dialog', { name: 'Remove Puppet CA Proxy' })
61
+ ).not.toBeInTheDocument();
62
+ });
63
+
64
+ it('shows the remove modal with warning for selected hosts when opened', async () => {
65
+ renderScene();
66
+
67
+ const dialog = await screen.findByRole('dialog', {
68
+ name: 'Remove Puppet CA Proxy',
69
+ });
46
70
 
47
- expect(props).toEqual(
71
+ expect(dialog).toBeInTheDocument();
72
+ expect(
73
+ within(dialog).getByText(/Removing the Puppet CA proxy will affect/)
74
+ ).toBeInTheDocument();
75
+ expect(within(dialog).getByText('2', { selector: 'strong' })).toBeInTheDocument();
76
+ expect(within(dialog).getByText(/selected hosts/)).toBeInTheDocument();
77
+ expect(
78
+ within(dialog).getByText(
79
+ /Warning: If a Puppet Proxy is still set, the Puppet CA Proxy will fall back to that value after removal!/
80
+ )
81
+ ).toBeInTheDocument();
82
+ expect(
83
+ within(dialog).getByRole('button', { name: 'Remove Puppet CA Proxy' })
84
+ ).toBeInTheDocument();
85
+ expect(
86
+ within(dialog).getByRole('button', { name: 'Cancel' })
87
+ ).toBeInTheDocument();
88
+ });
89
+
90
+ it('shows all hosts warning message when select all hosts mode is enabled', async () => {
91
+ renderScene({ selectAllHostsMode: true });
92
+
93
+ const dialog = await screen.findByRole('dialog', {
94
+ name: 'Remove Puppet CA Proxy',
95
+ });
96
+
97
+ expect(
98
+ within(dialog).getByText(/Removing the Puppet CA proxy will affect/)
99
+ ).toBeInTheDocument();
100
+ expect(within(dialog).getByText('All', { selector: 'strong' })).toBeInTheDocument();
101
+ expect(within(dialog).getByText(/selected hosts/)).toBeInTheDocument();
102
+ expect(
103
+ within(dialog).getByText(
104
+ /Warning: If a Puppet Proxy is still set, the Puppet CA Proxy will fall back to that value after removal!/
105
+ )
106
+ ).toBeInTheDocument();
107
+ });
108
+
109
+ it('closes the modal when cancel is clicked', async () => {
110
+ renderScene();
111
+
112
+ await screen.findByRole('dialog', { name: 'Remove Puppet CA Proxy' });
113
+ await userEvent.click(screen.getByRole('button', { name: 'Cancel' }));
114
+
115
+ expect(
116
+ screen.queryByRole('dialog', { name: 'Remove Puppet CA Proxy' })
117
+ ).not.toBeInTheDocument();
118
+ });
119
+
120
+ it('submits CA proxy removal request when confirm is clicked', async () => {
121
+ renderScene();
122
+
123
+ await screen.findByRole('dialog', { name: 'Remove Puppet CA Proxy' });
124
+ await act(async () => {
125
+ await userEvent.click(
126
+ screen.getByRole('button', { name: 'Remove Puppet CA Proxy' })
127
+ );
128
+ });
129
+
130
+ expect(fetchBulkParams).toHaveBeenCalledTimes(1);
131
+ expect(APIActions.put).toHaveBeenCalledWith(
48
132
  expect.objectContaining({
49
- isCAProxy: true,
50
- selectAllHostsMode: false,
51
- selectedCount: 2,
52
- selectedResults: [1, 2],
53
- fetchBulkParams,
54
- isOpen: true,
55
- closeModal: expect.any(Function),
56
- onSuccess: refreshTableData,
57
- handleErrorMessage: 'Failed to remove Puppet CA Proxy',
58
- allHostsMessage:
59
- 'Removing the Puppet CA proxy will affect {boldCount} selected hosts. Warning: If a Puppet Proxy is still set, the Puppet CA Proxy will fall back to that value after removal!',
60
- someHostsMessage:
61
- 'Removing the Puppet CA proxy will affect {boldCount} selected {count, plural, one {host} other {hosts}}. Warning: If a Puppet Proxy is still set, the Puppet CA Proxy will fall back to that value after removal!',
62
- removeMessage: 'Remove Puppet CA Proxy',
133
+ key: BULK_REMOVE_PUPPET_CA_PROXY_KEY,
134
+ params: {
135
+ included: {
136
+ search: 'id ^ (1,2)',
137
+ },
138
+ ca_proxy: true,
139
+ },
63
140
  })
64
141
  );
142
+ });
143
+
144
+ it('refreshes table data after successful removal', async () => {
145
+ API.put.mockImplementation(() =>
146
+ Promise.resolve({ data: { message: 'Removal started' } })
147
+ );
148
+
149
+ renderScene();
150
+
151
+ await screen.findByRole('dialog', { name: 'Remove Puppet CA Proxy' });
152
+ await act(async () => {
153
+ await userEvent.click(
154
+ screen.getByRole('button', { name: 'Remove Puppet CA Proxy' })
155
+ );
156
+ });
65
157
 
66
- wrapper.unmount();
158
+ await waitFor(() => {
159
+ expect(refreshTableData).toHaveBeenCalledTimes(1);
160
+ });
161
+ expect(
162
+ screen.queryByRole('dialog', { name: 'Remove Puppet CA Proxy' })
163
+ ).not.toBeInTheDocument();
67
164
  });
68
165
  });
@@ -0,0 +1,148 @@
1
+ import React from 'react';
2
+ import PropTypes from 'prop-types';
3
+ import { FormattedMessage } from 'react-intl';
4
+ import { useDispatch } from 'react-redux';
5
+ import { Modal, Button, TextContent, Text } from '@patternfly/react-core';
6
+ import { translate as __ } from 'foremanReact/common/I18n';
7
+ import { addToast } from 'foremanReact/components/ToastsList/slice';
8
+ import { useForemanOrganization } from 'foremanReact/Root/Context/ForemanContext';
9
+ import { bulkActionErrorToastParams } from '../toastHelpers';
10
+ import {
11
+ bulkChangePuppetEnvironment,
12
+ BULK_CHANGE_PUPPET_ENVIRONMENT_KEY,
13
+ } from '../BulkChangePuppetEnvironment/actions';
14
+
15
+ const BulkRemovePuppetEnvironmentModal = ({
16
+ isOpen,
17
+ closeModal,
18
+ selectAllHostsMode,
19
+ selectedCount,
20
+ fetchBulkParams,
21
+ onSuccess: onSuccessCallback,
22
+ }) => {
23
+ const dispatch = useDispatch();
24
+ const currentOrganization = useForemanOrganization();
25
+
26
+ const handleModalClose = () => {
27
+ closeModal();
28
+ };
29
+
30
+ const handleError = response => {
31
+ handleModalClose();
32
+ dispatch(
33
+ addToast(
34
+ bulkActionErrorToastParams(
35
+ response,
36
+ __('Failed to remove Puppet Environment'),
37
+ BULK_CHANGE_PUPPET_ENVIRONMENT_KEY
38
+ )
39
+ )
40
+ );
41
+ };
42
+
43
+ const handleSuccess = response => {
44
+ dispatch(
45
+ addToast({
46
+ type: 'success',
47
+ message: response.data.message,
48
+ })
49
+ );
50
+ try {
51
+ if (onSuccessCallback) onSuccessCallback();
52
+ } finally {
53
+ handleModalClose();
54
+ }
55
+ };
56
+
57
+ const handleConfirm = () => {
58
+ const requestBody = {
59
+ included: {
60
+ search: fetchBulkParams(),
61
+ },
62
+ environment_id: null,
63
+ organization_id: currentOrganization?.id,
64
+ };
65
+
66
+ dispatch(
67
+ bulkChangePuppetEnvironment(requestBody, handleSuccess, handleError)
68
+ );
69
+ };
70
+
71
+ const modalActions = [
72
+ <Button
73
+ key="remove"
74
+ ouiaId="bulk-remove-puppet-environment-modal-remove-button"
75
+ variant="primary"
76
+ onClick={handleConfirm}
77
+ >
78
+ {__('Remove Puppet Environment')}
79
+ </Button>,
80
+ <Button
81
+ key="cancel"
82
+ ouiaId="bulk-remove-puppet-environment-modal-cancel-button"
83
+ variant="link"
84
+ onClick={handleModalClose}
85
+ >
86
+ {__('Cancel')}
87
+ </Button>,
88
+ ];
89
+
90
+ return (
91
+ <Modal
92
+ isOpen={isOpen}
93
+ onClose={handleModalClose}
94
+ onEscapePress={handleModalClose}
95
+ title={__('Remove Puppet Environment')}
96
+ width="50%"
97
+ position="top"
98
+ actions={modalActions}
99
+ id="bulk-remove-puppet-environment"
100
+ key="bulk-remove-puppet-environment"
101
+ ouiaId="bulk-remove-puppet-environment"
102
+ >
103
+ <TextContent>
104
+ <Text ouiaId="bulk-remove-puppet-environment-options">
105
+ {selectAllHostsMode ? (
106
+ <FormattedMessage
107
+ id="bulk-remove-puppet-environment-warning-message-all"
108
+ defaultMessage={__(
109
+ 'Removing the Puppet environment will affect {boldCount} selected hosts.'
110
+ )}
111
+ values={{
112
+ boldCount: <strong>{__('All')}</strong>,
113
+ }}
114
+ />
115
+ ) : (
116
+ <FormattedMessage
117
+ id="bulk-remove-puppet-environment-warning-message"
118
+ defaultMessage={__(
119
+ 'Removing the Puppet environment will affect {boldCount} selected {count, plural, one {host} other {hosts}}.'
120
+ )}
121
+ values={{
122
+ count: selectedCount,
123
+ boldCount: <strong>{selectedCount}</strong>,
124
+ }}
125
+ />
126
+ )}
127
+ </Text>
128
+ </TextContent>
129
+ </Modal>
130
+ );
131
+ };
132
+
133
+ BulkRemovePuppetEnvironmentModal.propTypes = {
134
+ isOpen: PropTypes.bool,
135
+ closeModal: PropTypes.func,
136
+ fetchBulkParams: PropTypes.func.isRequired,
137
+ onSuccess: PropTypes.func,
138
+ selectedCount: PropTypes.number.isRequired,
139
+ selectAllHostsMode: PropTypes.bool.isRequired,
140
+ };
141
+
142
+ BulkRemovePuppetEnvironmentModal.defaultProps = {
143
+ isOpen: false,
144
+ closeModal: () => {},
145
+ onSuccess: undefined,
146
+ };
147
+
148
+ export default BulkRemovePuppetEnvironmentModal;
@@ -0,0 +1,80 @@
1
+ import React from 'react';
2
+ import { render, screen, fireEvent } from '@testing-library/react';
3
+ import '@testing-library/jest-dom';
4
+ import { IntlProvider } from 'react-intl';
5
+ import { useDispatch } from 'react-redux';
6
+
7
+ import BulkRemovePuppetEnvironmentModal from '../BulkRemovePuppetEnvironmentModal';
8
+ import { bulkChangePuppetEnvironment } from '../../BulkChangePuppetEnvironment/actions';
9
+
10
+ jest.mock('react-redux', () => ({
11
+ ...jest.requireActual('react-redux'),
12
+ useDispatch: jest.fn(),
13
+ }));
14
+
15
+ jest.mock('foremanReact/Root/Context/ForemanContext', () => ({
16
+ useForemanOrganization: jest.fn(() => ({ id: 23 })),
17
+ }));
18
+
19
+ jest.mock('../../BulkChangePuppetEnvironment/actions', () => ({
20
+ bulkChangePuppetEnvironment: jest.fn(() => ({
21
+ type: 'BULK_CHANGE_PUPPET_ENVIRONMENT',
22
+ })),
23
+ BULK_CHANGE_PUPPET_ENVIRONMENT_KEY: 'BULK_CHANGE_PUPPET_ENVIRONMENT',
24
+ }));
25
+
26
+ describe('BulkRemovePuppetEnvironmentModal', () => {
27
+ const dispatch = jest.fn();
28
+
29
+ const renderComponent = (fetchBulkParams = jest.fn()) =>
30
+ render(
31
+ <IntlProvider locale="en">
32
+ <BulkRemovePuppetEnvironmentModal
33
+ isOpen
34
+ closeModal={jest.fn()}
35
+ fetchBulkParams={fetchBulkParams}
36
+ selectedCount={2}
37
+ selectAllHostsMode={false}
38
+ />
39
+ </IntlProvider>
40
+ );
41
+
42
+ beforeEach(() => {
43
+ jest.clearAllMocks();
44
+ useDispatch.mockReturnValue(dispatch);
45
+ });
46
+
47
+ it('renders the removal warning', () => {
48
+ renderComponent();
49
+
50
+ expect(
51
+ screen.getAllByText('Remove Puppet Environment')[0]
52
+ ).toBeInTheDocument();
53
+ expect(
54
+ screen.getByText(/Removing the Puppet environment will affect/)
55
+ ).toBeInTheDocument();
56
+ });
57
+
58
+ it('submits organization scope with the bulk remove request', () => {
59
+ const fetchBulkParams = jest.fn(
60
+ () => 'organization = "Default Organization"'
61
+ );
62
+ renderComponent(fetchBulkParams);
63
+
64
+ fireEvent.click(
65
+ screen.getByRole('button', { name: 'Remove Puppet Environment' })
66
+ );
67
+
68
+ expect(bulkChangePuppetEnvironment).toHaveBeenCalledWith(
69
+ {
70
+ included: {
71
+ search: 'organization = "Default Organization"',
72
+ },
73
+ environment_id: null,
74
+ organization_id: 23,
75
+ },
76
+ expect.any(Function),
77
+ expect.any(Function)
78
+ );
79
+ });
80
+ });