Skip to content
Open
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
16 changes: 10 additions & 6 deletions static/app/views/authV2/authLogin/hooks/useAuthConfig.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,15 @@ import {useQuery} from '@tanstack/react-query';
import type {AuthConfigResponse} from 'sentry/types/auth';
import {apiOptions} from 'sentry/utils/api/apiOptions';

export const authConfigQueryOptions = apiOptions.as<AuthConfigResponse>()(
'/auth/config/',
{staleTime: 0}
);
export function getAuthConfigQueryOptions(nextUri?: string) {
return apiOptions.as<AuthConfigResponse>()('/auth/config/', {
query: nextUri ? {next: nextUri} : undefined,
staleTime: 0,
});
}

export const authConfigQueryOptions = getAuthConfigQueryOptions();

export function useAuthConfig() {
return useQuery(authConfigQueryOptions);
export function useAuthConfig(nextUri?: string) {
return useQuery(getAuthConfigQueryOptions(nextUri));
}
25 changes: 16 additions & 9 deletions static/app/views/authV2/authLogin/hooks/useSecondFactorAuth.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {useMutation, useQuery, useQueryClient} from '@tanstack/react-query';

import {t} from 'sentry/locale';
import type {AuthConfigResponse} from 'sentry/types/auth';
import type {ApiResponse} from 'sentry/utils/api/apiFetch';
import {apiOptions} from 'sentry/utils/api/apiOptions';
import {getApiUrl} from 'sentry/utils/api/getApiUrl';
import {fetchMutation} from 'sentry/utils/queryClient';
Expand Down Expand Up @@ -129,17 +131,22 @@ export function useCancelSecondFactorAuth() {
// MFA session. A later session must not render them while refetching or if the
// refetch fails.
queryClient.removeQueries({queryKey: secondFactorMethodsQueryOptions.queryKey});
queryClient.setQueryData(authConfigQueryOptions.queryKey, cachedResponse => {
if (!cachedResponse || 'nextUri' in cachedResponse.json) {
return cachedResponse;
queryClient.setQueriesData<ApiResponse<AuthConfigResponse>>(
{queryKey: [authConfigQueryOptions.queryKey[0]]},
cachedResponse => {
if (!cachedResponse || 'nextUri' in cachedResponse.json) {
return cachedResponse;
}

return {
...cachedResponse,
json: {...cachedResponse.json, pendingMfa: null},
};
}

return {
...cachedResponse,
json: {...cachedResponse.json, pendingMfa: null},
};
);
void queryClient.invalidateQueries({
queryKey: [authConfigQueryOptions.queryKey[0]],
});
void queryClient.invalidateQueries({queryKey: authConfigQueryOptions.queryKey});
},
});

Expand Down
231 changes: 231 additions & 0 deletions static/app/views/authV2/authLogin/index.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,9 @@ describe('AuthLogin', () => {
expect(
screen.queryByRole('button', {name: 'Clear organization login context'})
).not.toBeInTheDocument();
expect(
screen.queryByRole('button', {name: 'Sign in with a different account'})
).not.toBeInTheDocument();
});

it('requires SSO without offering another org in single-org mode', async () => {
Expand Down Expand Up @@ -444,6 +447,9 @@ describe('AuthLogin', () => {
expect(
screen.queryByRole('button', {name: 'Clear organization login context'})
).not.toBeInTheDocument();
expect(
screen.queryByRole('button', {name: 'Sign in with a different account'})
).not.toBeInTheDocument();
expect(testableWindowLocation.assign).not.toHaveBeenCalled();
});

Expand Down Expand Up @@ -900,4 +906,229 @@ describe('AuthLogin', () => {
screen.queryByRole('textbox', {name: 'One-time password'})
).not.toBeInTheDocument();
});

it('shows membership warning and account switch for a signed-in non-member without SSO', async () => {
const authConfigRequest = MockApiClient.addMockResponse({
url: '/auth/config/',
body: {nextUri: '/organizations/acme/issues/?query=is:unresolved'},
});
MockApiClient.addMockResponse({
url: '/auth/organizations/acme/config/',
body: {
authenticated: true,
memberAuthenticated: false,
canRegister: false,
joinRequestUrl: null,
loginMethod: 'password',
ssoRequired: false,
organization: {avatarUrl: null, name: 'Acme', slug: 'acme'},
provider: null,
warnings: [
'Your account (ada@example.com) is not a member of the Acme organization.',
],
},
});
const logoutDelay = Promise.withResolvers<void>();
const logoutRequest = MockApiClient.addMockResponse({
url: '/auth/',
method: 'DELETE',
asyncDelay: logoutDelay.promise,
});

render(<AuthLogin />, {
initialRouterConfig: {
location: {
pathname: '/auth/login/acme/',
query: {next: '/organizations/acme/issues/?query=is:unresolved'},
},
route: '/auth/login/:orgSlug/',
},
});

expect(
await screen.findByText(
'Your account (ada@example.com) is not a member of the Acme organization.'
)
).toBeVisible();
expect(authConfigRequest).toHaveBeenCalledWith(
'/auth/config/',
expect.objectContaining({
query: {next: '/organizations/acme/issues/?query=is:unresolved'},
})
);
expect(screen.getByRole('button', {name: 'SSO'})).toBeDisabled();
expect(screen.queryByRole('textbox', {name: 'Email'})).not.toBeInTheDocument();
expect(screen.queryByLabelText('Password')).not.toBeInTheDocument();

const switchButton = screen.getByRole('button', {
name: 'Sign in with a different account',
});
await userEvent.click(switchButton);
await waitFor(() => expect(logoutRequest).toHaveBeenCalledTimes(1));
expect(switchButton).toBeDisabled();
expect(switchButton).toHaveAttribute('aria-busy', 'true');

await userEvent.click(switchButton);
expect(logoutRequest).toHaveBeenCalledTimes(1);
expect(testableWindowLocation.assign).not.toHaveBeenCalled();

await act(async () => {
logoutDelay.resolve();
await logoutDelay.promise;
});

await waitFor(() =>
expect(testableWindowLocation.assign).toHaveBeenCalledWith(
'/auth/login/acme/?next=%2Forganizations%2Facme%2Fissues%2F%3Fquery%3Dis%3Aunresolved'
)
);
});

it('shows a retryable error when switching accounts fails and follows sloUrl on retry', async () => {
MockApiClient.addMockResponse({
url: '/auth/config/',
body: {nextUri: '/organizations/acme/issues/'},
});
MockApiClient.addMockResponse({
url: '/auth/organizations/acme/config/',
body: {
authenticated: true,
memberAuthenticated: false,
canRegister: false,
joinRequestUrl: null,
loginMethod: 'password',
ssoRequired: false,
organization: {avatarUrl: null, name: 'Acme', slug: 'acme'},
provider: null,
warnings: [
'Your account (ada@example.com) is not a member of the Acme organization.',
],
},
});
const failedLogout = MockApiClient.addMockResponse({
url: '/auth/',
method: 'DELETE',
statusCode: 500,
body: {detail: 'Logout failed'},
});

render(<AuthLogin />, {
initialRouterConfig: {
location: {pathname: '/auth/login/acme/'},
route: '/auth/login/:orgSlug/',
},
});

await userEvent.click(
await screen.findByRole('button', {name: 'Sign in with a different account'})
);

expect(
await screen.findByText('Unable to sign in with a different account. Try again.')
).toBeVisible();
expect(failedLogout).toHaveBeenCalledTimes(1);
expect(testableWindowLocation.assign).not.toHaveBeenCalled();

MockApiClient.addMockResponse({
url: '/auth/',
method: 'DELETE',
body: {sloUrl: 'https://idp.example.com/logout'},
});
await userEvent.click(
screen.getByRole('button', {name: 'Sign in with a different account'})
);

await waitFor(() =>
expect(testableWindowLocation.assign).toHaveBeenCalledWith(
'https://idp.example.com/logout'
)
);
});

it('offers account switch for a signed-in non-member when SSO is optional', async () => {
MockApiClient.addMockResponse({
url: '/auth/config/',
body: {nextUri: '/organizations/acme/issues/'},
});
MockApiClient.addMockResponse({
url: '/auth/organizations/acme/config/',
body: {
authenticated: true,
memberAuthenticated: false,
canRegister: false,
joinRequestUrl: null,
loginMethod: 'sso',
ssoRequired: false,
organization: {avatarUrl: null, name: 'Acme', slug: 'acme'},
provider: {key: 'saml2', name: 'SAML'},
warnings: [
'Your account (ada@example.com) is not a member of the Acme organization.',
],
},
});

render(<AuthLogin />, {
initialRouterConfig: {
location: {pathname: '/auth/login/acme/'},
route: '/auth/login/:orgSlug/',
},
});

expect(await screen.findByRole('button', {name: 'SSO'})).toBeEnabled();
expect(
screen.getByRole('button', {name: 'Sign in with a different account'})
).toBeEnabled();
});

it('renders plain-text organization warnings once ahead of login banner', async () => {
const membershipWarning =
'Your account (ada@example.com) is not a member of <b>Acme</b> [docs](https://example.com).';
const sessionExpired = 'Your session has expired.';
MockApiClient.addMockResponse({
url: '/auth/config/',
body: {
canRegister: true,
githubLoginLink: '',
googleLoginLink: '',
hasNewsletter: false,
loginBannerMarkdown:
'Try agent monitoring. [Learn more](https://example.com/agents).',
pendingMfa: null,
serverHostname: 'sentry.example.com',
vstsLoginLink: '',
warning: sessionExpired,
} satisfies AuthConfig,
});
MockApiClient.addMockResponse({
url: '/auth/organizations/acme/config/',
body: {
authenticated: false,
memberAuthenticated: false,
canRegister: false,
joinRequestUrl: null,
loginMethod: 'password',
ssoRequired: false,
organization: {avatarUrl: null, name: 'Acme', slug: 'acme'},
provider: null,
warnings: [membershipWarning, sessionExpired],
},
});

render(<AuthLogin />, {
initialRouterConfig: {
location: {pathname: '/auth/login/acme/'},
route: '/auth/login/:orgSlug/',
},
});

const membership = await screen.findByText(membershipWarning);
const bannerLink = screen.getByRole('link', {name: 'Learn more'});

expect(screen.getAllByText(sessionExpired)).toHaveLength(1);
expect(membership.compareDocumentPosition(bannerLink)).toBe(
Node.DOCUMENT_POSITION_FOLLOWING
);
expect(screen.queryByRole('link', {name: 'docs'})).not.toBeInTheDocument();
expect(document.querySelector('b')).not.toBeInTheDocument();
});
});
Loading
Loading