-
Notifications
You must be signed in to change notification settings - Fork 66.9k
Expand file tree
/
Copy pathmiddleware.ts
More file actions
191 lines (171 loc) · 5.01 KB
/
middleware.ts
File metadata and controls
191 lines (171 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import { describe, expect, test, vi } from 'vitest'
import { post } from '@/tests/helpers/e2etest'
import { contentTypesEnum } from '@/frame/lib/frontmatter'
describe('POST /events', () => {
vi.setConfig({ testTimeout: 60 * 1000 })
async function checkEvent(data: any) {
if (!Array.isArray(data)) {
data = [data]
}
const body = JSON.stringify(data)
const res = await post('/api/events', {
body,
headers: {
'content-type': 'application/json',
},
})
return res
}
const pageExample = {
type: 'page',
context: {
// Primitives
event_id: 'a35d7f88-3f48-4f36-ad89-5e3c8ebc3df7',
user: '703d32a8-ed0f-45f9-8d78-a913d4dc6f19',
version: '1.0.0',
created: '2020-10-02T17:12:18.620Z',
// Content information
path: '/github/docs/issues',
hostname: 'github.com',
referrer: 'https://github.com/github/docs',
title: 'Issues · github/docs',
search: '?q=is%3Aissue+is%3Aopen+example+',
href: 'https://github.com/github/docs/issues?q=is%3Aissue+is%3Aopen+example+',
path_language: 'en',
// Device information
os: 'linux',
os_version: '18.04',
browser: 'chrome',
browser_version: '85.0.4183.121',
is_headless: false,
viewport_width: 1418,
viewport_height: 501,
screen_width: 1920,
screen_height: 1080,
pixel_ratio: 2,
// Location information
timezone: -7,
user_language: 'en-US',
ip: '192.0.2.1',
user_agent:
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
},
}
const exitExample = {
type: 'exit',
context: {
// Primitives
event_id: 'a35d7f88-3f48-4f36-ad89-5e3c8ebc3df7',
user: '703d32a8-ed0f-45f9-8d78-a913d4dc6f19',
version: '1.0.0',
created: '2020-10-02T17:12:18.620Z',
// Content information
path: '/github/docs/issues',
hostname: 'github.com',
referrer: 'https://github.com/github/docs',
title: 'Issues · github/docs',
search: '?q=is%3Aissue+is%3Aopen+example+',
href: 'https://github.com/github/docs/issues?q=is%3Aissue+is%3Aopen+example+',
path_language: 'en',
// Device information
os: 'linux',
os_version: '18.04',
browser: 'chrome',
browser_version: '85.0.4183.121',
is_headless: false,
viewport_width: 1418,
viewport_height: 501,
screen_width: 1920,
screen_height: 1080,
pixel_ratio: 2,
// Location information
timezone: -7,
user_language: 'en-US',
ip: '192.0.2.1',
user_agent:
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36',
},
}
test('should record a page and exit event', async () => {
const eventQueue = [pageExample, exitExample]
const { statusCode } = await checkEvent(eventQueue)
expect(statusCode).toBe(200)
})
test('should require a type', async () => {
const { statusCode } = await checkEvent({ ...pageExample, type: undefined })
// should skip events with no type
expect(statusCode).toBe(200)
})
test('should require an event_id in uuid', async () => {
const { statusCode } = await checkEvent({
...pageExample,
context: {
...pageExample.context,
event_id: 'asdfghjkl',
},
})
expect(statusCode).toBe(400)
})
test('should require a user in uuid', async () => {
const { statusCode } = await checkEvent({
...pageExample,
context: {
...pageExample.context,
user: 'asdfghjkl',
},
})
expect(statusCode).toBe(400)
})
test('should require a version', async () => {
const { statusCode } = await checkEvent({
...pageExample,
context: {
...pageExample.context,
version: undefined,
},
})
expect(statusCode).toBe(400)
})
test('should require created timestamp', async () => {
const { statusCode } = await checkEvent({
...pageExample,
context: {
...pageExample.context,
timestamp: 1234,
},
})
expect(statusCode).toBe(400)
})
test('should not allow a honeypot token', async () => {
const { statusCode } = await checkEvent({
...pageExample,
context: {
...pageExample.context,
token: 'zxcv',
},
})
expect(statusCode).toBe(400)
})
test('should accept content_type field', async () => {
const { statusCode } = await checkEvent({
...pageExample,
context: {
...pageExample.context,
content_type: 'how-tos',
},
})
expect(statusCode).toBe(200)
})
test('should accept valid content_type values from EDI content models', async () => {
for (const contentType of contentTypesEnum) {
const { statusCode } = await checkEvent({
...pageExample,
context: {
...pageExample.context,
content_type: contentType,
},
})
expect(statusCode).toBe(200)
}
})
})