|
|
|
@ -1,3 +1,6 @@ |
|
|
|
const core = require("@budibase/backend-core") |
|
|
|
const CouchDB = require("../../db") |
|
|
|
core.init(CouchDB) |
|
|
|
const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles") |
|
|
|
const env = require("../../environment") |
|
|
|
const { |
|
|
|
@ -17,14 +20,11 @@ const supertest = require("supertest") |
|
|
|
const { cleanup } = require("../../utilities/fileSystem") |
|
|
|
const { Cookies, Headers } = require("@budibase/backend-core/constants") |
|
|
|
const { jwt } = require("@budibase/backend-core/auth") |
|
|
|
const core = require("@budibase/backend-core") |
|
|
|
const { getGlobalDB } = require("@budibase/backend-core/tenancy") |
|
|
|
const { createASession } = require("@budibase/backend-core/sessions") |
|
|
|
const { user: userCache } = require("@budibase/backend-core/cache") |
|
|
|
const CouchDB = require("../../db") |
|
|
|
const newid = require("../../db/newid") |
|
|
|
const context = require("@budibase/backend-core/context") |
|
|
|
core.init(CouchDB) |
|
|
|
|
|
|
|
const GLOBAL_USER_ID = "us_uuid1" |
|
|
|
const EMAIL = "babs@babs.com" |
|
|
|
@ -51,7 +51,6 @@ class TestConfiguration { |
|
|
|
} |
|
|
|
|
|
|
|
async _req(config, params, controlFunc) { |
|
|
|
context.updateAppId(this.appId) |
|
|
|
const request = {} |
|
|
|
// fake cookies, we don't need them
|
|
|
|
request.cookies = { set: () => {}, get: () => {} } |
|
|
|
@ -62,11 +61,21 @@ class TestConfiguration { |
|
|
|
request.request = { |
|
|
|
body: config, |
|
|
|
} |
|
|
|
if (params) { |
|
|
|
request.params = params |
|
|
|
async function run() { |
|
|
|
if (params) { |
|
|
|
request.params = params |
|
|
|
} |
|
|
|
await controlFunc(request) |
|
|
|
return request.body |
|
|
|
} |
|
|
|
// check if already in a context
|
|
|
|
if (context.getAppId() == null) { |
|
|
|
return context.doInAppContext(this.appId, async () => { |
|
|
|
return run() |
|
|
|
}) |
|
|
|
} else { |
|
|
|
return run() |
|
|
|
} |
|
|
|
await controlFunc(request) |
|
|
|
return request.body |
|
|
|
} |
|
|
|
|
|
|
|
async globalUser({ |
|
|
|
@ -182,12 +191,14 @@ class TestConfiguration { |
|
|
|
async deploy() { |
|
|
|
await this._req(null, null, controllers.deploy.deployApp) |
|
|
|
const prodAppId = this.getAppId().replace("_dev", "") |
|
|
|
const appPackage = await this._req( |
|
|
|
null, |
|
|
|
{ appId: prodAppId }, |
|
|
|
controllers.app.fetchAppPackage |
|
|
|
) |
|
|
|
return appPackage.application |
|
|
|
return context.doInAppContext(prodAppId, async () => { |
|
|
|
const appPackage = await this._req( |
|
|
|
null, |
|
|
|
{ appId: prodAppId }, |
|
|
|
controllers.app.fetchAppPackage |
|
|
|
) |
|
|
|
return appPackage.application |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
async updateTable(config = null) { |
|
|
|
@ -416,46 +427,47 @@ class TestConfiguration { |
|
|
|
|
|
|
|
async login({ roleId, userId, builder, prodApp = false } = {}) { |
|
|
|
const appId = prodApp ? this.prodAppId : this.appId |
|
|
|
|
|
|
|
userId = !userId ? `us_uuid1` : userId |
|
|
|
if (!this.request) { |
|
|
|
throw "Server has not been opened, cannot login." |
|
|
|
} |
|
|
|
// make sure the user exists in the global DB
|
|
|
|
if (roleId !== BUILTIN_ROLE_IDS.PUBLIC) { |
|
|
|
await this.globalUser({ |
|
|
|
userId, |
|
|
|
builder, |
|
|
|
roles: { [this.prodAppId]: roleId }, |
|
|
|
return context.doInAppContext(appId, async () => { |
|
|
|
userId = !userId ? `us_uuid1` : userId |
|
|
|
if (!this.request) { |
|
|
|
throw "Server has not been opened, cannot login." |
|
|
|
} |
|
|
|
// make sure the user exists in the global DB
|
|
|
|
if (roleId !== BUILTIN_ROLE_IDS.PUBLIC) { |
|
|
|
await this.globalUser({ |
|
|
|
id: userId, |
|
|
|
builder, |
|
|
|
roles: { [this.prodAppId]: roleId }, |
|
|
|
}) |
|
|
|
} |
|
|
|
await createASession(userId, { |
|
|
|
sessionId: "sessionid", |
|
|
|
tenantId: TENANT_ID, |
|
|
|
}) |
|
|
|
} |
|
|
|
await createASession(userId, { |
|
|
|
sessionId: "sessionid", |
|
|
|
tenantId: TENANT_ID, |
|
|
|
// have to fake this
|
|
|
|
const auth = { |
|
|
|
userId, |
|
|
|
sessionId: "sessionid", |
|
|
|
tenantId: TENANT_ID, |
|
|
|
} |
|
|
|
const app = { |
|
|
|
roleId: roleId, |
|
|
|
appId, |
|
|
|
} |
|
|
|
const authToken = jwt.sign(auth, env.JWT_SECRET) |
|
|
|
const appToken = jwt.sign(app, env.JWT_SECRET) |
|
|
|
|
|
|
|
// returning necessary request headers
|
|
|
|
await userCache.invalidateUser(userId) |
|
|
|
return { |
|
|
|
Accept: "application/json", |
|
|
|
Cookie: [ |
|
|
|
`${Cookies.Auth}=${authToken}`, |
|
|
|
`${Cookies.CurrentApp}=${appToken}`, |
|
|
|
], |
|
|
|
[Headers.APP_ID]: appId, |
|
|
|
} |
|
|
|
}) |
|
|
|
// have to fake this
|
|
|
|
const auth = { |
|
|
|
userId, |
|
|
|
sessionId: "sessionid", |
|
|
|
tenantId: TENANT_ID, |
|
|
|
} |
|
|
|
const app = { |
|
|
|
roleId: roleId, |
|
|
|
appId, |
|
|
|
} |
|
|
|
const authToken = jwt.sign(auth, env.JWT_SECRET) |
|
|
|
const appToken = jwt.sign(app, env.JWT_SECRET) |
|
|
|
|
|
|
|
// returning necessary request headers
|
|
|
|
await userCache.invalidateUser(userId) |
|
|
|
return { |
|
|
|
Accept: "application/json", |
|
|
|
Cookie: [ |
|
|
|
`${Cookies.Auth}=${authToken}`, |
|
|
|
`${Cookies.CurrentApp}=${appToken}`, |
|
|
|
], |
|
|
|
[Headers.APP_ID]: appId, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|