-
-
Notifications
You must be signed in to change notification settings - Fork 478
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(core): sign webhook payload data
- Loading branch information
Showing
2 changed files
with
144 additions
and
49 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
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,90 @@ | ||
import { HookEvent, type HookEventPayload } from '@logto/schemas'; | ||
import { createMockUtils } from '@logto/shared/esm'; | ||
|
||
const { jest } = import.meta; | ||
const { mockEsm } = createMockUtils(jest); | ||
|
||
const mockSignature = 'mock-signature'; | ||
const { generateSignature } = mockEsm('#src/utils/signature.js', () => ({ | ||
generateSignature: jest.fn().mockReturnValue(mockSignature), | ||
})); | ||
|
||
const { createHookRequestOptions } = await import('#src/libraries/hook/utils.js'); | ||
|
||
describe('createHookRequestOptions', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should call generateSignature with correct values', () => { | ||
const signingKey = 'mock-signing-key'; | ||
const payload: HookEventPayload = { | ||
hookId: 'hookId', | ||
event: HookEvent.PostSignIn, | ||
createdAt: '123456', | ||
}; | ||
|
||
createHookRequestOptions({ signingKey, payload }); | ||
|
||
expect(generateSignature).toBeCalledWith(signingKey, payload); | ||
}); | ||
|
||
it('should create correct hook request options', () => { | ||
const signingKey = 'mock-signing-key'; | ||
const payload: HookEventPayload = { | ||
hookId: 'hookId', | ||
event: HookEvent.PostSignIn, | ||
createdAt: '123456', | ||
}; | ||
|
||
const customHeaders = { | ||
'x-custom-header': 'custom-header', | ||
'x-logto-signature-256': 'custom-signature', | ||
}; | ||
|
||
const options = createHookRequestOptions({ signingKey, payload, customHeaders }); | ||
|
||
expect(options).toEqual({ | ||
headers: { | ||
'user-agent': 'Logto (https://logto.io)', | ||
'x-custom-header': 'custom-header', | ||
'x-logto-signature-256': mockSignature, | ||
}, | ||
json: payload, | ||
retry: { limit: 3 }, | ||
timeout: { request: 10_000 }, | ||
}); | ||
}); | ||
|
||
it('ensure the x-logto-signature-256 header is not set when signingKey is not provided', () => { | ||
const signingKey = ''; | ||
const payload: HookEventPayload = { | ||
hookId: 'hookId', | ||
event: HookEvent.PostSignIn, | ||
createdAt: '123456', | ||
}; | ||
|
||
const options = createHookRequestOptions({ signingKey, payload }); | ||
|
||
expect(options).toBeTruthy(); | ||
expect(options.headers).not.toHaveProperty('x-logto-signature-256'); | ||
}); | ||
|
||
it('ensure the reserved x-logto-signature-256 header will not be overridden', () => { | ||
const signingKey = 'mock-signing-key'; | ||
const payload: HookEventPayload = { | ||
hookId: 'hookId', | ||
event: HookEvent.PostSignIn, | ||
createdAt: '123456', | ||
}; | ||
|
||
const customHeaders = { | ||
'x-logto-signature-256': 'custom-signature', | ||
}; | ||
|
||
const options = createHookRequestOptions({ signingKey, payload, customHeaders }); | ||
|
||
expect(options).toBeTruthy(); | ||
expect(options.headers).toHaveProperty('x-logto-signature-256', mockSignature); | ||
}); | ||
}); |