aboutsummaryrefslogtreecommitdiff
path: root/test/types.test.ts
blob: 1823c1584e5ad86f55cedd1ffb15969f576b6261 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import { describe, expect, it } from 'vitest';

import { ContentType, isRouteConfig, isSafeRouteName } from '../src/types/index.js';

describe('isSafeRouteName', () => {
    it('accepts typical route names', () => {
        expect(isSafeRouteName('route1')).toBe(true);
        expect(isSafeRouteName('Route_1')).toBe(true);
        expect(isSafeRouteName('route-1')).toBe(true);
    });

    it('enforces length and character rules', () => {
        expect(isSafeRouteName('a'.repeat(64))).toBe(true);
        expect(isSafeRouteName('a'.repeat(65))).toBe(false);
        expect(isSafeRouteName('bad!name')).toBe(false);
    });

    it('rejects unsafe or ambiguous names', () => {
        expect(isSafeRouteName('')).toBe(false);
        expect(isSafeRouteName(' route')).toBe(false);
        expect(isSafeRouteName('route ')).toBe(false);
        expect(isSafeRouteName('.')).toBe(false);
        expect(isSafeRouteName('..')).toBe(false);
        expect(isSafeRouteName('foo/bar')).toBe(false);
        expect(isSafeRouteName('foo\\bar')).toBe(false);
    });
});

describe('isRouteConfig', () => {
    it('accepts a valid configuration', () => {
        const config = {
            name: 'route1',
            contentType: ContentType.JSON,
            hcaptchaProtected: false,
            ntfy: { enabled: false },
            requireToken: true,
        };

        expect(isRouteConfig(config)).toBe(true);
    });

    it('requires an hCaptcha secret when protected', () => {
        const config = {
            name: 'route1',
            contentType: ContentType.JSON,
            hcaptchaProtected: true,
        };

        expect(isRouteConfig(config)).toBe(false);
    });

    it('validates ntfy config when enabled', () => {
        const config = {
            name: 'route1',
            contentType: ContentType.JSON,
            hcaptchaProtected: false,
            ntfy: { enabled: true },
        };

        expect(isRouteConfig(config)).toBe(false);
    });

    it('validates requireToken type when present', () => {
        const config = {
            name: 'route1',
            contentType: ContentType.JSON,
            hcaptchaProtected: false,
            requireToken: 'yes',
        };

        expect(isRouteConfig(config)).toBe(false);
    });
});