84cb74e8b3
* each log line is a json object * structured logging allows logs to be ingested by machines and the logs to be indexed and queried addresses #1105 * structured logging allows arbitrary properties to be appended to each log line, and ingestion of logs to remain robust * Structured logging provided by `pino` library * Add `express-pino-logger` dependency * Remove `debug`, `morgan`, and `morgan-debug` and replace with structured logging * `console.log` & `console.error` replaced with structured logging in backend * Remove `DEBUG` environment variable and replace with `LOG_LEVEL` - Note that there is a test which reads from a logged line on `stdout`. This is not robust, it would be better to use the childProcess.pid to look up the port number. * Log errors at points we explicitly send error to Sentry * Amend indentation of a couple of log messages to align indentation with others
99 lines
2.5 KiB
JavaScript
99 lines
2.5 KiB
JavaScript
require('../bootstrap')
|
|
const chai = require('chai')
|
|
const chaiHttp = require('chai-http')
|
|
const deepEqualInAnyOrder = require('deep-equal-in-any-order')
|
|
const knex = require(`@/db/knex`)
|
|
const { init, startHttp, shutdown } = require(`@/app`)
|
|
const { default: graphqlChaiPlugin } = require('@/test/plugins/graphql')
|
|
const { logger } = require('@/logging/logging')
|
|
|
|
// Register chai plugins
|
|
chai.use(chaiHttp)
|
|
chai.use(deepEqualInAnyOrder)
|
|
chai.use(graphqlChaiPlugin)
|
|
|
|
// Register global mocks
|
|
require('@/test/mocks/global')
|
|
|
|
const unlock = async () => {
|
|
const exists = await knex.schema.hasTable('knex_migrations_lock')
|
|
if (exists) {
|
|
await knex('knex_migrations_lock').update('is_locked', '0')
|
|
}
|
|
}
|
|
|
|
exports.truncateTables = async (tableNames) => {
|
|
if (!tableNames?.length) {
|
|
//why is server config only created once!????
|
|
const protectedTables = ['server_config']
|
|
// const protectedTables = [ 'server_config', 'user_roles', 'scopes', 'server_acl' ]
|
|
tableNames = (
|
|
await knex('pg_tables')
|
|
.select('tablename')
|
|
.where({ schemaname: 'public' })
|
|
.whereRaw("tablename not like '%knex%'")
|
|
.whereNotIn('tablename', protectedTables)
|
|
).map((table) => table.tablename)
|
|
}
|
|
|
|
await knex.raw(`truncate table ${tableNames.join(',')} cascade`)
|
|
}
|
|
|
|
/**
|
|
* @param {import('http').Server} server
|
|
* @param {import('express').Express} app
|
|
*/
|
|
const initializeTestServer = async (server, app) => {
|
|
let serverAddress
|
|
let wsAddress
|
|
await startHttp(server, app, 0)
|
|
|
|
app.on('appStarted', () => {
|
|
const port = server.address().port
|
|
serverAddress = `http://localhost:${port}`
|
|
wsAddress = `ws://localhost:${port}`
|
|
})
|
|
while (!serverAddress) {
|
|
await new Promise((resolve) => setTimeout(resolve, 100))
|
|
}
|
|
return {
|
|
server,
|
|
serverAddress,
|
|
wsAddress,
|
|
sendRequest(auth, obj) {
|
|
return chai
|
|
.request(serverAddress)
|
|
.post('/graphql')
|
|
.set('Authorization', auth)
|
|
.send(obj)
|
|
}
|
|
}
|
|
}
|
|
|
|
exports.mochaHooks = {
|
|
beforeAll: async () => {
|
|
logger.info('running before all')
|
|
await unlock()
|
|
await knex.migrate.rollback()
|
|
await knex.migrate.latest()
|
|
await init()
|
|
},
|
|
afterAll: async () => {
|
|
logger.info('running after all')
|
|
await unlock()
|
|
await shutdown()
|
|
}
|
|
}
|
|
|
|
exports.buildApp = async () => {
|
|
const { app, graphqlServer, server } = await init()
|
|
return { app, graphqlServer, server }
|
|
}
|
|
|
|
exports.beforeEachContext = async () => {
|
|
await exports.truncateTables()
|
|
return await exports.buildApp()
|
|
}
|
|
|
|
exports.initializeTestServer = initializeTestServer
|