index.spec.tsx 5.8 KB

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