|
| 1 | +import { afterEach, describe, expect, it, vi } from 'vitest' |
| 2 | +import { consumeOtpFromUrl, pairWithUrlOtp, readOtpFromUrl } from '../otp' |
| 3 | + |
| 4 | +afterEach(() => { |
| 5 | + vi.unstubAllGlobals() |
| 6 | +}) |
| 7 | + |
| 8 | +describe('otp url helpers', () => { |
| 9 | + it('reads the OTP from the page URL query string (default param)', () => { |
| 10 | + vi.stubGlobal('location', { search: '?devframe_otp=123456&x=1', href: 'http://localhost:3000/?devframe_otp=123456&x=1' }) |
| 11 | + expect(readOtpFromUrl()).toBe('123456') |
| 12 | + }) |
| 13 | + |
| 14 | + it('supports a custom param name', () => { |
| 15 | + vi.stubGlobal('location', { search: '?code=999', href: 'http://localhost:3000/?code=999' }) |
| 16 | + expect(readOtpFromUrl('code')).toBe('999') |
| 17 | + }) |
| 18 | + |
| 19 | + it('returns undefined when the param is absent and is safe without location', () => { |
| 20 | + vi.stubGlobal('location', { search: '?x=1', href: 'http://localhost:3000/?x=1' }) |
| 21 | + expect(readOtpFromUrl()).toBeUndefined() |
| 22 | + vi.stubGlobal('location', undefined) |
| 23 | + expect(readOtpFromUrl()).toBeUndefined() |
| 24 | + expect(() => consumeOtpFromUrl()).not.toThrow() |
| 25 | + }) |
| 26 | + |
| 27 | + it('consume reads then strips the OTP via history.replaceState, keeping other params', () => { |
| 28 | + const replaceState = vi.fn() |
| 29 | + vi.stubGlobal('location', { search: '?devframe_otp=123456&x=1', href: 'http://localhost:3000/?devframe_otp=123456&x=1' }) |
| 30 | + vi.stubGlobal('history', { state: { a: 1 }, replaceState }) |
| 31 | + |
| 32 | + expect(consumeOtpFromUrl()).toBe('123456') |
| 33 | + expect(replaceState).toHaveBeenCalledTimes(1) |
| 34 | + const [state, , href] = replaceState.mock.calls[0] |
| 35 | + expect(state).toEqual({ a: 1 }) |
| 36 | + expect(href).toBe('http://localhost:3000/?x=1') |
| 37 | + }) |
| 38 | + |
| 39 | + it('does not touch the URL when no OTP is present', () => { |
| 40 | + const replaceState = vi.fn() |
| 41 | + vi.stubGlobal('location', { search: '?x=1', href: 'http://localhost:3000/?x=1' }) |
| 42 | + vi.stubGlobal('history', { state: null, replaceState }) |
| 43 | + |
| 44 | + expect(consumeOtpFromUrl()).toBeUndefined() |
| 45 | + expect(replaceState).not.toHaveBeenCalled() |
| 46 | + }) |
| 47 | +}) |
| 48 | + |
| 49 | +describe('pairWithUrlOtp', () => { |
| 50 | + it('exchanges the OTP via the client and resolves true on success', async () => { |
| 51 | + vi.stubGlobal('location', { search: '?devframe_otp=123456', href: 'http://localhost:3000/?devframe_otp=123456' }) |
| 52 | + vi.stubGlobal('history', { state: null, replaceState: vi.fn() }) |
| 53 | + const requestTrustWithCode = vi.fn().mockResolvedValue(true) |
| 54 | + |
| 55 | + const ok = await pairWithUrlOtp({ isTrusted: false, requestTrustWithCode }) |
| 56 | + |
| 57 | + expect(requestTrustWithCode).toHaveBeenCalledWith('123456') |
| 58 | + expect(ok).toBe(true) |
| 59 | + }) |
| 60 | + |
| 61 | + it('returns false (and does not exchange) when no OTP is present', async () => { |
| 62 | + vi.stubGlobal('location', { search: '', href: 'http://localhost:3000/' }) |
| 63 | + const requestTrustWithCode = vi.fn() |
| 64 | + |
| 65 | + const ok = await pairWithUrlOtp({ isTrusted: false, requestTrustWithCode }) |
| 66 | + |
| 67 | + expect(requestTrustWithCode).not.toHaveBeenCalled() |
| 68 | + expect(ok).toBe(false) |
| 69 | + }) |
| 70 | + |
| 71 | + it('skips the exchange but still consumes the OTP when already trusted', async () => { |
| 72 | + const replaceState = vi.fn() |
| 73 | + vi.stubGlobal('location', { search: '?devframe_otp=123456', href: 'http://localhost:3000/?devframe_otp=123456' }) |
| 74 | + vi.stubGlobal('history', { state: null, replaceState }) |
| 75 | + const requestTrustWithCode = vi.fn() |
| 76 | + |
| 77 | + const ok = await pairWithUrlOtp({ isTrusted: true, requestTrustWithCode }) |
| 78 | + |
| 79 | + expect(ok).toBe(true) |
| 80 | + expect(requestTrustWithCode).not.toHaveBeenCalled() |
| 81 | + expect(replaceState).toHaveBeenCalledTimes(1) |
| 82 | + }) |
| 83 | +}) |
0 commit comments