index.spec.tsx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. import pick from 'lodash/pick';
  2. import {ConfigFixture} from 'sentry-fixture/config';
  3. import {OrganizationFixture} from 'sentry-fixture/organization';
  4. import {RouteComponentPropsFixture} from 'sentry-fixture/routeComponentPropsFixture';
  5. import {SentryAppFixture} from 'sentry-fixture/sentryApp';
  6. import {initializeOrg} from 'sentry-test/initializeOrg';
  7. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  8. import selectEvent from 'sentry-test/selectEvent';
  9. import {textWithMarkupMatcher} from 'sentry-test/utils';
  10. import ConfigStore from 'sentry/stores/configStore';
  11. import type {Organization as TOrganization} from 'sentry/types';
  12. import {generateOrgSlugUrl} from 'sentry/utils';
  13. import SentryAppExternalInstallation from 'sentry/views/sentryAppExternalInstallation';
  14. describe('SentryAppExternalInstallation', () => {
  15. let sentryApp: ReturnType<typeof SentryAppFixture>,
  16. getOrgsMock: ReturnType<typeof MockApiClient.addMockResponse>,
  17. getOrgMock: ReturnType<typeof MockApiClient.addMockResponse>,
  18. getAppMock: ReturnType<typeof MockApiClient.addMockResponse>,
  19. getInstallationsMock: ReturnType<typeof MockApiClient.addMockResponse>,
  20. getFeaturesMock: ReturnType<typeof MockApiClient.addMockResponse>,
  21. org1: TOrganization,
  22. org1Lite: Pick<TOrganization, 'slug' | 'name' | 'id'>,
  23. org2: TOrganization,
  24. org2Lite: Pick<TOrganization, 'slug' | 'name' | 'id'>;
  25. beforeEach(() => {
  26. MockApiClient.clearMockResponses();
  27. org1 = OrganizationFixture({
  28. slug: 'org1',
  29. name: 'Organization 1',
  30. });
  31. org2 = OrganizationFixture({
  32. slug: 'org2',
  33. name: 'Organization 2',
  34. });
  35. org1Lite = pick(org1, ['slug', 'name', 'id']);
  36. org2Lite = pick(org2, ['slug', 'name', 'id']);
  37. sentryApp = SentryAppFixture({
  38. status: 'published',
  39. redirectUrl: 'https://google.com',
  40. });
  41. getAppMock = MockApiClient.addMockResponse({
  42. url: `/sentry-apps/${sentryApp.slug}/`,
  43. body: sentryApp,
  44. });
  45. getFeaturesMock = MockApiClient.addMockResponse({
  46. url: `/sentry-apps/${sentryApp.slug}/features/`,
  47. body: [],
  48. });
  49. MockApiClient.addMockResponse({
  50. url: `/sentry-apps/${sentryApp.slug}/interaction/`,
  51. method: 'POST',
  52. statusCode: 200,
  53. body: {},
  54. });
  55. });
  56. describe('single organization', () => {
  57. beforeEach(() => {
  58. getOrgsMock = MockApiClient.addMockResponse({
  59. url: '/organizations/',
  60. body: [org1Lite],
  61. });
  62. getOrgMock = MockApiClient.addMockResponse({
  63. url: `/organizations/${org1.slug}/`,
  64. body: org1,
  65. });
  66. getInstallationsMock = MockApiClient.addMockResponse({
  67. url: `/organizations/${org1.slug}/sentry-app-installations/`,
  68. body: [],
  69. });
  70. window.__initialData = ConfigFixture({
  71. customerDomain: {
  72. subdomain: 'org1',
  73. organizationUrl: 'https://org1.sentry.io',
  74. sentryUrl: 'https://sentry.io',
  75. },
  76. links: {
  77. ...(window.__initialData?.links ?? {}),
  78. sentryUrl: 'https://sentry.io',
  79. },
  80. });
  81. ConfigStore.loadInitialData(window.__initialData);
  82. });
  83. it('sets the org automatically', async () => {
  84. render(
  85. <SentryAppExternalInstallation
  86. {...RouteComponentPropsFixture()}
  87. params={{sentryAppSlug: sentryApp.slug}}
  88. />
  89. );
  90. expect(
  91. await screen.findByText(
  92. textWithMarkupMatcher(
  93. 'You are installing Sample App for organization Organization 1'
  94. )
  95. )
  96. ).toBeInTheDocument();
  97. expect(getAppMock).toHaveBeenCalled();
  98. expect(getOrgsMock).toHaveBeenCalled();
  99. expect(getOrgMock).toHaveBeenCalled();
  100. expect(getInstallationsMock).toHaveBeenCalled();
  101. expect(screen.queryByText('Select an organization')).not.toBeInTheDocument();
  102. });
  103. it('installs and redirects', async () => {
  104. const installUrl = `/organizations/${org1.slug}/sentry-app-installations/`;
  105. const install = {
  106. uuid: 'fake-id',
  107. code: 'some-code',
  108. };
  109. const installMock = MockApiClient.addMockResponse({
  110. url: installUrl,
  111. method: 'POST',
  112. body: install,
  113. });
  114. render(
  115. <SentryAppExternalInstallation
  116. {...RouteComponentPropsFixture()}
  117. params={{sentryAppSlug: sentryApp.slug}}
  118. />
  119. );
  120. await userEvent.click(await screen.findByTestId('install')); // failing currently
  121. expect(installMock).toHaveBeenCalledWith(
  122. installUrl,
  123. expect.objectContaining({
  124. data: {slug: sentryApp.slug},
  125. })
  126. );
  127. await waitFor(() => {
  128. expect(window.location.assign).toHaveBeenCalledWith(
  129. `https://google.com/?code=${install.code}&installationId=${install.uuid}&orgSlug=${org1.slug}`
  130. );
  131. });
  132. (window.location.assign as jest.Mock).mockClear();
  133. });
  134. });
  135. describe('multiple organizations', () => {
  136. beforeEach(() => {
  137. getOrgsMock = MockApiClient.addMockResponse({
  138. url: '/organizations/',
  139. body: [org1Lite, org2Lite],
  140. });
  141. getOrgMock = MockApiClient.addMockResponse({
  142. url: `/organizations/${org1.slug}/`,
  143. body: org1,
  144. });
  145. getInstallationsMock = MockApiClient.addMockResponse({
  146. url: `/organizations/${org1.slug}/sentry-app-installations/`,
  147. body: [],
  148. });
  149. window.__initialData = ConfigFixture({
  150. customerDomain: {
  151. subdomain: 'org1',
  152. organizationUrl: 'https://org1.sentry.io',
  153. sentryUrl: 'https://sentry.io',
  154. },
  155. links: {
  156. ...(window.__initialData?.links ?? {}),
  157. sentryUrl: 'https://sentry.io',
  158. },
  159. });
  160. ConfigStore.loadInitialData(window.__initialData);
  161. });
  162. it('sets the org automatically', async () => {
  163. render(
  164. <SentryAppExternalInstallation
  165. {...RouteComponentPropsFixture()}
  166. params={{sentryAppSlug: sentryApp.slug}}
  167. />
  168. );
  169. expect(getAppMock).toHaveBeenCalled();
  170. expect(getOrgsMock).toHaveBeenCalled();
  171. expect(getOrgMock).toHaveBeenCalled();
  172. expect(getInstallationsMock).toHaveBeenCalled();
  173. expect(screen.queryByText('Select an organization')).not.toBeInTheDocument();
  174. await waitFor(() => expect(screen.getByTestId('install')).toBeEnabled());
  175. });
  176. it('selecting org changes the url', async () => {
  177. const preselectedOrg = OrganizationFixture();
  178. const {routerProps} = initializeOrg({organization: preselectedOrg});
  179. window.__initialData = ConfigFixture({
  180. customerDomain: {
  181. subdomain: 'org1',
  182. organizationUrl: 'https://org1.sentry.io',
  183. sentryUrl: 'https://sentry.io',
  184. },
  185. links: {
  186. ...(window.__initialData?.links ?? {}),
  187. sentryUrl: 'https://sentry.io',
  188. },
  189. });
  190. ConfigStore.loadInitialData(window.__initialData);
  191. getOrgMock = MockApiClient.addMockResponse({
  192. url: `/organizations/org1/`,
  193. body: preselectedOrg,
  194. });
  195. getInstallationsMock = MockApiClient.addMockResponse({
  196. url: `/organizations/${org1.slug}/sentry-app-installations/`,
  197. body: [],
  198. });
  199. render(
  200. <SentryAppExternalInstallation
  201. {...routerProps}
  202. params={{sentryAppSlug: sentryApp.slug}}
  203. />
  204. );
  205. await selectEvent.select(screen.getByRole('textbox'), 'org2');
  206. expect(window.location.assign).toHaveBeenCalledWith(generateOrgSlugUrl('org2'));
  207. expect(getFeaturesMock).toHaveBeenCalled();
  208. });
  209. });
  210. });