|
|
|
@ -60,6 +60,26 @@ class TestConfiguration { |
|
|
|
return this.prodAppId |
|
|
|
} |
|
|
|
|
|
|
|
// SETUP / TEARDOWN
|
|
|
|
|
|
|
|
// use a new id as the name to avoid name collisions
|
|
|
|
async init(appName = newid()) { |
|
|
|
await this.globalUser() |
|
|
|
return this.createApp(appName) |
|
|
|
} |
|
|
|
|
|
|
|
end() { |
|
|
|
if (!this) { |
|
|
|
return |
|
|
|
} |
|
|
|
if (this.server) { |
|
|
|
this.server.close() |
|
|
|
} |
|
|
|
cleanup(this.allApps.map(app => app.appId)) |
|
|
|
} |
|
|
|
|
|
|
|
// UTILS
|
|
|
|
|
|
|
|
async _req(config, params, controlFunc) { |
|
|
|
const request = {} |
|
|
|
// fake cookies, we don't need them
|
|
|
|
@ -88,19 +108,7 @@ class TestConfiguration { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async generateApiKey(userId = GLOBAL_USER_ID) { |
|
|
|
const db = getGlobalDB(TENANT_ID) |
|
|
|
const id = generateDevInfoID(userId) |
|
|
|
let devInfo |
|
|
|
try { |
|
|
|
devInfo = await db.get(id) |
|
|
|
} catch (err) { |
|
|
|
devInfo = { _id: id, userId } |
|
|
|
} |
|
|
|
devInfo.apiKey = encrypt(`${TENANT_ID}${SEPARATOR}${newid()}`) |
|
|
|
await db.put(devInfo) |
|
|
|
return devInfo.apiKey |
|
|
|
} |
|
|
|
// USER / AUTH
|
|
|
|
|
|
|
|
async globalUser({ |
|
|
|
id = GLOBAL_USER_ID, |
|
|
|
@ -138,20 +146,59 @@ class TestConfiguration { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
// use a new id as the name to avoid name collisions
|
|
|
|
async init(appName = newid()) { |
|
|
|
await this.globalUser() |
|
|
|
return this.createApp(appName) |
|
|
|
async createUser(id = null, email = EMAIL) { |
|
|
|
const globalId = !id ? `us_${Math.random()}` : `us_${id}` |
|
|
|
const resp = await this.globalUser({ id: globalId, email }) |
|
|
|
await userCache.invalidateUser(globalId) |
|
|
|
return { |
|
|
|
...resp, |
|
|
|
globalId, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
end() { |
|
|
|
if (!this) { |
|
|
|
return |
|
|
|
} |
|
|
|
if (this.server) { |
|
|
|
this.server.close() |
|
|
|
} |
|
|
|
cleanup(this.allApps.map(app => app.appId)) |
|
|
|
async login({ roleId, userId, builder, prodApp = false } = {}) { |
|
|
|
const appId = prodApp ? this.prodAppId : this.appId |
|
|
|
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, |
|
|
|
}) |
|
|
|
// 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, |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
defaultHeaders(extras = {}) { |
|
|
|
@ -202,6 +249,24 @@ class TestConfiguration { |
|
|
|
return this.login({ email, roleId, builder, prodApp }) |
|
|
|
} |
|
|
|
|
|
|
|
// API
|
|
|
|
|
|
|
|
async generateApiKey(userId = GLOBAL_USER_ID) { |
|
|
|
const db = getGlobalDB(TENANT_ID) |
|
|
|
const id = generateDevInfoID(userId) |
|
|
|
let devInfo |
|
|
|
try { |
|
|
|
devInfo = await db.get(id) |
|
|
|
} catch (err) { |
|
|
|
devInfo = { _id: id, userId } |
|
|
|
} |
|
|
|
devInfo.apiKey = encrypt(`${TENANT_ID}${SEPARATOR}${newid()}`) |
|
|
|
await db.put(devInfo) |
|
|
|
return devInfo.apiKey |
|
|
|
} |
|
|
|
|
|
|
|
// APP
|
|
|
|
|
|
|
|
async createApp(appName) { |
|
|
|
// create dev app
|
|
|
|
this.app = await this._req({ name: appName }, null, controllers.app.create) |
|
|
|
@ -231,6 +296,8 @@ class TestConfiguration { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// TABLE
|
|
|
|
|
|
|
|
async updateTable(config = null) { |
|
|
|
config = config || basicTable() |
|
|
|
this.table = await this._req(config, null, controllers.table.save) |
|
|
|
@ -279,6 +346,8 @@ class TestConfiguration { |
|
|
|
return this.createTable(table) |
|
|
|
} |
|
|
|
|
|
|
|
// ROW
|
|
|
|
|
|
|
|
async createRow(config = null) { |
|
|
|
if (!this.table) { |
|
|
|
throw "Test requires table to be configured." |
|
|
|
@ -299,6 +368,8 @@ class TestConfiguration { |
|
|
|
return this._req(null, { tableId }, controllers.row.fetch) |
|
|
|
} |
|
|
|
|
|
|
|
// ROLE
|
|
|
|
|
|
|
|
async createRole(config = null) { |
|
|
|
config = config || basicRole() |
|
|
|
return this._req(config, null, controllers.role.save) |
|
|
|
@ -316,6 +387,8 @@ class TestConfiguration { |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
// VIEW
|
|
|
|
|
|
|
|
async createView(config) { |
|
|
|
if (!this.table) { |
|
|
|
throw "Test requires table to be configured." |
|
|
|
@ -328,6 +401,8 @@ class TestConfiguration { |
|
|
|
return this._req(view, null, controllers.view.save) |
|
|
|
} |
|
|
|
|
|
|
|
// AUTOMATION
|
|
|
|
|
|
|
|
async createAutomation(config) { |
|
|
|
config = config || basicAutomation() |
|
|
|
if (config._rev) { |
|
|
|
@ -355,6 +430,16 @@ class TestConfiguration { |
|
|
|
) |
|
|
|
} |
|
|
|
|
|
|
|
async createWebhook(config = null) { |
|
|
|
if (!this.automation) { |
|
|
|
throw "Must create an automation before creating webhook." |
|
|
|
} |
|
|
|
config = config || basicWebhook(this.automation._id) |
|
|
|
return (await this._req(config, null, controllers.webhook.save)).webhook |
|
|
|
} |
|
|
|
|
|
|
|
// DATASOURCE
|
|
|
|
|
|
|
|
async createDatasource(config = null) { |
|
|
|
config = config || basicDatasource() |
|
|
|
const response = await this._req(config, null, controllers.datasource.save) |
|
|
|
@ -405,6 +490,8 @@ class TestConfiguration { |
|
|
|
return { datasource, query: basedOnQuery } |
|
|
|
} |
|
|
|
|
|
|
|
// QUERY
|
|
|
|
|
|
|
|
async previewQuery(request, config, datasource, fields) { |
|
|
|
return request |
|
|
|
.post(`/api/queries/preview`) |
|
|
|
@ -428,78 +515,19 @@ class TestConfiguration { |
|
|
|
return this._req(config, null, controllers.query.save) |
|
|
|
} |
|
|
|
|
|
|
|
// SCREEN
|
|
|
|
|
|
|
|
async createScreen(config = null) { |
|
|
|
config = config || basicScreen() |
|
|
|
return this._req(config, null, controllers.screen.save) |
|
|
|
} |
|
|
|
|
|
|
|
async createWebhook(config = null) { |
|
|
|
if (!this.automation) { |
|
|
|
throw "Must create an automation before creating webhook." |
|
|
|
} |
|
|
|
config = config || basicWebhook(this.automation._id) |
|
|
|
return (await this._req(config, null, controllers.webhook.save)).webhook |
|
|
|
} |
|
|
|
// LAYOUT
|
|
|
|
|
|
|
|
async createLayout(config = null) { |
|
|
|
config = config || basicLayout() |
|
|
|
return await this._req(config, null, controllers.layout.save) |
|
|
|
} |
|
|
|
|
|
|
|
async createUser(id = null, email = EMAIL) { |
|
|
|
const globalId = !id ? `us_${Math.random()}` : `us_${id}` |
|
|
|
const resp = await this.globalUser({ id: globalId, email }) |
|
|
|
await userCache.invalidateUser(globalId) |
|
|
|
return { |
|
|
|
...resp, |
|
|
|
globalId, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
async login({ roleId, userId, builder, prodApp = false } = {}) { |
|
|
|
const appId = prodApp ? this.prodAppId : this.appId |
|
|
|
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, |
|
|
|
}) |
|
|
|
// 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, |
|
|
|
} |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
module.exports = TestConfiguration |
|
|
|
|