|
| 1 | +/* |
| 2 | + * Copyright (c) 2026-present, Okta, Inc. and/or its affiliates. All rights reserved. |
| 3 | + * The Okta software accompanied by this notice is provided pursuant to the Apache License, Version 2.0 (the "License.") |
| 4 | + * |
| 5 | + * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 | + * Unless required by applicable law or agreed to in writing, software |
| 7 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 8 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | + * |
| 10 | + * See the License for the specific language governing permissions and limitations under the License. |
| 11 | + */ |
| 12 | + |
| 13 | +import { IDX_STEP } from 'src/constants'; |
| 14 | +import { getStubFormBag, getStubTransactionWithNextStep } from 'src/mocks/utils/utils'; |
| 15 | +import { |
| 16 | + DescriptionElement, |
| 17 | + FieldElement, |
| 18 | + LinkElement, |
| 19 | + OpenOktaVerifyFPButtonElement, |
| 20 | + TitleElement, |
| 21 | + WidgetProps, |
| 22 | +} from 'src/types'; |
| 23 | + |
| 24 | +import * as idxUtils from '../../util/idxUtils'; |
| 25 | +import { transformNfcPinEnroll } from './transformNfcPinEnroll'; |
| 26 | + |
| 27 | +describe('transformNfcPinEnroll', () => { |
| 28 | + const widgetProps: WidgetProps = {}; |
| 29 | + |
| 30 | + describe('Device Challenge Phase (no passcode field)', () => { |
| 31 | + let transaction: ReturnType<typeof getStubTransactionWithNextStep>; |
| 32 | + let formBag: ReturnType<typeof getStubFormBag>; |
| 33 | + |
| 34 | + beforeEach(() => { |
| 35 | + transaction = getStubTransactionWithNextStep(); |
| 36 | + formBag = getStubFormBag(); |
| 37 | + formBag.uischema.elements = []; |
| 38 | + transaction.nextStep = { name: 'enroll-poll' }; |
| 39 | + transaction.rawIdxState = { |
| 40 | + ...transaction.rawIdxState, |
| 41 | + currentAuthenticator: { |
| 42 | + type: 'object', |
| 43 | + value: { |
| 44 | + // @ts-expect-error contextualData not fully typed |
| 45 | + contextualData: { |
| 46 | + setupNfcUrl: 'com-okta-authenticator:/actions/enroll?authenticator_key=nfc&nonce=mock', |
| 47 | + }, |
| 48 | + type: 'proximity', |
| 49 | + key: 'nfc_pin', |
| 50 | + id: 'aut123', |
| 51 | + displayName: 'NFC', |
| 52 | + methods: [{ type: 'nfc_pin' }], |
| 53 | + }, |
| 54 | + }, |
| 55 | + }; |
| 56 | + transaction.availableSteps = [{ name: IDX_STEP.SELECT_AUTHENTICATOR_ENROLL }]; |
| 57 | + jest.spyOn(idxUtils, 'hasMinAuthenticatorOptions').mockReturnValue(true); |
| 58 | + }); |
| 59 | + |
| 60 | + afterEach(() => { |
| 61 | + jest.restoreAllMocks(); |
| 62 | + }); |
| 63 | + |
| 64 | + it('should show "Set up NFC" title', () => { |
| 65 | + const result = transformNfcPinEnroll({ transaction, formBag, widgetProps }); |
| 66 | + |
| 67 | + const titleEl = result.uischema.elements[0] as TitleElement; |
| 68 | + expect(titleEl.type).toBe('Title'); |
| 69 | + expect(titleEl.options.content).toBe('oie.enroll.nfc_pin.title'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('should show NFC enrollment description', () => { |
| 73 | + const result = transformNfcPinEnroll({ transaction, formBag, widgetProps }); |
| 74 | + |
| 75 | + const descEl = result.uischema.elements[1] as DescriptionElement; |
| 76 | + expect(descEl.type).toBe('Description'); |
| 77 | + expect(descEl.options.content).toBe('oie.enroll.nfc_pin.instructions'); |
| 78 | + }); |
| 79 | + |
| 80 | + it('should include OpenOktaVerifyFPButton with setupNfcUrl', () => { |
| 81 | + const result = transformNfcPinEnroll({ transaction, formBag, widgetProps }); |
| 82 | + |
| 83 | + const buttonEl = result.uischema.elements[2] as OpenOktaVerifyFPButtonElement; |
| 84 | + expect(buttonEl.type).toBe('OpenOktaVerifyFPButton'); |
| 85 | + expect(buttonEl.options.href).toBe('com-okta-authenticator:/actions/enroll?authenticator_key=nfc&nonce=mock'); |
| 86 | + expect(buttonEl.options.challengeMethod).toBe('CUSTOM_URI'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('should include "Return to authenticator list" link', () => { |
| 90 | + const result = transformNfcPinEnroll({ transaction, formBag, widgetProps }); |
| 91 | + |
| 92 | + // Element order: [Title, Description, OpenOktaVerifyFPButton, ReturnLink, CancelLink] |
| 93 | + const returnLink = result.uischema.elements[3] as LinkElement; |
| 94 | + expect(returnLink.type).toBe('Link'); |
| 95 | + expect(returnLink.options.label).toBe('oie.enroll.switch.authenticator'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('should include "Back to sign in" cancel link', () => { |
| 99 | + const result = transformNfcPinEnroll({ transaction, formBag, widgetProps }); |
| 100 | + |
| 101 | + const lastEl = result.uischema.elements[result.uischema.elements.length - 1] as LinkElement; |
| 102 | + expect(lastEl.type).toBe('Link'); |
| 103 | + expect(lastEl.options.step).toBe('cancel'); |
| 104 | + expect(lastEl.options.label).toBe('goback'); |
| 105 | + }); |
| 106 | + |
| 107 | + it('should NOT include download link', () => { |
| 108 | + const result = transformNfcPinEnroll({ transaction, formBag, widgetProps }); |
| 109 | + |
| 110 | + // Only 5 elements: [Title, Description, OpenOktaVerifyFPButton, ReturnLink, CancelLink] |
| 111 | + expect(result.uischema.elements.length).toBe(5); |
| 112 | + const hasDownloadLink = result.uischema.elements.some( |
| 113 | + (el) => (el as LinkElement).options?.href?.includes('apple.com'), |
| 114 | + ); |
| 115 | + expect(hasDownloadLink).toBe(false); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('PIN Creation Phase (has passcode field)', () => { |
| 120 | + let transaction: ReturnType<typeof getStubTransactionWithNextStep>; |
| 121 | + let formBag: ReturnType<typeof getStubFormBag>; |
| 122 | + |
| 123 | + beforeEach(() => { |
| 124 | + transaction = getStubTransactionWithNextStep(); |
| 125 | + formBag = getStubFormBag(); |
| 126 | + formBag.uischema.elements = [ |
| 127 | + { |
| 128 | + type: 'Field', |
| 129 | + label: 'NFC', |
| 130 | + key: 'credentials.passcode', |
| 131 | + options: { inputMeta: { name: 'credentials.passcode', secret: true } }, |
| 132 | + } as FieldElement, |
| 133 | + ]; |
| 134 | + transaction.nextStep = { |
| 135 | + name: 'enroll-authenticator', |
| 136 | + relatesTo: { |
| 137 | + value: { |
| 138 | + type: 'proximity', |
| 139 | + key: 'nfc_pin', |
| 140 | + id: 'aut123', |
| 141 | + displayName: 'NFC', |
| 142 | + methods: [{ type: 'nfc_pin' }], |
| 143 | + // @ts-expect-error settings not fully typed |
| 144 | + settings: { minLength: 4, maxLength: 4 }, |
| 145 | + }, |
| 146 | + }, |
| 147 | + }; |
| 148 | + transaction.availableSteps = [{ name: IDX_STEP.SELECT_AUTHENTICATOR_ENROLL }]; |
| 149 | + }); |
| 150 | + |
| 151 | + it('should delegate to transformNfcPinCreate when passcode field exists', () => { |
| 152 | + const result = transformNfcPinEnroll({ transaction, formBag, widgetProps }); |
| 153 | + |
| 154 | + // transformNfcPinCreate adds Title as first element |
| 155 | + const titleEl = result.uischema.elements[0] as TitleElement; |
| 156 | + expect(titleEl.type).toBe('Title'); |
| 157 | + expect(titleEl.options.content).toBe('oie.enroll.nfc_pin.create.title'); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments