|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import type { IncomingHttpHeaders } from 'node:http' |
| 3 | +import { |
| 4 | + NOTION_TOKEN_HEADER, |
| 5 | + isNotionToken, |
| 6 | + notionHeadersForToken, |
| 7 | + redactToken, |
| 8 | + resolveNotionToken, |
| 9 | +} from '../token' |
| 10 | + |
| 11 | +const NTN = `ntn_${'a'.repeat(40)}` |
| 12 | +const LEGACY = `secret_${'b'.repeat(40)}` |
| 13 | + |
| 14 | +describe('isNotionToken', () => { |
| 15 | + it('accepts current and legacy Notion token prefixes', () => { |
| 16 | + expect(isNotionToken(NTN)).toBe(true) |
| 17 | + expect(isNotionToken(LEGACY)).toBe(true) |
| 18 | + }) |
| 19 | + |
| 20 | + it('rejects values without a Notion prefix', () => { |
| 21 | + expect(isNotionToken('Bearer-abc')).toBe(false) |
| 22 | + expect(isNotionToken('some-gateway-secret')).toBe(false) |
| 23 | + }) |
| 24 | + |
| 25 | + it('rejects empty, too-short, and too-long values', () => { |
| 26 | + expect(isNotionToken('')).toBe(false) |
| 27 | + expect(isNotionToken(undefined)).toBe(false) |
| 28 | + expect(isNotionToken(null)).toBe(false) |
| 29 | + expect(isNotionToken('ntn_')).toBe(false) |
| 30 | + expect(isNotionToken(`ntn_${'x'.repeat(400)}`)).toBe(false) |
| 31 | + }) |
| 32 | + |
| 33 | + it('trims surrounding whitespace', () => { |
| 34 | + expect(isNotionToken(` ${NTN} `)).toBe(true) |
| 35 | + }) |
| 36 | +}) |
| 37 | + |
| 38 | +describe('notionHeadersForToken', () => { |
| 39 | + it('builds an Authorization header (Notion-Version is sourced per-operation from the spec)', () => { |
| 40 | + expect(notionHeadersForToken(NTN)).toEqual({ |
| 41 | + Authorization: `Bearer ${NTN}`, |
| 42 | + }) |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +describe('resolveNotionToken', () => { |
| 47 | + const headers = (h: IncomingHttpHeaders) => h |
| 48 | + |
| 49 | + it('reads a valid token from the dedicated header', () => { |
| 50 | + const result = resolveNotionToken(headers({ [NOTION_TOKEN_HEADER]: NTN }), { |
| 51 | + allowAuthorizationFallback: false, |
| 52 | + }) |
| 53 | + expect(result).toEqual({ status: 'ok', token: NTN }) |
| 54 | + }) |
| 55 | + |
| 56 | + it('errors when the dedicated header is present but malformed', () => { |
| 57 | + const result = resolveNotionToken(headers({ [NOTION_TOKEN_HEADER]: 'not-a-token' }), { |
| 58 | + allowAuthorizationFallback: false, |
| 59 | + }) |
| 60 | + expect(result.status).toBe('invalid') |
| 61 | + }) |
| 62 | + |
| 63 | + it('ignores Authorization when fallback is disabled (gateway auth in use)', () => { |
| 64 | + const result = resolveNotionToken(headers({ authorization: `Bearer ${NTN}` }), { |
| 65 | + allowAuthorizationFallback: false, |
| 66 | + }) |
| 67 | + expect(result).toEqual({ status: 'absent' }) |
| 68 | + }) |
| 69 | + |
| 70 | + it('reads a Notion token from Authorization when fallback is enabled', () => { |
| 71 | + const result = resolveNotionToken(headers({ authorization: `Bearer ${NTN}` }), { |
| 72 | + allowAuthorizationFallback: true, |
| 73 | + }) |
| 74 | + expect(result).toEqual({ status: 'ok', token: NTN }) |
| 75 | + }) |
| 76 | + |
| 77 | + it('ignores non-Notion Authorization bearer tokens', () => { |
| 78 | + const result = resolveNotionToken(headers({ authorization: 'Bearer gateway-secret' }), { |
| 79 | + allowAuthorizationFallback: true, |
| 80 | + }) |
| 81 | + expect(result).toEqual({ status: 'absent' }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('returns absent when no token headers are present', () => { |
| 85 | + expect(resolveNotionToken(headers({}), { allowAuthorizationFallback: true })).toEqual({ |
| 86 | + status: 'absent', |
| 87 | + }) |
| 88 | + }) |
| 89 | + |
| 90 | + it('prefers the dedicated header over Authorization', () => { |
| 91 | + const result = resolveNotionToken( |
| 92 | + headers({ [NOTION_TOKEN_HEADER]: NTN, authorization: `Bearer ${LEGACY}` }), |
| 93 | + { allowAuthorizationFallback: true }, |
| 94 | + ) |
| 95 | + expect(result).toEqual({ status: 'ok', token: NTN }) |
| 96 | + }) |
| 97 | + |
| 98 | + it('handles array-valued headers by using the first value', () => { |
| 99 | + const result = resolveNotionToken(headers({ [NOTION_TOKEN_HEADER]: [NTN, LEGACY] }), { |
| 100 | + allowAuthorizationFallback: false, |
| 101 | + }) |
| 102 | + expect(result).toEqual({ status: 'ok', token: NTN }) |
| 103 | + }) |
| 104 | +}) |
| 105 | + |
| 106 | +describe('redactToken', () => { |
| 107 | + it('keeps the prefix and masks the secret', () => { |
| 108 | + const redacted = redactToken(NTN) |
| 109 | + expect(redacted.startsWith('ntn_')).toBe(true) |
| 110 | + expect(redacted).not.toContain('aaaa') |
| 111 | + expect(redacted).toContain(String(NTN.length)) |
| 112 | + }) |
| 113 | +}) |
0 commit comments