index.spec.tsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. import {ProjectFixture} from 'sentry-fixture/project';
  2. import {ProjectKeysFixture} from 'sentry-fixture/projectKeys';
  3. import {RouterFixture} from 'sentry-fixture/routerFixture';
  4. import {initializeOrg} from 'sentry-test/initializeOrg';
  5. import {
  6. render,
  7. renderGlobalModal,
  8. screen,
  9. userEvent,
  10. waitFor,
  11. } from 'sentry-test/reactTestingLibrary';
  12. import ProjectKeys from 'sentry/views/settings/project/projectKeys/list';
  13. describe('ProjectKeys', function () {
  14. const {organization, project} = initializeOrg();
  15. const projectKeys = ProjectKeysFixture();
  16. let deleteMock: jest.Mock;
  17. beforeEach(function () {
  18. MockApiClient.clearMockResponses();
  19. MockApiClient.addMockResponse({
  20. url: `/projects/${organization.slug}/${project.slug}/keys/`,
  21. method: 'GET',
  22. body: projectKeys,
  23. });
  24. deleteMock = MockApiClient.addMockResponse({
  25. url: `/projects/${organization.slug}/${project.slug}/keys/${projectKeys[0]!.id}/`,
  26. method: 'DELETE',
  27. });
  28. });
  29. it('renders empty', async function () {
  30. MockApiClient.clearMockResponses();
  31. MockApiClient.addMockResponse({
  32. url: `/projects/${organization.slug}/${project.slug}/keys/`,
  33. method: 'GET',
  34. body: [],
  35. });
  36. const router = RouterFixture({projectId: project.slug});
  37. render(<ProjectKeys project={project} />, {router});
  38. expect(
  39. await screen.findByText('There are no keys active for this project.')
  40. ).toBeInTheDocument();
  41. });
  42. it('has clippable box', async function () {
  43. const router = RouterFixture({projectId: project.slug});
  44. render(<ProjectKeys project={ProjectFixture()} />, {router});
  45. const expandButton = await screen.findByRole('button', {name: 'Expand'});
  46. await userEvent.click(expandButton);
  47. expect(expandButton).not.toBeInTheDocument();
  48. });
  49. it('renders for default project', async function () {
  50. const router = RouterFixture({projectId: project.slug});
  51. render(<ProjectKeys project={ProjectFixture({platform: 'other'})} />, {router});
  52. const allDsn = await screen.findAllByRole('textbox', {name: 'DSN URL'});
  53. expect(allDsn).toHaveLength(1);
  54. const expandButton = screen.getByRole('button', {name: 'Expand'});
  55. const dsn = screen.getByRole('textbox', {name: 'DSN URL'});
  56. const minidumpEndpoint = screen.queryByRole('textbox', {
  57. name: 'Minidump Endpoint URL',
  58. });
  59. const unrealEndpoint = screen.queryByRole('textbox', {
  60. name: 'Unreal Engine Endpoint URL',
  61. });
  62. const securityHeaderEndpoint = screen.queryByRole('textbox', {
  63. name: 'Security Header Endpoint URL',
  64. });
  65. expect(expandButton).toBeInTheDocument();
  66. expect(dsn).toHaveValue(projectKeys[0]!.dsn.public);
  67. expect(minidumpEndpoint).toHaveValue(projectKeys[0]!.dsn.minidump);
  68. // this is empty in the default ProjectKey
  69. expect(unrealEndpoint).toHaveValue('');
  70. expect(securityHeaderEndpoint).toHaveValue(projectKeys[0]!.dsn.security);
  71. });
  72. it('renders for javascript project', async function () {
  73. const router = RouterFixture({projectId: project.slug});
  74. render(<ProjectKeys project={ProjectFixture({platform: 'javascript'})} />, {router});
  75. const expandButton = screen.queryByRole('button', {name: 'Expand'});
  76. const dsn = await screen.findByRole('textbox', {name: 'DSN URL'});
  77. const minidumpEndpoint = screen.queryByRole('textbox', {
  78. name: 'Minidump Endpoint URL',
  79. });
  80. const unrealEndpoint = screen.queryByRole('textbox', {
  81. name: 'Unreal Engine Endpoint URL',
  82. });
  83. const securityHeaderEndpoint = screen.queryByRole('textbox', {
  84. name: 'Security Header Endpoint URL',
  85. });
  86. expect(expandButton).not.toBeInTheDocument();
  87. expect(dsn).toHaveValue(projectKeys[0]!.dsn.public);
  88. expect(minidumpEndpoint).not.toBeInTheDocument();
  89. expect(unrealEndpoint).not.toBeInTheDocument();
  90. expect(securityHeaderEndpoint).not.toBeInTheDocument();
  91. // Loader Script is rendered
  92. expect(screen.getByText('Loader Script')).toBeInTheDocument();
  93. const loaderScript = screen.getByRole<HTMLInputElement>('textbox', {
  94. name: 'Loader Script',
  95. });
  96. expect(loaderScript).toHaveValue(
  97. `<script src='${projectKeys[0]!.dsn.cdn}' crossorigin="anonymous"></script>`
  98. );
  99. });
  100. it('renders for javascript-react project', async function () {
  101. const router = RouterFixture({projectId: project.slug});
  102. render(<ProjectKeys project={ProjectFixture({platform: 'javascript-react'})} />, {
  103. router,
  104. });
  105. const expandButton = screen.queryByRole('button', {name: 'Expand'});
  106. const dsn = await screen.findByRole('textbox', {name: 'DSN URL'});
  107. const minidumpEndpoint = screen.queryByRole('textbox', {
  108. name: 'Minidump Endpoint URL',
  109. });
  110. const unrealEndpoint = screen.queryByRole('textbox', {
  111. name: 'Unreal Engine Endpoint URL',
  112. });
  113. const securityHeaderEndpoint = screen.queryByRole('textbox', {
  114. name: 'Security Header Endpoint URL',
  115. });
  116. expect(expandButton).not.toBeInTheDocument();
  117. expect(dsn).toHaveValue(projectKeys[0]!.dsn.public);
  118. expect(minidumpEndpoint).not.toBeInTheDocument();
  119. expect(unrealEndpoint).not.toBeInTheDocument();
  120. expect(securityHeaderEndpoint).not.toBeInTheDocument();
  121. expect(screen.queryByText('Loader Script')).not.toBeInTheDocument();
  122. });
  123. it('renders multiple keys', async function () {
  124. const multipleProjectKeys = ProjectKeysFixture([
  125. {
  126. dsn: {
  127. secret:
  128. 'http://188ee45a58094d939428d8585aa6f662:a33bf9aba64c4bbdaf873bb9023b6d2c@dev.getsentry.net:8000/1',
  129. minidump:
  130. 'http://dev.getsentry.net:8000/api/1/minidump?sentry_key=188ee45a58094d939428d8585aa6f662',
  131. public: 'http://188ee45a58094d939428d8585aa6f662@dev.getsentry.net:8000/1',
  132. csp: 'http://dev.getsentry.net:8000/api/1/csp-report/?sentry_key=188ee45a58094d939428d8585aa6f662',
  133. security:
  134. 'http://dev.getsentry.net:8000/api/1/security-report/?sentry_key=188ee45a58094d939428d8585aa6f662',
  135. cdn: '',
  136. unreal: '',
  137. crons: '',
  138. },
  139. dateCreated: '2018-02-28T07:13:51.087Z',
  140. public: '188ee45a58094d939428d8585aa6f662',
  141. secret: 'a33bf9aba64c4bbdaf873bb9023b6d2c',
  142. name: 'Key 2',
  143. rateLimit: null,
  144. projectId: 1,
  145. id: '188ee45a58094d939428d8585aa6f662',
  146. isActive: true,
  147. label: 'Key 2',
  148. browserSdkVersion: 'latest',
  149. browserSdk: {
  150. choices: [
  151. ['latest', 'latest'],
  152. ['7.x', '7.x'],
  153. ['6.x', '6.x'],
  154. ['5.x', '5.x'],
  155. ['4.x', '4.x'],
  156. ],
  157. },
  158. dynamicSdkLoaderOptions: {
  159. hasPerformance: false,
  160. hasReplay: false,
  161. hasDebug: false,
  162. },
  163. },
  164. ]);
  165. MockApiClient.addMockResponse({
  166. url: `/projects/${organization.slug}/${project.slug}/keys/`,
  167. method: 'GET',
  168. body: multipleProjectKeys,
  169. });
  170. const router = RouterFixture({projectId: project.slug});
  171. render(<ProjectKeys project={ProjectFixture({platform: 'other'})} />, {router});
  172. const allDsn = await screen.findAllByRole('textbox', {name: 'DSN URL'});
  173. expect(allDsn).toHaveLength(2);
  174. });
  175. it('deletes key', async function () {
  176. const router = RouterFixture({projectId: project.slug});
  177. render(<ProjectKeys project={ProjectFixture()} />, {router});
  178. await userEvent.click(await screen.findByRole('button', {name: 'Delete'}));
  179. renderGlobalModal();
  180. await userEvent.click(screen.getByTestId('confirm-button'));
  181. expect(deleteMock).toHaveBeenCalled();
  182. });
  183. it('disable and enables key', async function () {
  184. const router = RouterFixture({projectId: project.slug});
  185. render(<ProjectKeys project={ProjectFixture()} />, {router});
  186. const enableMock = MockApiClient.addMockResponse({
  187. url: `/projects/${organization.slug}/${project.slug}/keys/${projectKeys[0]!.id}/`,
  188. method: 'PUT',
  189. });
  190. renderGlobalModal();
  191. await userEvent.click(await screen.findByRole('button', {name: 'Disable'}));
  192. await userEvent.click(screen.getByTestId('confirm-button'));
  193. await waitFor(() => {
  194. expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
  195. });
  196. expect(enableMock).toHaveBeenCalledWith(
  197. expect.anything(),
  198. expect.objectContaining({
  199. data: {isActive: false},
  200. })
  201. );
  202. await userEvent.click(screen.getByRole('button', {name: 'Enable'}));
  203. await userEvent.click(screen.getByTestId('confirm-button'));
  204. expect(enableMock).toHaveBeenCalledWith(
  205. expect.anything(),
  206. expect.objectContaining({
  207. data: {isActive: true},
  208. })
  209. );
  210. });
  211. });