index.spec.jsx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import * as PropTypes from 'prop-types';
  2. import {
  3. render,
  4. renderGlobalModal,
  5. screen,
  6. userEvent,
  7. } from 'sentry-test/reactTestingLibrary';
  8. import ProjectKeyDetails from 'sentry/views/settings/project/projectKeys/details';
  9. describe('ProjectKeyDetails', function () {
  10. let org;
  11. let project;
  12. let deleteMock;
  13. let statsMock;
  14. let putMock;
  15. let projectKeys;
  16. beforeEach(function () {
  17. org = TestStubs.Organization();
  18. project = TestStubs.Project();
  19. projectKeys = TestStubs.ProjectKeys();
  20. MockApiClient.clearMockResponses();
  21. MockApiClient.addMockResponse({
  22. url: `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  23. method: 'GET',
  24. body: projectKeys[0],
  25. });
  26. putMock = MockApiClient.addMockResponse({
  27. url: `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  28. method: 'PUT',
  29. });
  30. statsMock = MockApiClient.addMockResponse({
  31. url: `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/stats/`,
  32. method: 'GET',
  33. body: [
  34. {filtered: 0, accepted: 0, total: 0, ts: 1517270400, dropped: 0},
  35. {filtered: 0, accepted: 0, total: 0, ts: 1517356800, dropped: 0},
  36. {filtered: 0, accepted: 0, total: 0, ts: 1517443200, dropped: 0},
  37. {filtered: 0, accepted: 0, total: 0, ts: 1517529600, dropped: 0},
  38. {filtered: 0, accepted: 0, total: 0, ts: 1517616000, dropped: 0},
  39. {filtered: 0, accepted: 0, total: 0, ts: 1517702400, dropped: 0},
  40. {filtered: 0, accepted: 0, total: 0, ts: 1517788800, dropped: 0},
  41. {filtered: 0, accepted: 0, total: 0, ts: 1517875200, dropped: 0},
  42. {filtered: 0, accepted: 0, total: 0, ts: 1517961600, dropped: 0},
  43. {filtered: 0, accepted: 0, total: 0, ts: 1518048000, dropped: 0},
  44. {filtered: 0, accepted: 0, total: 0, ts: 1518134400, dropped: 0},
  45. {filtered: 0, accepted: 0, total: 0, ts: 1518220800, dropped: 0},
  46. {filtered: 0, accepted: 0, total: 0, ts: 1518307200, dropped: 0},
  47. {filtered: 0, accepted: 0, total: 0, ts: 1518393600, dropped: 0},
  48. {filtered: 0, accepted: 0, total: 0, ts: 1518480000, dropped: 0},
  49. {filtered: 0, accepted: 0, total: 0, ts: 1518566400, dropped: 0},
  50. {filtered: 0, accepted: 0, total: 0, ts: 1518652800, dropped: 0},
  51. {filtered: 0, accepted: 0, total: 0, ts: 1518739200, dropped: 0},
  52. {filtered: 0, accepted: 0, total: 0, ts: 1518825600, dropped: 0},
  53. {filtered: 0, accepted: 0, total: 0, ts: 1518912000, dropped: 0},
  54. {filtered: 0, accepted: 0, total: 0, ts: 1518998400, dropped: 0},
  55. {filtered: 0, accepted: 0, total: 0, ts: 1519084800, dropped: 0},
  56. {filtered: 0, accepted: 0, total: 0, ts: 1519171200, dropped: 0},
  57. {filtered: 0, accepted: 0, total: 0, ts: 1519257600, dropped: 0},
  58. {filtered: 0, accepted: 0, total: 0, ts: 1519344000, dropped: 0},
  59. {filtered: 0, accepted: 0, total: 0, ts: 1519430400, dropped: 0},
  60. {filtered: 0, accepted: 0, total: 0, ts: 1519516800, dropped: 0},
  61. {filtered: 0, accepted: 0, total: 0, ts: 1519603200, dropped: 0},
  62. {filtered: 0, accepted: 0, total: 0, ts: 1519689600, dropped: 0},
  63. {filtered: 0, accepted: 5, total: 12, ts: 1519776000, dropped: 7},
  64. {filtered: 0, accepted: 14, total: 14, ts: 1519862400, dropped: 0},
  65. ],
  66. });
  67. deleteMock = MockApiClient.addMockResponse({
  68. url: `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  69. method: 'DELETE',
  70. });
  71. const context = {
  72. context: {
  73. project: TestStubs.Project(),
  74. },
  75. childContextTypes: {
  76. project: PropTypes.object,
  77. },
  78. };
  79. render(
  80. <ProjectKeyDetails
  81. routes={[]}
  82. params={{
  83. keyId: projectKeys[0].id,
  84. orgId: org.slug,
  85. projectId: project.slug,
  86. }}
  87. />,
  88. {context}
  89. );
  90. });
  91. it('has stats box', function () {
  92. expect(screen.getByText('Key Details')).toBeInTheDocument();
  93. expect(statsMock).toHaveBeenCalled();
  94. });
  95. it('changes name', function () {
  96. userEvent.clear(screen.getByRole('textbox', {name: 'Name'}));
  97. userEvent.type(screen.getByRole('textbox', {name: 'Name'}), 'New Name');
  98. userEvent.tab();
  99. expect(putMock).toHaveBeenCalledWith(
  100. `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  101. expect.objectContaining({
  102. data: {
  103. name: 'New Name',
  104. },
  105. })
  106. );
  107. });
  108. it('disable and enables key', function () {
  109. userEvent.click(screen.getByRole('checkbox', {name: 'Enabled'}));
  110. expect(putMock).toHaveBeenCalledWith(
  111. `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  112. expect.objectContaining({
  113. data: {isActive: false},
  114. })
  115. );
  116. userEvent.click(screen.getByRole('checkbox', {name: 'Enabled'}));
  117. expect(putMock).toHaveBeenCalledWith(
  118. `/projects/${org.slug}/${project.slug}/keys/${projectKeys[0].id}/`,
  119. expect.objectContaining({
  120. data: {isActive: false},
  121. })
  122. );
  123. });
  124. it('revokes a key', function () {
  125. userEvent.click(screen.getByRole('button', {name: 'Revoke Key'}));
  126. renderGlobalModal();
  127. userEvent.click(screen.getByTestId('confirm-button'));
  128. expect(deleteMock).toHaveBeenCalled();
  129. });
  130. });