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 af3d84a1d9
commit 67f47df718
13 changed files with 89 additions and 135 deletions

View File

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

View File

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

View File

@ -1,11 +1,10 @@
const TestConfig = require("./utilities/TestConfiguration")
const {
checkBuilderEndpoint,
getAllTableRows,
clearAllAutomations,
} = require("./utilities/TestFunctions")
const { basicAutomation } = require("./utilities/structures")
const { delay } = require("./utilities")
const setup = require("./utilities")
const MAX_RETRIES = 4
@ -14,23 +13,16 @@ let TRIGGER_DEFINITIONS = {}
let LOGIC_DEFINITIONS = {}
describe("/automations", () => {
let request
let config
let request = setup.getRequest()
let config = setup.getConfig()
let automation
beforeAll(async () => {
config = new TestConfig()
request = config.request
})
afterAll(setup.afterAll)
beforeEach(async () => {
await config.init()
})
afterAll(() => {
config.end()
})
const triggerWorkflow = async automation => {
return await request
.post(`/api/automations/${automation._id}/trigger`)
@ -133,7 +125,7 @@ describe("/automations", () => {
automation.definition.trigger.inputs.tableId = table._id
automation.definition.steps[0].inputs.row.tableId = table._id
automation = await config.createAutomation(automation)
await delay(500)
await setup.delay(500)
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
// 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++) {
expect(res.body.message).toEqual(`Automation ${automation._id} has been triggered.`)
expect(res.body.automation.name).toEqual(automation.name)
await delay(500)
await setup.delay(500)
let elements = await getAllTableRows(config)
// don't test it unless there are values to test
if (elements.length > 1) {

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -1,5 +1,32 @@
const TEST_CLIENT_ID = "test-client-id"
exports.TEST_CLIENT_ID = TEST_CLIENT_ID
const TestConfig = require("./TestConfiguration")
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", () => {
let request
let config
let request = setup.getRequest()
let config = setup.getConfig()
let table
beforeAll(async () => {
config = new TestConfig()
request = config.request
})
afterAll(setup.afterAll)
beforeEach(async () => {
await config.init()
})
afterAll(() => {
config.end()
})
describe("create", () => {
beforeEach(async () => {
table = await config.createTable()