csp.spec.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. params={{orgId: organization.slug, projectId: project.slug}}
  32. />
  33. );
  34. expect(container).toSnapshot();
  35. });
  36. it('can enable default ignored sources', function () {
  37. render(
  38. <ProjectCspReports
  39. route={{}}
  40. routeParams={router.params}
  41. router={router}
  42. routes={router.routes}
  43. location={TestStubs.location({pathname: routeUrl})}
  44. params={{orgId: organization.slug, projectId: project.slug}}
  45. />
  46. );
  47. const mock = MockApiClient.addMockResponse({
  48. url: projectUrl,
  49. method: 'PUT',
  50. });
  51. expect(mock).not.toHaveBeenCalled();
  52. // Click Regenerate Token
  53. userEvent.click(screen.getByRole('checkbox', {name: 'Use default ignored sources'}));
  54. expect(mock).toHaveBeenCalledWith(
  55. projectUrl,
  56. expect.objectContaining({
  57. method: 'PUT',
  58. data: {
  59. options: {
  60. 'sentry:csp_ignored_sources_defaults': true,
  61. },
  62. },
  63. })
  64. );
  65. });
  66. it('can set additional ignored sources', function () {
  67. render(
  68. <ProjectCspReports
  69. route={{}}
  70. routeParams={router.params}
  71. router={router}
  72. routes={router.routes}
  73. location={TestStubs.location({pathname: routeUrl})}
  74. params={{orgId: organization.slug, projectId: project.slug}}
  75. />
  76. );
  77. const mock = MockApiClient.addMockResponse({
  78. url: projectUrl,
  79. method: 'PUT',
  80. });
  81. expect(mock).not.toHaveBeenCalled();
  82. userEvent.type(
  83. screen.getByRole('textbox', {name: 'Additional ignored sources'}),
  84. 'test\ntest2'
  85. );
  86. // Focus on other element, trigerring onBlur
  87. userEvent.tab();
  88. expect(mock).toHaveBeenCalledWith(
  89. projectUrl,
  90. expect.objectContaining({
  91. method: 'PUT',
  92. data: {
  93. // 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
  94. options: {
  95. 'sentry:csp_ignored_sources': `test\ntest2`,
  96. },
  97. },
  98. })
  99. );
  100. });
  101. });