Refactoring query tests.

This commit is contained in:
mike12345567 2021-03-04 10:57:41 +00:00
parent 54d12bd9e7
commit 68a5580fb9
1 changed files with 29 additions and 61 deletions

View File

@ -1,43 +1,18 @@
const { const {
supertest, supertest,
createApplication, } = require("./utilities")
defaultHeaders, const TestConfig = require("./utilities/TestConfiguration")
builderEndpointShouldBlockNormalUsers, const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
getDocument, const { basicQuery } = require("./utilities/structures")
insertDocument,
} = require("./couchTestUtils")
let { generateDatasourceID, generateQueryID } = require("../../../db/utils")
const DATASOURCE_ID = generateDatasourceID()
const TEST_DATASOURCE = {
_id: DATASOURCE_ID,
type: "datasource",
name: "Test",
source: "POSTGRES",
config: {},
type: "datasource",
}
const TEST_QUERY = {
_id: generateQueryID(DATASOURCE_ID),
datasourceId: DATASOURCE_ID,
name: "New Query",
parameters: [],
fields: {},
schema: {},
queryVerb: "read",
}
describe("/queries", () => { describe("/queries", () => {
let request let request
let server let server
let app let config
let appId
let datasource
let query
beforeAll(async () => { beforeAll(async () => {
;({ request, server } = await supertest()) ;({ request, server } = await supertest())
config = new TestConfig(request)
}) })
afterAll(() => { afterAll(() => {
@ -45,33 +20,27 @@ describe("/queries", () => {
}) })
beforeEach(async () => { beforeEach(async () => {
app = await createApplication(request) await config.init()
appId = app.instance._id
}) })
async function createDatasource() {
return await insertDocument(appId, TEST_DATASOURCE)
}
async function createQuery() {
return await insertDocument(appId, TEST_QUERY)
}
describe("create", () => { describe("create", () => {
it("should create a new query", async () => { it("should create a new query", async () => {
const { _id } = await config.createDatasource()
const query = basicQuery(_id)
const res = await request const res = await request
.post(`/api/queries`) .post(`/api/queries`)
.send(TEST_QUERY) .send(query)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(200) .expect(200)
expect(res.res.statusMessage).toEqual( expect(res.res.statusMessage).toEqual(
`Query ${TEST_QUERY.name} saved successfully.` `Query ${query.name} saved successfully.`
) )
expect(res.body).toEqual({ expect(res.body).toEqual({
_rev: res.body._rev, _rev: res.body._rev,
...TEST_QUERY, _id: res.body._id,
...query,
}) })
}) })
}) })
@ -80,7 +49,7 @@ describe("/queries", () => {
let datasource let datasource
beforeEach(async () => { beforeEach(async () => {
datasource = await createDatasource() datasource = await config.createDatasource()
}) })
afterEach(() => { afterEach(() => {
@ -88,29 +57,29 @@ describe("/queries", () => {
}) })
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 config.createQuery()
const res = await request const res = await request
.get(`/api/queries`) .get(`/api/queries`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.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, _id: query._id,
...basicQuery(datasource._id),
readable: true, readable: true,
}, },
]) ])
}) })
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/datasources`, url: `/api/datasources`,
appId: appId,
}) })
}) })
}) })
@ -119,7 +88,7 @@ describe("/queries", () => {
let datasource let datasource
beforeEach(async () => { beforeEach(async () => {
datasource = await createDatasource() datasource = await config.createDatasource()
}) })
afterEach(() => { afterEach(() => {
@ -127,16 +96,16 @@ describe("/queries", () => {
}) })
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 config.createQuery()
await request await request
.delete(`/api/queries/${query.id}/${query.rev}`) .delete(`/api/queries/${query._id}/${query._rev}`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect(200) .expect(200)
const res = await request const res = await request
.get(`/api/queries`) .get(`/api/queries`)
.set(defaultHeaders(appId)) .set(config.defaultHeaders())
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
.expect(200) .expect(200)
@ -144,11 +113,10 @@ describe("/queries", () => {
}) })
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/datasources/${datasource._id}/${datasource._rev}`, url: `/api/datasources/${datasource._id}/${datasource._rev}`,
appId: appId,
}) })
}) })
}) })