Skip to content

Commit

Permalink
test: add tests for custom and set_client events
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jun 27, 2021
1 parent 84ddc28 commit 6cb99a8
Showing 1 changed file with 76 additions and 20 deletions.
96 changes: 76 additions & 20 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import test from 'ava'
import { v4 } from 'uuid'
import dayjs from 'dayjs'

import * as Signing from '../src/signing.js'
import { getRandomPort, getGrpcClients, promisifyAll } from './helper.js'
import {
getRandomPort,
getGrpcClients,
promisifyAll,
sendEventRequest,
} from './helper.js'
import private_server from '../src/grpc.js'
import { app_open } from './seeds.js'

Expand Down Expand Up @@ -47,28 +52,12 @@ test('public api should accept first_app_open requests', async (t) => {
const { application, account_id } = t.context
const payload = [{ name: 'app_open', timestamp: Date.now(), ...app_open }]

const { build } = await import('../src/server.js')
const server = await build()
const { body, statusCode } = await server.inject({
method: 'POST',
url: '/v1/events',
headers: {
'x-socketkit-key': application.authorization_key.toString('base64'),
'x-client-id': client_id,
'x-signature': await Signing.sign(
JSON.stringify(payload),
application.application_key,
),
},
payload,
})

t.deepEqual(JSON.parse(body), {})
t.is(statusCode, 200)
await sendEventRequest(application, client_id, payload)

const events = await Events.findAll({
account_id,
application_id: application.application_id,
client_id,
})

t.falsy(events.cursor)
Expand All @@ -79,3 +68,70 @@ test('public api should accept first_app_open requests', async (t) => {
t.is(events.rows[0].title, 'first_app_open')
t.is(events.rows[0].client_id, client_id)
})

test('public api should accept set_client requests', async (t) => {
const client_id = v4()
const { application, account_id } = t.context
const payload = [
{
name: 'app_open',
timestamp: dayjs().subtract(1, 'days').unix() * 1000,
...app_open,
},
]

await sendEventRequest(application, client_id, payload)

const Clients = promisifyAll(t.context.clients.Clients)
const client = await Clients.findOne({
account_id,
application_id: application.application_id,
client_id,
})

t.is(client.row.account_id, account_id)
t.is(client.row.application_id, application.application_id)
t.is(client.row.client_id, client_id)

await sendEventRequest(application, client_id, [
{
name: 'set_client',
timestamp: dayjs().subtract(3, 'days').unix() * 1000,
distinct_id: 'hello-world-from-integration-tests',
},
])

const updated_client = await Clients.findOne({
account_id,
application_id: application.application_id,
client_id,
})

t.is(updated_client.row.account_id, account_id)
t.is(updated_client.row.application_id, application.application_id)
t.is(updated_client.row.client_id, client_id)
t.is(updated_client.row.distinct_id, 'hello-world-from-integration-tests')
})

test('public api should accept custom requests', async (t) => {
const client_id = v4()
const { application, account_id } = t.context
const payload = [
{
name: 'app_open',
timestamp: dayjs().subtract(1, 'days').unix() * 1000,
...app_open,
},
{
name: 'say_hi',
timestamp: dayjs().subtract(12, 'hours').unix() * 1000,
message: 'hello from the other side',
},
]

await sendEventRequest(application, client_id, payload)

const Events = promisifyAll(t.context.clients.Events)
const events = await Events.findAll({ account_id, client_id })
t.truthy(events.rows.find((r) => r.title === 'say_hi'))
})

0 comments on commit 6cb99a8

Please sign in to comment.