diff --git a/projects/packages/protect/changelog/update-protect-dashboard-login-card b/projects/packages/protect/changelog/update-protect-dashboard-login-card new file mode 100644 index 000000000000..61da80e93c3b --- /dev/null +++ b/projects/packages/protect/changelog/update-protect-dashboard-login-card @@ -0,0 +1,4 @@ +Significance: patch +Type: changed + +Dashboard: Redesign the Login protection card with toggles for account protection and Secure Sign On. diff --git a/projects/packages/protect/routes/dashboard/components/card.tsx b/projects/packages/protect/routes/dashboard/components/card.tsx index ab63b71438b4..a877f5ea170c 100644 --- a/projects/packages/protect/routes/dashboard/components/card.tsx +++ b/projects/packages/protect/routes/dashboard/components/card.tsx @@ -133,14 +133,21 @@ export function UnavailableCard( { icon, title }: { icon: ReactElement; title: s } /** - * A row showing the last settings save error, if any. + * A row showing why the last save of one of the card's settings failed, until dismissed. * * @param props - Component props. * @param props.settings - Jetpack settings, with their error. - * @return The row, or nothing. + * @param props.keys - The settings this card saves. + * @return The row, or nothing without an error from one of `keys`. */ -export function SettingsErrorRow( { settings }: { settings: ProtectSettingsData } ) { - if ( ! settings.error ) { +export function SettingsErrorRow( { + settings, + keys, +}: { + settings: ProtectSettingsData; + keys: string[]; +} ) { + if ( ! settings.error || ! settings.errorKeys.some( key => keys.includes( key ) ) ) { return null; } return ( diff --git a/projects/packages/protect/routes/dashboard/components/settings/setting-toggle.tsx b/projects/packages/protect/routes/dashboard/components/settings/setting-toggle.tsx index 5c406e8bf201..85c3fed3a77a 100644 --- a/projects/packages/protect/routes/dashboard/components/settings/setting-toggle.tsx +++ b/projects/packages/protect/routes/dashboard/components/settings/setting-toggle.tsx @@ -1,13 +1,12 @@ -import { ToggleControl } from '@wordpress/components'; import { useCallback } from '@wordpress/element'; +import { SwitchControl } from '@wordpress/ui'; import type { ProtectSettingsData } from '../../data/use-protect-settings'; -import type { ReactNode } from 'react'; type Props = { data: ProtectSettingsData; name: string; label: string; - help?: ReactNode; + help?: string; disabled?: boolean; }; @@ -27,13 +26,12 @@ export default function SettingToggle( { data, name, label, help, disabled = fal const onChange = useCallback( ( value: boolean ) => save( { [ name ]: value } ), [ save, name ] ); return ( - ); } diff --git a/projects/packages/protect/routes/dashboard/data/test/use-protect-settings.test.ts b/projects/packages/protect/routes/dashboard/data/test/use-protect-settings.test.ts index 8679f1e62cd6..a2d05459c4ed 100644 --- a/projects/packages/protect/routes/dashboard/data/test/use-protect-settings.test.ts +++ b/projects/packages/protect/routes/dashboard/data/test/use-protect-settings.test.ts @@ -85,6 +85,7 @@ describe( 'useProtectSettings', () => { expect( result.current.settings ).toEqual( expected ); expect( result.current.error ).toBe( 'Nope' ); + expect( result.current.errorKeys ).toEqual( [ 'list' ] ); expect( result.current.isSaving( 'list' ) ).toBe( false ); } ); @@ -149,6 +150,7 @@ describe( 'useProtectSettings', () => { const { result } = renderHook( () => useProtectSettings() ); await act( async () => result.current.load() ); expect( result.current.error ).toEqual( expect.any( String ) ); + expect( result.current.errorKeys ).toEqual( [] ); expect( result.current.isLoaded ).toBe( false ); const retry = defer(); diff --git a/projects/packages/protect/routes/dashboard/data/use-protect-settings.ts b/projects/packages/protect/routes/dashboard/data/use-protect-settings.ts index 18b6ec9cd8d3..d86f3fe0b44d 100644 --- a/projects/packages/protect/routes/dashboard/data/use-protect-settings.ts +++ b/projects/packages/protect/routes/dashboard/data/use-protect-settings.ts @@ -7,6 +7,9 @@ import { store as noticesStore } from '@wordpress/notices'; // One id, so a burst of saves shows one snackbar. const SAVED_NOTICE_ID = 'jetpack-protect-settings-saved'; +type Failure = { message: string; keys: string[] }; +const NO_KEYS: string[] = []; + /** Flat Jetpack settings, keyed by option or module slug, as `jetpack/v4/settings` returns them. */ export type ProtectSettings = Record< string, unknown >; @@ -22,6 +25,8 @@ export type ProtectSettingsData = { isLoaded: boolean; waf: WafConfig | null; error: string | null; + /** The keys whose save failed, so a card shows only its own errors; empty when loading failed. */ + errorKeys: string[]; dismissError: () => void; isSaving: ( key: string ) => boolean; /** Fetches the settings once; later calls do nothing unless the fetch failed. */ @@ -43,7 +48,7 @@ export default function useProtectSettings(): ProtectSettingsData { const [ settings, setSettingsState ] = useState< ProtectSettings | null >( null ); const [ waf, setWaf ] = useState< WafConfig | null >( null ); const [ isLoaded, setIsLoaded ] = useState( false ); - const [ error, setError ] = useState< string | null >( null ); + const [ failure, setFailure ] = useState< Failure | null >( null ); const [ saving, setSaving ] = useState< string[] >( [] ); const settingsRef = useRef< ProtectSettings | null >( null ); const loadStarted = useRef( false ); @@ -60,7 +65,7 @@ export default function useProtectSettings(): ProtectSettingsData { return; } loadStarted.current = true; - setError( null ); + setFailure( null ); Promise.all( [ apiFetch< ProtectSettings >( { path: '/jetpack/v4/settings' } ), @@ -77,7 +82,10 @@ export default function useProtectSettings(): ProtectSettingsData { } ) .catch( () => { loadStarted.current = false; - setError( __( 'Your settings couldn’t be loaded.', 'jetpack-protect-pkg' ) ); + setFailure( { + message: __( 'Your settings couldn’t be loaded.', 'jetpack-protect-pkg' ), + keys: NO_KEYS, + } ); } ); }, [ setSettings ] ); @@ -92,7 +100,7 @@ export default function useProtectSettings(): ProtectSettingsData { keys.forEach( key => savedKeys.current.add( key ) ); setSettings( { ...before, ...patch } ); setSaving( current => [ ...current, ...keys ] ); - setError( null ); + setFailure( null ); let saved = true; try { @@ -116,10 +124,12 @@ export default function useProtectSettings(): ProtectSettingsData { } } ); setSettings( rolledBack ); - setError( - ( e as { message?: string } )?.message || - __( 'Your change couldn’t be saved. Try again.', 'jetpack-protect-pkg' ) - ); + setFailure( { + message: + ( e as { message?: string } )?.message || + __( 'Your change couldn’t be saved. Try again.', 'jetpack-protect-pkg' ), + keys, + } ); saved = false; } finally { // One entry per save in flight, so an overlapping save of the same key keeps it busy. @@ -148,10 +158,21 @@ export default function useProtectSettings(): ProtectSettingsData { ); const isSaving = useCallback( ( key: string ) => saving.includes( key ), [ saving ] ); - const dismissError = useCallback( () => setError( null ), [] ); + const dismissError = useCallback( () => setFailure( null ), [] ); return useMemo( - () => ( { settings, isLoaded, waf, error, dismissError, isSaving, load, save, refresh } ), - [ settings, isLoaded, waf, error, dismissError, isSaving, load, save, refresh ] + () => ( { + settings, + isLoaded, + waf, + error: failure?.message ?? null, + errorKeys: failure?.keys ?? NO_KEYS, + dismissError, + isSaving, + load, + save, + refresh, + } ), + [ settings, isLoaded, waf, failure, dismissError, isSaving, load, save, refresh ] ); } diff --git a/projects/packages/protect/routes/dashboard/route.scss b/projects/packages/protect/routes/dashboard/route.scss index 2281b2fcebee..0568b734dc1b 100644 --- a/projects/packages/protect/routes/dashboard/route.scss +++ b/projects/packages/protect/routes/dashboard/route.scss @@ -38,10 +38,42 @@ body.toplevel_page_jetpack-protect { // The row is full-bleed for its divider; the padding re-aligns its contents. &__row { box-sizing: border-box; - padding: 16px var(--wp-ui-card-padding); + padding: var(--wpds-dimension-padding-lg) var(--wpds-dimension-padding-2xl); border-block-start: var(--wpds-border-width-xs) solid var(--wpds-color-stroke-surface-neutral); } + // Cells split by rules; each cell takes the row's padding, + // so the rules run edge to edge. + &__row.jp-protect-card__cells { + --jp-protect-cells: 2; + + display: grid; + grid-template-columns: repeat(var(--jp-protect-cells), minmax(0, 1fr)); + padding: 0; + + > * { + box-sizing: border-box; + padding: var(--wpds-dimension-padding-lg) var(--wpds-dimension-padding-2xl); + } + + > * + * { + border-inline-start: + var(--wpds-border-width-xs) solid + var(--wpds-color-stroke-surface-neutral); + } + + @media (max-width: 600px) { + grid-template-columns: minmax(0, 1fr); + + > * + * { + border-inline-start: 0; + border-block-start: + var(--wpds-border-width-xs) solid + var(--wpds-color-stroke-surface-neutral); + } + } + } + &__muted { color: var(--wpds-color-foreground-content-neutral-weak); } diff --git a/projects/packages/protect/routes/dashboard/sections/firewall/overview-card.tsx b/projects/packages/protect/routes/dashboard/sections/firewall/overview-card.tsx index a68a4c123637..ca9f357a25e9 100644 --- a/projects/packages/protect/routes/dashboard/sections/firewall/overview-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/firewall/overview-card.tsx @@ -41,6 +41,9 @@ const UNAVAILABLE: FirewallState = { ipRules: {}, }; +// The only setting this card saves. +const FIREWALL_KEYS = [ 'waf' ]; + // One id for the test's snackbars, so the result replaces "Testing the firewall…". const NOTICE_ID = 'jetpack-protect-firewall-test'; @@ -277,7 +280,7 @@ export default function FirewallOverviewCard( { state, settings, openTab }: Fire onOpenChange={ setDetailsOpen } /> ) } - + { ! active && ( diff --git a/projects/packages/protect/routes/dashboard/sections/firewall/test/overview-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/firewall/test/overview-card.test.tsx index b1d9c1dc7c72..cf93e207580e 100644 --- a/projects/packages/protect/routes/dashboard/sections/firewall/test/overview-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/firewall/test/overview-card.test.tsx @@ -118,7 +118,7 @@ describe( 'FirewallOverviewCard', () => { it( 'turns the firewall on from the card while it is off', async () => { render( ); - await userEvent.click( screen.getByRole( 'checkbox', { name: 'Enable firewall' } ) ); + await userEvent.click( screen.getByRole( 'switch', { name: 'Enable firewall' } ) ); expect( props.settings.save ).toHaveBeenCalledWith( { waf: true } ); } ); diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx index 2987b2fa0d73..fd71d4fdf56f 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-card.tsx @@ -1,76 +1,96 @@ -import { __, sprintf } from '@wordpress/i18n'; -import { lock } from '@wordpress/icons'; -import { Badge, Stack, Text } from '@wordpress/ui'; -import { CardRow, ProtectCard, Stat } from '../../components/card'; +import { useEffect, useRef } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { key } from '@wordpress/icons'; +import { Stack } from '@wordpress/ui'; +import { CardRow, ProtectCard, SettingsErrorRow, Stat } from '../../components/card'; +import SettingToggle from '../../components/settings/setting-toggle'; import TabLink from '../../components/tab-link'; -import isModuleActive from '../../data/is-module-active'; -import type { LoginProtectionContext, ModuleState } from './types'; -import type { CardStatus } from '../../components/card'; +import SsoToggle, { getSsoBlocker } from './sso-toggle'; +import type { LoginProtectionContext, LoginProtectionState } from './types'; +import type { ProtectSettingsData } from '../../data/use-protect-settings'; /** Opens the Settings tab at this section's card. */ const SETTINGS_PARAMS = { section: 'loginProtection' }; +// The settings this card saves. +const LOGIN_KEYS = [ 'protect', 'account-protection', 'sso' ]; + /** - * One way of protecting logins, with whether it's on. + * Blocked logins while brute force protection is on, else a toggle to turn it on. * - * @param props - Component props. - * @param props.name - The method's name. - * @param props.description - What it does. - * @param props.state - Its module's live state. + * @param props - Component props. + * @param props.data - The settings, with page-load state filled in. + * @param props.login - This section's state from PHP. + * @param props.openTab - Switches dashboard tabs, for the link to Settings. * @return The row. */ -function Method( { - name, - description, - state, +function BruteForceRow( { + data, + login, + openTab, }: { - name: string; - description: string; - state: ModuleState; + data: ProtectSettingsData; + login: LoginProtectionState; + openTab: LoginProtectionContext[ 'openTab' ]; } ) { - let badge = { __( 'Off', 'jetpack-protect-pkg' ) }; - if ( ! state.available ) { - badge = { __( 'Unavailable', 'jetpack-protect-pkg' ) }; - } else if ( state.active ) { - badge = { __( 'On', 'jetpack-protect-pkg' ) }; - } + const isOn = Boolean( data.settings?.protect ); + const rowRef = useRef< HTMLDivElement >( null ); + const wasOn = useRef( isOn ); + + useEffect( () => { + const row = rowRef.current; + // Switching swaps out the focused control, so keep keyboard focus in the row. + if ( + row && + isOn !== wasOn.current && + row.ownerDocument.activeElement === row.ownerDocument.body + ) { + row.querySelector< HTMLElement >( 'a, [role="switch"]' )?.focus(); + } + wasOn.current = isOn; + }, [ isOn ] ); return ( - - - { name } - - { description } - - - { badge } - +
+ { isOn ? ( + + + + { __( 'Configure brute force protection', 'jetpack-protect-pkg' ) } + + + ) : ( + + ) } +
); } /** - * The card badge's intent: neutral when no method is available. - * - * @param on - How many methods are on. - * @param available - How many methods are available. - * @return The intent. - */ -function getStatusIntent( on: number, available: number ): CardStatus[ 'intent' ] { - if ( available === 0 ) { - return 'none'; - } - return on === available ? 'stable' : 'medium'; -} - -/** - * The Login protection card: brute force protection, account protection and WordPress.com login. + * The Login protection card: blocked logins, and toggles for account protection and WordPress.com login. * * @param props - The dashboard context. * @param props.state - This section's state from PHP. * @param props.settings - Jetpack settings, for live module state. - * @param props.openTab - Switches dashboard tabs, for the link to Settings. + * @param props.openTab - Switches dashboard tabs, for the links to Settings. * @return The card, or nothing without the section's state. */ export default function LoginProtectionCard( { @@ -82,69 +102,62 @@ export default function LoginProtectionCard( { return null; } - const live = ( module: string, initial: ModuleState ): ModuleState => ( { - available: initial.available, - active: isModuleActive( settings.settings, module, initial.active ), - } ); - const bruteForce = live( 'protect', login.bruteForce ); - const accountProtection = live( 'account-protection', login.accountProtection ); - // The module can stay active after the owner disconnects, but WordPress.com login doesn't run then. - const sso = live( 'sso', { ...login.sso, available: login.sso.available && login.ssoUsable } ); - - const methods = [ bruteForce, accountProtection, sso ]; - const on = methods.filter( method => method.available && method.active ).length; - const available = methods.filter( method => method.available ).length; + // Settings load on the Settings tab; until then the toggles show the page-load state. + const data = { + ...settings, + settings: { + protect: login.bruteForce.active, + 'account-protection': login.accountProtection.active, + sso: login.sso.active, + ...settings.settings, + }, + }; + const ssoOn = Boolean( data.settings.sso ) && getSsoBlocker( login ) === null; return ( - { bruteForce.active && ( - - + + +
+ - - ) } - - - - - - { __( 'Configure login protection', 'jetpack-protect-pkg' ) } - +
+ + + { ssoOn && ( + + { __( 'Configure WordPress.com login (SSO)', 'jetpack-protect-pkg' ) } + + ) } +
); diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx index c253b01a2f8b..38f6cdebe5e0 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx @@ -1,15 +1,13 @@ -import { ToggleControl } from '@wordpress/components'; -import { useCallback, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; -import { lock } from '@wordpress/icons'; -import { AlertDialog, Stack, Text } from '@wordpress/ui'; +import { key } from '@wordpress/icons'; +import { Stack, SwitchControl, Text } from '@wordpress/ui'; import { CardRow, ProtectCard } from '../../components/card'; import IpListField from '../../components/settings/ip-list-field'; import SettingToggle from '../../components/settings/setting-toggle'; +import SsoToggle from './sso-toggle'; import type { LoginProtectionContext, LoginProtectionState } from './types'; import type { ProtectSettingsData } from '../../data/use-protect-settings'; -const noop = () => {}; const NO_LOCKS: LoginProtectionState[ 'ssoLocks' ] = { matchByEmail: false, twoStep: false }; /** @@ -47,39 +45,7 @@ function SsoOptionToggle( { } return ( - - ); -} - -/** - * What the WordPress.com login toggle says under it: what it does, or why it can't be changed. - * - * @param login - This section's state from PHP. - * @return The help text. - */ -function getSsoHelp( login: LoginProtectionState | undefined ): string { - if ( login && ! login.sso.available ) { - return __( 'WordPress.com login isn’t available on this site.', 'jetpack-protect-pkg' ); - } - if ( login?.isOffline ) { - return __( 'WordPress.com login isn’t available in offline mode.', 'jetpack-protect-pkg' ); - } - if ( ! login?.ssoUsable ) { - return __( - 'Needs the site owner’s WordPress.com account to be connected.', - 'jetpack-protect-pkg' - ); - } - return __( - 'If you have several sites with this on, you can log in to all of them with the same credentials.', - 'jetpack-protect-pkg' + ); } @@ -98,22 +64,10 @@ export default function LoginProtectionSettingsCard( { const settings = data.settings ?? {}; const ssoLocks = login?.ssoLocks ?? NO_LOCKS; const ssoEffective = login?.ssoEffective ?? NO_LOCKS; - const ssoUsable = login?.ssoUsable ?? false; const ssoOn = Boolean( settings.sso ); - const { save } = data; - - const [ confirmingSsoOff, setConfirmingSsoOff ] = useState( false ); - const onSsoChange = useCallback( - ( value: boolean ) => ( value ? save( { sso: true } ) : setConfirmingSsoOff( true ) ), - [ save ] - ); - // `save()` resolves to whether it worked; the dialog only needs to know it's done, then closes. - const disableSso = useCallback( async () => { - await save( { sso: false } ); - }, [ save ] ); return ( - + } className="jp-protect-card__subheading"> @@ -170,19 +124,19 @@ export default function LoginProtectionSettingsCard( { } className="jp-protect-card__subheading"> - { __( 'WordPress.com login', 'jetpack-protect-pkg' ) } + { __( 'WordPress.com login (SSO)', 'jetpack-protect-pkg' ) } - - - - ); } diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx new file mode 100644 index 000000000000..cee84f6cb3ee --- /dev/null +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/sso-toggle.tsx @@ -0,0 +1,88 @@ +import { useCallback, useState } from '@wordpress/element'; +import { __ } from '@wordpress/i18n'; +import { AlertDialog, SwitchControl } from '@wordpress/ui'; +import type { LoginProtectionState } from './types'; +import type { ProtectSettingsData } from '../../data/use-protect-settings'; + +/** + * Why WordPress.com login can't be turned on, if it can't. + * + * @param login - This section's state from PHP. + * @return The reason, or null when it can be used. + */ +export function getSsoBlocker( login: LoginProtectionState | undefined ): string | null { + if ( login && ! login.sso.available ) { + return __( 'WordPress.com login isn’t available on this site.', 'jetpack-protect-pkg' ); + } + if ( login?.isOffline ) { + return __( 'WordPress.com login isn’t available in offline mode.', 'jetpack-protect-pkg' ); + } + if ( ! login?.ssoUsable ) { + return __( + 'Needs the site owner’s WordPress.com account to be connected.', + 'jetpack-protect-pkg' + ); + } + return null; +} + +/** + * The WordPress.com login toggle, which asks before turning it off. + * + * @param props - Component props. + * @param props.data - The settings and their save function. + * @param props.login - This section's state from PHP. + * @param props.label - The toggle's label. + * @param props.help - What it does, shown unless it can't be used. + * @return The toggle. + */ +export default function SsoToggle( { + data, + login, + label, + help, +}: { + data: ProtectSettingsData; + login: LoginProtectionState | undefined; + label: string; + help: string; +} ) { + const { save } = data; + const blocker = getSsoBlocker( login ); + const [ confirmingOff, setConfirmingOff ] = useState( false ); + const onChange = useCallback( + ( value: boolean ) => ( value ? save( { sso: true } ) : setConfirmingOff( true ) ), + [ save ] + ); + // `save()` resolves to whether it worked; the dialog only needs to know it's done, then closes. + const turnOff = useCallback( async () => { + await save( { sso: false } ); + }, [ save ] ); + + return ( + <> + + + + + + ); +} diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx index a8e9f52bfc6b..763a9c4b0b50 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-card.test.tsx @@ -8,62 +8,118 @@ const on = { available: true, active: true }; const off = { available: true, active: false }; const unavailable = { available: false, active: false }; -const renderCard = ( +const card = ( state: Partial< LoginProtectionState > = {}, - settings: ProtectSettingsData[ 'settings' ] = null, + settings: Partial< ProtectSettingsData > = {}, openTab = jest.fn() -) => - render( - - ); +) => ( + false, + save: jest.fn(), + ...settings, + } as unknown as ProtectSettingsData + } + openTab={ openTab } + /> +); describe( 'LoginProtectionCard', () => { it.each( [ - [ 'every method is on', {}, null, '3 of 3 on', true ], - [ 'one method is off', { sso: off }, null, '2 of 3 on', true ], - [ 'a method is unavailable', { sso: unavailable }, null, '2 of 2 on', true ], - [ 'brute force protection is off', { bruteForce: off }, null, '2 of 3 on', false ], - [ 'a method was turned off since page load', {}, { protect: false }, '2 of 3 on', false ], - [ 'a method was turned on since page load', { sso: off }, { sso: true }, '3 of 3 on', true ], - [ 'WordPress.com login is set but can’t run', { ssoUsable: false }, null, '2 of 2 on', true ], - [ - 'an unavailable method is still set', - { sso: unavailable }, - { sso: true }, - '2 of 2 on', - true, - ], - ] )( 'counts the methods that are on when %s', ( _name, state, settings, badge, hasCount ) => { - renderCard( state, settings ); - - expect( screen.getByText( badge ) ).toBeInTheDocument(); - expect( screen.queryByText( '1,234' ) !== null ).toBe( hasCount ); + [ 'on', {}, null, true ], + [ 'off', { bruteForce: off }, null, false ], + [ 'turned off since page load', {}, { protect: false }, false ], + [ 'turned on since page load', { bruteForce: off }, { protect: true }, true ], + ] )( + 'shows blocked logins only while brute force protection is %s', + ( _name, state, settings, isOn ) => { + render( card( state, { settings } ) ); + + expect( screen.queryByText( '1,234' ) !== null ).toBe( isOn ); + expect( + screen.queryByRole( 'switch', { name: 'Enable brute force protection' } ) === null + ).toBe( isOn ); + } + ); + + it( 'shows the page-load state before the settings load', () => { + render( card( { accountProtection: on, sso: off } ) ); + + expect( screen.getByRole( 'switch', { name: 'Enable account protection' } ) ).toBeChecked(); + expect( + screen.getByRole( 'switch', { name: 'Enable WordPress.com login (SSO)' } ) + ).not.toBeChecked(); } ); it.each( [ - [ 'on', { sso: on }, { On: 1, Off: 2, Unavailable: 0 } ], - [ 'off', { sso: off }, { On: 0, Off: 3, Unavailable: 0 } ], - [ 'unavailable', { sso: unavailable }, { On: 0, Off: 2, Unavailable: 1 } ], - [ 'on but unable to run', { sso: on, ssoUsable: false }, { On: 0, Off: 2, Unavailable: 1 } ], - ] )( 'badges a method that is %s', ( _name, state, badges ) => { - renderCard( { bruteForce: off, accountProtection: off, ...state } ); - - Object.entries( badges ).forEach( ( [ text, count ] ) => - expect( screen.queryAllByText( text ) ).toHaveLength( count ) - ); + [ 'Enable brute force protection', { bruteForce: off }, { protect: true } ], + [ 'Enable account protection', { accountProtection: off }, { 'account-protection': true } ], + ] )( 'saves “%s” when it is switched on', async ( name, state, patch ) => { + const save = jest.fn(); + render( card( state, { save } ) ); + + await userEvent.setup().click( screen.getByRole( 'switch', { name } ) ); + + expect( save ).toHaveBeenCalledWith( patch ); + } ); + + it.each( [ + [ 'Enable brute force protection', { bruteForce: unavailable }, 'Brute force protection' ], + [ 'Enable account protection', { accountProtection: unavailable }, 'Account protection' ], + ] )( 'says why “%s” is disabled when unavailable', ( name, state, feature ) => { + render( card( state ) ); + + expect( screen.getByRole( 'switch', { name } ) ).toHaveAttribute( 'aria-disabled', 'true' ); + expect( screen.getByText( `${ feature } isn’t available on this site.` ) ).toBeInTheDocument(); + } ); + + it( 'keeps keyboard focus in the row when brute force protection turns on', async () => { + const { rerender } = render( card( { bruteForce: off } ) ); + await userEvent + .setup() + .click( screen.getByRole( 'switch', { name: 'Enable brute force protection' } ) ); + + rerender( card( { bruteForce: off }, { settings: { protect: true } } ) ); + + expect( + screen.getByRole( 'link', { name: 'Configure brute force protection' } ) + ).toHaveFocus(); + } ); + + it.each( [ + [ 'one of its settings', [ 'sso' ], true ], + [ 'another card’s setting', [ 'waf' ], false ], + [ 'loading the settings', [], false ], + ] )( 'shows a failure from %s only when it is the card’s own', ( _name, errorKeys, isShown ) => { + render( card( {}, { error: 'Nope', errorKeys } ) ); + + expect( screen.queryByText( 'Nope' ) !== null ).toBe( isShown ); + } ); + + it.each( [ + [ 'on', {}, true ], + [ 'off', { sso: off }, false ], + [ 'on but unable to run', { ssoUsable: false }, false ], + ] )( 'links to WordPress.com login settings only while it is %s', ( _name, state, hasLink ) => { + render( card( state ) ); + + expect( + screen.queryByRole( 'link', { name: 'Configure WordPress.com login (SSO)' } ) !== null + ).toBe( hasLink ); } ); it( 'renders nothing when PHP registered no section', () => { @@ -80,11 +136,11 @@ describe( 'LoginProtectionCard', () => { it( 'opens the Settings tab from its link', async () => { const openTab = jest.fn(); - renderCard( {}, null, openTab ); + render( card( {}, {}, openTab ) ); await userEvent .setup() - .click( screen.getByRole( 'link', { name: 'Configure login protection' } ) ); + .click( screen.getByRole( 'link', { name: 'Configure brute force protection' } ) ); expect( openTab ).toHaveBeenCalledWith( 'settings', { section: 'loginProtection' } ); } ); diff --git a/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-settings-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-settings-card.test.tsx index 3454341024ab..92303253edf6 100644 --- a/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-settings-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/login-protection/test/login-protection-settings-card.test.tsx @@ -52,7 +52,7 @@ describe( 'LoginProtectionSettingsCard', () => { const user = userEvent.setup(); renderCard( { save } ); - await user.click( screen.getByLabelText( SSO ) ); + await user.click( screen.getByRole( 'switch', { name: SSO } ) ); expect( save ).not.toHaveBeenCalled(); @@ -65,7 +65,7 @@ describe( 'LoginProtectionSettingsCard', () => { const save = jest.fn(); renderCard( { save, settings: { sso: false } } ); - await userEvent.setup().click( screen.getByLabelText( SSO ) ); + await userEvent.setup().click( screen.getByRole( 'switch', { name: SSO } ) ); expect( save ).toHaveBeenCalledWith( { sso: true } ); } ); @@ -81,7 +81,7 @@ describe( 'LoginProtectionSettingsCard', () => { renderCard( { state, settings } ); const enabled = ( label: string ) => - ! screen.getByLabelText( label ).hasAttribute( 'disabled' ); + screen.getByRole( 'switch', { name: label } ).getAttribute( 'aria-disabled' ) !== 'true'; expect( enabled( SSO ) ).toBe( canToggle ); expect( enabled( MATCH_BY_EMAIL ) ).toBe( canSetOptions ); expect( enabled( TWO_STEP ) ).toBe( canSetOptions ); @@ -116,7 +116,10 @@ describe( 'LoginProtectionSettingsCard', () => { ] )( 'locks "%s" when its module is unavailable', ( label, key ) => { renderCard( { state: { [ key ]: { available: false, active: false } } } ); - expect( screen.getByLabelText( label ) ).toBeDisabled(); + expect( screen.getByRole( 'switch', { name: label } ) ).toHaveAttribute( + 'aria-disabled', + 'true' + ); } ); it( 'locks a forced option to the value WordPress.com login applies', () => { @@ -128,12 +131,18 @@ describe( 'LoginProtectionSettingsCard', () => { settings: { jetpack_sso_require_two_step: false }, } ); - expect( screen.getByLabelText( TWO_STEP ) ).toBeDisabled(); - expect( screen.getByLabelText( TWO_STEP ) ).toBeChecked(); + expect( screen.getByRole( 'switch', { name: TWO_STEP } ) ).toHaveAttribute( + 'aria-disabled', + 'true' + ); + expect( screen.getByRole( 'switch', { name: TWO_STEP } ) ).toBeChecked(); expect( screen.getByText( 'Two-Step Authentication is enforced by your site’s configuration.' ) ).toBeInTheDocument(); - expect( screen.getByLabelText( MATCH_BY_EMAIL ) ).toBeEnabled(); + expect( screen.getByRole( 'switch', { name: MATCH_BY_EMAIL } ) ).not.toHaveAttribute( + 'aria-disabled', + 'true' + ); } ); it.each( [ diff --git a/projects/packages/protect/routes/dashboard/sections/monitor/monitor-card.tsx b/projects/packages/protect/routes/dashboard/sections/monitor/monitor-card.tsx index 05aed29a0968..6c20e09af94f 100644 --- a/projects/packages/protect/routes/dashboard/sections/monitor/monitor-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/monitor/monitor-card.tsx @@ -21,6 +21,9 @@ const PERCENT = { style: 'percent', maximumFractionDigits: 3 } as const; /** Opens the Settings tab at this section's card. */ const SETTINGS_PARAMS = { section: 'monitor' }; +// The only setting this card saves. +const MONITOR_KEYS = [ 'monitor' ]; + /** * A day's date, in the site's date format. * @@ -176,7 +179,7 @@ export default function MonitorCard( { state: monitor, settings, openTab }: Moni 'jetpack-protect-pkg' ) } > - + { ! active && ( diff --git a/projects/packages/protect/routes/dashboard/sections/monitor/test/monitor-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/monitor/test/monitor-card.test.tsx index 296ea52c9259..3cd4240c1b77 100644 --- a/projects/packages/protect/routes/dashboard/sections/monitor/test/monitor-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/monitor/test/monitor-card.test.tsx @@ -131,7 +131,7 @@ describe( 'MonitorCard', () => { render( ); await expect( - screen.findByLabelText( 'Enable downtime monitoring' ) + screen.findByRole( 'switch', { name: 'Enable downtime monitoring' } ) ).resolves.not.toBeChecked(); expect( screen.queryByText( 'Configure downtime monitoring' ) ).not.toBeInTheDocument(); expect( diff --git a/projects/packages/protect/routes/dashboard/sections/monitor/test/settings-card.test.tsx b/projects/packages/protect/routes/dashboard/sections/monitor/test/settings-card.test.tsx index 95946062164c..789247586ebd 100644 --- a/projects/packages/protect/routes/dashboard/sections/monitor/test/settings-card.test.tsx +++ b/projects/packages/protect/routes/dashboard/sections/monitor/test/settings-card.test.tsx @@ -41,7 +41,7 @@ describe( 'MonitorSettingsCard', () => { renderCard( options ); const enabled = ( label: string ) => - ! screen.getByLabelText( label ).hasAttribute( 'disabled' ); + screen.getByRole( 'switch', { name: label } ).getAttribute( 'aria-disabled' ) !== 'true'; expect( enabled( MONITOR ) ).toBe( canToggle ); expect( enabled( EMAIL ) ).toBe( canEmail ); } ); @@ -49,7 +49,10 @@ describe( 'MonitorSettingsCard', () => { it( 'keeps the email toggle locked while a Monitor save is in flight', () => { renderCard( { saving: true } ); - expect( screen.getByLabelText( EMAIL ) ).toBeDisabled(); + expect( screen.getByRole( 'switch', { name: EMAIL } ) ).toHaveAttribute( + 'aria-disabled', + 'true' + ); } ); it( 'keeps the email toggle locked until the re-read after turning Monitor on settles', async () => { @@ -63,13 +66,19 @@ describe( 'MonitorSettingsCard', () => { const save = jest.fn().mockResolvedValue( undefined ); const { rerender } = renderCard( { monitor: false, save, refresh } ); - await userEvent.setup().click( screen.getByLabelText( MONITOR ) ); + await userEvent.setup().click( screen.getByRole( 'switch', { name: MONITOR } ) ); // The Monitor save has settled and the setting now reads on; only the re-read is pending. rerender( card( { save, refresh } ) ); - expect( screen.getByLabelText( EMAIL ) ).toBeDisabled(); + expect( screen.getByRole( 'switch', { name: EMAIL } ) ).toHaveAttribute( + 'aria-disabled', + 'true' + ); await act( async () => finishRefresh() ); - expect( screen.getByLabelText( EMAIL ) ).toBeEnabled(); + expect( screen.getByRole( 'switch', { name: EMAIL } ) ).not.toHaveAttribute( + 'aria-disabled', + 'true' + ); } ); it.each( [ @@ -82,7 +91,7 @@ describe( 'MonitorSettingsCard', () => { const refresh = jest.fn().mockResolvedValue( undefined ); renderCard( { monitor, save, refresh } ); - await userEvent.setup().click( screen.getByLabelText( MONITOR ) ); + await userEvent.setup().click( screen.getByRole( 'switch', { name: MONITOR } ) ); expect( save ).toHaveBeenCalledWith( { monitor: ! monitor }, undefined ); expect( refresh.mock.calls ).toEqual( calls ); diff --git a/projects/packages/protect/routes/dashboard/sections/scan/scan-card.tsx b/projects/packages/protect/routes/dashboard/sections/scan/scan-card.tsx index 4f4f1962eb57..3a4ebacad08a 100644 --- a/projects/packages/protect/routes/dashboard/sections/scan/scan-card.tsx +++ b/projects/packages/protect/routes/dashboard/sections/scan/scan-card.tsx @@ -181,7 +181,7 @@ export default function ScanCard( { openTab }: Pick< DashboardContext, 'openTab' body = ( <> { hasThreats && ( - + * { - padding: 20px var(--wp-ui-card-padding); - } - - > * + * { - border-inline-start: - var(--wpds-border-width-xs) solid - var(--wpds-color-stroke-surface-neutral); - } - - @media (max-width: 600px) { - grid-template-columns: minmax(0, 1fr); - - > * + * { - border-inline-start: 0; - border-block-start: - var(--wpds-border-width-xs) solid - var(--wpds-color-stroke-surface-neutral); - } + padding-block: var(--wpds-dimension-padding-xl); } } + // Full-bleed, so the threat list sets its own padding. &__row.jp-protect-card__threats { - padding-block-end: 8px; + padding: 0 0 var(--wpds-dimension-padding-sm); } } diff --git a/projects/plugins/jetpack/changelog/update-protect-dashboard-login-card b/projects/plugins/jetpack/changelog/update-protect-dashboard-login-card new file mode 100644 index 000000000000..354ff4c05616 --- /dev/null +++ b/projects/plugins/jetpack/changelog/update-protect-dashboard-login-card @@ -0,0 +1,5 @@ +Significance: patch +Type: other +Comment: Updates the Login protection card added in an unreleased change, which already has an entry. + + diff --git a/tools/eslint-excludelist.json b/tools/eslint-excludelist.json index e1adbfb1ed6d..adc9643d41c7 100644 --- a/tools/eslint-excludelist.json +++ b/tools/eslint-excludelist.json @@ -156,10 +156,8 @@ "projects/packages/premium-analytics/packages/widgets-toolkit/src/components/legend/legend.tsx", "projects/packages/premium-analytics/packages/widgets-toolkit/src/components/page-options-menu/feedback-fields.tsx", "projects/packages/premium-analytics/packages/widgets-toolkit/src/components/wordads-earnings-history/fields.tsx", - "projects/packages/protect/routes/dashboard/components/settings/setting-toggle.tsx", "projects/packages/protect/routes/dashboard/sections/firewall/ip-rule.tsx", "projects/packages/protect/routes/dashboard/sections/firewall/settings-card.tsx", - "projects/packages/protect/routes/dashboard/sections/login-protection/login-protection-settings-card.tsx", "projects/packages/publicize/_inc/components/connection-management/mark-as-shared.tsx", "projects/packages/publicize/_inc/components/connections-toggle-list/index.tsx", "projects/packages/publicize/_inc/components/customize-and-preview/connection-toggle/index.tsx",