|
| 1 | +import { genAuthCode } from '@codebuff/common/util/credentials' |
| 2 | +import { afterEach, beforeEach, describe, expect, test } from 'bun:test' |
| 3 | + |
| 4 | +import { parseAuthCode, validateAuthCode, isAuthCodeExpired } from '../_helpers' |
| 5 | + |
| 6 | +describe('freebuff onboard/_helpers', () => { |
| 7 | + describe('parseAuthCode', () => { |
| 8 | + test('parses valid auth code with three parts', () => { |
| 9 | + const authCode = 'fingerprint-123.1704067200000.abc123hash' |
| 10 | + const result = parseAuthCode(authCode) |
| 11 | + |
| 12 | + expect(result.fingerprintId).toBe('fingerprint-123') |
| 13 | + expect(result.expiresAt).toBe('1704067200000') |
| 14 | + expect(result.receivedHash).toBe('abc123hash') |
| 15 | + }) |
| 16 | + |
| 17 | + test('handles auth code with dots in fingerprint id', () => { |
| 18 | + const authCode = 'fp.with.dots.1704067200000.hashvalue' |
| 19 | + const result = parseAuthCode(authCode) |
| 20 | + |
| 21 | + expect(result.fingerprintId).toBe('fp.with.dots') |
| 22 | + expect(result.expiresAt).toBe('1704067200000') |
| 23 | + expect(result.receivedHash).toBe('hashvalue') |
| 24 | + }) |
| 25 | + |
| 26 | + test('handles auth code missing separator before expiresAt', () => { |
| 27 | + const authCode = |
| 28 | + 'fingerprint-1231704067200000.abc123hashabc123hashabc123hash' |
| 29 | + const result = parseAuthCode(authCode) |
| 30 | + |
| 31 | + expect(result.fingerprintId).toBe('') |
| 32 | + expect(result.expiresAt).toBe('') |
| 33 | + expect(result.receivedHash).toBe('') |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + describe('validateAuthCode', () => { |
| 38 | + const testSecret = 'test-secret-key' |
| 39 | + const testFingerprintId = 'fp-abc123' |
| 40 | + const testExpiresAt = '1704067200000' |
| 41 | + |
| 42 | + test('returns valid=true when hash matches', () => { |
| 43 | + const expectedHash = genAuthCode( |
| 44 | + testFingerprintId, |
| 45 | + testExpiresAt, |
| 46 | + testSecret, |
| 47 | + ) |
| 48 | + const result = validateAuthCode( |
| 49 | + expectedHash, |
| 50 | + testFingerprintId, |
| 51 | + testExpiresAt, |
| 52 | + testSecret, |
| 53 | + ) |
| 54 | + |
| 55 | + expect(result.valid).toBe(true) |
| 56 | + expect(result.expectedHash).toBe(expectedHash) |
| 57 | + }) |
| 58 | + |
| 59 | + test('returns valid=false when hash does not match', () => { |
| 60 | + const result = validateAuthCode( |
| 61 | + 'wrong-hash-value', |
| 62 | + testFingerprintId, |
| 63 | + testExpiresAt, |
| 64 | + testSecret, |
| 65 | + ) |
| 66 | + |
| 67 | + expect(result.valid).toBe(false) |
| 68 | + }) |
| 69 | + }) |
| 70 | + |
| 71 | + describe('isAuthCodeExpired', () => { |
| 72 | + let originalDateNow: typeof Date.now |
| 73 | + |
| 74 | + beforeEach(() => { |
| 75 | + originalDateNow = Date.now |
| 76 | + }) |
| 77 | + |
| 78 | + afterEach(() => { |
| 79 | + Date.now = originalDateNow |
| 80 | + }) |
| 81 | + |
| 82 | + test('returns true when expiresAt is in the past', () => { |
| 83 | + Date.now = () => 1704067200000 |
| 84 | + expect(isAuthCodeExpired('1704067199999')).toBe(true) |
| 85 | + }) |
| 86 | + |
| 87 | + test('returns false when expiresAt is in the future', () => { |
| 88 | + Date.now = () => 1704067200000 |
| 89 | + expect(isAuthCodeExpired('1704067200001')).toBe(false) |
| 90 | + }) |
| 91 | + |
| 92 | + test('treats malformed timestamps as expired', () => { |
| 93 | + expect(isAuthCodeExpired('not-a-number')).toBe(true) |
| 94 | + }) |
| 95 | + }) |
| 96 | +}) |
0 commit comments