-
Notifications
You must be signed in to change notification settings - Fork 2
IS-11380 HaapiStepper: own bootstrap config and the haapiFetch lifecycle #207
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
Open
aleixsuau
wants to merge
2
commits into
integration/IS-5161/login-web-app
Choose a base branch
from
fix/integration/IS-5161/login-web-app/IS-11380-stepper-haapi-fetch-lifecycle
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.
+452
−179
Open
Changes from all commits
Commits
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
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
31 changes: 0 additions & 31 deletions
31
src/login-web-app/src/haapi-stepper/data-access/bootstrap-configuration.ts
This file was deleted.
Oops, something went wrong.
16 changes: 0 additions & 16 deletions
16
src/login-web-app/src/haapi-stepper/data-access/haapi-fetch-initializer.ts
This file was deleted.
Oops, something went wrong.
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
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
93 changes: 93 additions & 0 deletions
93
src/login-web-app/src/haapi-stepper/data-access/useHaapiFetch.spec.ts
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,93 @@ | ||
| /* | ||
| * Copyright (C) 2026 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 { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { renderHook } from '@testing-library/react'; | ||
| import type { HaapiConfiguration } from '@curity/identityserver-haapi-web-driver'; | ||
| import { MEDIA_TYPES } from '../../shared/util/types/media.types'; | ||
| import { useHaapiFetch } from './useHaapiFetch'; | ||
| import { HAAPI_STEPS, type HaapiLink } from './types/haapi-step.types'; | ||
| import { HAAPI_ACTION_TYPES, type HaapiFormAction } from './types/haapi-action.types'; | ||
| import { HAAPI_FORM_FIELDS, HTTP_METHODS } from './types/haapi-form.types'; | ||
|
|
||
| // Hoist the spies so the vi.mock factory (which runs at module-load time, before | ||
| // the test file body executes) can reference them without hitting the TDZ. | ||
| const { mockHaapiFetch, createHaapiFetchSpy } = vi.hoisted(() => { | ||
| const mockHaapiFetch = vi.fn(); | ||
| const createHaapiFetchSpy = vi.fn(() => mockHaapiFetch); | ||
| return { mockHaapiFetch, createHaapiFetchSpy }; | ||
| }); | ||
| vi.mock('@curity/identityserver-haapi-web-driver', () => ({ | ||
| createHaapiFetch: createHaapiFetchSpy, | ||
| })); | ||
|
|
||
| describe('useHaapiFetch', () => { | ||
| const haapiConfig = { clientId: 'test-client', tokenEndpoint: 'https://example/token' } as HaapiConfiguration; | ||
|
|
||
| beforeEach(() => { | ||
| mockHaapiFetch.mockReset(); | ||
| }); | ||
|
|
||
| it('builds the fetcher via createHaapiFetch with the supplied HaapiConfiguration', () => { | ||
| renderHook(() => useHaapiFetch(haapiConfig)); | ||
|
|
||
| expect(createHaapiFetchSpy).toHaveBeenCalledWith(haapiConfig); | ||
| }); | ||
|
|
||
| it('sendHaapiFetchRequest forwards link actions to the underlying haapiFetch as a GET to the link href', async () => { | ||
| mockHaapiFetch.mockResolvedValueOnce( | ||
| new Response(JSON.stringify({ type: HAAPI_STEPS.AUTHENTICATION }), { | ||
| headers: { 'Content-Type': MEDIA_TYPES.AUTH }, | ||
| }) | ||
| ); | ||
|
|
||
| const { result } = renderHook(() => useHaapiFetch(haapiConfig)); | ||
|
|
||
| const link: HaapiLink = { href: '/test/href', rel: 'self' }; | ||
| await result.current.sendHaapiFetchRequest(link); | ||
|
|
||
| expect(mockHaapiFetch).toHaveBeenCalledWith(link.href, { method: 'GET' }); | ||
| }); | ||
|
|
||
| it('sendHaapiFetchRequest forwards form actions to the underlying haapiFetch with the payload encoded into the request body', async () => { | ||
| mockHaapiFetch.mockResolvedValueOnce( | ||
| new Response(JSON.stringify({ type: HAAPI_STEPS.AUTHENTICATION }), { | ||
| headers: { 'Content-Type': MEDIA_TYPES.AUTH }, | ||
| }) | ||
| ); | ||
|
|
||
| const { result } = renderHook(() => useHaapiFetch(haapiConfig)); | ||
|
|
||
| const formAction: HaapiFormAction = { | ||
| template: HAAPI_ACTION_TYPES.FORM, | ||
| kind: 'login', | ||
| model: { | ||
| method: HTTP_METHODS.POST, | ||
| href: '/api/login', | ||
| type: MEDIA_TYPES.FORM_URLENCODED, | ||
| fields: [{ name: 'username', type: HAAPI_FORM_FIELDS.USERNAME }], | ||
| }, | ||
| }; | ||
|
|
||
| await result.current.sendHaapiFetchRequest({ | ||
| action: formAction, | ||
| payload: { username: 'alice' }, | ||
| }); | ||
|
|
||
| expect(mockHaapiFetch).toHaveBeenCalledTimes(1); | ||
| const [url, init] = mockHaapiFetch.mock.calls[0] as [string, RequestInit]; | ||
| expect(url).toBe(formAction.model.href); | ||
| expect(init.method).toBe(formAction.model.method); | ||
| expect(init.headers).toEqual({ 'Content-Type': formAction.model.type }); | ||
| expect(init.body).toBeInstanceOf(URLSearchParams); | ||
| expect((init.body as URLSearchParams).get('username')).toBe('alice'); | ||
| }); | ||
| }); |
37 changes: 37 additions & 0 deletions
37
src/login-web-app/src/haapi-stepper/data-access/useHaapiFetch.ts
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,37 @@ | ||
| /* | ||
| * Copyright (C) 2026 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 { useMemo } from 'react'; | ||
| import { createHaapiFetch } from '@curity/identityserver-haapi-web-driver'; | ||
| import type { FetchLike, HaapiConfiguration } from '@curity/identityserver-haapi-web-driver'; | ||
| import { sendHaapiFetchRequest } from './happi-fetch-request'; | ||
| import type { HaapiFetchAction } from './types/haapi-fetch.types'; | ||
|
|
||
| // The @curity/identityserver-haapi-web-driver is a *process-global singleton*: | ||
| // the docs state "at most one active fetch-like function", and in practice | ||
| // `createHaapiFetch` registers an iframe + postMessage channel that can't | ||
| // survive rapid create/close/create cycles (e.g. React StrictMode dev). | ||
| // Caching the created fetch function allows us to reuse it and avoid such issues. | ||
| let cachedHaapiFetch: FetchLike | undefined; | ||
|
|
||
| function getHaapiFetch(haapi: HaapiConfiguration): FetchLike { | ||
| return (cachedHaapiFetch ??= createHaapiFetch(haapi)); | ||
| } | ||
|
|
||
| export function useHaapiFetch(haapi: HaapiConfiguration) { | ||
| const haapiFetch = useMemo(() => getHaapiFetch(haapi), [haapi]); | ||
|
Comment on lines
+23
to
+30
|
||
| return useMemo( | ||
| () => ({ | ||
| sendHaapiFetchRequest: (action: HaapiFetchAction) => sendHaapiFetchRequest(action, haapiFetch), | ||
| }), | ||
| [haapiFetch] | ||
| ); | ||
| } | ||
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.