Merge branch 'feature/test-refactor' of https://github.com/Budibase/budibase into feature/test-refactor

This commit is contained in:
Martin McKeaveney 2021-03-04 16:54:50 +00:00
commit 41043ee6ff
2 changed files with 62 additions and 68 deletions

View File

@ -1,30 +1,22 @@
const { const TestConfig = require("./utilities/TestConfiguration")
createTable, const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
supertest,
createApplication,
defaultHeaders,
builderEndpointShouldBlockNormalUsers,
getDocument
} = require("./couchTestUtils")
describe("/tables", () => { describe("/tables", () => {
let request let request
let server let config
let app
let appId
beforeAll(async () => { beforeAll(async () => {
({ request, server } = await supertest()) config = new TestConfig()
}); request = config.request
})
afterAll(() => { afterAll(() => {
server.close() config.end()
}) })
beforeEach(async () => { beforeEach(async () => {
app = await createApplication(request) await config.init()
appId = app.instance._id })
});
describe("create", () => { describe("create", () => {
it("returns a success message when the table is successfully created", done => { it("returns a success message when the table is successfully created", done => {
@ -37,25 +29,25 @@ describe("/tables", () => {
name: { type: "string" } name: { type: "string" }
} }
}) })
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(async (err, res) => { .end(async (err, res) => {
expect(res.res.statusMessage).toEqual("Table TestTable saved successfully."); expect(res.res.statusMessage).toEqual("Table TestTable saved successfully.")
expect(res.body.name).toEqual("TestTable"); expect(res.body.name).toEqual("TestTable")
done(); done()
}); })
}) })
it("renames all the row fields for a table when a schema key is renamed", async () => { it("renames all the row fields for a table when a schema key is renamed", async () => {
const testTable = await createTable(request, appId); const testTable = await config.createTable()
const testRow = await request const testRow = await request
.post(`/api/${testTable._id}/rows`) .post(`/api/${testTable._id}/rows`)
.send({ .send({
name: "test" name: "test"
}) })
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -74,29 +66,28 @@ describe("/tables", () => {
updatedName: { type: "string" } updatedName: { type: "string" }
} }
}) })
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
expect(updatedTable.res.statusMessage).toEqual("Table TestTable saved successfully."); expect(updatedTable.res.statusMessage).toEqual("Table TestTable saved successfully.")
expect(updatedTable.body.name).toEqual("TestTable"); expect(updatedTable.body.name).toEqual("TestTable")
const res = await request const res = await request
.get(`/api/${testTable._id}/rows/${testRow.body._id}`) .get(`/api/${testTable._id}/rows/${testRow.body._id}`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
expect(res.body.updatedName).toEqual("test"); expect(res.body.updatedName).toEqual("test")
expect(res.body.name).toBeUndefined(); expect(res.body.name).toBeUndefined()
}); })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({ await checkBuilderEndpoint({
request, config,
method: "POST", method: "POST",
url: `/api/tables`, url: `/api/tables`,
appId: appId,
body: { body: {
name: "TestTable", name: "TestTable",
key: "name", key: "name",
@ -106,68 +97,67 @@ describe("/tables", () => {
} }
}) })
}) })
}); })
describe("fetch", () => { describe("fetch", () => {
let testTable let testTable
beforeEach(async () => { beforeEach(async () => {
testTable = await createTable(request, appId, testTable) testTable = await config.createTable(testTable)
}); })
afterEach(() => { afterEach(() => {
delete testTable._rev delete testTable._rev
}); })
it("returns all the tables for that instance in the response body", done => { it("returns all the tables for that instance in the response body", done => {
request request
.get(`/api/tables`) .get(`/api/tables`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(async (_, res) => { .end(async (_, res) => {
const fetchedTable = res.body[0]; const fetchedTable = res.body[0]
expect(fetchedTable.name).toEqual(testTable.name); expect(fetchedTable.name).toEqual(testTable.name)
expect(fetchedTable.type).toEqual("table"); expect(fetchedTable.type).toEqual("table")
done(); done()
}); })
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({ await checkBuilderEndpoint({
request, config,
method: "GET", method: "GET",
url: `/api/tables`, url: `/api/tables`,
appId: appId,
}) })
}) })
}); })
describe("destroy", () => { describe("destroy", () => {
let testTable; let testTable
beforeEach(async () => { beforeEach(async () => {
testTable = await createTable(request, appId, testTable) testTable = await config.createTable(testTable)
}); })
afterEach(() => { afterEach(() => {
delete testTable._rev delete testTable._rev
}); })
it("returns a success response when a table is deleted.", async done => { it("returns a success response when a table is deleted.", async done => {
request request
.delete(`/api/tables/${testTable._id}/${testTable._rev}`) .delete(`/api/tables/${testTable._id}/${testTable._rev}`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(async (_, res) => { .end(async (_, res) => {
expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`); expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`)
done(); done()
}); })
}) })
it("deletes linked references to the table after deletion", async done => { it("deletes linked references to the table after deletion", async done => {
const linkedTable = await createTable(request, appId, { const linkedTable = await config.createTable({
name: "LinkedTable", name: "LinkedTable",
type: "table", type: "table",
key: "name", key: "name",
@ -190,25 +180,24 @@ describe("/tables", () => {
request request
.delete(`/api/tables/${testTable._id}/${testTable._rev}`) .delete(`/api/tables/${testTable._id}/${testTable._rev}`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(async (_, res) => { .end(async (_, res) => {
expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`); expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`)
const dependentTable = await getDocument(appId, linkedTable._id) const dependentTable = await config.getTable(linkedTable._id)
expect(dependentTable.schema.TestTable).not.toBeDefined(); expect(dependentTable.schema.TestTable).not.toBeDefined()
done(); done()
}); })
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({ await checkBuilderEndpoint({
request, config,
method: "DELETE", method: "DELETE",
url: `/api/tables/${testTable._id}/${testTable._rev}`, url: `/api/tables/${testTable._id}/${testTable._rev}`,
appId: appId,
}) })
}) })
}); })
}); })

View File

@ -94,6 +94,11 @@ class TestConfiguration {
return this.updateTable(config) return this.updateTable(config)
} }
async getTable(tableId = null) {
tableId = tableId || this.table._id
return this._req(null, { id: tableId }, controllers.table.find)
}
async createLinkedTable() { async createLinkedTable() {
if (!this.table) { if (!this.table) {
throw "Must have created a table first." throw "Must have created a table first."