Reducing the boilerplate needed around setting up a test case.

This commit is contained in:
mike12345567 2021-03-05 12:11:44 +00:00
parent 110ae15fb3
commit 0878d692c2
13 changed files with 89 additions and 135 deletions

View File

@ -8,14 +8,13 @@ const {
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
const database = new CouchDB(ctx.user.appId) const database = new CouchDB(ctx.user.appId)
const datasources = ( ctx.body = (
await database.allDocs( await database.allDocs(
getDatasourceParams(null, { getDatasourceParams(null, {
include_docs: true, include_docs: true,
}) })
) )
).rows.map(row => row.doc) ).rows.map(row => row.doc)
ctx.body = datasources
} }
exports.save = async function(ctx) { exports.save = async function(ctx) {

View File

@ -1,24 +1,17 @@
const TestConfig = require("./utilities/TestConfiguration")
const { clearAllApps, checkBuilderEndpoint } = require("./utilities/TestFunctions") const { clearAllApps, checkBuilderEndpoint } = require("./utilities/TestFunctions")
const setup = require("./utilities")
describe("/applications", () => { describe("/applications", () => {
let request let request = setup.getRequest()
let config let config = setup.getConfig()
beforeAll(async () => { afterAll(setup.afterAll)
config = new TestConfig()
request = config.request
})
beforeEach(async () => { beforeEach(async () => {
await clearAllApps() await clearAllApps()
await config.init() await config.init()
}) })
afterAll(() => {
config.end()
})
describe("create", () => { describe("create", () => {
it("returns a success message when the application is successfully created", async () => { it("returns a success message when the application is successfully created", async () => {
const res = await request const res = await request

View File

@ -1,11 +1,10 @@
const TestConfig = require("./utilities/TestConfiguration")
const { const {
checkBuilderEndpoint, checkBuilderEndpoint,
getAllTableRows, getAllTableRows,
clearAllAutomations, clearAllAutomations,
} = require("./utilities/TestFunctions") } = require("./utilities/TestFunctions")
const { basicAutomation } = require("./utilities/structures") const { basicAutomation } = require("./utilities/structures")
const { delay } = require("./utilities") const setup = require("./utilities")
const MAX_RETRIES = 4 const MAX_RETRIES = 4
@ -14,23 +13,16 @@ let TRIGGER_DEFINITIONS = {}
let LOGIC_DEFINITIONS = {} let LOGIC_DEFINITIONS = {}
describe("/automations", () => { describe("/automations", () => {
let request let request = setup.getRequest()
let config let config = setup.getConfig()
let automation let automation
beforeAll(async () => { afterAll(setup.afterAll)
config = new TestConfig()
request = config.request
})
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()
}) })
afterAll(() => {
config.end()
})
const triggerWorkflow = async automation => { const triggerWorkflow = async automation => {
return await request return await request
.post(`/api/automations/${automation._id}/trigger`) .post(`/api/automations/${automation._id}/trigger`)
@ -133,7 +125,7 @@ describe("/automations", () => {
automation.definition.trigger.inputs.tableId = table._id automation.definition.trigger.inputs.tableId = table._id
automation.definition.steps[0].inputs.row.tableId = table._id automation.definition.steps[0].inputs.row.tableId = table._id
automation = await config.createAutomation(automation) automation = await config.createAutomation(automation)
await delay(500) await setup.delay(500)
const res = await triggerWorkflow(automation) const res = await triggerWorkflow(automation)
// this looks a bit mad but we don't actually have a way to wait for a response from the automation to // this looks a bit mad but we don't actually have a way to wait for a response from the automation to
// know that it has finished all of its actions - this is currently the best way // know that it has finished all of its actions - this is currently the best way
@ -142,7 +134,7 @@ describe("/automations", () => {
for (let tries = 0; tries < MAX_RETRIES; tries++) { for (let tries = 0; tries < MAX_RETRIES; tries++) {
expect(res.body.message).toEqual(`Automation ${automation._id} has been triggered.`) expect(res.body.message).toEqual(`Automation ${automation._id} has been triggered.`)
expect(res.body.automation.name).toEqual(automation.name) expect(res.body.automation.name).toEqual(automation.name)
await delay(500) await setup.delay(500)
let elements = await getAllTableRows(config) let elements = await getAllTableRows(config)
// don't test it unless there are values to test // don't test it unless there are values to test
if (elements.length > 1) { if (elements.length > 1) {

View File

@ -1,23 +1,16 @@
let TestConfig = require("./utilities/TestConfiguration")
let { basicDatasource } = require("./utilities/structures") let { basicDatasource } = require("./utilities/structures")
let { checkBuilderEndpoint } = require("./utilities/TestFunctions") let { checkBuilderEndpoint } = require("./utilities/TestFunctions")
let setup = require("./utilities")
describe("/datasources", () => { describe("/datasources", () => {
let request let request = setup.getRequest()
let config let config = setup.getConfig()
beforeAll(async () => { afterAll(setup.afterAll)
config = new TestConfig()
request = config.request
})
afterAll(() => {
config.end()
})
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()
}); })
describe("create", () => { describe("create", () => {
it("should create a new datasource", async () => { it("should create a new datasource", async () => {

View File

@ -1,25 +1,18 @@
const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles") const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles")
const TestConfig = require("./utilities/TestConfiguration") const setup = require("./utilities")
const { basicRow } = require("./utilities/structures") const { basicRow } = require("./utilities/structures")
const HIGHER_ROLE_ID = BUILTIN_ROLE_IDS.BASIC const HIGHER_ROLE_ID = BUILTIN_ROLE_IDS.BASIC
const STD_ROLE_ID = BUILTIN_ROLE_IDS.PUBLIC const STD_ROLE_ID = BUILTIN_ROLE_IDS.PUBLIC
describe("/permission", () => { describe("/permission", () => {
let request let request = setup.getRequest()
let config = setup.getConfig()
let table let table
let perms let perms
let row let row
let config
beforeAll(async () => { afterAll(setup.afterAll)
config = new TestConfig()
request = config.request
})
afterAll(() => {
config.end()
})
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()

View File

@ -1,19 +1,12 @@
const TestConfig = require("./utilities/TestConfiguration")
const { checkBuilderEndpoint } = require("./utilities/TestFunctions") const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const { basicQuery } = require("./utilities/structures") const { basicQuery } = require("./utilities/structures")
const setup = require("./utilities")
describe("/queries", () => { describe("/queries", () => {
let request let request = setup.getRequest()
let config let config = setup.getConfig()
beforeAll(async () => { afterAll(setup.afterAll)
config = new TestConfig()
request = config.request
})
afterAll(() => {
config.end()
})
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()

View File

@ -3,20 +3,13 @@ const {
BUILTIN_PERMISSION_IDS, BUILTIN_PERMISSION_IDS,
} = require("../../../utilities/security/permissions") } = require("../../../utilities/security/permissions")
const { basicRole } = require("./utilities/structures") const { basicRole } = require("./utilities/structures")
const TestConfig = require("./utilities/TestConfiguration") const setup = require("./utilities")
describe("/roles", () => { describe("/roles", () => {
let request let request = setup.getRequest()
let config let config = setup.getConfig()
beforeAll(async () => { afterAll(setup.afterAll)
config = new TestConfig()
request = config.request
})
afterAll(() => {
config.close()
})
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()

View File

@ -1,28 +1,18 @@
const { outputProcessing } = require("../../../utilities/rowProcessor") const { outputProcessing } = require("../../../utilities/rowProcessor")
const env = require("../../../environment") const env = require("../../../environment")
const TestConfig = require("./utilities/TestConfiguration")
const { basicRow } = require("./utilities/structures") const { basicRow } = require("./utilities/structures")
const setup = require("./utilities")
describe("/rows", () => { describe("/rows", () => {
let request let request = setup.getRequest()
let appId let config = setup.getConfig()
let table let table
let row let row
let app
let config
beforeAll(async () => { afterAll(setup.afterAll)
config = new TestConfig()
request = config.request
})
afterAll(() => {
config.end()
})
beforeEach(async () => { beforeEach(async () => {
app = await config.init() await config.init()
appId = app.instance._id
table = await config.createTable() table = await config.createTable()
row = basicRow(table._id) row = basicRow(table._id)
}) })
@ -272,7 +262,7 @@ describe("/rows", () => {
link: [{_id: firstRow._id}], link: [{_id: firstRow._id}],
tableId: table._id, tableId: table._id,
}) })
const enriched = await outputProcessing(appId, table, [secondRow]) const enriched = await outputProcessing(config.getAppId(), table, [secondRow])
expect(enriched[0].link.length).toBe(1) expect(enriched[0].link.length).toBe(1)
expect(enriched[0].link[0]._id).toBe(firstRow._id) expect(enriched[0].link[0]._id).toBe(firstRow._id)
expect(enriched[0].link[0].primaryDisplay).toBe("Test Contact") expect(enriched[0].link[0].primaryDisplay).toBe("Test Contact")
@ -292,8 +282,8 @@ describe("/rows", () => {
// the environment needs configured for this // the environment needs configured for this
env.CLOUD = 1 env.CLOUD = 1
env.SELF_HOSTED = 1 env.SELF_HOSTED = 1
const enriched = await outputProcessing(appId, table, [row]) const enriched = await outputProcessing(config.getAppId(), table, [row])
expect(enriched[0].attachment[0].url).toBe(`/app-assets/assets/${appId}/test/thing`) expect(enriched[0].attachment[0].url).toBe(`/app-assets/assets/${config.getAppId()}/test/thing`)
// remove env config // remove env config
env.CLOUD = undefined env.CLOUD = undefined
env.SELF_HOSTED = undefined env.SELF_HOSTED = undefined

View File

@ -1,18 +1,11 @@
const TestConfig = require("./utilities/TestConfiguration")
const { checkBuilderEndpoint } = require("./utilities/TestFunctions") const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const setup = require("./utilities")
describe("/tables", () => { describe("/tables", () => {
let request let request = setup.getRequest()
let config let config = setup.getConfig()
beforeAll(async () => { afterAll(setup.afterAll)
config = new TestConfig()
request = config.request
})
afterAll(() => {
config.end()
})
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()

View File

@ -1,31 +1,18 @@
const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles") const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles")
const TestConfig = require("./utilities/TestConfiguration")
const { checkPermissionsEndpoint } = require("./utilities/TestFunctions") const { checkPermissionsEndpoint } = require("./utilities/TestFunctions")
const { cloneDeep } = require("lodash/fp") const { basicUser } = require("./utilities/structures")
const setup = require("./utilities")
const baseBody = {
email: "bill@bill.com",
password: "yeeooo",
roleId: BUILTIN_ROLE_IDS.POWER,
}
describe("/users", () => { describe("/users", () => {
let request let request = setup.getRequest()
let config let config = setup.getConfig()
beforeAll(async () => { afterAll(setup.afterAll)
config = new TestConfig()
request = config.request
})
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()
}) })
afterAll(() => {
config.end()
})
describe("fetch", () => { describe("fetch", () => {
it("returns a list of users from an instance db", async () => { it("returns a list of users from an instance db", async () => {
await config.createUser("brenda@brenda.com", "brendas_password") await config.createUser("brenda@brenda.com", "brendas_password")
@ -56,7 +43,7 @@ describe("/users", () => {
describe("create", () => { describe("create", () => {
it("returns a success message when a user is successfully created", async () => { it("returns a success message when a user is successfully created", async () => {
const body = cloneDeep(baseBody) const body = basicUser(BUILTIN_ROLE_IDS.POWER)
body.email = "bill@budibase.com" body.email = "bill@budibase.com"
const res = await request const res = await request
.post(`/api/users`) .post(`/api/users`)
@ -70,7 +57,7 @@ describe("/users", () => {
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
const body = cloneDeep(baseBody) const body = basicUser(BUILTIN_ROLE_IDS.POWER)
body.email = "brandNewUser@user.com" body.email = "brandNewUser@user.com"
await checkPermissionsEndpoint({ await checkPermissionsEndpoint({
config, config,

View File

@ -24,6 +24,14 @@ class TestConfiguration {
this.appId = null this.appId = null
} }
getRequest() {
return this.request
}
getAppId() {
return this.appId
}
async _req(config, params, controlFunc) { async _req(config, params, controlFunc) {
const request = {} const request = {}
// fake cookies, we don't need them // fake cookies, we don't need them

View File

@ -1,5 +1,32 @@
const TEST_CLIENT_ID = "test-client-id" const TestConfig = require("./TestConfiguration")
exports.TEST_CLIENT_ID = TEST_CLIENT_ID
exports.delay = ms => new Promise(resolve => setTimeout(resolve, ms)) exports.delay = ms => new Promise(resolve => setTimeout(resolve, ms))
let request, config
exports.beforeAll = () => {
config = new TestConfig()
request = config.getRequest()
}
exports.afterAll = () => {
if (config) {
config.end()
}
request = null
config = null
}
exports.getRequest = () => {
if (!request) {
exports.beforeAll()
}
return request
}
exports.getConfig = () => {
if (!config) {
exports.beforeAll()
}
return config
}

View File

@ -1,23 +1,16 @@
const TestConfig = require("./utilities/TestConfiguration") const setup = require("./utilities")
describe("/views", () => { describe("/views", () => {
let request let request = setup.getRequest()
let config let config = setup.getConfig()
let table let table
beforeAll(async () => { afterAll(setup.afterAll)
config = new TestConfig()
request = config.request
})
beforeEach(async () => { beforeEach(async () => {
await config.init() await config.init()
}) })
afterAll(() => {
config.end()
})
describe("create", () => { describe("create", () => {
beforeEach(async () => { beforeEach(async () => {
table = await config.createTable() table = await config.createTable()