You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AuditApp-it.tsx 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { screen } from '@testing-library/react';
  21. import userEvent from '@testing-library/user-event';
  22. import { getDate, getMonth, getYear, subDays } from 'date-fns';
  23. import selectEvent from 'react-select-event';
  24. import { byPlaceholderText, byRole, byText } from 'testing-library-selector';
  25. import SettingsServiceMock from '../../../../api/mocks/SettingsServiceMock';
  26. import { now } from '../../../../helpers/dates';
  27. import { getShortMonthName } from '../../../../helpers/l10n';
  28. import { renderAppWithAdminContext } from '../../../../helpers/testReactTestingUtils';
  29. import { AdminPageExtension } from '../../../../types/extension';
  30. import routes from '../../routes';
  31. jest.mock('../../../../api/settings');
  32. const extensions = [
  33. { key: AdminPageExtension.GovernanceConsole, name: 'Portfolios' },
  34. { key: 'license/app', name: 'License Manager' },
  35. { key: 'license/support', name: 'Support' }
  36. ];
  37. jest.mock('date-fns', () => {
  38. // Timezone will not play well so we fake the response from lib.
  39. const dateFns = jest.requireActual('date-fns');
  40. return {
  41. ...dateFns,
  42. endOfDay: jest.fn().mockImplementation(d => d),
  43. startOfDay: jest.fn().mockImplementation(d => d)
  44. };
  45. });
  46. jest.mock('../../../../helpers/dates', () => {
  47. return {
  48. ...jest.requireActual('../../../../helpers/dates'),
  49. now: jest.fn(() => new Date('2020-07-21T12:00:00Z'))
  50. };
  51. });
  52. const ui = {
  53. pageTitle: byRole('heading', { name: 'audit_logs.page' }),
  54. downloadButton: byRole('link', { name: 'download_verb' }),
  55. todayRadio: byRole('radio', { name: 'audit_logs.range_option.today' }),
  56. weekRadio: byRole('radio', { name: 'audit_logs.range_option.7days' }),
  57. monthRadio: byRole('radio', { name: 'audit_logs.range_option.30days' }),
  58. trimesterRadio: byRole('radio', { name: 'audit_logs.range_option.90days' }),
  59. customRadio: byRole('radio', { name: 'audit_logs.range_option.custom' }),
  60. downloadSentenceStart: byText('audit_logs.download_start.sentence.1'),
  61. startDateInput: byPlaceholderText('start_date'),
  62. endDateInput: byPlaceholderText('end_date')
  63. };
  64. let handler: SettingsServiceMock;
  65. beforeAll(() => {
  66. handler = new SettingsServiceMock();
  67. });
  68. afterEach(() => handler.resetSettingvalues());
  69. it('should handle download button click', async () => {
  70. const user = userEvent.setup();
  71. handler.setYearlyHousekeepingPolicy();
  72. renderAuditLogs();
  73. const downloadButton = await ui.downloadButton.find();
  74. expect(downloadButton).toBeInTheDocument();
  75. expect(downloadButton).toHaveAttribute(
  76. 'href',
  77. '/api/audit_logs/download?from=2020-07-21T12%3A00%3A00.000Z&to=2020-07-21T12%3A00%3A00.000Z'
  78. );
  79. await user.click(ui.weekRadio.get());
  80. expect(downloadButton).toHaveAttribute(
  81. 'href',
  82. '/api/audit_logs/download?from=2020-07-14T12%3A00%3A00.000Z&to=2020-07-21T12%3A00%3A00.000Z'
  83. );
  84. await user.click(ui.monthRadio.get());
  85. expect(downloadButton).toHaveAttribute(
  86. 'href',
  87. '/api/audit_logs/download?from=2020-06-21T12%3A00%3A00.000Z&to=2020-07-21T12%3A00%3A00.000Z'
  88. );
  89. await user.click(ui.trimesterRadio.get());
  90. expect(downloadButton).toHaveAttribute(
  91. 'href',
  92. '/api/audit_logs/download?from=2020-04-22T12%3A00%3A00.000Z&to=2020-07-21T12%3A00%3A00.000Z'
  93. );
  94. await user.click(downloadButton);
  95. expect(await ui.downloadButton.find()).toHaveAttribute('aria-disabled', 'true');
  96. expect(ui.downloadSentenceStart.get()).toBeInTheDocument();
  97. // Custom date
  98. const startDay = subDays(now(), 5);
  99. const endDate = subDays(now(), 1);
  100. await user.click(ui.customRadio.get());
  101. expect(ui.downloadButton.get()).toHaveAttribute('aria-disabled', 'true');
  102. await user.click(ui.startDateInput.get());
  103. await selectEvent.select(screen.getByRole('textbox', { name: 'select_month' }), [
  104. getShortMonthName(getMonth(startDay))
  105. ]);
  106. await selectEvent.select(screen.getByRole('textbox', { name: 'select_year' }), [
  107. getYear(startDay)
  108. ]);
  109. await user.click(screen.getByText(getDate(startDay)));
  110. await user.click(ui.endDateInput.get());
  111. await selectEvent.select(screen.getByRole('textbox', { name: 'select_month' }), [
  112. getShortMonthName(getMonth(endDate))
  113. ]);
  114. await selectEvent.select(screen.getByRole('textbox', { name: 'select_year' }), [
  115. getYear(endDate)
  116. ]);
  117. await user.click(screen.getByText(getDate(endDate)));
  118. expect(await ui.downloadButton.find()).toHaveAttribute('aria-disabled', 'false');
  119. await user.click(downloadButton);
  120. expect(await ui.downloadButton.find()).toHaveAttribute('aria-disabled', 'true');
  121. });
  122. it('should not render if governance is not enable', () => {
  123. renderAuditLogs([]);
  124. expect(ui.pageTitle.query()).not.toBeInTheDocument();
  125. });
  126. it('should show right option when keeping log for month', async () => {
  127. handler.unsetHousekeepingPolicy();
  128. renderAuditLogs();
  129. expect(await ui.pageTitle.find()).toBeInTheDocument();
  130. expect(ui.todayRadio.get()).toBeInTheDocument();
  131. expect(ui.weekRadio.get()).toBeInTheDocument();
  132. expect(ui.monthRadio.get()).toBeInTheDocument();
  133. expect(ui.customRadio.get()).toBeInTheDocument();
  134. expect(ui.trimesterRadio.query()).not.toBeInTheDocument();
  135. });
  136. it('should show right option when keeping log for year', async () => {
  137. handler.setYearlyHousekeepingPolicy();
  138. renderAuditLogs();
  139. expect(await ui.pageTitle.find()).toBeInTheDocument();
  140. expect(ui.todayRadio.get()).toBeInTheDocument();
  141. expect(ui.weekRadio.get()).toBeInTheDocument();
  142. expect(ui.monthRadio.get()).toBeInTheDocument();
  143. expect(ui.trimesterRadio.get()).toBeInTheDocument();
  144. expect(ui.customRadio.get()).toBeInTheDocument();
  145. });
  146. function renderAuditLogs(adminPages = extensions) {
  147. renderAppWithAdminContext('admin/audit', routes, {}, { adminPages });
  148. }