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 ed0ee4fe60
2 changed files with 62 additions and 68 deletions

View File

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

View File

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