index.spec.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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/organization';
  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. it('installs and redirects with state', async () => {
  137. const installUrl = `/organizations/${org1.slug}/sentry-app-installations/`;
  138. const install = {
  139. uuid: 'fake-id',
  140. code: 'some-code',
  141. };
  142. const installMock = MockApiClient.addMockResponse({
  143. url: installUrl,
  144. method: 'POST',
  145. body: install,
  146. });
  147. const state = 'some-state';
  148. const location = {
  149. ...RouteComponentPropsFixture(),
  150. location: {
  151. ...RouteComponentPropsFixture().location,
  152. query: {state},
  153. },
  154. };
  155. render(
  156. <SentryAppExternalInstallation
  157. {...location}
  158. params={{sentryAppSlug: sentryApp.slug}}
  159. />
  160. );
  161. await waitFor(() => expect(getInstallationsMock).toHaveBeenCalled());
  162. await userEvent.click(await screen.findByTestId('install')); // failing currently
  163. expect(installMock).toHaveBeenCalledWith(
  164. installUrl,
  165. expect.objectContaining({
  166. data: {slug: sentryApp.slug},
  167. })
  168. );
  169. await waitFor(() => {
  170. expect(window.location.assign).toHaveBeenCalledWith(
  171. `https://google.com/?code=${install.code}&installationId=${install.uuid}&orgSlug=${org1.slug}&state=${state}`
  172. );
  173. });
  174. (window.location.assign as jest.Mock).mockClear();
  175. });
  176. });
  177. describe('multiple organizations', () => {
  178. beforeEach(() => {
  179. getOrgsMock = MockApiClient.addMockResponse({
  180. url: '/organizations/',
  181. body: [org1Lite, org2Lite],
  182. });
  183. getOrgMock = MockApiClient.addMockResponse({
  184. url: `/organizations/${org1.slug}/`,
  185. body: org1,
  186. });
  187. getInstallationsMock = MockApiClient.addMockResponse({
  188. url: `/organizations/${org1.slug}/sentry-app-installations/`,
  189. body: [],
  190. });
  191. window.__initialData = ConfigFixture({
  192. customerDomain: {
  193. subdomain: 'org1',
  194. organizationUrl: 'https://org1.sentry.io',
  195. sentryUrl: 'https://sentry.io',
  196. },
  197. links: {
  198. ...(window.__initialData?.links ?? {}),
  199. sentryUrl: 'https://sentry.io',
  200. },
  201. });
  202. ConfigStore.loadInitialData(window.__initialData);
  203. });
  204. it('sets the org automatically', async () => {
  205. render(
  206. <SentryAppExternalInstallation
  207. {...RouteComponentPropsFixture()}
  208. params={{sentryAppSlug: sentryApp.slug}}
  209. />
  210. );
  211. await waitFor(() => expect(getInstallationsMock).toHaveBeenCalled());
  212. expect(getAppMock).toHaveBeenCalled();
  213. expect(getOrgsMock).toHaveBeenCalled();
  214. expect(getOrgMock).toHaveBeenCalled();
  215. expect(getInstallationsMock).toHaveBeenCalled();
  216. expect(screen.queryByText('Select an organization')).not.toBeInTheDocument();
  217. await waitFor(() => expect(screen.getByTestId('install')).toBeEnabled());
  218. });
  219. it('loads orgs from multiple regions', async () => {
  220. window.__initialData = {
  221. ...window.__initialData,
  222. memberRegions: [
  223. {name: 'us', url: 'https://us.example.org'},
  224. {name: 'de', url: 'https://de.example.org'},
  225. ],
  226. };
  227. ConfigStore.loadInitialData(window.__initialData);
  228. const deorg = OrganizationFixture({slug: 'de-org'});
  229. const getDeOrgs = MockApiClient.addMockResponse({
  230. url: '/organizations/',
  231. body: [deorg],
  232. match: [
  233. function (_url: string, options: Record<string, any>) {
  234. return options.host === 'https://de.example.org';
  235. },
  236. ],
  237. });
  238. render(
  239. <SentryAppExternalInstallation
  240. {...RouteComponentPropsFixture()}
  241. params={{sentryAppSlug: sentryApp.slug}}
  242. />
  243. );
  244. await waitFor(() => expect(getInstallationsMock).toHaveBeenCalled());
  245. expect(getDeOrgs).toHaveBeenCalled();
  246. });
  247. it('selecting org changes the url', async () => {
  248. const preselectedOrg = OrganizationFixture();
  249. const {routerProps} = initializeOrg({organization: preselectedOrg});
  250. window.__initialData = ConfigFixture({
  251. customerDomain: {
  252. subdomain: 'org1',
  253. organizationUrl: 'https://org1.sentry.io',
  254. sentryUrl: 'https://sentry.io',
  255. },
  256. links: {
  257. ...(window.__initialData?.links ?? {}),
  258. sentryUrl: 'https://sentry.io',
  259. },
  260. });
  261. ConfigStore.loadInitialData(window.__initialData);
  262. getOrgMock = MockApiClient.addMockResponse({
  263. url: `/organizations/org1/`,
  264. body: preselectedOrg,
  265. });
  266. getInstallationsMock = MockApiClient.addMockResponse({
  267. url: `/organizations/${org1.slug}/sentry-app-installations/`,
  268. body: [],
  269. });
  270. render(
  271. <SentryAppExternalInstallation
  272. {...routerProps}
  273. params={{sentryAppSlug: sentryApp.slug}}
  274. />
  275. );
  276. await waitFor(() => expect(getInstallationsMock).toHaveBeenCalled());
  277. await selectEvent.select(screen.getByRole('textbox'), 'org2');
  278. expect(window.location.assign).toHaveBeenCalledWith(generateOrgSlugUrl('org2'));
  279. expect(getFeaturesMock).toHaveBeenCalled();
  280. });
  281. });
  282. });