0
Fork 0
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:
Xiao Yijun 2023-05-29 11:37:00 +08:00 committed by GitHub
parent 0edd549365
commit 79daf253a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 8 deletions

View file

@ -13,9 +13,8 @@ const { mockEsmWithActual, mockEsm } = createMockUtils(jest);
const nanoIdMock = 'mockId'; const nanoIdMock = 'mockId';
await mockEsmWithActual('@logto/shared', () => ({ await mockEsmWithActual('@logto/shared', () => ({
// eslint-disable-next-line unicorn/consistent-function-scoping buildIdGenerator: jest.fn().mockReturnValue(nanoIdMock),
buildIdGenerator: () => () => nanoIdMock, generateStandardId: jest.fn().mockReturnValue(nanoIdMock),
generateStandardId: () => nanoIdMock,
})); }));
const mockSignature = 'mockSignature'; const mockSignature = 'mockSignature';
@ -53,11 +52,15 @@ const findHookById = jest.fn().mockResolvedValue(hook);
const { createHookLibrary } = await import('./index.js'); const { createHookLibrary } = await import('./index.js');
const { triggerInteractionHooksIfNeeded, attachExecutionStatsToHook, testHook } = createHookLibrary( const { triggerInteractionHooksIfNeeded, attachExecutionStatsToHook, testHook } = createHookLibrary(
new MockQueries({ new MockQueries({
// @ts-expect-error users: {
users: { findUserById: () => ({ id: 'user_id', username: 'user', extraField: 'not_ok' }) }, findUserById: jest.fn().mockReturnValue({
id: 'user_id',
username: 'user',
extraField: 'not_ok',
}),
},
applications: { applications: {
// @ts-expect-error findApplicationById: jest.fn().mockResolvedValue({ id: 'app_id', extraField: 'not_ok' }),
findApplicationById: async () => ({ id: 'app_id', extraField: 'not_ok' }),
}, },
logs: { insertLog, getHookExecutionStatsByHookId }, logs: { insertLog, getHookExecutionStatsByHookId },
hooks: { findAllHooks, findHookById }, hooks: { findAllHooks, findHookById },

View file

@ -4,7 +4,7 @@ import { got } from 'got';
const { jest } = import.meta; const { jest } = import.meta;
const { mockEsm } = createMockUtils(jest); const { mockEsm, mockEsmWithActual } = createMockUtils(jest);
const post = jest const post = jest
.spyOn(got, 'post') .spyOn(got, 'post')
@ -16,6 +16,12 @@ mockEsm('#src/utils/sign.js', () => ({
sign: () => mockSignature, 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'); const { generateHookTestPayload, sendWebhookRequest } = await import('./utils.js');
describe('sendWebhookRequest', () => { describe('sendWebhookRequest', () => {
@ -41,6 +47,7 @@ describe('sendWebhookRequest', () => {
'user-agent': 'Logto (https://logto.io/)', 'user-agent': 'Logto (https://logto.io/)',
foo: 'bar', foo: 'bar',
'logto-signature-sha-256': mockSignature, 'logto-signature-sha-256': mockSignature,
'logto-message-id': mockNanoId,
}, },
json: testPayload, json: testPayload,
retry: { limit: 3 }, retry: { limit: 3 },

View file

@ -4,6 +4,7 @@ import {
ApplicationType, ApplicationType,
type HookConfig, type HookConfig,
} from '@logto/schemas'; } from '@logto/schemas';
import { generateStandardId } from '@logto/shared';
import { conditional, trySafe } from '@silverhand/essentials'; import { conditional, trySafe } from '@silverhand/essentials';
import { got, type Response } from 'got'; import { got, type Response } from 'got';
@ -33,6 +34,7 @@ export const sendWebhookRequest = async ({
'user-agent': 'Logto (https://logto.io/)', 'user-agent': 'Logto (https://logto.io/)',
...headers, ...headers,
...conditional(signingKey && { 'logto-signature-sha-256': sign(signingKey, payload) }), ...conditional(signingKey && { 'logto-signature-sha-256': sign(signingKey, payload) }),
'logto-message-id': generateStandardId(),
}, },
json: payload, json: payload,
retry: { limit: retries ?? 3 }, retry: { limit: retries ?? 3 },