|
| 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 } from '../types.js'; |
| 11 | +import { type Step } from '../index.js'; |
| 12 | + |
| 13 | +import { createJourneyObject, handleJourneyResponse } from './journey.utils.js'; |
| 14 | +import type { JourneyLoginFailure } from './login-failure.utils.js'; |
| 15 | + |
| 16 | +describe('createJourneyObject', () => { |
| 17 | + it('returns Step when provided a step with authId', () => { |
| 18 | + const stepPayload: Step = { |
| 19 | + authId: 'test-auth-id', |
| 20 | + callbacks: [], |
| 21 | + }; |
| 22 | + |
| 23 | + const result = createJourneyObject(stepPayload); |
| 24 | + |
| 25 | + expect(result).not.toHaveProperty('error'); |
| 26 | + expect(result).toHaveProperty('type', StepType.Step); |
| 27 | + expect(result).toHaveProperty('payload'); |
| 28 | + expect((result as { payload: Step }).payload).toEqual(stepPayload); |
| 29 | + }); |
| 30 | + |
| 31 | + it('returns LoginSuccess when provided a step with successUrl', () => { |
| 32 | + const successPayload: Step = { |
| 33 | + successUrl: 'https://example.com/success', |
| 34 | + realm: 'root', |
| 35 | + tokenId: 'token-123', |
| 36 | + }; |
| 37 | + |
| 38 | + const result = createJourneyObject(successPayload); |
| 39 | + |
| 40 | + expect(result).not.toHaveProperty('error'); |
| 41 | + expect(result).toHaveProperty('type', StepType.LoginSuccess); |
| 42 | + expect(result).toHaveProperty('payload', successPayload); |
| 43 | + }); |
| 44 | + |
| 45 | + it('returns LoginFailure when provided a step without authId or successUrl', () => { |
| 46 | + const failurePayload: Step = { |
| 47 | + code: 401, |
| 48 | + message: 'Access Denied', |
| 49 | + reason: 'Unauthorized', |
| 50 | + detail: { failureUrl: 'https://example.com/failure' }, |
| 51 | + }; |
| 52 | + |
| 53 | + const result = createJourneyObject(failurePayload); |
| 54 | + |
| 55 | + expect(result).not.toHaveProperty('error'); |
| 56 | + expect(result).toHaveProperty('type', StepType.LoginFailure); |
| 57 | + expect(result).toHaveProperty('payload', failurePayload); |
| 58 | + |
| 59 | + const failure = result as JourneyLoginFailure; |
| 60 | + expect(failure.getCode()).toBe(401); |
| 61 | + expect(failure.getMessage()).toBe('Access Denied'); |
| 62 | + expect(failure.getReason()).toBe('Unauthorized'); |
| 63 | + }); |
| 64 | +}); |
| 65 | + |
| 66 | +describe('handleJourneyResponse', () => { |
| 67 | + it('returns Step data when FetchBaseQueryError has numeric status and object body', () => { |
| 68 | + const body = { code: 401, message: 'Access Denied', reason: 'Unauthorized' }; |
| 69 | + const error = { status: 401, data: body }; |
| 70 | + |
| 71 | + const result = handleJourneyResponse(undefined, error); |
| 72 | + |
| 73 | + expect(result).toBe(body); |
| 74 | + }); |
| 75 | + |
| 76 | + it('returns GenericError when FetchBaseQueryError has numeric status but non-object body', () => { |
| 77 | + const error = { status: 500, data: 'Internal Server Error' }; |
| 78 | + |
| 79 | + const result = handleJourneyResponse(undefined, error); |
| 80 | + |
| 81 | + expect(result).toMatchObject({ error: 'request_failed', type: 'unknown_error' }); |
| 82 | + }); |
| 83 | + |
| 84 | + it('returns GenericError for FETCH_ERROR', () => { |
| 85 | + const error = { status: 'FETCH_ERROR' as const, error: 'Network error' }; |
| 86 | + |
| 87 | + const result = handleJourneyResponse(undefined, error); |
| 88 | + |
| 89 | + expect(result).toMatchObject({ error: 'request_failed', type: 'unknown_error' }); |
| 90 | + expect((result as { message: string }).message).toContain('Network error'); |
| 91 | + }); |
| 92 | + |
| 93 | + it('returns GenericError for PARSING_ERROR', () => { |
| 94 | + const error = { |
| 95 | + status: 'PARSING_ERROR' as const, |
| 96 | + originalStatus: 200, |
| 97 | + data: '<html>Not JSON</html>', |
| 98 | + error: 'JSON parse error', |
| 99 | + }; |
| 100 | + |
| 101 | + const result = handleJourneyResponse(undefined, error); |
| 102 | + |
| 103 | + expect(result).toMatchObject({ error: 'request_failed', type: 'unknown_error' }); |
| 104 | + expect((result as { message: string }).message).toContain('JSON parse error'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('returns GenericError for TIMEOUT_ERROR', () => { |
| 108 | + const error = { status: 'TIMEOUT_ERROR' as const, error: 'Request timed out' }; |
| 109 | + |
| 110 | + const result = handleJourneyResponse(undefined, error); |
| 111 | + |
| 112 | + expect(result).toMatchObject({ error: 'request_failed', type: 'unknown_error' }); |
| 113 | + expect((result as { message: string }).message).toContain('Request timed out'); |
| 114 | + }); |
| 115 | + |
| 116 | + it('returns GenericError for CUSTOM_ERROR', () => { |
| 117 | + const error = { status: 'CUSTOM_ERROR' as const, error: 'Custom error occurred' }; |
| 118 | + |
| 119 | + const result = handleJourneyResponse(undefined, error); |
| 120 | + |
| 121 | + expect(result).toMatchObject({ error: 'request_failed', type: 'unknown_error' }); |
| 122 | + expect((result as { message: string }).message).toContain('Custom error occurred'); |
| 123 | + }); |
| 124 | + |
| 125 | + it('returns GenericError for SerializedError', () => { |
| 126 | + const error = { name: 'Error', message: 'Something went wrong', stack: '...' }; |
| 127 | + |
| 128 | + const result = handleJourneyResponse(undefined, error); |
| 129 | + |
| 130 | + expect(result).toMatchObject({ error: 'request_failed', type: 'unknown_error' }); |
| 131 | + expect((result as { message: string }).message).toContain('Something went wrong'); |
| 132 | + }); |
| 133 | + |
| 134 | + it('returns GenericError when no data and no error', () => { |
| 135 | + const result = handleJourneyResponse(undefined, undefined); |
| 136 | + |
| 137 | + expect(result).toMatchObject({ error: 'no_response_data', type: 'unknown_error' }); |
| 138 | + }); |
| 139 | + |
| 140 | + it('returns data when no error and data is present', () => { |
| 141 | + const data: Step = { authId: 'test-auth-id', callbacks: [] }; |
| 142 | + |
| 143 | + const result = handleJourneyResponse(data, undefined); |
| 144 | + |
| 145 | + expect(result).toBe(data); |
| 146 | + }); |
| 147 | +}); |
0 commit comments