123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- import {GitHubIntegrationFixture} from 'sentry-fixture/githubIntegration';
- import {OrganizationFixture} from 'sentry-fixture/organization';
- import {RepositoryFixture} from 'sentry-fixture/repository';
- import {render, screen, userEvent, waitFor} from 'sentry-test/reactTestingLibrary';
- import RepositoryStore from 'sentry/stores/repositoryStore';
- import IntegrationRepos from 'sentry/views/settings/organizationIntegrations/integrationRepos';
- describe('IntegrationRepos', function () {
- const org = OrganizationFixture();
- const integration = GitHubIntegrationFixture();
- let resetReposSpy: jest.SpyInstance;
- beforeEach(() => {
- MockApiClient.clearMockResponses();
- RepositoryStore.init();
- resetReposSpy = jest.spyOn(RepositoryStore, 'resetRepositories');
- });
- afterEach(() => {
- resetReposSpy.mockClear();
- });
- describe('Getting repositories', function () {
- it('handles broken integrations', async function () {
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/integrations/1/repos/`,
- statusCode: 400,
- body: {detail: 'Invalid grant'},
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'GET',
- body: [],
- });
- render(<IntegrationRepos integration={integration} />);
- // we only attempt to fetch repositories upon typing
- await userEvent.click(screen.getByText('Add Repository'));
- await userEvent.type(screen.getByRole('textbox'), 'asdf');
- expect(
- await screen.findByText(
- /We were unable to fetch repositories for this integration/
- )
- ).toBeInTheDocument();
- });
- it('does not fetch repositories with empty query', async function () {
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'GET',
- body: [],
- });
- render(<IntegrationRepos integration={integration} />);
- await userEvent.click(screen.getByText('Add Repository'));
- expect(
- await screen.findByText(/Please enter a repository name/)
- ).toBeInTheDocument();
- });
- });
- describe('Adding repositories', function () {
- it('can save successfully', async function () {
- const addRepo = MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'POST',
- body: RepositoryFixture({integrationId: '1'}),
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/integrations/1/repos/`,
- body: {
- repos: [{identifier: 'example/repo-name', name: 'repo-name'}],
- },
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'GET',
- body: [],
- });
- render(<IntegrationRepos integration={integration} />);
- await userEvent.click(screen.getByText('Add Repository'));
- await userEvent.type(screen.getByRole('textbox'), 'repo-name');
- await userEvent.click(screen.getByText('repo-name'));
- expect(addRepo).toHaveBeenCalledWith(
- `/organizations/${org.slug}/repos/`,
- expect.objectContaining({
- data: {
- installation: '1',
- provider: 'integrations:github',
- identifier: 'example/repo-name',
- },
- })
- );
- expect(await screen.findByText('example/repo-name')).toBeInTheDocument();
- expect(resetReposSpy).toHaveBeenCalled();
- });
- it('handles failure during save', async function () {
- const addRepo = MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'POST',
- statusCode: 400,
- body: {
- errors: {
- __all__: 'Repository already exists.',
- },
- },
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/integrations/1/repos/`,
- body: {
- repos: [{identifier: 'getsentry/sentry', name: 'sentry-repo'}],
- },
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'GET',
- body: [],
- });
- render(<IntegrationRepos integration={integration} />);
- await userEvent.click(screen.getByText('Add Repository'));
- await userEvent.type(screen.getByRole('textbox'), 'sentry-repo');
- await userEvent.click(screen.getByText('sentry-repo'));
- expect(addRepo).toHaveBeenCalled();
- expect(screen.queryByText('getsentry/sentry')).not.toBeInTheDocument();
- });
- it('does not disable add repo for members', async function () {
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/integrations/1/repos/`,
- body: {
- repos: [{identifier: 'example/repo-name', name: 'repo-name'}],
- },
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'GET',
- body: [],
- });
- render(
- <IntegrationRepos
- integration={integration}
- organization={OrganizationFixture({access: []})}
- />
- );
- await waitFor(() => expect(screen.getByText('Add Repository')).toBeEnabled());
- });
- });
- describe('migratable repo', function () {
- it('associates repository with integration', async () => {
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- body: [
- RepositoryFixture({
- integrationId: undefined,
- externalSlug: 'example/repo-name',
- provider: {
- id: 'integrations:github',
- name: 'GitHub',
- },
- }),
- ],
- });
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/integrations/${integration.id}/repos/`,
- body: {repos: [{identifier: 'example/repo-name', name: 'repo-name'}]},
- });
- const updateRepo = MockApiClient.addMockResponse({
- method: 'PUT',
- url: `/organizations/${org.slug}/repos/4/`,
- body: {id: 244},
- });
- render(<IntegrationRepos integration={integration} />);
- await userEvent.click(screen.getByText('Add Repository'));
- await userEvent.type(screen.getByRole('textbox'), 'repo-name');
- await userEvent.click(screen.getByText('repo-name'));
- expect(updateRepo).toHaveBeenCalledWith(
- `/organizations/${org.slug}/repos/4/`,
- expect.objectContaining({
- data: {integrationId: '1'},
- })
- );
- await waitFor(() => expect(resetReposSpy).toHaveBeenCalled());
- });
- it('uses externalSlug not name for comparison', async () => {
- MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/repos/`,
- method: 'GET',
- body: [RepositoryFixture({name: 'repo-name-other', externalSlug: '9876'})],
- });
- const getItems = MockApiClient.addMockResponse({
- url: `/organizations/${org.slug}/integrations/${integration.id}/repos/`,
- method: 'GET',
- body: {
- repos: [{identifier: '9876', name: 'repo-name'}],
- },
- });
- const updateRepo = MockApiClient.addMockResponse({
- method: 'PUT',
- url: `/organizations/${org.slug}/repos/4/`,
- body: {id: 4320},
- });
- render(<IntegrationRepos integration={integration} />);
- await userEvent.click(screen.getByText('Add Repository'));
- await userEvent.type(screen.getByRole('textbox'), 'repo-name');
- await userEvent.click(screen.getByText('repo-name'));
- expect(getItems).toHaveBeenCalled();
- expect(updateRepo).toHaveBeenCalledWith(
- `/organizations/${org.slug}/repos/4/`,
- expect.objectContaining({
- data: {integrationId: '1'},
- })
- );
- });
- });
- });
|