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
2 changes: 2 additions & 0 deletions .changeset/oauth-consent-multiple-organizations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { describe, expect, it, vi } from 'vitest';
import { Card } from '../../../components/card';
import { MosaicProvider } from '../../../mosaic-provider';
import { oauthConsentMessages } from '../oauth-consent.messages';
import type { OAuthConsentOrganizationSelection, OAuthConsentViewProps } from '../oauth-consent.types';
import type {
OAuthConsentOrganizationAccess,
OAuthConsentOrganizationSelection,
OAuthConsentViewProps,
} from '../oauth-consent.types';
import { OAuthConsentView } from '../oauth-consent.view';

function propsFor(overrides: Partial<OAuthConsentViewProps> = {}): OAuthConsentViewProps {
Expand All @@ -23,8 +27,7 @@ function propsFor(overrides: Partial<OAuthConsentViewProps> = {}): OAuthConsentV
scope: 'email projects',
hasOfflineAccess: false,
organizations: [],
organizationId: null,
onOrganizationChange: vi.fn(),
organizationSelection: { multiple: true, organizationIds: [], onOrganizationIdsChange: vi.fn(), limit: 5 },
redirectUrl: 'https://app.linear.example.com/oauth/callback',
redirectDomain: 'example.com',
isRedirectUrlOpen: false,
Expand Down Expand Up @@ -86,87 +89,185 @@ describe('OAuthConsentView', () => {
const organizations = [
{ value: 'org_1', label: 'My workspace' },
{ value: 'org_2', label: 'Globex' },
{ value: 'org_3', label: 'Initech' },
];

function selection(overrides: Partial<OAuthConsentOrganizationSelection> = {}): OAuthConsentOrganizationSelection {
return { access: 'all', onAccessChange: vi.fn(), selected: [], onSelectedChange: vi.fn(), ...overrides };
}

function postedOrganizationIds() {
return [...document.querySelectorAll<HTMLInputElement>('input[name="organization_id"]')].map(
input => input.value,
);
}

it('grants every organization without a picker when all organizations is chosen', () => {
renderView({ organizations, organizationSelection: selection({ access: 'all' }) });
function multiple(
overrides: { organizationIds?: string[]; onOrganizationIdsChange?: (ids: string[]) => void; limit?: number } = {},
): OAuthConsentOrganizationSelection {
return { multiple: true, organizationIds: [], onOrganizationIdsChange: vi.fn(), ...overrides };
}

expect(screen.getByRole('radio', { name: 'All organizations' })).toBeChecked();
expect(screen.queryByRole('combobox')).not.toBeInTheDocument();
expect(postedOrganizationIds()).toEqual(['org_1', 'org_2']);
});
function access(
overrides: {
access?: OAuthConsentOrganizationAccess;
onAccessChange?: (access: OAuthConsentOrganizationAccess) => void;
organizationIds?: string[];
onOrganizationIdsChange?: (ids: string[]) => void;
} = {},
): OAuthConsentOrganizationSelection {
return {
multiple: true,
access: 'all',
onAccessChange: vi.fn(),
organizationIds: [],
onOrganizationIdsChange: vi.fn(),
...overrides,
};
}

it('reports a switch to only selected organizations', async () => {
const onAccessChange = vi.fn();
const user = renderView({ organizations, organizationSelection: selection({ onAccessChange }) });
describe('single', () => {
it('reports the picked organization and posts only it', async () => {
const onChange = vi.fn();
const user = renderView({
organizations,
organizationSelection: { organizationId: 'org_1', onOrganizationIdChange: onChange },
});

await user.click(screen.getByRole('radio', { name: 'Only selected organizations' }));
expect(postedOrganizationIds()).toEqual(['org_1']);
await user.click(screen.getByRole('combobox', { name: /^Grant access for:/ }));
await user.click(screen.getByRole('option', { name: 'Globex' }));

expect(onAccessChange).toHaveBeenCalledWith('selected');
expect(onChange).toHaveBeenCalledWith('org_2');
});
});

it('clears the picked organizations when switching back to all organizations', async () => {
const onAccessChange = vi.fn();
const onSelectedChange = vi.fn();
const user = renderView({
organizations,
organizationSelection: selection({ access: 'selected', selected: ['org_2'], onAccessChange, onSelectedChange }),
describe('multiple', () => {
it('reports every organization the user picks', async () => {
const onChange = vi.fn();
const user = renderView({
organizations,
organizationSelection: multiple({ organizationIds: ['org_1'], onOrganizationIdsChange: onChange }),
});

await user.click(screen.getByRole('combobox', { name: /^Grant access for:/ }));
await user.click(screen.getByRole('option', { name: 'Globex' }));

expect(onChange).toHaveBeenCalledWith(['org_1', 'org_2']);
});

await user.click(screen.getByRole('radio', { name: 'All organizations' }));
it('posts the selected organizations as one space-separated organization_id', () => {
renderView({ organizations, organizationSelection: multiple({ organizationIds: ['org_1', 'org_3'] }) });

expect(onAccessChange).toHaveBeenCalledWith('all');
expect(onSelectedChange).toHaveBeenCalledWith([]);
});
expect(postedOrganizationIds()).toEqual(['org_1 org_3']);
});

it('shows a placeholder while no organization is picked', () => {
renderView({ organizations, organizationSelection: selection({ access: 'selected', selected: [] }) });
it('lets the user deselect the last picked organization', async () => {
const onChange = vi.fn();
const user = renderView({
organizations,
organizationSelection: multiple({ organizationIds: ['org_2'], onOrganizationIdsChange: onChange }),
});

expect(screen.getByRole('combobox')).toHaveTextContent('Select organizations');
});
await user.click(screen.getByRole('combobox', { name: /^Grant access for:/ }));
await user.click(screen.getByRole('option', { name: 'Globex' }));

it('reports every organization the user picks', async () => {
const onSelectedChange = vi.fn();
const user = renderView({
organizations,
organizationSelection: selection({ access: 'selected', selected: ['org_1'], onSelectedChange }),
expect(onChange).toHaveBeenCalledWith([]);
});

await user.click(screen.getByRole('combobox'));
await user.click(screen.getByRole('option', { name: 'Globex' }));
it('blocks Allow and posts no organization_id while none is selected', () => {
renderView({ organizations, organizationSelection: multiple({ organizationIds: [] }) });

expect(onSelectedChange).toHaveBeenCalledWith(['org_1', 'org_2']);
});
expect(postedOrganizationIds()).toEqual([]);
expect(screen.getByRole('button', { name: 'Allow' })).toBeDisabled();
});

it('posts only the selected organizations', () => {
renderView({ organizations, organizationSelection: selection({ access: 'selected', selected: ['org_2'] }) });
it('names a single selected organization in the picker', () => {
renderView({ organizations, organizationSelection: multiple({ organizationIds: ['org_2'] }) });

expect(postedOrganizationIds()).toEqual(['org_2']);
});
expect(screen.getByRole('combobox', { name: /^Grant access for:/ })).toHaveTextContent('Globex');
});

it('counts several selected organizations in the picker', () => {
renderView({ organizations, organizationSelection: multiple({ organizationIds: ['org_1', 'org_2'] }) });

expect(screen.getByRole('combobox', { name: /^Grant access for:/ })).toHaveTextContent('2 organizations');
});

it('names a single selected organization in the picker', () => {
renderView({ organizations, organizationSelection: selection({ access: 'selected', selected: ['org_2'] }) });
it('disables the unpicked organizations once the limit is picked', async () => {
const user = renderView({
organizations,
organizationSelection: multiple({ organizationIds: ['org_1', 'org_2'], limit: 2 }),
});

expect(screen.getByRole('combobox')).toHaveTextContent('Globex');
await user.click(screen.getByRole('combobox', { name: /^Grant access for:/ }));

expect(screen.getByRole('option', { name: 'Initech' })).toHaveAttribute('aria-disabled', 'true');
expect(screen.getByRole('option', { name: 'Globex' })).not.toHaveAttribute('aria-disabled');
expect(screen.getByText('You can select up to 2 organizations.')).toBeVisible();
});

it('blocks Allow and shows an error when more than the limit are selected', () => {
renderView({
organizations,
organizationSelection: multiple({ organizationIds: ['org_1', 'org_2', 'org_3'], limit: 2 }),
});

expect(screen.getByRole('combobox', { name: /^Grant access for:/ })).toHaveAttribute('aria-invalid', 'true');
expect(screen.getByText('Select 2 or fewer organizations to continue.')).toBeVisible();
expect(screen.getByRole('button', { name: 'Allow' })).toBeDisabled();
});
});

it('counts several selected organizations in the picker', () => {
renderView({
organizations,
organizationSelection: selection({ access: 'selected', selected: ['org_1', 'org_2'] }),
describe('access', () => {
it('grants every organization without a picker when all organizations is chosen', () => {
renderView({ organizations, organizationSelection: access({ access: 'all' }) });

expect(screen.getByRole('radio', { name: 'All organizations' })).toBeChecked();
expect(screen.queryByRole('combobox')).not.toBeInTheDocument();
expect(postedOrganizationIds()).toEqual(['org_1 org_2 org_3']);
});

expect(screen.getByRole('combobox')).toHaveTextContent('2 organizations');
it('reports a switch to only selected organizations', async () => {
const onAccessChange = vi.fn();
const user = renderView({ organizations, organizationSelection: access({ onAccessChange }) });

await user.click(screen.getByRole('radio', { name: 'Only selected organizations' }));

expect(onAccessChange).toHaveBeenCalledWith('selected');
});

it('clears the picked organizations when switching back to all organizations', async () => {
const onAccessChange = vi.fn();
const onChange = vi.fn();
const user = renderView({
organizations,
organizationSelection: access({
access: 'selected',
organizationIds: ['org_2'],
onAccessChange,
onOrganizationIdsChange: onChange,
}),
});

await user.click(screen.getByRole('radio', { name: 'All organizations' }));

expect(onAccessChange).toHaveBeenCalledWith('all');
expect(onChange).toHaveBeenCalledWith([]);
});

it('shows a placeholder and blocks Allow while no organization is picked', () => {
renderView({ organizations, organizationSelection: access({ access: 'selected' }) });

expect(screen.getByRole('combobox')).toHaveTextContent('Select organizations');
expect(postedOrganizationIds()).toEqual([]);
expect(screen.getByRole('button', { name: 'Allow' })).toBeDisabled();
});

it('posts only the selected organizations', () => {
renderView({
organizations,
organizationSelection: access({ access: 'selected', organizationIds: ['org_2'] }),
});

expect(postedOrganizationIds()).toEqual(['org_2']);
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ export function useOAuthConsentController(
},
scope: checked.join(' ') || OFFLINE_ACCESS_SCOPE,
organizations: sharesOrganizations ? organizations : [],
organizationId: sharesOrganizations ? (selectedOrganizationId ?? defaultOrganizationId) : null,
onOrganizationChange: setSelectedOrganizationId,
organizationSelection: {
organizationId: sharesOrganizations ? (selectedOrganizationId ?? defaultOrganizationId) : null,
onOrganizationIdChange: setSelectedOrganizationId,
},
isRedirectUrlOpen,
onRedirectUrlOpenChange: setIsRedirectUrlOpen,
pendingAction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ export const oauthConsentMessages = {
selectedOrganizations: 'Only selected organizations',
selectedOrganizationsDescription: 'Choose which organizations to grant access to.',
selectOrganizations: 'Select organizations',
organizationLimit: 'You can select up to {limit} organizations.',
organizationLimitExceeded: 'Select {limit} or fewer organizations to continue.',
organizationCount: { one: '{count} organization', other: '{count} organizations' },
required: 'Required',
viewFullUrl: 'View full URL',
Expand Down
28 changes: 20 additions & 8 deletions packages/mosaic/src/features/oauth-consent/oauth-consent.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,27 @@ export interface OAuthConsentScopeSelection {

export type OAuthConsentOrganizationAccess = 'all' | 'selected';

export interface OAuthConsentOrganizationSelection {
access: OAuthConsentOrganizationAccess;
onAccessChange: (access: OAuthConsentOrganizationAccess) => void;
selected: readonly string[];
onSelectedChange: (organizationIds: string[]) => void;
interface OAuthConsentSingleOrganizationSelection {
multiple?: false;
organizationId: string | null;
onOrganizationIdChange: (organizationId: string) => void;
}

interface OAuthConsentMultipleOrganizationSelection {
multiple: true;
organizationIds: readonly string[];
onOrganizationIdsChange: (organizationIds: string[]) => void;
}

export type OAuthConsentOrganizationSelection =
| OAuthConsentSingleOrganizationSelection
| (OAuthConsentMultipleOrganizationSelection & { limit?: number; access?: never; onAccessChange?: never })
| (OAuthConsentMultipleOrganizationSelection & {
access: OAuthConsentOrganizationAccess;
onAccessChange: (access: OAuthConsentOrganizationAccess) => void;
limit?: never;
});

export interface OAuthConsentViewProps {
messages: OAuthConsentMessages;
oauthApplicationName: string;
Expand All @@ -72,9 +86,7 @@ export interface OAuthConsentViewProps {
scope: string;
hasOfflineAccess: boolean;
organizations: OAuthConsentOrganization[];
organizationId: string | null;
onOrganizationChange: (organizationId: string) => void;
organizationSelection?: OAuthConsentOrganizationSelection;
organizationSelection: OAuthConsentOrganizationSelection;
redirectUrl: string;
redirectDomain: string;
isRedirectUrlOpen: boolean;
Expand Down
Loading
Loading