Fix server query tests

This commit is contained in:
Andrew Kingston 2021-02-09 15:24:56 +00:00
parent 9b21eb78b6
commit 4c53e4bcec
1 changed files with 45 additions and 42 deletions

View File

@ -4,7 +4,7 @@ const {
defaultHeaders, defaultHeaders,
builderEndpointShouldBlockNormalUsers, builderEndpointShouldBlockNormalUsers,
getDocument, getDocument,
insertDocument insertDocument,
} = require("./couchTestUtils") } = require("./couchTestUtils")
let { generateDatasourceID, generateQueryID } = require("../../../db/utils") let { generateDatasourceID, generateQueryID } = require("../../../db/utils")
@ -21,11 +21,11 @@ const TEST_DATASOURCE = {
const TEST_QUERY = { const TEST_QUERY = {
_id: generateQueryID(DATASOURCE_ID), _id: generateQueryID(DATASOURCE_ID),
datasourceId: DATASOURCE_ID, datasourceId: DATASOURCE_ID,
name:"New Query", name: "New Query",
parameters:[], parameters: [],
fields:{}, fields: {},
schema:{}, schema: {},
queryVerb:"read", queryVerb: "read",
} }
describe("/queries", () => { describe("/queries", () => {
@ -37,8 +37,8 @@ describe("/queries", () => {
let query let query
beforeAll(async () => { beforeAll(async () => {
({ request, server } = await supertest()) ;({ request, server } = await supertest())
}); })
afterAll(() => { afterAll(() => {
server.close() server.close()
@ -47,7 +47,7 @@ describe("/queries", () => {
beforeEach(async () => { beforeEach(async () => {
app = await createApplication(request) app = await createApplication(request)
appId = app.instance._id appId = app.instance._id
}); })
async function createDatasource() { async function createDatasource() {
return await insertDocument(appId, TEST_DATASOURCE) return await insertDocument(appId, TEST_DATASOURCE)
@ -63,43 +63,46 @@ describe("/queries", () => {
.post(`/api/queries`) .post(`/api/queries`)
.send(TEST_QUERY) .send(TEST_QUERY)
.set(defaultHeaders(appId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect("Content-Type", /json/)
.expect(200) .expect(200)
expect(res.res.statusMessage).toEqual(`Query ${TEST_QUERY.name} saved successfully.`); expect(res.res.statusMessage).toEqual(
`Query ${TEST_QUERY.name} saved successfully.`
)
expect(res.body).toEqual({ expect(res.body).toEqual({
_rev: res.body._rev, _rev: res.body._rev,
...TEST_QUERY, ...TEST_QUERY,
});
}) })
}); })
})
describe("fetch", () => { describe("fetch", () => {
let datasource let datasource
beforeEach(async () => { beforeEach(async () => {
datasource = await createDatasource() datasource = await createDatasource()
}); })
afterEach(() => { afterEach(() => {
delete datasource._rev delete datasource._rev
}); })
it("returns all the queries from the server", async () => { it("returns all the queries from the server", async () => {
const query = await createQuery() const query = await createQuery()
const res = await request const res = await request
.get(`/api/queries`) .get(`/api/queries`)
.set(defaultHeaders(appId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect("Content-Type", /json/)
.expect(200) .expect(200)
const queries = res.body; const queries = res.body
expect(queries).toEqual([ expect(queries).toEqual([
{ {
"_rev": query.rev, _rev: query.rev,
...TEST_QUERY ...TEST_QUERY,
} readable: true,
]); },
])
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
@ -110,18 +113,18 @@ describe("/queries", () => {
appId: appId, appId: appId,
}) })
}) })
}); })
describe("destroy", () => { describe("destroy", () => {
let datasource; let datasource
beforeEach(async () => { beforeEach(async () => {
datasource = await createDatasource() datasource = await createDatasource()
}); })
afterEach(() => { afterEach(() => {
delete datasource._rev delete datasource._rev
}); })
it("deletes a query and returns a success message", async () => { it("deletes a query and returns a success message", async () => {
const query = await createQuery() const query = await createQuery()
@ -134,7 +137,7 @@ describe("/queries", () => {
const res = await request const res = await request
.get(`/api/queries`) .get(`/api/queries`)
.set(defaultHeaders(appId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect("Content-Type", /json/)
.expect(200) .expect(200)
expect(res.body).toEqual([]) expect(res.body).toEqual([])
@ -148,5 +151,5 @@ describe("/queries", () => {
appId: appId, appId: appId,
}) })
}) })
}); })
}); })