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

View File

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