|
| 1 | +/* |
| 2 | + * Copyright (c) 2026 Ping Identity Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * This software may be modified and distributed under the terms |
| 5 | + * of the MIT license. See the LICENSE file for details. |
| 6 | + */ |
| 7 | + |
| 8 | +import { describe, expect, it } from 'vitest'; |
| 9 | + |
| 10 | +import { StepType, type Step } from '@forgerock/sdk-types'; |
| 11 | + |
| 12 | +import { createJourneyObject, resolveJourneyResult } from './journey.utils.js'; |
| 13 | +import type { JourneyLoginFailure } from './login-failure.utils.js'; |
| 14 | + |
| 15 | +describe('createJourneyObject', () => { |
| 16 | + it('returns Step when provided a step with authId', () => { |
| 17 | + const stepPayload: Step = { |
| 18 | + authId: 'test-auth-id', |
| 19 | + callbacks: [], |
| 20 | + }; |
| 21 | + |
| 22 | + const result = createJourneyObject(stepPayload); |
| 23 | + |
| 24 | + expect(result).not.toHaveProperty('error'); |
| 25 | + expect(result).toHaveProperty('type', StepType.Step); |
| 26 | + expect(result).toHaveProperty('payload'); |
| 27 | + expect((result as { payload: Step }).payload).toEqual(stepPayload); |
| 28 | + }); |
| 29 | + |
| 30 | + it('returns LoginSuccess when provided a step with successUrl', () => { |
| 31 | + const successPayload: Step = { |
| 32 | + successUrl: 'https://example.com/success', |
| 33 | + realm: 'root', |
| 34 | + tokenId: 'token-123', |
| 35 | + }; |
| 36 | + |
| 37 | + const result = createJourneyObject(successPayload); |
| 38 | + |
| 39 | + expect(result).not.toHaveProperty('error'); |
| 40 | + expect(result).toHaveProperty('type', StepType.LoginSuccess); |
| 41 | + expect(result).toHaveProperty('payload', successPayload); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe('resolveJourneyResult - no_response_data', () => { |
| 46 | + it('returns no_response_data GenericError when no data and no error', () => { |
| 47 | + const result = resolveJourneyResult(undefined, undefined); |
| 48 | + |
| 49 | + expect(result).toMatchObject({ |
| 50 | + error: 'no_response_data', |
| 51 | + message: 'No data received from server', |
| 52 | + type: 'unknown_error', |
| 53 | + }); |
| 54 | + }); |
| 55 | +}); |
| 56 | + |
| 57 | +describe('toJourneyResult', () => { |
| 58 | + it('returns request_failed GenericError for FetchBaseQueryError without Step payload', () => { |
| 59 | + const result = resolveJourneyResult(undefined, { status: 500, data: { foo: 'bar' } }); |
| 60 | + |
| 61 | + expect(result).toMatchObject({ |
| 62 | + error: 'request_failed', |
| 63 | + message: 'Request failed: {"foo":"bar"}', |
| 64 | + type: 'unknown_error', |
| 65 | + }); |
| 66 | + }); |
| 67 | + |
| 68 | + it('returns request_failed GenericError for SerializedError', () => { |
| 69 | + const result = resolveJourneyResult(undefined, { message: 'Network failure' }); |
| 70 | + |
| 71 | + expect(result).toMatchObject({ |
| 72 | + error: 'request_failed', |
| 73 | + message: 'Request failed: Network failure', |
| 74 | + type: 'unknown_error', |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + it('returns LoginFailure when FetchBaseQueryError contains a failure Step payload', () => { |
| 79 | + const failurePayload: Step = { |
| 80 | + code: 401, |
| 81 | + message: 'Access Denied', |
| 82 | + reason: 'Unauthorized', |
| 83 | + detail: { failureUrl: 'https://example.com/failure' }, |
| 84 | + }; |
| 85 | + |
| 86 | + const result = resolveJourneyResult(undefined, { status: 401, data: failurePayload }); |
| 87 | + |
| 88 | + expect(result).not.toHaveProperty('error'); |
| 89 | + expect(result).toHaveProperty('type', StepType.LoginFailure); |
| 90 | + expect(result).toHaveProperty('payload', failurePayload); |
| 91 | + |
| 92 | + const failure = result as JourneyLoginFailure; |
| 93 | + expect(failure.getCode()).toBe(401); |
| 94 | + expect(failure.getMessage()).toBe('Access Denied'); |
| 95 | + expect(failure.getReason()).toBe('Unauthorized'); |
| 96 | + }); |
| 97 | +}); |
0 commit comments