databaseWebSocket.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict'
  2. import test from 'ava'
  3. import WebSocket from 'ws'
  4. import { json, getHttpServer, getWebSocketServer, connectDb } from './_test_functions'
  5. import DataController from '../../server/database/controllers/Data'
  6. // Database and WebSocket testing
  7. // Before all tests, connect to the database
  8. test.beforeEach(async t => (t.context.server = await getHttpServer()))
  9. test('Check database is working', async t => {
  10. // Connect to database
  11. await connectDb()
  12. // Add the document
  13. const testData = { AUTOMATED_TEST_DB: true, TEST_DATABASE_OBJ: { msg: 'add' } }
  14. const doc = await DataController.add(testData)
  15. t.deepEqual(doc.data, testData, json(doc))
  16. // Find the document
  17. const findDoc = await DataController.find(doc.id)
  18. t.deepEqual(findDoc.data, testData, json(findDoc))
  19. // Update the document
  20. testData.TEST_DATABASE_OBJ.msg = 'updated'
  21. const updateTo = { AUTOMATED_TEST_DB: true, newObject: 'test', newProperties: { test: true } }
  22. const docUpdated = await DataController.update(doc.id, updateTo)
  23. t.deepEqual(docUpdated.data, updateTo, json(docUpdated))
  24. // Delete the added document
  25. await t.notThrowsAsync(DataController.del(doc.id))
  26. })
  27. test('Check WebSocket server is working', async t => {
  28. // Connect to database
  29. await connectDb()
  30. // Start the server and get its ephemeral port
  31. const server = t.context.server.listen(0)
  32. const { port } = server.address()
  33. // Start the WebSocket server
  34. getWebSocketServer(server)
  35. t.timeout(15000)
  36. t.plan(11)
  37. // Start the WebSocket client
  38. const ws = new WebSocket(`ws://localhost:${port}`)
  39. await new Promise((resolve, reject) => {
  40. let sent = 0
  41. let received = 0
  42. ws.on('open', async () => {
  43. // Send data on connect
  44. ws.send(JSON.stringify({ AUTOMATED_TEST_WS: true, TEST_OBJECT: { msg: 'open' } }))
  45. t.pass()
  46. sent++
  47. })
  48. ws.on('message', async receivedData => {
  49. received++
  50. if (sent === 1) {
  51. // Send data on receive
  52. t.is('{"message":"ok"}', receivedData, json(receivedData))
  53. ws.send(JSON.stringify({ AUTOMATED_TEST_WS: true, TEST_OBJECT: { msg: 'message' } }))
  54. t.pass()
  55. sent++
  56. }
  57. else if (sent === 2) {
  58. // Send invalid JSON data
  59. t.is('{"message":"ok"}', receivedData, json(receivedData))
  60. ws.send('Not a valid JSON string')
  61. t.pass()
  62. sent++
  63. }
  64. else if (sent === 3) {
  65. // Received error from server, check it is valid JSON
  66. let obj = null
  67. t.notThrows(() => {
  68. try {
  69. obj = JSON.parse(receivedData)
  70. }
  71. catch (err) {
  72. throw new Error('Not valid JSON')
  73. }
  74. })
  75. t.truthy(obj, json(receivedData))
  76. t.is(obj.log.error, 'Invalid JSON data.', json(obj))
  77. t.truthy(obj.log.stack, json(obj))
  78. t.is(sent, received)
  79. resolve()
  80. }
  81. })
  82. ws.on('error', async err => {
  83. // Unknown WebSocket error
  84. t.fail(json(err.message))
  85. reject(err)
  86. })
  87. })
  88. // Delete every collected data during test
  89. const db = DataController.Model
  90. const found = await db.deleteMany({ 'data.AUTOMATED_TEST_WS': true })
  91. t.true(found.deletedCount >= 2)
  92. })