sentryAppExternalInstallation.spec.jsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import selectEvent from 'react-select-event';
  2. import pick from 'lodash/pick';
  3. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  4. import {textWithMarkupMatcher} from 'sentry-test/utils';
  5. import SentryAppExternalInstallation from 'sentry/views/sentryAppExternalInstallation';
  6. describe('SentryAppExternalInstallation', () => {
  7. let sentryApp,
  8. getOrgsMock,
  9. getOrgMock,
  10. getAppMock,
  11. getInstallationsMock,
  12. getFeaturesMock,
  13. org1,
  14. org1Lite,
  15. org2,
  16. org2Lite;
  17. beforeEach(() => {
  18. MockApiClient.clearMockResponses();
  19. org1 = TestStubs.Organization({
  20. slug: 'org1',
  21. name: 'Organization 1',
  22. });
  23. org2 = TestStubs.Organization({
  24. slug: 'org2',
  25. name: 'Organization 2',
  26. });
  27. org1Lite = pick(org1, ['slug', 'name', 'id']);
  28. org2Lite = pick(org2, ['slug', 'name', 'id']);
  29. sentryApp = TestStubs.SentryApp({
  30. status: 'published',
  31. redirectUrl: 'https://google.com',
  32. });
  33. getAppMock = MockApiClient.addMockResponse({
  34. url: `/sentry-apps/${sentryApp.slug}/`,
  35. body: sentryApp,
  36. });
  37. getFeaturesMock = MockApiClient.addMockResponse({
  38. url: `/sentry-apps/${sentryApp.slug}/features/`,
  39. body: [],
  40. });
  41. MockApiClient.addMockResponse({
  42. url: `/sentry-apps/${sentryApp.slug}/interaction/`,
  43. method: 'POST',
  44. statusCode: 200,
  45. body: {},
  46. });
  47. });
  48. describe('single organization', () => {
  49. beforeEach(() => {
  50. getOrgsMock = MockApiClient.addMockResponse({
  51. url: '/organizations/',
  52. body: [org1Lite],
  53. });
  54. getOrgMock = MockApiClient.addMockResponse({
  55. url: `/organizations/${org1.slug}/`,
  56. body: org1,
  57. });
  58. getInstallationsMock = MockApiClient.addMockResponse({
  59. url: `/organizations/${org1.slug}/sentry-app-installations/`,
  60. body: [],
  61. });
  62. });
  63. it('sets the org automatically', () => {
  64. render(<SentryAppExternalInstallation params={{sentryAppSlug: sentryApp.slug}} />);
  65. expect(getAppMock).toHaveBeenCalled();
  66. expect(getOrgsMock).toHaveBeenCalled();
  67. expect(getOrgMock).toHaveBeenCalled();
  68. expect(getInstallationsMock).toHaveBeenCalled();
  69. expect(
  70. screen.getByText(
  71. textWithMarkupMatcher(
  72. 'You are installing Sample App for organization Organization 1'
  73. )
  74. )
  75. ).toBeInTheDocument();
  76. expect(screen.queryByText('Select an organization')).not.toBeInTheDocument();
  77. });
  78. it('installs and redirects', async () => {
  79. const installUrl = `/organizations/${org1.slug}/sentry-app-installations/`;
  80. const install = {
  81. uuid: 'fake-id',
  82. code: 'some-code',
  83. };
  84. const installMock = MockApiClient.addMockResponse({
  85. url: installUrl,
  86. method: 'POST',
  87. body: install,
  88. });
  89. render(<SentryAppExternalInstallation params={{sentryAppSlug: sentryApp.slug}} />);
  90. userEvent.click(await screen.findByTestId('install'));
  91. expect(installMock).toHaveBeenCalledWith(
  92. installUrl,
  93. expect.objectContaining({
  94. data: {slug: sentryApp.slug},
  95. })
  96. );
  97. await waitFor(() => {
  98. expect(window.location.assign).toHaveBeenCalledWith(
  99. `https://google.com/?code=${install.code}&installationId=${install.uuid}&orgSlug=${org1.slug}`
  100. );
  101. });
  102. window.location.assign.mockClear();
  103. });
  104. });
  105. describe('multiple organizations', () => {
  106. beforeEach(() => {
  107. getOrgsMock = MockApiClient.addMockResponse({
  108. url: '/organizations/',
  109. body: [org1Lite, org2Lite],
  110. });
  111. });
  112. it('renders org dropdown', () => {
  113. render(<SentryAppExternalInstallation params={{sentryAppSlug: sentryApp.slug}} />);
  114. expect(getAppMock).toHaveBeenCalled();
  115. expect(getOrgsMock).toHaveBeenCalled();
  116. expect(screen.getByText('Select an organization')).toBeInTheDocument();
  117. });
  118. it('selecting org from dropdown loads the org through the API', async () => {
  119. getOrgMock = MockApiClient.addMockResponse({
  120. url: `/organizations/${org2.slug}/`,
  121. body: org2,
  122. });
  123. getInstallationsMock = MockApiClient.addMockResponse({
  124. url: `/organizations/${org2.slug}/sentry-app-installations/`,
  125. body: [],
  126. });
  127. render(<SentryAppExternalInstallation params={{sentryAppSlug: sentryApp.slug}} />);
  128. await selectEvent.select(screen.getByText('Select an organization'), 'org2');
  129. expect(getOrgMock).toHaveBeenCalledTimes(1);
  130. expect(getOrgMock).toHaveBeenLastCalledWith(
  131. '/organizations/org2/',
  132. expect.anything()
  133. );
  134. expect(getInstallationsMock).toHaveBeenCalled();
  135. expect(getFeaturesMock).toHaveBeenCalled();
  136. await waitFor(() => expect(screen.getByTestId('install')).toBeEnabled());
  137. });
  138. });
  139. });