-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logs): POST /logs/search (#2063)
## Describe your changes Fixes NAN-846 > [!NOTE] > This is the proposed change for new API routes, discussed last week - **New route** `POST /api/v1/logs/search` POST because it will require a lot of params that are easier to send with a json body. - **Integration test** This is the first e2e API test. To achieve it I needed an auth method not tied to a session, so when it's in test mode we are using SecretKey auth, let me know if that's alright. - Add async wrapper to catch errors Note that it won't be useful anymore after express 5 but it's still a long way before release
- Loading branch information
1 parent
0085347
commit e08d85f
Showing
27 changed files
with
346 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,9 @@ | |
"outDir": "dist" | ||
}, | ||
"references": [ | ||
{ | ||
"path": "../types" | ||
}, | ||
{ | ||
"path": "../utils" | ||
} | ||
|
160 changes: 160 additions & 0 deletions
160
packages/server/lib/controllers/v1/logs/searchLogs.integration.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import { logContextGetter, migrateMapping } from '@nangohq/logs'; | ||
import { multipleMigrations, seeders } from '@nangohq/shared'; | ||
import { afterAll, beforeAll, describe, expect, it } from 'vitest'; | ||
import { runServer, shouldBeProtected, shouldRequireQueryEnv } from '../../../utils/tests.js'; | ||
|
||
let api: Awaited<ReturnType<typeof runServer>>; | ||
describe('GET /logs', () => { | ||
beforeAll(async () => { | ||
await multipleMigrations(); | ||
await migrateMapping(); | ||
|
||
api = await runServer(); | ||
}); | ||
afterAll(() => { | ||
api.server.close(); | ||
}); | ||
|
||
it('should be protected', async () => { | ||
const res = await api.fetch('/api/v1/logs/search', { method: 'POST', query: { env: 'dev' } }); | ||
|
||
shouldBeProtected(res); | ||
}); | ||
|
||
it('should enforce env query params', async () => { | ||
const { env } = await seeders.seedAccountEnvAndUser(); | ||
const res = await api.fetch('/api/v1/logs/search', { method: 'POST', token: env.secret_key }); | ||
|
||
shouldRequireQueryEnv(res); | ||
}); | ||
|
||
it('should validate body', async () => { | ||
const { env } = await seeders.seedAccountEnvAndUser(); | ||
const res = await api.fetch('/api/v1/logs/search', { | ||
method: 'POST', | ||
query: { env: 'dev' }, | ||
token: env.secret_key, | ||
// @ts-expect-error on purpose | ||
body: { limit: 'a', foo: 'bar' } | ||
}); | ||
|
||
expect(res.json).toStrictEqual({ | ||
error: { | ||
code: 'invalid_body', | ||
errors: [ | ||
{ | ||
code: 'invalid_type', | ||
message: 'Expected number, received string', | ||
path: ['limit'] | ||
}, | ||
{ | ||
code: 'unrecognized_keys', | ||
message: "Unrecognized key(s) in object: 'foo'", | ||
path: [] | ||
} | ||
] | ||
} | ||
}); | ||
expect(res.res.status).toBe(400); | ||
}); | ||
|
||
it('should search logs and get empty results', async () => { | ||
const { env } = await seeders.seedAccountEnvAndUser(); | ||
const res = await api.fetch('/api/v1/logs/search', { | ||
method: 'POST', | ||
query: { env: 'dev' }, | ||
token: env.secret_key, | ||
body: { limit: 10 } | ||
}); | ||
|
||
expect(res.res.status).toBe(200); | ||
expect(res.json).toStrictEqual({ | ||
data: [], | ||
pagination: { total: 0 } | ||
}); | ||
}); | ||
|
||
it('should search logs and get one result', async () => { | ||
const { env } = await seeders.seedAccountEnvAndUser(); | ||
|
||
const logCtx = await logContextGetter.create( | ||
{ message: 'test 1', operation: { type: 'auth' } }, | ||
{ account: { id: env.account_id }, environment: { id: env.id } } | ||
); | ||
await logCtx.info('test info'); | ||
await logCtx.success(); | ||
|
||
const res = await api.fetch('/api/v1/logs/search', { | ||
method: 'POST', | ||
query: { env: 'dev' }, | ||
token: env.secret_key, | ||
body: { limit: 10 } | ||
}); | ||
|
||
expect(res.res.status).toBe(200); | ||
expect(res.json).toStrictEqual<typeof res.json>({ | ||
data: [ | ||
{ | ||
accountId: env.account_id, | ||
accountName: null, | ||
code: null, | ||
configId: null, | ||
configName: null, | ||
connectionId: null, | ||
connectionName: null, | ||
createdAt: expect.toBeIsoDate(), | ||
endedAt: expect.toBeIsoDate(), | ||
environmentId: env.id, | ||
environmentName: null, | ||
error: null, | ||
id: logCtx.id, | ||
jobId: null, | ||
level: 'info', | ||
message: 'test 1', | ||
meta: null, | ||
operation: { | ||
type: 'auth' | ||
}, | ||
parentId: null, | ||
request: null, | ||
response: null, | ||
source: 'internal', | ||
startedAt: expect.toBeIsoDate(), | ||
state: 'success', | ||
syncId: null, | ||
syncName: null, | ||
title: null, | ||
type: 'log', | ||
updatedAt: expect.toBeIsoDate(), | ||
userId: null | ||
} | ||
], | ||
pagination: { total: 1 } | ||
}); | ||
}); | ||
|
||
it('should search logs and not return results from an other account', async () => { | ||
const { env } = await seeders.seedAccountEnvAndUser(); | ||
const env2 = await seeders.seedAccountEnvAndUser(); | ||
|
||
const logCtx = await logContextGetter.create( | ||
{ message: 'test 1', operation: { type: 'auth' } }, | ||
{ account: { id: env.account_id }, environment: { id: env.id } } | ||
); | ||
await logCtx.info('test info'); | ||
await logCtx.success(); | ||
|
||
const res = await api.fetch('/api/v1/logs/search', { | ||
method: 'POST', | ||
query: { env: 'dev' }, | ||
token: env2.env.secret_key, | ||
body: { limit: 10 } | ||
}); | ||
|
||
expect(res.res.status).toBe(200); | ||
expect(res.json).toStrictEqual<typeof res.json>({ | ||
data: [], | ||
pagination: { total: 0 } | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { z } from 'zod'; | ||
import { asyncWrapper } from '../../../utils/asyncWrapper.js'; | ||
import { requireEmptyQuery, zodErrorToHTTP } from '../../../utils/validation.js'; | ||
import type { SearchLogs } from '@nangohq/types'; | ||
import { model, envs } from '@nangohq/logs'; | ||
|
||
const validation = z | ||
.object({ | ||
limit: z.number().optional().default(100) | ||
}) | ||
.strict(); | ||
|
||
export const searchLogs = asyncWrapper<SearchLogs>(async (req, res) => { | ||
if (!envs.NANGO_LOGS_ENABLED) { | ||
res.status(404).send({ error: { code: 'feature_disabled' } }); | ||
return; | ||
} | ||
|
||
const emptyQuery = requireEmptyQuery(req, { withEnv: true }); | ||
if (emptyQuery) { | ||
res.status(400).send({ error: { code: 'invalid_query_params', errors: zodErrorToHTTP(emptyQuery.error) } }); | ||
return; | ||
} | ||
|
||
const val = validation.safeParse(req.body); | ||
if (!val.success) { | ||
res.status(400).send({ | ||
error: { code: 'invalid_body', errors: zodErrorToHTTP(val.error) } | ||
}); | ||
return; | ||
} | ||
|
||
const env = res.locals['environment']; | ||
const body: Required<SearchLogs['Body']> = val.data; | ||
const rawOps = await model.listOperations({ accountId: env.account_id, environmentId: env.id, limit: body.limit }); | ||
|
||
res.status(200).send({ | ||
data: rawOps.items, | ||
pagination: { total: rawOps.count } | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.