|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 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, beforeEach } from 'vitest'; |
| 9 | +import { |
| 10 | + generateAndStoreAuthUrlValues, |
| 11 | + getStorageKey, |
| 12 | + getStoredAuthUrlValues, |
| 13 | +} from './state-pkce.effects.js'; |
| 14 | +import type { GenerateAndStoreAuthUrlValues } from '@forgerock/sdk-types'; |
| 15 | + |
| 16 | +const mockSessionStorage = (() => { |
| 17 | + let store: { [key: string]: string } = {}; |
| 18 | + return { |
| 19 | + getItem: (key: string) => store[key] || null, |
| 20 | + setItem: (key: string, value: string) => { |
| 21 | + store[key] = value; |
| 22 | + }, |
| 23 | + removeItem: (key: string) => { |
| 24 | + delete store[key]; |
| 25 | + }, |
| 26 | + clear: () => { |
| 27 | + store = {}; |
| 28 | + }, |
| 29 | + length: 0, |
| 30 | + key: (index: number) => Object.keys(store)[index] || null, |
| 31 | + }; |
| 32 | +})(); |
| 33 | + |
| 34 | +describe('PKCE', () => { |
| 35 | + beforeEach(() => { |
| 36 | + if (typeof sessionStorage === 'undefined') { |
| 37 | + global.sessionStorage = mockSessionStorage; |
| 38 | + } |
| 39 | + |
| 40 | + sessionStorage.clear(); |
| 41 | + }); |
| 42 | + |
| 43 | + const mockOptions: GenerateAndStoreAuthUrlValues = { |
| 44 | + clientId: 'test-client', |
| 45 | + redirectUri: 'http://localhost:8080', |
| 46 | + scope: 'openid profile', |
| 47 | + responseType: 'code', |
| 48 | + }; |
| 49 | + |
| 50 | + describe('getStorageKey', () => { |
| 51 | + const clientId = 'test-client-id'; |
| 52 | + |
| 53 | + it('should generate storage key with default prefix', () => { |
| 54 | + const key = getStorageKey(clientId); |
| 55 | + expect(key).toBe('FR-SDK-authflow-test-client-id'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should generate storage key with custom prefix', () => { |
| 59 | + const customPrefix = 'CUSTOM'; |
| 60 | + const key = getStorageKey(clientId, customPrefix); |
| 61 | + expect(key).toBe('CUSTOM-authflow-test-client-id'); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe('generateAndStoreAuthUrlValues', () => { |
| 66 | + it('should generate PKCE values', () => { |
| 67 | + const [options] = generateAndStoreAuthUrlValues(mockOptions); |
| 68 | + |
| 69 | + expect(options).toBeDefined(); |
| 70 | + expect(options).toHaveProperty('state'); |
| 71 | + expect(options).toHaveProperty('verifier'); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should store options in sessionStorage when storage function is called', () => { |
| 75 | + const [options, storeAuthUrl] = generateAndStoreAuthUrlValues(mockOptions); |
| 76 | + storeAuthUrl(); |
| 77 | + |
| 78 | + const storageKey = getStorageKey(mockOptions.clientId, mockOptions.prefix); |
| 79 | + const storedValue = sessionStorage.getItem(storageKey); |
| 80 | + expect(storedValue).toBeDefined(); |
| 81 | + |
| 82 | + const parsedValue = JSON.parse(storedValue as string); |
| 83 | + expect(parsedValue).toEqual(options); |
| 84 | + }); |
| 85 | + }); |
| 86 | + |
| 87 | + describe('getStoredAuthUrlValues', () => { |
| 88 | + it('should retrieve and parse stored values', () => { |
| 89 | + const [options, storeAuthUrl] = generateAndStoreAuthUrlValues(mockOptions); |
| 90 | + storeAuthUrl(); |
| 91 | + |
| 92 | + const storedValues = getStoredAuthUrlValues(mockOptions.clientId, mockOptions.prefix); |
| 93 | + expect(storedValues).toEqual(options); |
| 94 | + }); |
| 95 | + |
| 96 | + it('should remove values from storage after retrieval', () => { |
| 97 | + const [, storeAuthUrl] = generateAndStoreAuthUrlValues(mockOptions); |
| 98 | + storeAuthUrl(); |
| 99 | + |
| 100 | + const storageKey = getStorageKey(mockOptions.clientId, mockOptions.prefix); |
| 101 | + |
| 102 | + // Verify value exists before retrieval |
| 103 | + expect(sessionStorage.getItem(storageKey)).toBeDefined(); |
| 104 | + |
| 105 | + // Retrieve values |
| 106 | + getStoredAuthUrlValues(mockOptions.clientId, mockOptions.prefix); |
| 107 | + |
| 108 | + // Verify value was removed |
| 109 | + expect(sessionStorage.getItem(storageKey)).toBeNull(); |
| 110 | + }); |
| 111 | + |
| 112 | + it('should throw error when stored values cannot be parsed', () => { |
| 113 | + const storageKey = getStorageKey(mockOptions.clientId, mockOptions.prefix); |
| 114 | + sessionStorage.setItem(storageKey, 'invalid json'); |
| 115 | + |
| 116 | + expect(() => getStoredAuthUrlValues(mockOptions.clientId, mockOptions.prefix)).toThrow( |
| 117 | + 'Stored values for Auth URL could not be parsed', |
| 118 | + ); |
| 119 | + }); |
| 120 | + }); |
| 121 | +}); |
0 commit comments