Updating query test to include mocked preview/execute and adding layout tests.

This commit is contained in:
mike12345567 2021-03-09 11:56:32 +00:00
parent 2bf227ab58
commit 6231c25ed5
8 changed files with 167 additions and 13 deletions

View File

@ -0,0 +1,21 @@
const pg = {}
// constructor
function Client() {}
Client.prototype.query = async function() {
return {
rows: [
{
a: "string",
b: 1,
},
],
}
}
Client.prototype.connect = async function() {}
pg.Client = Client
module.exports = pg

View File

@ -38,6 +38,6 @@ exports.destroy = async function(ctx) {
}
await db.remove(layoutId, layoutRev)
ctx.message = "Layout deleted successfully"
ctx.body = { message: "Layout deleted successfully" }
ctx.status = 200
}

View File

@ -108,7 +108,6 @@ exports.preview = async function(ctx) {
if (!Integration) {
ctx.throw(400, "Integration type does not exist.")
return
}
const { fields, parameters, queryVerb } = ctx.request.body
@ -138,7 +137,6 @@ exports.execute = async function(ctx) {
if (!Integration) {
ctx.throw(400, "Integration type does not exist.")
return
}
const enrichedQuery = await enrichQueryFields(

View File

@ -0,0 +1,55 @@
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const setup = require("./utilities")
const { basicLayout } = require("./utilities/structures")
describe("/queries", () => {
let request = setup.getRequest()
let config = setup.getConfig()
let layout
afterAll(setup.afterAll)
beforeEach(async () => {
await config.init()
layout = await config.createLayout()
})
describe("save", () => {
it("should be able to create a layout", async () => {
const res = await request
.post(`/api/layouts`)
.send(basicLayout())
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body._rev).toBeDefined()
})
it("should apply authorization to endpoint", async () => {
await checkBuilderEndpoint({
config,
method: "POST",
url: `/api/layouts`,
})
})
})
describe("destroy", () => {
it("should be able to delete the layout", async () => {
const res = await request
.delete(`/api/layouts/${layout._id}/${layout._rev}`)
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.message).toBeDefined()
})
it("should apply authorization to endpoint", async () => {
await checkBuilderEndpoint({
config,
method: "DELETE",
url: `/api/layouts/${layout._id}/${layout._rev}`,
})
})
})
})

View File

@ -1,20 +1,32 @@
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const { basicQuery } = require("./utilities/structures")
const setup = require("./utilities")
// mock out postgres for this
jest.mock("pg")
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const { basicQuery, basicDatasource } = require("./utilities/structures")
const setup = require("./utilities")
describe("/queries", () => {
let request = setup.getRequest()
let config = setup.getConfig()
let datasource
let datasource, query
afterAll(setup.afterAll)
beforeEach(async () => {
await config.init()
datasource = await config.createDatasource()
query = await config.createQuery()
})
async function createInvalidIntegration() {
const datasource = await config.createDatasource({
...basicDatasource(),
source: "INVALID_INTEGRATION",
})
const query = await config.createQuery()
return { datasource, query }
}
describe("create", () => {
it("should create a new query", async () => {
const { _id } = await config.createDatasource()
@ -39,7 +51,6 @@ describe("/queries", () => {
describe("fetch", () => {
it("returns all the queries from the server", async () => {
const query = await config.createQuery()
const res = await request
.get(`/api/queries`)
.set(config.defaultHeaders())
@ -94,8 +105,6 @@ describe("/queries", () => {
describe("destroy", () => {
it("deletes a query and returns a success message", async () => {
const query = await config.createQuery()
await request
.delete(`/api/queries/${query._id}/${query._rev}`)
.set(config.defaultHeaders())
@ -114,16 +123,74 @@ describe("/queries", () => {
await checkBuilderEndpoint({
config,
method: "DELETE",
url: `/api/datasources/${datasource._id}/${datasource._rev}`,
url: `/api/queries/${config._id}/${config._rev}`,
})
})
})
describe("preview", () => {
// TODO: need to mock out an integration with a test one and try this
it("should be able to preview the query", async () => {
const res = await request
.post(`/api/queries/preview`)
.send({
datasourceId: datasource._id,
parameters: {},
fields: {},
queryVerb: "read",
})
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
// these responses come from the mock
expect(res.body.schemaFields).toEqual(["a", "b"])
expect(res.body.rows.length).toEqual(1)
})
it("should apply authorization to endpoint", async () => {
await checkBuilderEndpoint({
config,
method: "POST",
url: `/api/queries/preview`,
})
})
it("should fail with invalid integration type", async () => {
const { datasource } = await createInvalidIntegration()
await request
.post(`/api/queries/preview`)
.send({
datasourceId: datasource._id,
parameters: {},
fields: {},
queryVerb: "read",
})
.set(config.defaultHeaders())
.expect(400)
})
})
describe("execute", () => {
// TODO: need to mock out an integration with a test one and try this
it("should be able to execute the query", async () => {
const res = await request
.post(`/api/queries/${query._id}`)
.send({
parameters: {},
})
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
expect(res.body.length).toEqual(1)
})
it("should fail with invalid integration type", async () => {
const { query } = await createInvalidIntegration()
await request
.post(`/api/queries/${query._id}`)
.send({
parameters: {},
})
.set(config.defaultHeaders())
.expect(400)
})
})
})

View File

@ -9,6 +9,7 @@ const {
basicDatasource,
basicQuery,
basicScreen,
basicLayout,
basicWebhook,
} = require("./structures")
const controllers = require("./controllers")
@ -232,6 +233,11 @@ class TestConfiguration {
return (await this._req(config, null, controllers.webhook.save)).webhook
}
async createLayout(config = null) {
config = config || basicLayout()
return await this._req(config, null, controllers.layout.save)
}
async createUser(
email = EMAIL,
password = PASSWORD,

View File

@ -11,4 +11,5 @@ module.exports = {
query: require("../../../controllers/query"),
screen: require("../../../controllers/screen"),
webhook: require("../../../controllers/webhook"),
layout: require("../../../controllers/layout"),
}

View File

@ -3,6 +3,8 @@ const {
BUILTIN_PERMISSION_IDS,
} = require("../../../../utilities/security/permissions")
const { createHomeScreen } = require("../../../../constants/screens")
const { EMPTY_LAYOUT } = require("../../../../constants/layouts")
const { cloneDeep } = require("lodash/fp")
exports.basicTable = () => {
return {
@ -91,6 +93,10 @@ exports.basicScreen = () => {
return createHomeScreen()
}
exports.basicLayout = () => {
return cloneDeep(EMPTY_LAYOUT)
}
exports.basicWebhook = automationId => {
return {
live: true,