Minor test rework after improving startup, a lot of mocks weren't being used correctly.
This commit is contained in:
parent
4c1269462f
commit
a12045917e
|
@ -30,11 +30,21 @@ module FetchMock {
|
||||||
}
|
}
|
||||||
|
|
||||||
if (url.includes("/api/global")) {
|
if (url.includes("/api/global")) {
|
||||||
return json({
|
const user = {
|
||||||
email: "test@test.com",
|
email: "test@test.com",
|
||||||
_id: "us_test@test.com",
|
_id: "us_test@test.com",
|
||||||
status: "active",
|
status: "active",
|
||||||
})
|
roles: {},
|
||||||
|
builder: {
|
||||||
|
global: false,
|
||||||
|
},
|
||||||
|
admin: {
|
||||||
|
global: false,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
return url.endsWith("/users") && opts.method === "GET"
|
||||||
|
? json([user])
|
||||||
|
: json(user)
|
||||||
}
|
}
|
||||||
// mocked data based on url
|
// mocked data based on url
|
||||||
else if (url.includes("api/apps")) {
|
else if (url.includes("api/apps")) {
|
||||||
|
|
|
@ -15,30 +15,16 @@ db.init()
|
||||||
const Koa = require("koa")
|
const Koa = require("koa")
|
||||||
const destroyable = require("server-destroy")
|
const destroyable = require("server-destroy")
|
||||||
const koaBody = require("koa-body")
|
const koaBody = require("koa-body")
|
||||||
const pino = require("koa-pino-logger")
|
|
||||||
const http = require("http")
|
const http = require("http")
|
||||||
const api = require("./api")
|
const api = require("./api")
|
||||||
const eventEmitter = require("./events")
|
|
||||||
const automations = require("./automations/index")
|
const automations = require("./automations/index")
|
||||||
const Sentry = require("@sentry/node")
|
const Sentry = require("@sentry/node")
|
||||||
const fileSystem = require("./utilities/fileSystem")
|
|
||||||
const bullboard = require("./automations/bullboard")
|
|
||||||
const { logAlert } = require("@budibase/backend-core/logging")
|
const { logAlert } = require("@budibase/backend-core/logging")
|
||||||
const { pinoSettings } = require("@budibase/backend-core")
|
|
||||||
const { Thread } = require("./threads")
|
const { Thread } = require("./threads")
|
||||||
const fs = require("fs")
|
|
||||||
import redis from "./utilities/redis"
|
import redis from "./utilities/redis"
|
||||||
import * as migrations from "./migrations"
|
import { events } from "@budibase/backend-core"
|
||||||
import { events, installation, tenancy } from "@budibase/backend-core"
|
|
||||||
import {
|
|
||||||
createAdminUser,
|
|
||||||
generateApiKey,
|
|
||||||
getChecklist,
|
|
||||||
} from "./utilities/workerRequests"
|
|
||||||
import { watch } from "./watch"
|
|
||||||
import { initialise as initialiseWebsockets } from "./websocket"
|
import { initialise as initialiseWebsockets } from "./websocket"
|
||||||
import sdk from "./sdk"
|
import { startup } from "./startup"
|
||||||
import * as pro from "@budibase/pro"
|
|
||||||
|
|
||||||
const app = new Koa()
|
const app = new Koa()
|
||||||
|
|
||||||
|
@ -91,103 +77,8 @@ server.on("close", async () => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
async function initRoutes() {
|
|
||||||
app.use(pino(pinoSettings()))
|
|
||||||
|
|
||||||
if (!env.isTest()) {
|
|
||||||
const plugin = await bullboard.init()
|
|
||||||
app.use(plugin)
|
|
||||||
}
|
|
||||||
|
|
||||||
app.context.eventEmitter = eventEmitter
|
|
||||||
app.context.auth = {}
|
|
||||||
|
|
||||||
// api routes
|
|
||||||
app.use(api.router.routes())
|
|
||||||
}
|
|
||||||
|
|
||||||
async function initPro() {
|
|
||||||
await pro.init({
|
|
||||||
backups: {
|
|
||||||
processing: {
|
|
||||||
exportAppFn: sdk.backups.exportApp,
|
|
||||||
importAppFn: sdk.backups.importApp,
|
|
||||||
statsFn: sdk.backups.calculateBackupStats,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = server.listen(env.PORT || 0, async () => {
|
module.exports = server.listen(env.PORT || 0, async () => {
|
||||||
console.log(`Budibase running on ${JSON.stringify(server.address())}`)
|
await startup(app, server)
|
||||||
env._set("PORT", server.address().port)
|
|
||||||
eventEmitter.emitPort(env.PORT)
|
|
||||||
fileSystem.init()
|
|
||||||
await redis.init()
|
|
||||||
|
|
||||||
// run migrations on startup if not done via http
|
|
||||||
// not recommended in a clustered environment
|
|
||||||
if (!env.HTTP_MIGRATIONS && !env.isTest()) {
|
|
||||||
try {
|
|
||||||
await migrations.migrate()
|
|
||||||
} catch (e) {
|
|
||||||
logAlert("Error performing migrations. Exiting.", e)
|
|
||||||
shutdown()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// check and create admin user if required
|
|
||||||
if (
|
|
||||||
env.SELF_HOSTED &&
|
|
||||||
!env.MULTI_TENANCY &&
|
|
||||||
env.BB_ADMIN_USER_EMAIL &&
|
|
||||||
env.BB_ADMIN_USER_PASSWORD
|
|
||||||
) {
|
|
||||||
const checklist = await getChecklist()
|
|
||||||
if (!checklist?.adminUser?.checked) {
|
|
||||||
try {
|
|
||||||
const tenantId = tenancy.getTenantId()
|
|
||||||
const user = await createAdminUser(
|
|
||||||
env.BB_ADMIN_USER_EMAIL,
|
|
||||||
env.BB_ADMIN_USER_PASSWORD,
|
|
||||||
tenantId
|
|
||||||
)
|
|
||||||
// Need to set up an API key for automated integration tests
|
|
||||||
if (env.isTest()) {
|
|
||||||
await generateApiKey(user._id)
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(
|
|
||||||
"Admin account automatically created for",
|
|
||||||
env.BB_ADMIN_USER_EMAIL
|
|
||||||
)
|
|
||||||
} catch (e) {
|
|
||||||
logAlert("Error creating initial admin user. Exiting.", e)
|
|
||||||
shutdown()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// monitor plugin directory if required
|
|
||||||
if (
|
|
||||||
env.SELF_HOSTED &&
|
|
||||||
!env.MULTI_TENANCY &&
|
|
||||||
env.PLUGINS_DIR &&
|
|
||||||
fs.existsSync(env.PLUGINS_DIR)
|
|
||||||
) {
|
|
||||||
watch()
|
|
||||||
}
|
|
||||||
|
|
||||||
// check for version updates
|
|
||||||
await installation.checkInstallVersion()
|
|
||||||
|
|
||||||
// get the references to the queue promises, don't await as
|
|
||||||
// they will never end, unless the processing stops
|
|
||||||
let queuePromises = []
|
|
||||||
queuePromises.push(automations.init())
|
|
||||||
queuePromises.push(initPro())
|
|
||||||
// bring routes online as final step once everything ready
|
|
||||||
await initRoutes()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
const shutdown = () => {
|
const shutdown = () => {
|
||||||
|
|
|
@ -1,13 +1,11 @@
|
||||||
const TestConfig = require("../../../tests/utilities/TestConfiguration")
|
|
||||||
|
|
||||||
const syncApps = jest.fn()
|
const syncApps = jest.fn()
|
||||||
const syncRows = jest.fn()
|
const syncRows = jest.fn()
|
||||||
const syncPlugins = jest.fn()
|
const syncPlugins = jest.fn()
|
||||||
|
|
||||||
jest.mock("../usageQuotas/syncApps", () => ({ run: syncApps }) )
|
jest.mock("../usageQuotas/syncApps", () => ({ run: syncApps }) )
|
||||||
jest.mock("../usageQuotas/syncRows", () => ({ run: syncRows }) )
|
jest.mock("../usageQuotas/syncRows", () => ({ run: syncRows }) )
|
||||||
jest.mock("../usageQuotas/syncPlugins", () => ({ run: syncPlugins }) )
|
jest.mock("../usageQuotas/syncPlugins", () => ({ run: syncPlugins }) )
|
||||||
|
|
||||||
|
const TestConfig = require("../../../tests/utilities/TestConfiguration")
|
||||||
const migration = require("../syncQuotas")
|
const migration = require("../syncQuotas")
|
||||||
|
|
||||||
describe("run", () => {
|
describe("run", () => {
|
||||||
|
|
|
@ -1,11 +1,13 @@
|
||||||
|
jest.mock("@budibase/backend-core/db", () => ({
|
||||||
|
...jest.requireActual("@budibase/backend-core/db"),
|
||||||
|
createNewUserEmailView: jest.fn(),
|
||||||
|
}))
|
||||||
|
const coreDb = require("@budibase/backend-core/db")
|
||||||
const TestConfig = require("../../../tests/utilities/TestConfiguration")
|
const TestConfig = require("../../../tests/utilities/TestConfiguration")
|
||||||
const { TENANT_ID } = require("../../../tests/utilities/structures")
|
const { TENANT_ID } = require("../../../tests/utilities/structures")
|
||||||
const { getGlobalDB, doInTenant } = require("@budibase/backend-core/tenancy")
|
const { getGlobalDB, doInTenant } = require("@budibase/backend-core/tenancy")
|
||||||
|
|
||||||
// mock email view creation
|
// mock email view creation
|
||||||
const coreDb = require("@budibase/backend-core/db")
|
|
||||||
const createNewUserEmailView = jest.fn()
|
|
||||||
coreDb.createNewUserEmailView = createNewUserEmailView
|
|
||||||
|
|
||||||
const migration = require("../userEmailViewCasing")
|
const migration = require("../userEmailViewCasing")
|
||||||
|
|
||||||
|
@ -22,7 +24,7 @@ describe("run", () => {
|
||||||
await doInTenant(TENANT_ID, async () => {
|
await doInTenant(TENANT_ID, async () => {
|
||||||
const globalDb = getGlobalDB()
|
const globalDb = getGlobalDB()
|
||||||
await migration.run(globalDb)
|
await migration.run(globalDb)
|
||||||
expect(createNewUserEmailView).toHaveBeenCalledTimes(1)
|
expect(coreDb.createNewUserEmailView).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -0,0 +1,138 @@
|
||||||
|
import * as env from "./environment"
|
||||||
|
import redis from "./utilities/redis"
|
||||||
|
import {
|
||||||
|
createAdminUser,
|
||||||
|
generateApiKey,
|
||||||
|
getChecklist,
|
||||||
|
} from "./utilities/workerRequests"
|
||||||
|
import {
|
||||||
|
installation,
|
||||||
|
pinoSettings,
|
||||||
|
tenancy,
|
||||||
|
logging,
|
||||||
|
} from "@budibase/backend-core"
|
||||||
|
import fs from "fs"
|
||||||
|
import { watch } from "./watch"
|
||||||
|
import automations from "./automations"
|
||||||
|
import fileSystem from "./utilities/fileSystem"
|
||||||
|
import eventEmitter from "./events"
|
||||||
|
import * as migrations from "./migrations"
|
||||||
|
import bullboard from "./automations/bullboard"
|
||||||
|
import * as pro from "../../../../budibase-pro/packages/pro"
|
||||||
|
import api from "./api"
|
||||||
|
import sdk from "./sdk"
|
||||||
|
const pino = require("koa-pino-logger")
|
||||||
|
|
||||||
|
let STARTUP_RAN = false
|
||||||
|
|
||||||
|
async function initRoutes(app: any) {
|
||||||
|
app.use(pino(pinoSettings()))
|
||||||
|
|
||||||
|
if (!env.isTest()) {
|
||||||
|
const plugin = await bullboard.init()
|
||||||
|
app.use(plugin)
|
||||||
|
}
|
||||||
|
|
||||||
|
app.context.eventEmitter = eventEmitter
|
||||||
|
app.context.auth = {}
|
||||||
|
|
||||||
|
// api routes
|
||||||
|
app.use(api.router.routes())
|
||||||
|
}
|
||||||
|
|
||||||
|
async function initPro() {
|
||||||
|
await pro.init({
|
||||||
|
backups: {
|
||||||
|
processing: {
|
||||||
|
exportAppFn: sdk.backups.exportApp,
|
||||||
|
importAppFn: sdk.backups.importApp,
|
||||||
|
statsFn: sdk.backups.calculateBackupStats,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
function shutdown(server?: any) {
|
||||||
|
server.close()
|
||||||
|
server.destroy()
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function startup(app?: any, server?: any) {
|
||||||
|
if (STARTUP_RAN) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
STARTUP_RAN = true
|
||||||
|
if (server) {
|
||||||
|
console.log(`Budibase running on ${JSON.stringify(server.address())}`)
|
||||||
|
env._set("PORT", server.address().port)
|
||||||
|
}
|
||||||
|
eventEmitter.emitPort(env.PORT)
|
||||||
|
fileSystem.init()
|
||||||
|
await redis.init()
|
||||||
|
|
||||||
|
// run migrations on startup if not done via http
|
||||||
|
// not recommended in a clustered environment
|
||||||
|
if (!env.HTTP_MIGRATIONS && !env.isTest()) {
|
||||||
|
try {
|
||||||
|
await migrations.migrate()
|
||||||
|
} catch (e) {
|
||||||
|
logging.logAlert("Error performing migrations. Exiting.", e)
|
||||||
|
shutdown()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check and create admin user if required
|
||||||
|
if (
|
||||||
|
env.SELF_HOSTED &&
|
||||||
|
!env.MULTI_TENANCY &&
|
||||||
|
env.BB_ADMIN_USER_EMAIL &&
|
||||||
|
env.BB_ADMIN_USER_PASSWORD
|
||||||
|
) {
|
||||||
|
const checklist = await getChecklist()
|
||||||
|
if (!checklist?.adminUser?.checked) {
|
||||||
|
try {
|
||||||
|
const tenantId = tenancy.getTenantId()
|
||||||
|
const user = await createAdminUser(
|
||||||
|
env.BB_ADMIN_USER_EMAIL,
|
||||||
|
env.BB_ADMIN_USER_PASSWORD,
|
||||||
|
tenantId
|
||||||
|
)
|
||||||
|
// Need to set up an API key for automated integration tests
|
||||||
|
if (env.isTest()) {
|
||||||
|
await generateApiKey(user._id)
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(
|
||||||
|
"Admin account automatically created for",
|
||||||
|
env.BB_ADMIN_USER_EMAIL
|
||||||
|
)
|
||||||
|
} catch (e) {
|
||||||
|
logging.logAlert("Error creating initial admin user. Exiting.", e)
|
||||||
|
shutdown()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// monitor plugin directory if required
|
||||||
|
if (
|
||||||
|
env.SELF_HOSTED &&
|
||||||
|
!env.MULTI_TENANCY &&
|
||||||
|
env.PLUGINS_DIR &&
|
||||||
|
fs.existsSync(env.PLUGINS_DIR)
|
||||||
|
) {
|
||||||
|
watch()
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for version updates
|
||||||
|
await installation.checkInstallVersion()
|
||||||
|
|
||||||
|
// get the references to the queue promises, don't await as
|
||||||
|
// they will never end, unless the processing stops
|
||||||
|
let queuePromises = []
|
||||||
|
queuePromises.push(automations.init())
|
||||||
|
queuePromises.push(initPro())
|
||||||
|
if (app) {
|
||||||
|
// bring routes online as final step once everything ready
|
||||||
|
await initRoutes(app)
|
||||||
|
}
|
||||||
|
}
|
|
@ -26,6 +26,7 @@ const context = require("@budibase/backend-core/context")
|
||||||
const { generateDevInfoID, SEPARATOR } = require("@budibase/backend-core/db")
|
const { generateDevInfoID, SEPARATOR } = require("@budibase/backend-core/db")
|
||||||
const { encrypt } = require("@budibase/backend-core/encryption")
|
const { encrypt } = require("@budibase/backend-core/encryption")
|
||||||
const { DocumentType, generateUserMetadataID } = require("../../db/utils")
|
const { DocumentType, generateUserMetadataID } = require("../../db/utils")
|
||||||
|
const { startup } = require("../../startup")
|
||||||
|
|
||||||
const GLOBAL_USER_ID = "us_uuid1"
|
const GLOBAL_USER_ID = "us_uuid1"
|
||||||
const EMAIL = "babs@babs.com"
|
const EMAIL = "babs@babs.com"
|
||||||
|
@ -41,6 +42,9 @@ class TestConfiguration {
|
||||||
this.server = require("../../app")
|
this.server = require("../../app")
|
||||||
// we need the request for logging in, involves cookies, hard to fake
|
// we need the request for logging in, involves cookies, hard to fake
|
||||||
this.request = supertest(this.server)
|
this.request = supertest(this.server)
|
||||||
|
this.started = true
|
||||||
|
} else {
|
||||||
|
this.started = false
|
||||||
}
|
}
|
||||||
this.appId = null
|
this.appId = null
|
||||||
this.allApps = []
|
this.allApps = []
|
||||||
|
@ -95,6 +99,9 @@ class TestConfiguration {
|
||||||
|
|
||||||
// use a new id as the name to avoid name collisions
|
// use a new id as the name to avoid name collisions
|
||||||
async init(appName = newid()) {
|
async init(appName = newid()) {
|
||||||
|
if (!this.started) {
|
||||||
|
await startup()
|
||||||
|
}
|
||||||
this.user = await this.globalUser()
|
this.user = await this.globalUser()
|
||||||
this.globalUserId = this.user._id
|
this.globalUserId = this.user._id
|
||||||
this.userMetadataId = generateUserMetadataID(this.globalUserId)
|
this.userMetadataId = generateUserMetadataID(this.globalUserId)
|
||||||
|
|
Loading…
Reference in New Issue