Skip to content

Commit

Permalink
add e2e tests for GET /api/v1/sessions/@this
Browse files Browse the repository at this point in the history
  • Loading branch information
negrel committed Aug 29, 2024
1 parent c06adb5 commit 14f4937
Show file tree
Hide file tree
Showing 10 changed files with 169 additions and 16 deletions.
1 change: 1 addition & 0 deletions tests/bun/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ 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_SESSIONS_THIS_URL = PRISME_API_URL + '/sessions/@this'

export const PRISME_METRICS_URL = PRISME_ADMIN_URL + '/metrics'

Expand Down
4 changes: 0 additions & 4 deletions tests/bun/events/prisme.env
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ export PRISME_CLICKHOUSE_HOSTPORT='clickhouse.localhost:9000'
export PRISME_CLICKHOUSE_USER="clickhouse"
export PRISME_CLICKHOUSE_PASSWORD="password"

export PRISME_GRAFANA_URL="http://grafana.localhost:3000"
export PRISME_GRAFANA_USER="admin"
export PRISME_GRAFANA_PASSWORD="admin"

export PRISME_ORIGIN_REGISTRY_ORIGINS="mywebsite.localhost,foo.mywebsite.localhost"

export PRISME_EVENTSTORE_MAX_BATCH_SIZE="1"
Expand Down
29 changes: 29 additions & 0 deletions tests/bun/sessions/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
repository_root := $(shell git rev-parse --show-toplevel)
export repository_root := $(or $(repository_root), $(CURDIR))
include $(repository_root)/variables.mk

export COMPOSE_PROJECT_NAME := prisme-bun-sessions

.PHONY: start
start:
source $(CURDIR)/prisme.env && \
$(DOCKER_COMPOSE) \
-f ../docker-compose.yml \
-f ./docker-compose.env.yml \
up --wait -d

.PHONY: test
test:
$(DOCKER_COMPOSE) \
-f ./docker-compose.test.yml \
up \
--abort-on-container-exit \
--exit-code-from bun

.PHONY: clean
clean:
$(DOCKER_COMPOSE) \
-f ../docker-compose.yml \
-f ./docker-compose.env.yml \
-f ./docker-compose.test.yml \
down --volumes --remove-orphans
11 changes: 11 additions & 0 deletions tests/bun/sessions/docker-compose.env.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
services:
prisme:
image: prismelabs/analytics:dev
env_file: $PWD/prisme.env
healthcheck:
test: ["CMD", "/healthcheck"]
networks:
default:
aliases:
- prisme.localhost

7 changes: 7 additions & 0 deletions tests/bun/sessions/docker-compose.test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
services:
bun:
image: docker.io/oven/bun
command: sh -c "bun install && cd ./sessions && exec bun test"
working_dir: /data
volumes:
- ..:/data
12 changes: 12 additions & 0 deletions tests/bun/sessions/prisme.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export PRISME_MODE="ingestion"

export PRISME_CLICKHOUSE_HOSTPORT='clickhouse.localhost:9000'
export PRISME_CLICKHOUSE_USER="clickhouse"
export PRISME_CLICKHOUSE_PASSWORD="password"

export PRISME_ORIGIN_REGISTRY_ORIGINS="mywebsite.localhost,foo.mywebsite.localhost"

export PRISME_EVENTSTORE_MAX_BATCH_SIZE="1"

# Trust proxy so we can change rate limited IP address using X-Forwarded-For
export PRISME_TRUST_PROXY="true"
109 changes: 109 additions & 0 deletions tests/bun/sessions/sessions_this.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { expect, test } from 'bun:test'
import { faker } from '@faker-js/faker'
import { PRISME_PAGEVIEWS_URL, PRISME_SESSIONS_THIS_URL, PRISME_VISITOR_ID_REGEX, UUID_V7_REGEX } from '../const'

const seed = new Date().getTime()
console.log('faker seed', seed)
faker.seed(seed)

test('', async () => {
const ipAddr = '8.8.8.8'
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.3'
// Create session.
let response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
headers: {
Origin: 'http://foo.mywebsite.localhost',
'X-Forwarded-For': ipAddr,
Referer: 'http://foo.mywebsite.localhost/?utm_source=my_utm_source',
'X-Prisme-Document-Referrer': 'http://example.com/',
'User-Agent': userAgent
}
})
expect(response.status).toBe(200)

// Retrieve session.
response = await fetch(PRISME_SESSIONS_THIS_URL, {
method: 'POST',
headers: {
Origin: 'http://foo.mywebsite.localhost',
'X-Forwarded-For': ipAddr,
Referer: 'http://foo.mywebsite.localhost/',
'User-Agent': userAgent
}
})
expect(response.status).toBe(200)

let data = await response.json()
const visitorId = data.VisitorId
const sessionUuid = data.SessionUuid
expect(data).toMatchObject({
Client: {
OperatingSystem: 'Windows',
BrowserFamily: 'Chrome',
Device: 'Other',
IsBot: false
},
CountryCode: 'US',
PageUri: 'http://foo.mywebsite.localhost/?utm_source=my_utm_source',
PageviewCount: 1,
ReferrerUri: 'http://example.com/',
Utm: {
Campaign: '',
Content: '',
Medium: '',
Source: 'my_utm_source',
Term: ''
},
VisitorId: expect.stringMatching(PRISME_VISITOR_ID_REGEX),
SessionUuid: expect.stringMatching(UUID_V7_REGEX)
})

// Second page view.
response = await fetch(PRISME_PAGEVIEWS_URL, {
method: 'POST',
headers: {
Origin: 'http://foo.mywebsite.localhost',
'X-Forwarded-For': ipAddr,
'X-Prisme-Document-Referrer': 'http://foo.mywebsite.localhost/',
Referer: 'http://foo.mywebsite.localhost/bar',
'User-Agent': userAgent
}
})
expect(response.status).toBe(200)

// Retrieve session.
response = await fetch(PRISME_SESSIONS_THIS_URL, {
method: 'POST',
headers: {
Origin: 'http://foo.mywebsite.localhost',
'X-Forwarded-For': ipAddr,
Referer: 'http://foo.mywebsite.localhost',
'User-Agent': userAgent
}
})
expect(response.status).toBe(200)

data = await response.json()
expect(data).toMatchObject({
Client: {
OperatingSystem: 'Windows',
BrowserFamily: 'Chrome',
Device: 'Other',
IsBot: false
},
CountryCode: 'US',
PageUri: 'http://foo.mywebsite.localhost/?utm_source=my_utm_source',
PageviewCount: 2,
ReferrerUri: 'http://example.com/',
Utm: {
Campaign: '',
Content: '',
Medium: '',
Source: 'my_utm_source',
Term: ''
},
VisitorId: visitorId,
SessionUuid: sessionUuid
})
})
4 changes: 0 additions & 4 deletions tests/bun/trusted-proxy-custom-header/prisme.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ export PRISME_CLICKHOUSE_DB="prisme"
export PRISME_CLICKHOUSE_USER="clickhouse"
export PRISME_CLICKHOUSE_PASSWORD="password"

export PRISME_GRAFANA_URL="http://grafana.localhost:3000"
export PRISME_GRAFANA_USER="admin"
export PRISME_GRAFANA_PASSWORD="admin"

export PRISME_ORIGIN_REGISTRY_ORIGINS="mywebsite.localhost,foo.mywebsite.localhost"
4 changes: 0 additions & 4 deletions tests/bun/trusted-proxy/prisme.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,4 @@ export PRISME_CLICKHOUSE_DB="prisme"
export PRISME_CLICKHOUSE_USER="clickhouse"
export PRISME_CLICKHOUSE_PASSWORD="password"

export PRISME_GRAFANA_URL="http://grafana.localhost:3000"
export PRISME_GRAFANA_USER="admin"
export PRISME_GRAFANA_PASSWORD="admin"

export PRISME_ORIGIN_REGISTRY_ORIGINS="mywebsite.localhost,foo.mywebsite.localhost"
4 changes: 0 additions & 4 deletions tests/bun/untrusted-proxy/prisme.env
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,4 @@ export PRISME_CLICKHOUSE_DB="prisme"
export PRISME_CLICKHOUSE_USER="clickhouse"
export PRISME_CLICKHOUSE_PASSWORD="password"

export PRISME_GRAFANA_URL="http://grafana.localhost:3000"
export PRISME_GRAFANA_USER="admin"
export PRISME_GRAFANA_PASSWORD="admin"

export PRISME_ORIGIN_REGISTRY_ORIGINS="mywebsite.localhost,foo.mywebsite.localhost"

0 comments on commit 14f4937

Please sign in to comment.