Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 108 additions & 0 deletions static/app/components/pipeline/pipelineIntegrationSlack.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import {act, render, screen, userEvent} from 'sentry-test/reactTestingLibrary';

import {slackIntegrationPipeline} from './pipelineIntegrationSlack';
import type {PipelineStepProps} from './types';

const SlackOAuthLoginStep = slackIntegrationPipeline.steps[0].component;

function makeStepProps<D, A>(
overrides: Partial<PipelineStepProps<D, A>> & {stepData: D}
): PipelineStepProps<D, A> {
return {
advance: jest.fn(),
advanceError: null,
isAdvancing: false,
stepIndex: 0,
totalSteps: 1,
...overrides,
};
}

let mockPopup: Window;

function dispatchPipelineMessage({
data,
origin = document.location.origin,
source = mockPopup,
}: {
data: Record<string, string>;
origin?: string;
source?: Window | MessageEventSource | null;
}) {
act(() => {
const event = new MessageEvent('message', {data, origin});
Object.defineProperty(event, 'source', {value: source});
window.dispatchEvent(event);
});
}

beforeEach(() => {
mockPopup = {
closed: false,
close: jest.fn(),
focus: jest.fn(),
} as unknown as Window;
jest.spyOn(window, 'open').mockReturnValue(mockPopup);
});

afterEach(() => {
jest.restoreAllMocks();
});

describe('SlackOAuthLoginStep', () => {
it('renders the OAuth login step for Slack', () => {
render(
<SlackOAuthLoginStep
{...makeStepProps({stepData: {oauthUrl: 'https://slack.com/oauth/authorize'}})}
/>
);

expect(screen.getByRole('button', {name: 'Authorize Slack'})).toBeInTheDocument();
});

it('calls advance with code and state on OAuth callback', async () => {
const advance = jest.fn();
render(
<SlackOAuthLoginStep
{...makeStepProps({
stepData: {oauthUrl: 'https://slack.com/oauth/authorize'},
advance,
})}
/>
);

await userEvent.click(screen.getByRole('button', {name: 'Authorize Slack'}));

dispatchPipelineMessage({
data: {
_pipeline_source: 'sentry-pipeline',
code: 'auth-code-123',
state: 'state-xyz',
},
});

expect(advance).toHaveBeenCalledWith({
code: 'auth-code-123',
state: 'state-xyz',
});
});

it('shows loading state when isAdvancing is true', () => {
render(
<SlackOAuthLoginStep
{...makeStepProps({
stepData: {oauthUrl: 'https://slack.com/oauth/authorize'},
isAdvancing: true,
})}
/>
);

expect(screen.getByRole('button', {name: 'Authorizing...'})).toBeDisabled();
});

it('disables authorize button when oauthUrl is not provided', () => {
render(<SlackOAuthLoginStep {...makeStepProps({stepData: {}})} />);

expect(screen.getByRole('button', {name: 'Authorize Slack'})).toBeDisabled();
});
});
46 changes: 46 additions & 0 deletions static/app/components/pipeline/pipelineIntegrationSlack.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {useCallback} from 'react';

import {t} from 'sentry/locale';
import type {IntegrationWithConfig} from 'sentry/types/integrations';

import type {OAuthCallbackData} from './shared/oauthLoginStep';
import {OAuthLoginStep} from './shared/oauthLoginStep';
import type {PipelineDefinition, PipelineStepProps} from './types';
import {pipelineComplete} from './types';

function SlackOAuthLoginStep({
stepData,
advance,
isAdvancing,
}: PipelineStepProps<{oauthUrl?: string}, {code: string; state: string}>) {
const handleOAuthCallback = useCallback(
(data: OAuthCallbackData) => {
advance({code: data.code, state: data.state});
},
[advance]
);

return (
<OAuthLoginStep
oauthUrl={stepData.oauthUrl}
isLoading={isAdvancing}
serviceName="Slack"
onOAuthCallback={handleOAuthCallback}
popup={{height: 900}}
/>
);
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

New OAuth step duplicates existing GitLab implementation

Low Severity

SlackOAuthLoginStep is nearly identical to GitLabOAuthLoginStep in pipelineIntegrationGitLab.tsx — same type signature, same handleOAuthCallback body, same OAuthLoginStep rendering — differing only in serviceName and the popup option. As more OAuth-only integrations are added, this boilerplate will keep getting copied. A small shared factory (parameterized by serviceName and optional PopupOptions) would eliminate the duplication.

Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit 65b70bf. Configure here.


export const slackIntegrationPipeline = {
type: 'integration',
provider: 'slack',
actionTitle: t('Installing Slack Integration'),
getCompletionData: pipelineComplete<IntegrationWithConfig>,
steps: [
{
stepId: 'oauth_login',
shortDescription: t('Authorizing via Slack OAuth'),
component: SlackOAuthLoginStep,
},
],
} as const satisfies PipelineDefinition;
2 changes: 2 additions & 0 deletions static/app/components/pipeline/registry.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {dummyIntegrationPipeline} from './pipelineDummyProvider';
import {githubIntegrationPipeline} from './pipelineIntegrationGitHub';
import {gitlabIntegrationPipeline} from './pipelineIntegrationGitLab';
import {slackIntegrationPipeline} from './pipelineIntegrationSlack';

/**
* All registered pipeline definitions.
Expand All @@ -9,6 +10,7 @@ export const PIPELINE_REGISTRY = [
dummyIntegrationPipeline,
githubIntegrationPipeline,
gitlabIntegrationPipeline,
slackIntegrationPipeline,
] as const;

type AllPipelines = (typeof PIPELINE_REGISTRY)[number];
Expand Down
Loading