Skip to content

Commit

Permalink
update events handlers e2e tests and add new for noscript handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
negrel committed Jul 17, 2024
1 parent b692595 commit 6a16658
Show file tree
Hide file tree
Showing 7 changed files with 1,819 additions and 26 deletions.
3 changes: 3 additions & 0 deletions tests/bun/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ export const PRISME_ADMIN_URL = 'http://prisme.localhost:9090'

export const PRISME_API_URL = PRISME_URL + '/api/v1'
export const PRISME_PAGEVIEWS_URL = PRISME_API_URL + '/events/pageviews'
export const PRISME_NOSCRIPT_PAGEVIEWS_URL = PRISME_API_URL + '/noscript/events/pageviews'
export const PRISME_CUSTOM_EVENTS_URL = PRISME_API_URL + '/events/custom'
export const PRISME_NOSCRIPT_CUSTOM_EVENTS_URL = PRISME_API_URL + '/noscript/events/custom'
export const PRISME_IDENTIFY_EVENTS_URL = PRISME_API_URL + '/events/identify'
export const PRISME_NOSCRIPT_IDENTIFY_EVENTS_URL = PRISME_API_URL + '/noscript/events/identify'

export const PRISME_METRICS_URL = PRISME_ADMIN_URL + '/metrics'

Expand Down
24 changes: 19 additions & 5 deletions tests/bun/events/events_customs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ test('non registered domain in Origin header is rejected', async () => {
Origin: 'https://example.com',
'X-Forwarded-For': await randomIpWithSession('mywebsite.localhost'),
'X-Prisme-Referrer': 'https://example.com/foo?bar=baz#qux',
'Content-Type': 'application/json',
body: JSON.stringify({})
}
'Content-Type': 'application/json'
},
body: JSON.stringify({})
})
expect(response.status).toBe(400)
})
Expand Down Expand Up @@ -80,7 +80,21 @@ test('invalid sessionless custom event', async () => {
expect(response.status).toBe(400)
})

test('valid test cases pause', async () => {
test('malformed json body', async () => {
const response = await fetch(PRISME_CUSTOM_EVENTS_URL + '/foo', {
method: 'POST',
headers: {
Origin: 'https://mywebsite.localhost',
'X-Forwarded-For': await randomIpWithSession('mywebsite.localhost'),
'X-Prisme-Referrer': 'https://mywebsite.localhost/foo?bar=baz#qux',
'Content-Type': 'application/json'
},
body: '{"foo": "bar and foo, "num": 100' // No closing brace.
})
expect(response.status).toBe(400)
})

test('valid test cases break', async () => {
// Sleep so pageviews and identify timestamps are different for valid test
// cases.
// Without this sleep, getLatestXXX function may return rows from invalid test
Expand All @@ -93,7 +107,7 @@ test('concurrent pageview and custom events', async () => {
const ipAddr = faker.internet.ip()

await Promise.all([
// Identify events first.
// Custom events first.
fetch(PRISME_CUSTOM_EVENTS_URL + '/foo', {
method: 'POST',
headers: {
Expand Down
20 changes: 13 additions & 7 deletions tests/bun/events/events_identify.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ test('content type different than application/json is rejected', async () => {
expect(response.status).toBe(400)
})

test('valid test cases pause', async () => {
test('valid test cases break', async () => {
// Sleep so pageviews and identify timestamps are different for valid test
// cases.
// Without this sleep, getLatestXXX function may return rows from invalid test
Expand Down Expand Up @@ -117,9 +117,10 @@ test('concurrent pageview and identify events', async () => {
expect(user).toMatchObject({
// Visitor ID B is used to store user props.
visitor_id: visitorId,
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initial_session_uuid: sessionUuid,
initial_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
latest_session_uuid: sessionUuid,
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initialProperties: {},
properties: {}
})
Expand Down Expand Up @@ -147,9 +148,10 @@ test('valid identify with visitor_id only', async () => {
expect(user).toMatchObject({
// Visitor ID B is used to store user props.
visitor_id: visitorIdB,
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initial_session_uuid: expect.stringMatching(UUID_V7_REGEX),
initial_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
latest_session_uuid: expect.stringMatching(UUID_V7_REGEX),
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initialProperties: {},
properties: {}
})
Expand Down Expand Up @@ -217,9 +219,10 @@ test('multiple identify events for same visitor id with different "set" props ov
let user = await getLatestUser()
expect(user).toMatchObject({
visitor_id: visitorId,
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initial_session_uuid: expect.stringMatching(UUID_V7_REGEX),
initial_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
latest_session_uuid: expect.stringMatching(UUID_V7_REGEX),
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initialProperties: {},
properties: {
date,
Expand Down Expand Up @@ -261,9 +264,10 @@ test('multiple identify events for same visitor id with different "set" props ov
user = await getLatestUser()
expect(user).toMatchObject({
visitor_id: visitorId,
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initial_session_uuid: expect.stringMatching(UUID_V7_REGEX),
initial_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
latest_session_uuid: expect.stringMatching(UUID_V7_REGEX),
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initialProperties: {},
properties: {
date,
Expand Down Expand Up @@ -307,9 +311,10 @@ test('multiple identify events for same visitor id with different "setOnce" prop
let user = await getLatestUser()
expect(user).toMatchObject({
visitor_id: visitorId,
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initial_session_uuid: expect.stringMatching(UUID_V7_REGEX),
initial_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
latest_session_uuid: expect.stringMatching(UUID_V7_REGEX),
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initialProperties: {
date,
foo: 'bar',
Expand Down Expand Up @@ -350,9 +355,10 @@ test('multiple identify events for same visitor id with different "setOnce" prop
user = await getLatestUser()
expect(user).toMatchObject({
visitor_id: visitorId,
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initial_session_uuid: expect.stringMatching(UUID_V7_REGEX),
initial_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
latest_session_uuid: expect.stringMatching(UUID_V7_REGEX),
latest_session_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
initialProperties: {
date, // Unchanged.
foo: 'bar',
Expand Down
105 changes: 91 additions & 14 deletions tests/bun/events/events_pageviews.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,6 @@ test('non registered domain in Origin header is rejected', async () => {
expect(response.status).toBe(400)
})

test('internal traffic with no session associated is rejected', async () => {
const response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
headers: {
Origin: 'http://mywebsite.localhost',
'X-Forwarded-For': faker.internet.ip(),
// internal traffic, but no session exist, pageview should be rejected.
'X-Prisme-Document-Referrer': 'https://mywebsite.localhost/foo?bar=baz#qux'
}
})
expect(response.status).toBe(400)
})

test('robot user agent is rejected', async () => {
const response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
Expand All @@ -95,7 +82,7 @@ test('robot user agent is rejected', async () => {
expect(response.status).toBe(400)
})

test('valid test cases pause', async () => {
test('valid test cases break', async () => {
// Sleep so pageviews and identify timestamps are different for valid test
// cases.
// Without this sleep, getLatestXXX function may return rows from invalid test
Expand All @@ -104,6 +91,51 @@ test('valid test cases pause', async () => {
Bun.sleepSync(1000)
})

test('valid internal pageview traffic with no session associated', async () => {
const response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
headers: {
Origin: 'http://mywebsite.localhost',
'X-Forwarded-For': faker.internet.ip(),
Referer: 'https://mywebsite.localhost/foo?bar=baz#qux',
// internal traffic, but no session exist, pageview should be rejected.
'X-Prisme-Document-Referrer': 'https://mywebsite.localhost/foo?bar=baz#qux'
}
})
expect(response.status).toBe(200)

const data = await getLatestPageview()

expect(data).toMatchObject({
session: {
domain: 'mywebsite.localhost',
entry_path: '/foo',
exit_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
exit_path: '/foo',
operating_system: 'Other',
browser_family: 'Other',
device: 'Other',
referrer_domain: 'mywebsite.localhost',
country_code: expect.stringMatching(COUNTRY_CODE_REGEX),
visitor_id: expect.stringMatching(PRISME_VISITOR_ID_REGEX),
session_uuid: expect.stringMatching(UUID_V7_REGEX),
utm_source: '',
utm_medium: '',
utm_campaign: '',
utm_term: '',
utm_content: '',
version: 1
},
pageview: {
timestamp: expect.stringMatching(TIMESTAMP_REGEX),
domain: 'mywebsite.localhost',
path: '/foo',
visitor_id: expect.stringMatching(PRISME_VISITOR_ID_REGEX),
session_uuid: expect.stringMatching(UUID_V7_REGEX)
}
})
})

test('registered domain in Origin header and valid referrer is accepted', async () => {
const response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
Expand Down Expand Up @@ -323,6 +355,51 @@ test('valid pageview without X-Prisme-Document-Referrer', async () => {
})
})

test('valid pageview with different Referer and X-Prisme-Referrer headers defined', async () => {
const response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
headers: {
Origin: 'http://foo.mywebsite.localhost',
'X-Forwarded-For': faker.internet.ip(),
Referer: 'http://foo.mywebsite.localhost/',
// bar.mywebsite.... will be used be selected.
'X-Prisme-Referrer': 'http://bar.mywebsite.localhost/'
}
})
expect(response.status).toBe(200)

const data = await getLatestPageview()

expect(data).toMatchObject({
session: {
domain: 'bar.mywebsite.localhost',
entry_path: '/', // path contains /
exit_timestamp: expect.stringMatching(TIMESTAMP_REGEX),
exit_path: '/', // path contains /
operating_system: 'Other',
browser_family: 'Other',
device: 'Other',
referrer_domain: 'direct',
country_code: expect.stringMatching(COUNTRY_CODE_REGEX),
visitor_id: expect.stringMatching(PRISME_VISITOR_ID_REGEX),
session_uuid: expect.stringMatching(UUID_V7_REGEX),
utm_source: '',
utm_medium: '',
utm_campaign: '',
utm_term: '',
utm_content: '',
version: 1
},
pageview: {
timestamp: expect.stringMatching(TIMESTAMP_REGEX),
domain: 'bar.mywebsite.localhost',
path: '/',
visitor_id: expect.stringMatching(PRISME_VISITOR_ID_REGEX),
session_uuid: expect.stringMatching(UUID_V7_REGEX)
}
})
})

test('valid pageview without trailing slash in referrer', async () => {
const response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
Expand Down
Loading

0 comments on commit 6a16658

Please sign in to comment.