diff --git a/packages/server/src/api/routes/tests/routing.spec.js b/packages/server/src/api/routes/tests/routing.spec.js new file mode 100644 index 0000000000..3b7523f586 --- /dev/null +++ b/packages/server/src/api/routes/tests/routing.spec.js @@ -0,0 +1,85 @@ +const setup = require("./utilities") +const { basicScreen } = require("./utilities/structures") +const { checkBuilderEndpoint } = require("./utilities/TestFunctions") +const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles") + +describe("/routing", () => { + let request = setup.getRequest() + let config = setup.getConfig() + let screen, screen2 + + afterAll(setup.afterAll) + + beforeEach(async () => { + await config.init() + screen = await config.createScreen(basicScreen()) + screen2 = basicScreen() + screen2.routing.roleId = BUILTIN_ROLE_IDS.POWER + screen2 = await config.createScreen(screen2) + }) + + describe("fetch", () => { + it("returns the correct routing for basic user", async () => { + const res = await request + .get(`/api/routing/client`) + .set(await config.roleHeaders("basic@test.com", BUILTIN_ROLE_IDS.BASIC)) + .expect("Content-Type", /json/) + .expect(200) + expect(res.body.routes).toBeDefined() + expect(res.body.routes["/"]).toEqual({ + subpaths: { + ["/"]: { + screenId: screen._id, + roleId: screen.routing.roleId + } + } + }) + }) + + it("returns the correct routing for power user", async () => { + const res = await request + .get(`/api/routing/client`) + .set(await config.roleHeaders("basic@test.com", BUILTIN_ROLE_IDS.POWER)) + .expect("Content-Type", /json/) + .expect(200) + expect(res.body.routes).toBeDefined() + expect(res.body.routes["/"]).toEqual({ + subpaths: { + ["/"]: { + screenId: screen2._id, + roleId: screen2.routing.roleId + } + } + }) + }) + }) + + describe("fetch all", () => { + it("should fetch all routes for builder", async () => { + const res = await request + .get(`/api/routing`) + .set(config.defaultHeaders()) + .expect("Content-Type", /json/) + .expect(200) + expect(res.body.routes).toBeDefined() + expect(res.body.routes["/"]).toEqual({ + subpaths: { + ["/"]: { + screens: { + [screen2.routing.roleId]: screen2._id, + [screen.routing.roleId]: screen._id, + } + } + } + }) + }) + + it("make sure it is a builder only endpoint", async () => { + await checkBuilderEndpoint({ + config, + method: "GET", + url: `/api/routing`, + }) + }) + }) +}) \ No newline at end of file diff --git a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js index 5f6b1cc267..a124ca607e 100644 --- a/packages/server/src/api/routes/tests/utilities/TestConfiguration.js +++ b/packages/server/src/api/routes/tests/utilities/TestConfiguration.js @@ -8,6 +8,7 @@ const { basicAutomation, basicDatasource, basicQuery, + basicScreen, } = require("./structures") const controllers = require("./controllers") const supertest = require("supertest") @@ -83,6 +84,15 @@ class TestConfiguration { return headers } + async roleHeaders(email = EMAIL, roleId) { + try { + await this.createUser(email, PASSWORD, roleId) + } catch (err) { + // allow errors here + } + return this.login(email, PASSWORD) + } + async createApp(appName) { this.app = await this._req({ name: appName }, null, controllers.app.create) this.appId = this.app._id @@ -208,6 +218,11 @@ class TestConfiguration { return this._req(config, null, controllers.query.save) } + async createScreen(config = null) { + config = config || basicScreen() + return this._req(config, null, controllers.screen.save) + } + async createUser( email = EMAIL, password = PASSWORD, @@ -241,6 +256,7 @@ class TestConfiguration { return { Accept: "application/json", Cookie: result.headers["set-cookie"], + "x-budibase-app-id": this.appId, } } } diff --git a/packages/server/src/api/routes/tests/utilities/controllers.js b/packages/server/src/api/routes/tests/utilities/controllers.js index 541495bec8..e16aa6964c 100644 --- a/packages/server/src/api/routes/tests/utilities/controllers.js +++ b/packages/server/src/api/routes/tests/utilities/controllers.js @@ -9,4 +9,5 @@ module.exports = { automation: require("../../../controllers/automation"), datasource: require("../../../controllers/datasource"), query: require("../../../controllers/query"), + screen: require("../../../controllers/screen"), } diff --git a/packages/server/src/api/routes/tests/utilities/structures.js b/packages/server/src/api/routes/tests/utilities/structures.js index 922228aadf..aec482bb24 100644 --- a/packages/server/src/api/routes/tests/utilities/structures.js +++ b/packages/server/src/api/routes/tests/utilities/structures.js @@ -2,6 +2,7 @@ const { BUILTIN_ROLE_IDS } = require("../../../../utilities/security/roles") const { BUILTIN_PERMISSION_IDS, } = require("../../../../utilities/security/permissions") +const { createHomeScreen } = require("../../../../constants/screens") exports.basicTable = () => { return { @@ -85,3 +86,7 @@ exports.basicUser = role => { roleId: role, } } + +exports.basicScreen = () => { + return createHomeScreen() +}