csp.spec.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {initializeOrg} from 'sentry-test/initializeOrg';
  2. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  3. import ProjectCspReports from 'sentry/views/settings/projectSecurityHeaders/csp';
  4. describe('ProjectCspReports', function () {
  5. const {project, organization, router} = initializeOrg();
  6. const projectUrl = `/projects/${organization.slug}/${project.slug}/`;
  7. const routeUrl = `/projects/${organization.slug}/${project.slug}/csp/`;
  8. beforeEach(function () {
  9. MockApiClient.clearMockResponses();
  10. MockApiClient.addMockResponse({
  11. url: `/projects/${organization.slug}/${project.slug}/keys/`,
  12. method: 'GET',
  13. body: [],
  14. });
  15. MockApiClient.addMockResponse({
  16. url: projectUrl,
  17. method: 'GET',
  18. body: {
  19. options: {},
  20. },
  21. });
  22. });
  23. it('renders', function () {
  24. const {container} = render(
  25. <ProjectCspReports
  26. route={{}}
  27. routeParams={router.params}
  28. router={router}
  29. routes={router.routes}
  30. location={TestStubs.location({pathname: routeUrl})}
  31. organization={organization}
  32. params={{projectId: project.slug}}
  33. />
  34. );
  35. expect(container).toSnapshot();
  36. });
  37. it('can enable default ignored sources', function () {
  38. render(
  39. <ProjectCspReports
  40. route={{}}
  41. routeParams={router.params}
  42. router={router}
  43. routes={router.routes}
  44. location={TestStubs.location({pathname: routeUrl})}
  45. organization={organization}
  46. params={{projectId: project.slug}}
  47. />
  48. );
  49. const mock = MockApiClient.addMockResponse({
  50. url: projectUrl,
  51. method: 'PUT',
  52. });
  53. expect(mock).not.toHaveBeenCalled();
  54. // Click Regenerate Token
  55. userEvent.click(screen.getByRole('checkbox', {name: 'Use default ignored sources'}));
  56. expect(mock).toHaveBeenCalledWith(
  57. projectUrl,
  58. expect.objectContaining({
  59. method: 'PUT',
  60. data: {
  61. options: {
  62. 'sentry:csp_ignored_sources_defaults': true,
  63. },
  64. },
  65. })
  66. );
  67. });
  68. it('can set additional ignored sources', function () {
  69. render(
  70. <ProjectCspReports
  71. route={{}}
  72. routeParams={router.params}
  73. router={router}
  74. routes={router.routes}
  75. location={TestStubs.location({pathname: routeUrl})}
  76. organization={organization}
  77. params={{projectId: project.slug}}
  78. />
  79. );
  80. const mock = MockApiClient.addMockResponse({
  81. url: projectUrl,
  82. method: 'PUT',
  83. });
  84. expect(mock).not.toHaveBeenCalled();
  85. userEvent.type(
  86. screen.getByRole('textbox', {name: 'Additional ignored sources'}),
  87. 'test\ntest2'
  88. );
  89. // Focus on other element, trigerring onBlur
  90. userEvent.tab();
  91. expect(mock).toHaveBeenCalledWith(
  92. projectUrl,
  93. expect.objectContaining({
  94. method: 'PUT',
  95. data: {
  96. // XXX: Org details endpoints accept these multiline inputs as a list, where as it looks like project details accepts it as a string with newlines
  97. options: {
  98. 'sentry:csp_ignored_sources': `test\ntest2`,
  99. },
  100. },
  101. })
  102. );
  103. });
  104. });