-
Notifications
You must be signed in to change notification settings - Fork 1
IS-11252 WebAuthn client operations in the LWA #154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
aleixsuau
wants to merge
6
commits into
integration/IS-5161/login-web-app
Choose a base branch
from
feature/IS-11252/webauthn-client-operations
base: integration/IS-5161/login-web-app
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
1a79671
IS-11252 Add WebAuthn client operations support to LWA
aleixsuau 803d720
IS-11252 Suffix WebAuthn split-action titles with the device label
aleixsuau 31da44a
IS-11252 Fix CI, duplicate keys, JSDoc, and add capability-gating tests
Copilot 375c298
IS-11252 Move client-operation action factories to shared mocks.ts
aleixsuau f8fd0f5
IS-11252 Flatten WebAuthn test suite into a single describe
aleixsuau 68763d0
IS-11252 Fix stale dataHelpers paths in JSDoc examples
aleixsuau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
158 changes: 158 additions & 0 deletions
158
...src/haapi-stepper/feature/actions/client-operation/HaapiStepperClientOperationUI.spec.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,158 @@ | ||
| /* | ||
| * Copyright (C) 2025 Curity AB. All rights reserved. | ||
| * | ||
| * The contents of this file are the property of Curity AB. | ||
| * You may not copy or use this file, in either source code | ||
| * or executable form, except in compliance with terms | ||
| * set by Curity AB. | ||
| * | ||
| * For further information, please contact Curity AB. | ||
| */ | ||
|
|
||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { render, screen } from '@testing-library/react'; | ||
| import userEvent from '@testing-library/user-event'; | ||
|
|
||
| import { HAAPI_STEPS } from '../../../data-access/types/haapi-step.types'; | ||
| import { | ||
| createMockBankIdAction, | ||
| createMockExternalBrowserFlowAction, | ||
| createMockStep, | ||
| createMockWebAuthnAnyDeviceBothOptionsAction, | ||
| createMockWebAuthnPlatformOnlyAnyDeviceAction, | ||
| createMockWebAuthnRegistrationAction, | ||
| externalBrowserFlowActionTitle, | ||
| webAuthnAnyDeviceActionTitle, | ||
| webAuthnPlatformOnlyAnyDeviceActionTitle, | ||
| webAuthnRegistrationActionTitle, | ||
| } from '../../../util/tests/mocks'; | ||
| import { HaapiStepperActionsUI } from '../../../ui/actions/HaapiStepperActionsUI'; | ||
| import { HaapiStepperClientOperationUI } from './HaapiStepperClientOperationUI'; | ||
| import { useIsWebAuthnPlatformAuthenticatorAvailable } from './operations/webauthn'; | ||
|
|
||
| vi.mock('./operations/webauthn', async () => { | ||
| const actual = await vi.importActual('./operations/webauthn'); | ||
| return { | ||
| ...(actual as object), | ||
| useIsWebAuthnPlatformAuthenticatorAvailable: vi.fn(() => undefined), | ||
| }; | ||
| }); | ||
|
|
||
| describe('HaapiStepperClientOperationUI', () => { | ||
| let user: ReturnType<typeof userEvent.setup>; | ||
|
|
||
| beforeEach(() => { | ||
| user = userEvent.setup(); | ||
| }); | ||
|
|
||
| describe('Default rendering', () => { | ||
| it('renders the action title as an enabled button', () => { | ||
| const action = createMockExternalBrowserFlowAction(); | ||
|
|
||
| render(<HaapiStepperClientOperationUI action={action} onAction={vi.fn()} />); | ||
|
|
||
| expect(screen.getByRole('button', { name: externalBrowserFlowActionTitle })).toBeEnabled(); | ||
| }); | ||
|
|
||
| it('does not render a progress bar when the action has no remaining wait time', () => { | ||
| const action = createMockExternalBrowserFlowAction(); | ||
|
|
||
| render(<HaapiStepperClientOperationUI action={action} onAction={vi.fn()} />); | ||
|
|
||
| expect(screen.queryByRole('progressbar')).not.toBeInTheDocument(); | ||
| }); | ||
|
|
||
| it('forwards the action to onAction when clicked', async () => { | ||
| const action = createMockExternalBrowserFlowAction(); | ||
| const onAction = vi.fn(); | ||
|
|
||
| render(<HaapiStepperClientOperationUI action={action} onAction={onAction} />); | ||
|
|
||
| await user.click(screen.getByRole('button', { name: externalBrowserFlowActionTitle })); | ||
|
|
||
| expect(onAction).toHaveBeenCalledTimes(1); | ||
| expect(onAction).toHaveBeenCalledWith(action); | ||
| }); | ||
| }); | ||
|
|
||
| describe('BankID polling progress', () => { | ||
| it('renders a progress bar reflecting the session remaining time', () => { | ||
| const action = createMockBankIdAction({ maxWaitTime: 60, maxWaitRemainingTime: 30 }); | ||
|
|
||
| render(<HaapiStepperClientOperationUI action={action} onAction={vi.fn()} />); | ||
|
|
||
| const progress = screen.getByRole('progressbar'); | ||
| expect(progress).toHaveAttribute('value', '30'); | ||
| expect(progress).toHaveAttribute('max', '60'); | ||
| }); | ||
|
|
||
| it('hides the progress bar when showBankIdSessionTimeLeft is false', () => { | ||
| const action = createMockBankIdAction({ maxWaitTime: 60, maxWaitRemainingTime: 30 }); | ||
|
|
||
| render(<HaapiStepperClientOperationUI action={action} onAction={vi.fn()} showBankIdSessionTimeLeft={false} />); | ||
|
|
||
| expect(screen.queryByRole('progressbar')).not.toBeInTheDocument(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('WebAuthn', () => { | ||
| afterEach(() => { | ||
| vi.unstubAllGlobals(); | ||
| vi.mocked(useIsWebAuthnPlatformAuthenticatorAvailable).mockReset(); | ||
| }); | ||
|
|
||
| it('enables the button when the WebAuthn API is available', () => { | ||
| vi.stubGlobal('PublicKeyCredential', stubPublicKeyCredential()); | ||
| const action = createMockWebAuthnRegistrationAction(); | ||
|
|
||
| render(<HaapiStepperClientOperationUI action={action} onAction={vi.fn()} />); | ||
|
|
||
| expect(screen.getByRole('button', { name: webAuthnRegistrationActionTitle })).toBeEnabled(); | ||
| }); | ||
|
|
||
| it('disables the button when the WebAuthn API is unavailable', () => { | ||
| // jsdom does not expose `PublicKeyCredential`, so `isWebAuthnApiSupported()` returns false | ||
| // and the gate disables WebAuthn buttons. | ||
| const action = createMockWebAuthnRegistrationAction(); | ||
|
|
||
| render(<HaapiStepperClientOperationUI action={action} onAction={vi.fn()} />); | ||
|
|
||
| expect(screen.getByRole('button', { name: webAuthnRegistrationActionTitle })).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('disables a platform-only any-device registration when no platform authenticator is available', () => { | ||
| vi.stubGlobal('PublicKeyCredential', stubPublicKeyCredential()); | ||
| vi.mocked(useIsWebAuthnPlatformAuthenticatorAvailable).mockReturnValue(false); | ||
| const action = createMockWebAuthnPlatformOnlyAnyDeviceAction(); | ||
|
|
||
| render(<HaapiStepperClientOperationUI action={action} onAction={vi.fn()} />); | ||
|
|
||
| expect(screen.getByRole('button', { name: webAuthnPlatformOnlyAnyDeviceActionTitle })).toBeDisabled(); | ||
| }); | ||
|
|
||
| it('renders one button per credential option for any-device-mode with both options, suffixing the original title', () => { | ||
| const action = createMockWebAuthnAnyDeviceBothOptionsAction(); | ||
| const step = createMockStep(HAAPI_STEPS.AUTHENTICATION, { actions: [action] }); | ||
|
|
||
| render(<HaapiStepperActionsUI actions={step.dataHelpers.actions?.all} onAction={vi.fn()} />); | ||
|
|
||
| expect(screen.getByRole('button', { name: `${webAuthnAnyDeviceActionTitle} (This device)` })).toBeInTheDocument(); | ||
| expect( | ||
| screen.getByRole('button', { name: `${webAuthnAnyDeviceActionTitle} (Another device)` }) | ||
| ).toBeInTheDocument(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| /** | ||
| * Minimal stand-in for the static `PublicKeyCredential` interface — enough for | ||
| * `isWebAuthnApiSupported()` to return true and for the platform-authenticator hook to resolve. | ||
| * jsdom doesn't expose `PublicKeyCredential`, so tests stub it to emulate a WebAuthn-capable | ||
| * browser without reaching into the real `navigator.credentials` API. | ||
| */ | ||
| const stubPublicKeyCredential = () => | ||
| Object.assign(vi.fn(), { | ||
| parseCreationOptionsFromJSON: vi.fn(), | ||
| parseRequestOptionsFromJSON: vi.fn(), | ||
| isUserVerifyingPlatformAuthenticatorAvailable: vi.fn(() => Promise.resolve(true)), | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.