Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 975ad53

Browse files
committedSep 10, 2024·
🧪 test: + webhooks trigger
1 parent 203fa60 commit 975ad53

File tree

1 file changed

+92
-0
lines changed

1 file changed

+92
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { createHmac } from 'node:crypto';
2+
import { describe, expect, it } from 'vitest';
3+
4+
interface UserDataUpdatedEvent {
5+
event: string;
6+
createdAt: string;
7+
userAgent: string;
8+
ip: string;
9+
path: string;
10+
method: string;
11+
status: number;
12+
params: {
13+
userId: string;
14+
};
15+
matchedRoute: string;
16+
data: {
17+
id: string;
18+
username: string;
19+
primaryEmail: string;
20+
primaryPhone: string | null;
21+
name: string;
22+
avatar: string | null;
23+
customData: Record<string, unknown>;
24+
identities: Record<string, unknown>;
25+
lastSignInAt: number;
26+
createdAt: number;
27+
updatedAt: number;
28+
profile: Record<string, unknown>;
29+
applicationId: string;
30+
isSuspended: boolean;
31+
};
32+
hookId: string;
33+
}
34+
35+
const userDataUpdatedEvent: UserDataUpdatedEvent = {
36+
event: 'User.Data.Updated',
37+
createdAt: '2024-09-07T08:29:09.381Z',
38+
userAgent:
39+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0',
40+
ip: '223.104.76.217',
41+
path: '/users/rra41h9vmpnd',
42+
method: 'PATCH',
43+
status: 200,
44+
params: {
45+
userId: 'rra41h9vmpnd',
46+
},
47+
matchedRoute: '/users/:userId',
48+
data: {
49+
id: 'uid',
50+
username: 'test',
51+
primaryEmail: 'user@example.com',
52+
primaryPhone: null,
53+
name: 'test',
54+
avatar: null,
55+
customData: {},
56+
identities: {},
57+
lastSignInAt: 1725446291545,
58+
createdAt: 1725440405556,
59+
updatedAt: 1725697749337,
60+
profile: {},
61+
applicationId: 'appid',
62+
isSuspended: false,
63+
},
64+
hookId: 'hookId',
65+
};
66+
67+
const LOGTO_WEBHOOK_SIGNING_KEY = 'logto-signing-key';
68+
69+
// Test Logto Webhooks in Local dev, here is some tips:
70+
// - Replace the var `LOGTO_WEBHOOK_SIGNING_KEY` with the actual value in your `.env` file
71+
// - Start web request: If you want to run the test, replace `describe.skip` with `describe` below
72+
73+
describe.skip('Test Logto Webhooks in Local dev', () => {
74+
// describe('Test Logto Webhooks in Local dev', () => {
75+
it('should send a POST request with logto headers', async () => {
76+
const url = 'http://localhost:3010/api/webhooks/logto'; // 替换为目标URL
77+
const data = userDataUpdatedEvent;
78+
// Generate data signature
79+
const hmac = createHmac('sha256', LOGTO_WEBHOOK_SIGNING_KEY!);
80+
hmac.update(JSON.stringify(data));
81+
const signature = hmac.digest('hex');
82+
const response = await fetch(url, {
83+
method: 'POST',
84+
headers: {
85+
'Content-Type': 'application/json',
86+
'logto-signature-sha-256': signature,
87+
},
88+
body: JSON.stringify(data),
89+
});
90+
expect(response.status).toBe(200); // 检查响应状态
91+
});
92+
});

0 commit comments

Comments
 (0)
Please sign in to comment.