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: unknown) { 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) } }) })