|
| 1 | +import { renderHook, waitFor } from '@testing-library/react'; |
| 2 | +import useAuth0 from '../src/use-auth0'; |
| 3 | +import { createWrapper } from './helpers'; |
| 4 | + |
| 5 | +describe('MFA API', () => { |
| 6 | + describe('Basic Availability', () => { |
| 7 | + it('should provide mfa client through useAuth0', async () => { |
| 8 | + const wrapper = createWrapper(); |
| 9 | + const { result } = renderHook(() => useAuth0(), { wrapper }); |
| 10 | + |
| 11 | + await waitFor(() => { |
| 12 | + expect(result.current.mfa).toBeDefined(); |
| 13 | + }); |
| 14 | + }); |
| 15 | + |
| 16 | + it('should provide all five MFA methods', async () => { |
| 17 | + const wrapper = createWrapper(); |
| 18 | + const { result } = renderHook(() => useAuth0(), { wrapper }); |
| 19 | + |
| 20 | + await waitFor(() => { |
| 21 | + expect(result.current.mfa.getAuthenticators).toBeDefined(); |
| 22 | + expect(result.current.mfa.enroll).toBeDefined(); |
| 23 | + expect(result.current.mfa.challenge).toBeDefined(); |
| 24 | + expect(result.current.mfa.verify).toBeDefined(); |
| 25 | + expect(result.current.mfa.getEnrollmentFactors).toBeDefined(); |
| 26 | + }); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('Method Success Tests', () => { |
| 31 | + it('should call mfa.getAuthenticators', async () => { |
| 32 | + const wrapper = createWrapper(); |
| 33 | + const { result } = renderHook(() => useAuth0(), { wrapper }); |
| 34 | + |
| 35 | + await waitFor(async () => { |
| 36 | + const authenticators = await result.current.mfa.getAuthenticators('test-mfa-token'); |
| 37 | + expect(authenticators).toBeDefined(); |
| 38 | + expect(Array.isArray(authenticators)).toBe(true); |
| 39 | + }); |
| 40 | + }); |
| 41 | + |
| 42 | + it('should call mfa.enroll', async () => { |
| 43 | + const wrapper = createWrapper(); |
| 44 | + const { result } = renderHook(() => useAuth0(), { wrapper }); |
| 45 | + |
| 46 | + await waitFor(async () => { |
| 47 | + const enrollment = await result.current.mfa.enroll({ |
| 48 | + mfaToken: 'test-mfa-token', |
| 49 | + factorType: 'otp', |
| 50 | + }); |
| 51 | + expect(enrollment).toBeDefined(); |
| 52 | + expect(enrollment.id).toBe('test-id'); |
| 53 | + }); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should call mfa.challenge', async () => { |
| 57 | + const wrapper = createWrapper(); |
| 58 | + const { result } = renderHook(() => useAuth0(), { wrapper }); |
| 59 | + |
| 60 | + await waitFor(async () => { |
| 61 | + const response = await result.current.mfa.challenge({ |
| 62 | + mfaToken: 'test-mfa-token', |
| 63 | + challengeType: 'otp', |
| 64 | + authenticatorId: 'test-auth-id', |
| 65 | + }); |
| 66 | + expect(response).toBeDefined(); |
| 67 | + expect(response.challengeType).toBe('otp'); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should call mfa.verify', async () => { |
| 72 | + const wrapper = createWrapper(); |
| 73 | + const { result } = renderHook(() => useAuth0(), { wrapper }); |
| 74 | + |
| 75 | + await waitFor(async () => { |
| 76 | + const tokens = await result.current.mfa.verify({ |
| 77 | + mfaToken: 'test-mfa-token', |
| 78 | + otp: '123456', |
| 79 | + }); |
| 80 | + expect(tokens).toBeDefined(); |
| 81 | + expect(tokens.access_token).toBe('test-token'); |
| 82 | + }); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should call mfa.getEnrollmentFactors', async () => { |
| 86 | + const wrapper = createWrapper(); |
| 87 | + const { result } = renderHook(() => useAuth0(), { wrapper }); |
| 88 | + |
| 89 | + await waitFor(async () => { |
| 90 | + const factors = await result.current.mfa.getEnrollmentFactors('test-mfa-token'); |
| 91 | + expect(factors).toBeDefined(); |
| 92 | + expect(Array.isArray(factors)).toBe(true); |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | +}); |
0 commit comments