sentryAppExternalInstallation.spec.jsx 4.7 KB

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