summaryrefslogtreecommitdiff
path: root/tst/server_filters_activity.test.ts
blob: a992e10f8148dcafbd05a4c55bf3e8de3f1c660f (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import {
    Either,
    HealthCheckActivityImpl,
    HealthCheckOutput,
    PenguenoError,
    PenguenoRequest,
    jsonModel,
    requireMethod,
    type BaseRequest,
    type ServerTrace,
} from '../lib/index';
import { CollectingTrace, TestTraceable } from './test_utils';

const makeBaseRequest = (overrides: Partial<BaseRequest> = {}): BaseRequest => ({
    url: 'https://example.com/hello',
    method: 'GET',
    header: () => ({}),
    formData: async () => new FormData(),
    json: async () => ({}),
    text: async () => '',
    param: () => undefined,
    query: () => ({}),
    queries: () => ({}),
    ...overrides,
});

describe('server filters + activities', () => {
    beforeEach(() => {
        jest.useFakeTimers();
        jest.setSystemTime(new Date('2020-01-01T00:00:00.000Z'));
        jest.spyOn(globalThis.crypto, 'randomUUID').mockReturnValue('00000000-0000-0000-0000-000000000000');
    });

    afterEach(() => {
        jest.useRealTimers();
        jest.restoreAllMocks();
    });

    test('requireMethod rejects unallowed methods', () => {
        const trace = new CollectingTrace<ServerTrace>();
        const req = PenguenoRequest.from(TestTraceable.of(makeBaseRequest({ method: 'GET' }), trace));

        const res = requireMethod(['POST'])(req);
        const err = res.left().get();

        expect(err).toBeInstanceOf(PenguenoError);
        expect(err.status).toBe(405);
    });

    test('jsonModel transforms valid JSON', async () => {
        const trace = new CollectingTrace<ServerTrace>();
        const req = PenguenoRequest.from(
            TestTraceable.of(
                makeBaseRequest({
                    json: async () => ({ hello: 'world' }),
                }),
                trace,
            ),
        );

        const filter = jsonModel((json) => {
            const body = json.get() as { hello?: string };
            return body.hello === 'world' ? Either.right('ok') : Either.left(new PenguenoError('bad json', 400));
        });

        const res = await filter(req);
        expect(res.right().get()).toBe('ok');
    });

    test('jsonModel returns 400 for invalid JSON', async () => {
        const trace = new CollectingTrace<ServerTrace>();
        const req = PenguenoRequest.from(
            TestTraceable.of(
                makeBaseRequest({
                    json: async () => Promise.reject(new Error('nope')),
                }),
                trace,
            ),
        );

        const filter = jsonModel((_json) => Either.right('never'));
        const res = await filter(req);
        expect(res.left().get().status).toBe(400);
    });

    test('HealthCheckActivityImpl returns 200 on success', async () => {
        const trace = new CollectingTrace<ServerTrace>();
        const req = PenguenoRequest.from(TestTraceable.of(makeBaseRequest(), trace));

        const activity = new HealthCheckActivityImpl(async () => Either.right(HealthCheckOutput.YAASSSLAYQUEEN));
        const res = await activity.checkHealth(req);

        expect(res.status).toBe(200);
        expect(res.body()).toBe('{"ok":"ok"}');
    });
});