index.spec.tsx 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. await waitFor(() => expect(getInstallationsMock).toHaveBeenCalled());
  91. expect(
  92. await screen.findByText(
  93. textWithMarkupMatcher(
  94. 'You are installing Sample App for organization Organization 1'
  95. )
  96. )
  97. ).toBeInTheDocument();
  98. expect(getAppMock).toHaveBeenCalled();
  99. expect(getOrgsMock).toHaveBeenCalled();
  100. expect(getOrgMock).toHaveBeenCalled();
  101. expect(getInstallationsMock).toHaveBeenCalled();
  102. expect(screen.queryByText('Select an organization')).not.toBeInTheDocument();
  103. });
  104. it('installs and redirects', async () => {
  105. const installUrl = `/organizations/${org1.slug}/sentry-app-installations/`;
  106. const install = {
  107. uuid: 'fake-id',
  108. code: 'some-code',
  109. };
  110. const installMock = MockApiClient.addMockResponse({
  111. url: installUrl,
  112. method: 'POST',
  113. body: install,
  114. });
  115. render(
  116. <SentryAppExternalInstallation
  117. {...RouteComponentPropsFixture()}
  118. params={{sentryAppSlug: sentryApp.slug}}
  119. />
  120. );
  121. await waitFor(() => expect(getInstallationsMock).toHaveBeenCalled());
  122. await userEvent.click(await screen.findByTestId('install')); // failing currently
  123. expect(installMock).toHaveBeenCalledWith(
  124. installUrl,
  125. expect.objectContaining({
  126. data: {slug: sentryApp.slug},
  127. })
  128. );
  129. await waitFor(() => {
  130. expect(window.location.assign).toHaveBeenCalledWith(
  131. `https://google.com/?code=${install.code}&installationId=${install.uuid}&orgSlug=${org1.slug}`
  132. );
  133. });
  134. (window.location.assign as jest.Mock).mockClear();
  135. });
  136. });
  137. describe('multiple organizations', () => {
  138. beforeEach(() => {
  139. getOrgsMock = MockApiClient.addMockResponse({
  140. url: '/organizations/',
  141. body: [org1Lite, org2Lite],
  142. });
  143. getOrgMock = MockApiClient.addMockResponse({
  144. url: `/organizations/${org1.slug}/`,
  145. body: org1,
  146. });
  147. getInstallationsMock = MockApiClient.addMockResponse({
  148. url: `/organizations/${org1.slug}/sentry-app-installations/`,
  149. body: [],
  150. });
  151. window.__initialData = ConfigFixture({
  152. customerDomain: {
  153. subdomain: 'org1',
  154. organizationUrl: 'https://org1.sentry.io',
  155. sentryUrl: 'https://sentry.io',
  156. },
  157. links: {
  158. ...(window.__initialData?.links ?? {}),
  159. sentryUrl: 'https://sentry.io',
  160. },
  161. });
  162. ConfigStore.loadInitialData(window.__initialData);
  163. });
  164. it('sets the org automatically', async () => {
  165. render(
  166. <SentryAppExternalInstallation
  167. {...RouteComponentPropsFixture()}
  168. params={{sentryAppSlug: sentryApp.slug}}
  169. />
  170. );
  171. await waitFor(() => expect(getInstallationsMock).toHaveBeenCalled());
  172. expect(getAppMock).toHaveBeenCalled();
  173. expect(getOrgsMock).toHaveBeenCalled();
  174. expect(getOrgMock).toHaveBeenCalled();
  175. expect(getInstallationsMock).toHaveBeenCalled();
  176. expect(screen.queryByText('Select an organization')).not.toBeInTheDocument();
  177. await waitFor(() => expect(screen.getByTestId('install')).toBeEnabled());
  178. });
  179. it('loads orgs from multiple regions', async () => {
  180. window.__initialData = {
  181. ...window.__initialData,
  182. memberRegions: [
  183. {name: 'us', url: 'https://us.example.org'},
  184. {name: 'de', url: 'https://de.example.org'},
  185. ],
  186. };
  187. ConfigStore.loadInitialData(window.__initialData);
  188. const deorg = OrganizationFixture({slug: 'de-org'});
  189. const getDeOrgs = MockApiClient.addMockResponse({
  190. url: '/organizations/',
  191. body: [deorg],
  192. match: [
  193. function (_url: string, options: Record<string, any>) {
  194. return options.host === 'https://de.example.org';
  195. },
  196. ],
  197. });
  198. render(
  199. <SentryAppExternalInstallation
  200. {...RouteComponentPropsFixture()}
  201. params={{sentryAppSlug: sentryApp.slug}}
  202. />
  203. );
  204. await waitFor(() => expect(getInstallationsMock).toHaveBeenCalled());
  205. expect(getDeOrgs).toHaveBeenCalled();
  206. });
  207. it('selecting org changes the url', async () => {
  208. const preselectedOrg = OrganizationFixture();
  209. const {routerProps} = initializeOrg({organization: preselectedOrg});
  210. window.__initialData = ConfigFixture({
  211. customerDomain: {
  212. subdomain: 'org1',
  213. organizationUrl: 'https://org1.sentry.io',
  214. sentryUrl: 'https://sentry.io',
  215. },
  216. links: {
  217. ...(window.__initialData?.links ?? {}),
  218. sentryUrl: 'https://sentry.io',
  219. },
  220. });
  221. ConfigStore.loadInitialData(window.__initialData);
  222. getOrgMock = MockApiClient.addMockResponse({
  223. url: `/organizations/org1/`,
  224. body: preselectedOrg,
  225. });
  226. getInstallationsMock = MockApiClient.addMockResponse({
  227. url: `/organizations/${org1.slug}/sentry-app-installations/`,
  228. body: [],
  229. });
  230. render(
  231. <SentryAppExternalInstallation
  232. {...routerProps}
  233. params={{sentryAppSlug: sentryApp.slug}}
  234. />
  235. );
  236. await waitFor(() => expect(getInstallationsMock).toHaveBeenCalled());
  237. await selectEvent.select(screen.getByRole('textbox'), 'org2');
  238. expect(window.location.assign).toHaveBeenCalledWith(generateOrgSlugUrl('org2'));
  239. expect(getFeaturesMock).toHaveBeenCalled();
  240. });
  241. });
  242. });