2023-04-05 16:33:56 +02:00
|
|
|
import { BudibaseInternalAPI } from "../internal-api"
|
|
|
|
import { AccountInternalAPI } from "../account-api"
|
|
|
|
import { CreateAppRequest, State } from "../types"
|
|
|
|
import * as fixtures from "../internal-api/fixtures"
|
|
|
|
|
|
|
|
export default class BudibaseTestConfiguration {
|
|
|
|
// apis
|
|
|
|
internalApi: BudibaseInternalAPI
|
|
|
|
accountsApi: AccountInternalAPI
|
|
|
|
|
|
|
|
// state
|
|
|
|
state: State
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.state = {}
|
|
|
|
this.internalApi = new BudibaseInternalAPI(this.state)
|
|
|
|
this.accountsApi = new AccountInternalAPI(this.state)
|
|
|
|
}
|
|
|
|
|
|
|
|
// LIFECYCLE
|
|
|
|
|
|
|
|
async beforeAll() {
|
|
|
|
// @ts-ignore
|
|
|
|
this.state.tenantId = global.qa.tenantId
|
|
|
|
// @ts-ignore
|
2023-07-14 16:23:22 +02:00
|
|
|
this.state.email = global.qa.email
|
|
|
|
// @ts-ignore
|
2023-04-05 16:33:56 +02:00
|
|
|
this.state.cookie = global.qa.authCookie
|
|
|
|
}
|
|
|
|
|
|
|
|
async afterAll() {
|
|
|
|
// nothing yet
|
|
|
|
}
|
|
|
|
|
|
|
|
async createApp(overrides: Partial<CreateAppRequest> = {}) {
|
|
|
|
const app = await this.internalApi.apps.create(
|
|
|
|
fixtures.apps.generateApp(overrides)
|
|
|
|
)
|
|
|
|
this.state.appId = app.appId
|
|
|
|
return app
|
|
|
|
}
|
|
|
|
|
|
|
|
// AUTH
|
|
|
|
|
2023-07-14 16:03:51 +02:00
|
|
|
async doInNewState(task: any) {
|
|
|
|
return this.doWithState(task, {})
|
|
|
|
}
|
|
|
|
|
|
|
|
async doWithState(task: any, state: State) {
|
|
|
|
const original = this.state
|
|
|
|
|
|
|
|
// override the state
|
|
|
|
this.state.apiKey = state.apiKey
|
|
|
|
this.state.appId = state.appId
|
|
|
|
this.state.cookie = state.cookie
|
|
|
|
this.state.tableId = state.tableId
|
|
|
|
this.state.tenantId = state.tenantId
|
2023-07-14 16:23:22 +02:00
|
|
|
this.state.email = state.email
|
2023-07-14 16:03:51 +02:00
|
|
|
|
|
|
|
await task()
|
|
|
|
|
|
|
|
// restore the state
|
|
|
|
this.state.apiKey = original.apiKey
|
|
|
|
this.state.appId = original.appId
|
|
|
|
this.state.cookie = original.cookie
|
|
|
|
this.state.tableId = original.tableId
|
|
|
|
this.state.tenantId = original.tenantId
|
2023-07-14 16:23:22 +02:00
|
|
|
this.state.email = original.email
|
2023-07-14 16:03:51 +02:00
|
|
|
}
|
|
|
|
|
2023-04-05 16:33:56 +02:00
|
|
|
async login(email: string, password: string, tenantId?: string) {
|
|
|
|
if (!tenantId && this.state.tenantId) {
|
|
|
|
tenantId = this.state.tenantId
|
2023-07-14 15:24:01 +02:00
|
|
|
}
|
|
|
|
if (!tenantId) {
|
2023-04-05 16:33:56 +02:00
|
|
|
throw new Error("Could not determine tenant id")
|
|
|
|
}
|
|
|
|
const [res, cookie] = await this.internalApi.auth.login(
|
|
|
|
tenantId,
|
|
|
|
email,
|
|
|
|
password
|
|
|
|
)
|
|
|
|
this.state.cookie = cookie
|
|
|
|
}
|
|
|
|
}
|