|
| 1 | +import { migrateLogsMapping } from '@nangohq/logs'; |
| 2 | +import { multipleMigrations } from '@nangohq/database'; |
| 3 | +import { seeders } from '@nangohq/shared'; |
| 4 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 5 | +import { isSuccess, runServer, shouldBeProtected, shouldRequireQueryEnv } from '../../../utils/tests.js'; |
| 6 | + |
| 7 | +let api: Awaited<ReturnType<typeof runServer>>; |
| 8 | +describe('POST /logs/insights', () => { |
| 9 | + beforeAll(async () => { |
| 10 | + await multipleMigrations(); |
| 11 | + await migrateLogsMapping(); |
| 12 | + |
| 13 | + api = await runServer(); |
| 14 | + }); |
| 15 | + afterAll(() => { |
| 16 | + api.server.close(); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should be protected', async () => { |
| 20 | + const res = await api.fetch('/api/v1/logs/insights', { method: 'POST', query: { env: 'dev' }, body: { type: 'action' } }); |
| 21 | + |
| 22 | + shouldBeProtected(res); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should enforce env query params', async () => { |
| 26 | + const { env } = await seeders.seedAccountEnvAndUser(); |
| 27 | + const res = await api.fetch( |
| 28 | + '/api/v1/logs/insights', |
| 29 | + // @ts-expect-error missing query on purpose |
| 30 | + { |
| 31 | + method: 'POST', |
| 32 | + token: env.secret_key, |
| 33 | + body: { type: 'action' } |
| 34 | + } |
| 35 | + ); |
| 36 | + |
| 37 | + shouldRequireQueryEnv(res); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should validate body', async () => { |
| 41 | + const { env } = await seeders.seedAccountEnvAndUser(); |
| 42 | + const res = await api.fetch('/api/v1/logs/insights', { |
| 43 | + method: 'POST', |
| 44 | + query: { |
| 45 | + env: 'dev', |
| 46 | + // @ts-expect-error on purpose |
| 47 | + foo: 'bar' |
| 48 | + }, |
| 49 | + token: env.secret_key, |
| 50 | + body: { |
| 51 | + // @ts-expect-error on purpose |
| 52 | + type: 'foobar' |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + expect(res.json).toStrictEqual<typeof res.json>({ |
| 57 | + error: { |
| 58 | + code: 'invalid_query_params', |
| 59 | + errors: [ |
| 60 | + { |
| 61 | + code: 'unrecognized_keys', |
| 62 | + message: "Unrecognized key(s) in object: 'foo'", |
| 63 | + path: [] |
| 64 | + } |
| 65 | + ] |
| 66 | + } |
| 67 | + }); |
| 68 | + expect(res.res.status).toBe(400); |
| 69 | + }); |
| 70 | + |
| 71 | + it('should get empty result', async () => { |
| 72 | + const { env } = await seeders.seedAccountEnvAndUser(); |
| 73 | + const res = await api.fetch('/api/v1/logs/insights', { |
| 74 | + method: 'POST', |
| 75 | + query: { env: 'dev' }, |
| 76 | + token: env.secret_key, |
| 77 | + body: { type: 'sync' } |
| 78 | + }); |
| 79 | + |
| 80 | + isSuccess(res.json); |
| 81 | + expect(res.res.status).toBe(200); |
| 82 | + expect(res.json).toStrictEqual<typeof res.json>({ |
| 83 | + data: { |
| 84 | + histogram: [] |
| 85 | + } |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments