Files
speckle-server/packages/server/test/hooks.js
T
Gergő Jedlicska f210d9b749 gergo/web 2109 project region based db connection selector (#3434)
* feat(projects): add project regions, default to null

* feat(multiregion): add projectRegion Db client lookup logic

* feat(multiregion): add project region repositories and caching

* feat(multiRegion): db initialization and get project db client

* feat(docker-compose): add second db for regions testing

* feat(multiRegion): initialize region with pubs and subs working

* fix(multiRegion): get region client even if it was registered in another pod

* feat(workspaces): create workspace resolver split

* feat: update server region metadata

* feat(projects): rewrite project creation

* feat(multiRegion): getRegionDb

* fix(workspaces): get projects now can retur null

* feat(multiRegion): make local multi region DB-s work

* feat: set d efault workspace region

* CR changes

* tests

* feat(multiRegion): bind region properly

* fe update

* test fixes

* feat(multiRegion): automatically create aiven extras plugin

* ci(postgres): use published postgres with aiven extras

* fix(multiRegion): roll back the aiven extras migration, there is a better way

* tests fix

* fix(billing): we do not need to add a seat, if the workspace is on a plan, but has no sub

---------

Co-authored-by: Kristaps Fabians Geikins <fabis94@live.com>
2024-11-06 17:29:08 +01:00

120 lines
3.5 KiB
JavaScript

require('../bootstrap')
// Register global mocks as early as possible
require('@/test/mocks/global')
const chai = require('chai')
const chaiAsPromised = require('chai-as-promised')
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')
const { once } = require('events')
// Register chai plugins
chai.use(chaiAsPromised)
chai.use(chaiHttp)
chai.use(deepEqualInAnyOrder)
chai.use(graphqlChaiPlugin)
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!????
// because its done in a migration, to not override existing configs
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)
if (!tableNames.length) return // Nothing to truncate
// We're deleting everything, so lets turn off triggers to avoid deadlocks/slowdowns
await knex.transaction(async (trx) => {
await trx.raw(`
-- Disable triggers and foreign key constraints for this session
SET session_replication_role = replica;
truncate table ${tableNames.join(',')};
-- Re-enable triggers and foreign key constraints
SET session_replication_role = DEFAULT;
`)
})
} else {
await knex.raw(`truncate table ${tableNames.join(',')} cascade`)
}
}
/**
* @param {import('http').Server} server
* @param {import('express').Express} app
*/
const initializeTestServer = async (server, app) => {
await startHttp(server, app, 0)
await once(app, 'appStarted')
const port = server.address().port
const serverAddress = `http://127.0.0.1:${port}`
const wsAddress = `ws://127.0.0.1:${port}`
return {
server,
serverAddress,
serverPort: port,
wsAddress,
sendRequest(auth, obj) {
return (
chai
.request(serverAddress)
.post('/graphql')
// if you set the header to null, the actual header in the req will be
// a string -> 'null'
// this is now treated as an invalid token, and gets forbidden
// switching to an empty string token
.set('Authorization', auth || '')
.send(obj)
)
}
}
}
exports.mochaHooks = {
beforeAll: async () => {
logger.info('running before all')
await unlock()
await exports.truncateTables()
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