integrationRepos.spec.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import {Repository} from 'sentry-fixture/repository';
  2. import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
  3. import RepositoryStore from 'sentry/stores/repositoryStore';
  4. import IntegrationRepos from 'sentry/views/settings/organizationIntegrations/integrationRepos';
  5. describe('IntegrationRepos', function () {
  6. const org = TestStubs.Organization();
  7. const integration = TestStubs.GitHubIntegration();
  8. let resetReposSpy;
  9. beforeEach(() => {
  10. MockApiClient.clearMockResponses();
  11. RepositoryStore.init();
  12. resetReposSpy = jest.spyOn(RepositoryStore, 'resetRepositories');
  13. });
  14. afterEach(() => {
  15. jest.restoreAllMocks();
  16. resetReposSpy();
  17. });
  18. describe('Getting repositories', function () {
  19. it('handles broken integrations', async function () {
  20. MockApiClient.addMockResponse({
  21. url: `/organizations/${org.slug}/integrations/1/repos/`,
  22. statusCode: 400,
  23. body: {detail: 'Invalid grant'},
  24. });
  25. MockApiClient.addMockResponse({
  26. url: `/organizations/${org.slug}/repos/`,
  27. method: 'GET',
  28. body: [],
  29. });
  30. render(<IntegrationRepos integration={integration} />);
  31. expect(
  32. await screen.findByText(
  33. /We were unable to fetch repositories for this integration/
  34. )
  35. ).toBeInTheDocument();
  36. });
  37. });
  38. describe('Adding repositories', function () {
  39. it('can save successfully', async function () {
  40. const addRepo = MockApiClient.addMockResponse({
  41. url: `/organizations/${org.slug}/repos/`,
  42. method: 'POST',
  43. body: Repository({integrationId: '1'}),
  44. });
  45. MockApiClient.addMockResponse({
  46. url: `/organizations/${org.slug}/integrations/1/repos/`,
  47. body: {
  48. repos: [{identifier: 'example/repo-name', name: 'repo-name'}],
  49. },
  50. });
  51. MockApiClient.addMockResponse({
  52. url: `/organizations/${org.slug}/repos/`,
  53. method: 'GET',
  54. body: [],
  55. });
  56. render(<IntegrationRepos integration={integration} />);
  57. await userEvent.click(screen.getByText('Add Repository'));
  58. await userEvent.click(screen.getByText('repo-name'));
  59. expect(addRepo).toHaveBeenCalledWith(
  60. `/organizations/${org.slug}/repos/`,
  61. expect.objectContaining({
  62. data: {
  63. installation: '1',
  64. provider: 'integrations:github',
  65. identifier: 'example/repo-name',
  66. },
  67. })
  68. );
  69. expect(await screen.findByText('example/repo-name')).toBeInTheDocument();
  70. expect(resetReposSpy).toHaveBeenCalled();
  71. });
  72. it('handles failure during save', async function () {
  73. const addRepo = MockApiClient.addMockResponse({
  74. url: `/organizations/${org.slug}/repos/`,
  75. method: 'POST',
  76. statusCode: 400,
  77. body: {
  78. errors: {
  79. __all__: 'Repository already exists.',
  80. },
  81. },
  82. });
  83. MockApiClient.addMockResponse({
  84. url: `/organizations/${org.slug}/integrations/1/repos/`,
  85. body: {
  86. repos: [{identifier: 'getsentry/sentry', name: 'sentry-repo'}],
  87. },
  88. });
  89. MockApiClient.addMockResponse({
  90. url: `/organizations/${org.slug}/repos/`,
  91. method: 'GET',
  92. body: [],
  93. });
  94. render(<IntegrationRepos integration={integration} />);
  95. await userEvent.click(screen.getByText('Add Repository'));
  96. await userEvent.click(screen.getByText('sentry-repo'));
  97. expect(addRepo).toHaveBeenCalled();
  98. expect(screen.queryByText('getsentry/sentry')).not.toBeInTheDocument();
  99. });
  100. it('does not disable add repo for members', function () {
  101. MockApiClient.addMockResponse({
  102. url: `/organizations/${org.slug}/integrations/1/repos/`,
  103. body: {
  104. repos: [{identifier: 'example/repo-name', name: 'repo-name'}],
  105. },
  106. });
  107. MockApiClient.addMockResponse({
  108. url: `/organizations/${org.slug}/repos/`,
  109. method: 'GET',
  110. body: [],
  111. });
  112. render(
  113. <IntegrationRepos
  114. integration={integration}
  115. organization={TestStubs.Organization({access: []})}
  116. />
  117. );
  118. expect(screen.getByText('Add Repository')).toBeEnabled();
  119. });
  120. });
  121. describe('migratable repo', function () {
  122. it('associates repository with integration', async () => {
  123. MockApiClient.addMockResponse({
  124. url: `/organizations/${org.slug}/repos/`,
  125. body: [
  126. Repository({
  127. integrationId: undefined,
  128. externalSlug: 'example/repo-name',
  129. provider: {
  130. id: 'integrations:github',
  131. name: 'GitHub',
  132. },
  133. }),
  134. ],
  135. });
  136. MockApiClient.addMockResponse({
  137. url: `/organizations/${org.slug}/integrations/${integration.id}/repos/`,
  138. body: {repos: [{identifier: 'example/repo-name', name: 'repo-name'}]},
  139. });
  140. const updateRepo = MockApiClient.addMockResponse({
  141. method: 'PUT',
  142. url: `/organizations/${org.slug}/repos/4/`,
  143. body: {id: 244},
  144. });
  145. render(<IntegrationRepos integration={integration} />);
  146. await userEvent.click(screen.getByText('Add Repository'));
  147. await userEvent.click(screen.getByText('repo-name'));
  148. expect(updateRepo).toHaveBeenCalledWith(
  149. `/organizations/${org.slug}/repos/4/`,
  150. expect.objectContaining({
  151. data: {integrationId: '1'},
  152. })
  153. );
  154. await waitFor(() => expect(resetReposSpy).toHaveBeenCalled());
  155. });
  156. it('uses externalSlug not name for comparison', async () => {
  157. MockApiClient.addMockResponse({
  158. url: `/organizations/${org.slug}/repos/`,
  159. method: 'GET',
  160. body: [Repository({name: 'repo-name-other', externalSlug: '9876'})],
  161. });
  162. const getItems = MockApiClient.addMockResponse({
  163. url: `/organizations/${org.slug}/integrations/${integration.id}/repos/`,
  164. method: 'GET',
  165. body: {
  166. repos: [{identifier: '9876', name: 'repo-name'}],
  167. },
  168. });
  169. const updateRepo = MockApiClient.addMockResponse({
  170. method: 'PUT',
  171. url: `/organizations/${org.slug}/repos/4/`,
  172. body: {id: 4320},
  173. });
  174. render(<IntegrationRepos integration={integration} />);
  175. await userEvent.click(screen.getByText('Add Repository'));
  176. await userEvent.click(screen.getByText('repo-name'));
  177. expect(getItems).toHaveBeenCalled();
  178. expect(updateRepo).toHaveBeenCalledWith(
  179. `/organizations/${org.slug}/repos/4/`,
  180. expect.objectContaining({
  181. data: {integrationId: '1'},
  182. })
  183. );
  184. });
  185. });
  186. });