Refactoring to remove the use of the supertest, making this part of the test config.

This commit is contained in:
mike12345567 2021-03-04 12:32:31 +00:00
parent 68a5580fb9
commit 317d6633a2
8 changed files with 42 additions and 79 deletions

View File

@ -1,26 +1,22 @@
const {
supertest,
} = require("./utilities")
const TestConfig = require("./utilities/TestConfiguration")
const { clearAllApps, checkBuilderEndpoint } = require("./utilities/TestFunctions")
describe("/applications", () => {
let request
let server
let config
beforeAll(async () => {
({ request, server } = await supertest())
});
config = new TestConfig()
request = config.request
})
beforeEach(async () => {
await clearAllApps()
config = new TestConfig(request)
await config.init()
})
afterAll(() => {
server.close()
config.end()
})
describe("create", () => {

View File

@ -1,6 +1,3 @@
const {
supertest,
} = require("./utilities")
const TestConfig = require("./utilities/TestConfiguration")
const {
checkBuilderEndpoint,
@ -19,22 +16,20 @@ let LOGIC_DEFINITIONS = {}
describe("/automations", () => {
let request
let server
let app
let config
let automation
beforeAll(async () => {
({ request, server } = await supertest())
config = new TestConfig()
request = config.request
})
beforeEach(async () => {
config = new TestConfig(request)
app = await config.init()
await config.init()
})
afterAll(() => {
server.close()
config.end()
})
const triggerWorkflow = async automation => {

View File

@ -1,22 +1,18 @@
const {
supertest,
} = require("./utilities")
let TestConfig = require("./utilities/TestConfiguration")
let { basicDatasource } = require("./utilities/structures")
let { checkBuilderEndpoint } = require("./utilities/TestFunctions")
describe("/datasources", () => {
let request
let server
let config
beforeAll(async () => {
({ request, server } = await supertest())
config = new TestConfig(request)
});
config = new TestConfig()
request = config.request
})
afterAll(() => {
server.close()
config.end()
})
beforeEach(async () => {

View File

@ -1,6 +1,3 @@
const {
supertest,
} = require("./utilities")
const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles")
const TestConfig = require("./utilities/TestConfiguration")
const { basicRow } = require("./utilities/structures")
@ -9,7 +6,6 @@ const HIGHER_ROLE_ID = BUILTIN_ROLE_IDS.BASIC
const STD_ROLE_ID = BUILTIN_ROLE_IDS.PUBLIC
describe("/permission", () => {
let server
let request
let table
let perms
@ -17,12 +13,12 @@ describe("/permission", () => {
let config
beforeAll(async () => {
;({ request, server } = await supertest())
config = new TestConfig(request)
config = new TestConfig()
request = config.request
})
afterAll(() => {
server.close()
config.end()
})
beforeEach(async () => {

View File

@ -1,22 +1,18 @@
const {
supertest,
} = require("./utilities")
const TestConfig = require("./utilities/TestConfiguration")
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const { basicQuery } = require("./utilities/structures")
describe("/queries", () => {
let request
let server
let config
beforeAll(async () => {
;({ request, server } = await supertest())
config = new TestConfig(request)
config = new TestConfig()
request = config.request
})
afterAll(() => {
server.close()
config.end()
})
beforeEach(async () => {

View File

@ -1,43 +1,33 @@
const {
createApplication,
supertest,
defaultHeaders,
} = require("./couchTestUtils")
const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles")
const {
BUILTIN_PERMISSION_IDS,
} = require("../../../utilities/security/permissions")
const roleBody = {
name: "NewRole",
inherits: BUILTIN_ROLE_IDS.BASIC,
permissionId: BUILTIN_PERMISSION_IDS.READ_ONLY,
}
const { basicRole } = require("./utilities/structures")
const TestConfig = require("./utilities/TestConfiguration")
describe("/roles", () => {
let server
let request
let appId
let config
beforeAll(async () => {
;({ request, server } = await supertest())
config = new TestConfig()
request = config.request
})
afterAll(() => {
server.close()
config.close()
})
beforeEach(async () => {
let app = await createApplication(request)
appId = app.instance._id
await config.init()
})
describe("create", () => {
it("returns a success message when role is successfully created", async () => {
const res = await request
.post(`/api/roles`)
.send(roleBody)
.set(defaultHeaders(appId))
.send(basicRole())
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
@ -53,8 +43,8 @@ describe("/roles", () => {
it("should list custom roles, plus 2 default roles", async () => {
const createRes = await request
.post(`/api/roles`)
.send(roleBody)
.set(defaultHeaders(appId))
.send(basicRole())
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
@ -62,7 +52,7 @@ describe("/roles", () => {
const res = await request
.get(`/api/roles`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
@ -92,7 +82,7 @@ describe("/roles", () => {
const createRes = await request
.post(`/api/roles`)
.send({ name: "user", permissionId: BUILTIN_PERMISSION_IDS.READ_ONLY })
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect("Content-Type", /json/)
.expect(200)
@ -100,12 +90,12 @@ describe("/roles", () => {
await request
.delete(`/api/roles/${customRole._id}/${customRole._rev}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect(200)
await request
.get(`/api/roles/${customRole._id}`)
.set(defaultHeaders(appId))
.set(config.defaultHeaders())
.expect(404)
})
})

View File

@ -10,14 +10,17 @@ const {
basicQuery,
} = require("./structures")
const controllers = require("./controllers")
const supertest = require("supertest")
const EMAIL = "babs@babs.com"
const PASSWORD = "babs_password"
class TestConfiguration {
constructor(request) {
constructor() {
env.PORT = 4002
this.server = require("../../../../app")
this.request = supertest(this.server)
// we need the request for logging in, involves cookies, hard to fake
this.request = request
this.appId = null
}
@ -42,6 +45,10 @@ class TestConfiguration {
return this.createApp(appName)
}
end() {
this.server.close()
}
defaultHeaders() {
const builderUser = {
userId: "BUILDER",

View File

@ -1,16 +1,3 @@
const supertest = require("supertest")
const env = require("../../../../environment")
const TEST_CLIENT_ID = "test-client-id"
exports.TEST_CLIENT_ID = TEST_CLIENT_ID
exports.supertest = async () => {
let request
let server
env.PORT = 4002
server = require("../../../../app")
request = supertest(server)
return { request, server }
}