Refactoring data source test cases.

This commit is contained in:
mike12345567 2021-03-04 10:05:50 +00:00
parent 798d29e743
commit 5d3b110985
4 changed files with 80 additions and 71 deletions

View File

@ -1,42 +1,22 @@
const {
supertest,
createApplication,
defaultHeaders,
builderEndpointShouldBlockNormalUsers,
getDocument,
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",
}
} = require("./utilities")
let TestConfig = require("./utilities/TestConfiguration")
let { basicDatasource } = require("./utilities/structures")
let { checkBuilderEndpoint } = require("./utilities/TestFunctions")
describe("/datasources", () => {
let request
let server
let app
let appId
let config
let datasource
beforeAll(async () => {
({ request, server } = await supertest())
config = new TestConfig(request)
});
afterAll(() => {
@ -44,23 +24,15 @@ describe("/datasources", () => {
})
beforeEach(async () => {
app = await createApplication(request)
app = 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", () => {
it("should create a new datasource", async () => {
const res = await request
.post(`/api/datasources`)
.send(TEST_DATASOURCE)
.send(basicDatasource())
.set(defaultHeaders(appId))
.expect('Content-Type', /json/)
.expect(200)
@ -74,7 +46,7 @@ describe("/datasources", () => {
let datasource
beforeEach(async () => {
datasource = await createDatasource()
datasource = await config.createDatasource()
});
afterEach(() => {
@ -88,30 +60,30 @@ describe("/datasources", () => {
.expect('Content-Type', /json/)
.expect(200)
const datasources = res.body;
const datasources = res.body
expect(datasources).toEqual([
{
"_id": datasources[0]._id,
"_rev": datasources[0]._rev,
...TEST_DATASOURCE
...basicDatasource()
}
]);
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "GET",
url: `/api/datasources`,
appId: appId,
})
})
});
describe("destroy", () => {
let datasource;
let datasource
beforeEach(async () => {
datasource = await createDatasource()
datasource = await config.createDatasource()
});
afterEach(() => {
@ -119,10 +91,10 @@ describe("/datasources", () => {
});
it("deletes queries for the datasource after deletion and returns a success message", async () => {
await createQuery(datasource.id)
await config.createQuery()
await request
.delete(`/api/datasources/${datasource.id}/${datasource.rev}`)
.delete(`/api/datasources/${datasource._id}/${datasource._rev}`)
.set(defaultHeaders(appId))
.expect(200)
@ -136,11 +108,10 @@ describe("/datasources", () => {
})
it("should apply authorization to endpoint", async () => {
await builderEndpointShouldBlockNormalUsers({
request,
await checkBuilderEndpoint({
config,
method: "DELETE",
url: `/api/datasources/${datasource._id}/${datasource._rev}`,
appId: appId,
})
})

View File

@ -5,15 +5,10 @@ const {
basicRow,
basicRole,
basicAutomation,
basicDatasource,
basicQuery,
} = require("./structures")
const tableController = require("../../../controllers/table")
const rowController = require("../../../controllers/row")
const roleController = require("../../../controllers/role")
const permsController = require("../../../controllers/permission")
const viewController = require("../../../controllers/view")
const appController = require("../../../controllers/application")
const userController = require("../../../controllers/user")
const autoController = require("../../../controllers/automation")
const controllers = require("./controllers")
const EMAIL = "babs@babs.com"
const PASSWORD = "babs_password"
@ -23,9 +18,6 @@ class TestConfiguration {
// we need the request for logging in, involves cookies, hard to fake
this.request = request
this.appId = null
this.table = null
this.linkedTable = null
this.automation = null
}
async _req(config, params, controlFunc) {
@ -50,14 +42,14 @@ class TestConfiguration {
}
async createApp(appName) {
this.app = await this._req({ name: appName }, null, appController.create)
this.app = await this._req({ name: appName }, null, controllers.app.create)
this.appId = this.app._id
return this.app
}
async updateTable(config = null) {
config = config || basicTable()
this.table = await this._req(config, null, tableController.save)
this.table = await this._req(config, null, controllers.table.save)
return this.table
}
@ -95,12 +87,12 @@ class TestConfiguration {
throw "Test requires table to be configured."
}
config = config || basicRow(this.table._id)
return this._req(config, { tableId: this.table._id }, rowController.save)
return this._req(config, { tableId: this.table._id }, controllers.row.save)
}
async createRole(config = null) {
config = config || basicRole()
return this._req(config, null, roleController.save)
return this._req(config, null, controllers.role.save)
}
async addPermission(roleId, resourceId, level = "read") {
@ -111,7 +103,7 @@ class TestConfiguration {
resourceId,
level,
},
permsController.addPermission
controllers.perms.addPermission
)
}
@ -123,7 +115,7 @@ class TestConfiguration {
map: "function(doc) { emit(doc[doc.key], doc._id); } ",
tableId: this.table._id,
}
return this._req(view, null, viewController.save)
return this._req(view, null, controllers.view.save)
}
async createAutomation(config) {
@ -132,16 +124,16 @@ class TestConfiguration {
delete config._rev
}
this.automation = (
await this._req(config, null, autoController.create)
await this._req(config, null, controllers.automation.create)
).automation
return this.automation
}
async getAllAutomations() {
return this._req(null, null, autoController.fetch)
return this._req(null, null, controllers.automation.fetch)
}
async deleteAutomation(automation) {
async deleteAutomation(automation = null) {
automation = automation || this.automation
if (!automation) {
return
@ -149,10 +141,24 @@ class TestConfiguration {
return this._req(
null,
{ id: automation._id, rev: automation._rev },
autoController.destroy
controllers.automation.destroy
)
}
async createDatasource(config = null) {
config = config || basicDatasource()
this.datasource = await this._req(config, null, controllers.datasource.save)
return this.datasource
}
async createQuery(config = null) {
if (!this.datasource && !config) {
throw "No data source created for query."
}
config = config || basicQuery(this.datasource._id)
return this._req(config, null, controllers.query.save)
}
async createUser(
email = EMAIL,
password = PASSWORD,
@ -165,7 +171,7 @@ class TestConfiguration {
roleId,
},
null,
userController.create
controllers.user.create
)
}

View File

@ -0,0 +1,12 @@
module.exports = {
table: require("../../../controllers/table"),
row: require("../../../controllers/row"),
role: require("../../../controllers/role"),
perms: require("../../../controllers/permission"),
view: require("../../../controllers/view"),
app: require("../../../controllers/application"),
user: require("../../../controllers/user"),
automation: require("../../../controllers/automation"),
datasource: require("../../../controllers/datasource"),
query: require("../../../controllers/query")
}

View File

@ -57,3 +57,23 @@ exports.basicRole = () => {
permissionId: BUILTIN_PERMISSION_IDS.READ_ONLY,
}
}
exports.basicDatasource = () => {
return {
type: "datasource",
name: "Test",
source: "POSTGRES",
config: {},
}
}
exports.basicQuery = datasourceId => {
return {
datasourceId: datasourceId,
name: "New Query",
parameters: [],
fields: {},
schema: {},
queryVerb: "read",
}
}