projectCspReports.spec.jsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {mountWithTheme} from 'sentry-test/enzyme';
  2. import ProjectCspReports from 'sentry/views/settings/projectSecurityHeaders/csp';
  3. describe('ProjectCspReports', function () {
  4. const org = TestStubs.Organization();
  5. const project = TestStubs.Project();
  6. const projectUrl = `/projects/${org.slug}/${project.slug}/`;
  7. const routeUrl = `/projects/${org.slug}/${project.slug}/csp/`;
  8. beforeEach(function () {
  9. MockApiClient.clearMockResponses();
  10. MockApiClient.addMockResponse({
  11. url: `/projects/${org.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 wrapper = mountWithTheme(
  25. <ProjectCspReports
  26. organization={org}
  27. project={project}
  28. {...TestStubs.routerProps({
  29. params: {orgId: org.slug, projectId: project.slug},
  30. location: TestStubs.location({pathname: routeUrl}),
  31. })}
  32. />
  33. );
  34. expect(wrapper).toSnapshot();
  35. });
  36. it('can enable default ignored sources', function () {
  37. const wrapper = mountWithTheme(
  38. <ProjectCspReports
  39. organization={org}
  40. project={project}
  41. {...TestStubs.routerProps({
  42. params: {orgId: org.slug, projectId: project.slug},
  43. location: TestStubs.location({pathname: routeUrl}),
  44. })}
  45. />
  46. );
  47. const mock = MockApiClient.addMockResponse({
  48. url: projectUrl,
  49. method: 'PUT',
  50. });
  51. expect(mock).not.toHaveBeenCalled();
  52. // Click Regenerate Token
  53. wrapper.find('Switch').simulate('click');
  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. const wrapper = mountWithTheme(
  68. <ProjectCspReports
  69. organization={org}
  70. project={project}
  71. {...TestStubs.routerProps({
  72. params: {orgId: org.slug, projectId: project.slug},
  73. location: TestStubs.location({pathname: routeUrl}),
  74. })}
  75. />
  76. );
  77. const mock = MockApiClient.addMockResponse({
  78. url: projectUrl,
  79. method: 'PUT',
  80. });
  81. expect(mock).not.toHaveBeenCalled();
  82. // Click Regenerate Token
  83. wrapper
  84. .find('textarea')
  85. .simulate('change', {
  86. target: {
  87. value: `test
  88. test2`,
  89. },
  90. })
  91. .simulate('blur');
  92. expect(mock).toHaveBeenCalledWith(
  93. projectUrl,
  94. expect.objectContaining({
  95. method: 'PUT',
  96. data: {
  97. // 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
  98. options: {
  99. 'sentry:csp_ignored_sources': `test
  100. test2`,
  101. },
  102. },
  103. })
  104. );
  105. });
  106. });