Browse Source

Added dataCollect route tests

rigwild 1 year ago
parent
commit
23aed153fc
4 changed files with 53 additions and 11 deletions
  1. 3 2
      server/functions.js
  2. 5 3
      server/routes/dataCollect.js
  3. 4 6
      test/api/_test_functions.js
  4. 41 0
      test/api/dataCollect.js

+ 3 - 2
server/functions.js

@@ -3,7 +3,7 @@
 import { promises as fs } from 'fs'
 import path from 'path'
 import boom from '@hapi/boom'
-import { logger, imagesPath, fileNameConvention, sceneFileNameBlackList } from '../config'
+import { logger, imagesPath, fileNameConvention, sceneFileNameBlackList, TEST_MODE } from '../config'
 
 /**
  * Call the error handler if a middleware function throw an error
@@ -35,7 +35,8 @@ export const errorHandler = (err, req, res, next) => {
   // Pass the error to the logging handler
   let errorLogged = new Error(`Error ${payload.statusCode} - ${payload.error} - Message :\n${payload.message}`)
   errorLogged.stack = err.stack
-  logger.error(formatError(errorLogged, err.data))
+
+  if (!TEST_MODE) logger.error(formatError(errorLogged, err.data))
 
   // Send the error to the client
   res.status(payload.statusCode).json({

+ 5 - 3
server/routes/dataCollect.js

@@ -4,6 +4,7 @@ import express from 'express'
 import boom from '@hapi/boom'
 import userAgentParser from 'ua-parser-js'
 
+import { TEST_MODE } from '../../config'
 import { COLLECT_DATA } from '../../config.messagesId'
 import DataController from '../database/controllers/Data'
 import { asyncMiddleware, checkRequiredParameters } from '../functions'
@@ -58,7 +59,7 @@ router.post('/', asyncMiddleware(async (req, res) => {
   if (typeof b.uuid !== 'string')
     errorList.push('"uuid" must be a string.')
 
-  if (!Object.isObject(b.screen) || Object.keys(b.screen).length > 30)
+  if (typeof b.screen !== 'object' || Object.keys(b.screen).length > 30)
     errorList.push('"screen" must be a valid object.')
 
   // Check there is no errors with parameters
@@ -78,8 +79,9 @@ router.post('/', asyncMiddleware(async (req, res) => {
     }
   }
 
-  await DataController.add(data)
-  res.send('OK')
+  if (!TEST_MODE) await DataController.add(data)
+
+  res.send({ message: 'OK' })
 }))
 
 export default router

+ 4 - 6
test/api/_test_functions.js

@@ -2,11 +2,13 @@
 
 import path from 'path'
 import express from 'express'
+import bodyParser from 'body-parser'
 import WebSocket from 'ws'
 import serveStatic from 'serve-static'
 import routes from '../../server/routes'
 import { apiPrefix, imageServedUrl, imagesPath } from '../../config'
 import connectDb from '../../server/database'
+import { errorHandler } from '../../server/functions'
 import { errorHandler as wsErrorHandler } from '../../server/webSocket'
 import wsMessageHandler from '../../server/webSocket/messageHandler'
 
@@ -29,14 +31,10 @@ export const json = obj => 'JSON DATA : ' + (JSON.stringify(obj, null, 2) || obj
  */
 export const getHttpServer = () => {
   const app = express()
+  app.use(bodyParser.json())
   app.use(imageServedUrl, serveStatic(imagesPath))
   app.use(apiPrefix, routes)
-  app.use((err, req, res, next) => {
-    res.status(err.output.payload.statusCode).json({
-      message: err.message || err.output.payload.message,
-      data: err.data || undefined
-    })
-  })
+  app.use(errorHandler)
   return app
 }
 

+ 41 - 0
test/api/dataCollect.js

@@ -0,0 +1,41 @@
+'use strict'
+
+import test from 'ava'
+import request from 'supertest'
+import { apiPrefix } from '../../config'
+import { json, getHttpServer } from './_test_functions'
+
+// ROUTE /dataCollect
+
+// Before each tests, start a server
+test.beforeEach(async t => (t.context.server = await getHttpServer()))
+
+test('POST /dataCollect - No body', async t => {
+  const res = await request(t.context.server)
+    .post(`${apiPrefix}/dataCollect`)
+
+  t.is(res.status, 400, json(res))
+  t.true(res.body.message.includes('Missing parameter'), json(res.body))
+  t.true(res.body.message.includes('uuid'), json(res.body))
+  t.true(res.body.message.includes('screen'), json(res.body))
+})
+
+test('POST /dataCollect - Invalid body parameters', async t => {
+  const res = await request(t.context.server)
+    .post(`${apiPrefix}/dataCollect`)
+    .send({ uuid: 42, screen: 'not an object' })
+
+  t.is(res.status, 400, json(res))
+  t.true(res.body.message.includes('Invalid body parameter'), json(res.body))
+  t.truthy(res.body.data.find(x => x.includes('"uuid" must be a string.')), json(res.body))
+  t.truthy(res.body.data.find(x => x.includes('"screen" must be a valid object.')), json(res.body))
+})
+
+test('POST /dataCollect - Valid body parameters', async t => {
+  const res = await request(t.context.server)
+    .post(`${apiPrefix}/dataCollect`)
+    .send({ uuid: 'test', screen: { width: 1920, height: 1080 } })
+
+  t.is(res.status, 200, json(res))
+  t.is(res.body.message, 'OK', json(res.body))
+})