dataCollect.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. 'use strict'
  2. import test from 'ava'
  3. import request from 'supertest'
  4. import { apiPrefix } from '../../../config'
  5. import { json, getHttpServer } from '../../utils/_test_functions'
  6. // ROUTE /dataCollect
  7. // Before each tests, start a server
  8. test.beforeEach(async t => (t.context.server = await getHttpServer()))
  9. test('POST /dataCollect - No body', async t => {
  10. const res = await request(t.context.server)
  11. .post(`${apiPrefix}/dataCollect`)
  12. t.is(res.status, 400, json(res))
  13. t.true(res.body.message.includes('Missing parameter'), json(res.body))
  14. t.true(res.body.message.includes('uuid'), json(res.body))
  15. t.true(res.body.message.includes('userId'), json(res.body))
  16. t.true(res.body.message.includes('experimentId'), json(res.body))
  17. t.true(res.body.message.includes('screen'), json(res.body))
  18. })
  19. test('POST /dataCollect - Invalid body parameters', async t => {
  20. const res = await request(t.context.server)
  21. .post(`${apiPrefix}/dataCollect`)
  22. .send({ uuid: 42, userId: 42, experimentId: 42, screen: 'not an object' })
  23. t.is(res.status, 400, json(res))
  24. t.true(res.body.message.includes('Invalid body parameter'), json(res.body))
  25. t.truthy(res.body.data.find(x => x.includes('"uuid" must be a string.')), json(res.body))
  26. t.truthy(res.body.data.find(x => x.includes('"userId" must be a string.')), json(res.body))
  27. t.truthy(res.body.data.find(x => x.includes('"experimentId" must be a string.')), json(res.body))
  28. t.truthy(res.body.data.find(x => x.includes('"screen" must be a valid object.')), json(res.body))
  29. })
  30. test('POST /dataCollect - Valid body parameters', async t => {
  31. const res = await request(t.context.server)
  32. .post(`${apiPrefix}/dataCollect`)
  33. .send({ uuid: 'test', userId: 'user-test', experimentId: 'expe-test', screen: { width: 1920, height: 1080 } })
  34. t.is(res.status, 200, json(res))
  35. t.is(res.body.message, 'OK', json(res.body))
  36. })