mirror of
https://github.com/logto-io/logto.git
synced 2024-12-30 20:33:54 -05:00
feat(core): add message id for the webhook payload (#3913)
This commit is contained in:
parent
0edd549365
commit
79daf253a8
3 changed files with 20 additions and 8 deletions
|
@ -13,9 +13,8 @@ const { mockEsmWithActual, mockEsm } = createMockUtils(jest);
|
|||
|
||||
const nanoIdMock = 'mockId';
|
||||
await mockEsmWithActual('@logto/shared', () => ({
|
||||
// eslint-disable-next-line unicorn/consistent-function-scoping
|
||||
buildIdGenerator: () => () => nanoIdMock,
|
||||
generateStandardId: () => nanoIdMock,
|
||||
buildIdGenerator: jest.fn().mockReturnValue(nanoIdMock),
|
||||
generateStandardId: jest.fn().mockReturnValue(nanoIdMock),
|
||||
}));
|
||||
|
||||
const mockSignature = 'mockSignature';
|
||||
|
@ -53,11 +52,15 @@ const findHookById = jest.fn().mockResolvedValue(hook);
|
|||
const { createHookLibrary } = await import('./index.js');
|
||||
const { triggerInteractionHooksIfNeeded, attachExecutionStatsToHook, testHook } = createHookLibrary(
|
||||
new MockQueries({
|
||||
// @ts-expect-error
|
||||
users: { findUserById: () => ({ id: 'user_id', username: 'user', extraField: 'not_ok' }) },
|
||||
users: {
|
||||
findUserById: jest.fn().mockReturnValue({
|
||||
id: 'user_id',
|
||||
username: 'user',
|
||||
extraField: 'not_ok',
|
||||
}),
|
||||
},
|
||||
applications: {
|
||||
// @ts-expect-error
|
||||
findApplicationById: async () => ({ id: 'app_id', extraField: 'not_ok' }),
|
||||
findApplicationById: jest.fn().mockResolvedValue({ id: 'app_id', extraField: 'not_ok' }),
|
||||
},
|
||||
logs: { insertLog, getHookExecutionStatsByHookId },
|
||||
hooks: { findAllHooks, findHookById },
|
||||
|
|
|
@ -4,7 +4,7 @@ import { got } from 'got';
|
|||
|
||||
const { jest } = import.meta;
|
||||
|
||||
const { mockEsm } = createMockUtils(jest);
|
||||
const { mockEsm, mockEsmWithActual } = createMockUtils(jest);
|
||||
|
||||
const post = jest
|
||||
.spyOn(got, 'post')
|
||||
|
@ -16,6 +16,12 @@ mockEsm('#src/utils/sign.js', () => ({
|
|||
sign: () => mockSignature,
|
||||
}));
|
||||
|
||||
const mockNanoId = 'mockNanoidId';
|
||||
await mockEsmWithActual('@logto/shared', () => ({
|
||||
buildIdGenerator: jest.fn().mockReturnValue(mockNanoId),
|
||||
generateStandardId: jest.fn().mockReturnValue(mockNanoId),
|
||||
}));
|
||||
|
||||
const { generateHookTestPayload, sendWebhookRequest } = await import('./utils.js');
|
||||
|
||||
describe('sendWebhookRequest', () => {
|
||||
|
@ -41,6 +47,7 @@ describe('sendWebhookRequest', () => {
|
|||
'user-agent': 'Logto (https://logto.io/)',
|
||||
foo: 'bar',
|
||||
'logto-signature-sha-256': mockSignature,
|
||||
'logto-message-id': mockNanoId,
|
||||
},
|
||||
json: testPayload,
|
||||
retry: { limit: 3 },
|
||||
|
|
|
@ -4,6 +4,7 @@ import {
|
|||
ApplicationType,
|
||||
type HookConfig,
|
||||
} from '@logto/schemas';
|
||||
import { generateStandardId } from '@logto/shared';
|
||||
import { conditional, trySafe } from '@silverhand/essentials';
|
||||
import { got, type Response } from 'got';
|
||||
|
||||
|
@ -33,6 +34,7 @@ export const sendWebhookRequest = async ({
|
|||
'user-agent': 'Logto (https://logto.io/)',
|
||||
...headers,
|
||||
...conditional(signingKey && { 'logto-signature-sha-256': sign(signingKey, payload) }),
|
||||
'logto-message-id': generateStandardId(),
|
||||
},
|
||||
json: payload,
|
||||
retry: { limit: retries ?? 3 },
|
||||
|
|
Loading…
Reference in a new issue