diff --git a/packages/server/src/api/routes/tests/user.spec.js b/packages/server/src/api/routes/tests/user.spec.js index 1f83ddfe33..3405f0716d 100644 --- a/packages/server/src/api/routes/tests/user.spec.js +++ b/packages/server/src/api/routes/tests/user.spec.js @@ -1,8 +1,6 @@ -const { - testPermissionsForEndpoint, -} = require("./couchTestUtils") const { BUILTIN_ROLE_IDS } = require("../../../utilities/security/roles") const TestConfig = require("./utilities/TestConfiguration") +const { checkPermissionsEndpoint } = require("./utilities/TestFunctions") const { cloneDeep } = require("lodash/fp") const baseBody = { @@ -13,9 +11,6 @@ const baseBody = { describe("/users", () => { let request - let server - let app - let appId let config beforeAll(async () => { @@ -24,8 +19,7 @@ describe("/users", () => { }) beforeEach(async () => { - app = await config.init() - appId = app.instance._id + await config.init() }) afterAll(() => { @@ -49,11 +43,11 @@ describe("/users", () => { it("should apply authorization to endpoint", async () => { await config.createUser("brenda@brenda.com", "brendas_password") - await testPermissionsForEndpoint({ + await checkPermissionsEndpoint({ + config, request, method: "GET", url: `/api/users`, - appId: appId, passRole: BUILTIN_ROLE_IDS.ADMIN, failRole: BUILTIN_ROLE_IDS.PUBLIC, }) @@ -66,7 +60,7 @@ describe("/users", () => { body.email = "bill@budibase.com" const res = await request .post(`/api/users`) - .set(defaultHeaders(appId)) + .set(config.defaultHeaders()) .send(body) .expect(200) .expect("Content-Type", /json/) @@ -78,12 +72,11 @@ describe("/users", () => { it("should apply authorization to endpoint", async () => { const body = cloneDeep(baseBody) body.email = "brandNewUser@user.com" - await testPermissionsForEndpoint({ - request, + await checkPermissionsEndpoint({ + config, method: "POST", body, url: `/api/users`, - appId: appId, passRole: BUILTIN_ROLE_IDS.ADMIN, failRole: BUILTIN_ROLE_IDS.PUBLIC, }) diff --git a/packages/server/src/api/routes/tests/utilities/TestFunctions.js b/packages/server/src/api/routes/tests/utilities/TestFunctions.js index d7a32f4416..02e4d8dfe5 100644 --- a/packages/server/src/api/routes/tests/utilities/TestFunctions.js +++ b/packages/server/src/api/routes/tests/utilities/TestFunctions.js @@ -54,24 +54,28 @@ exports.checkBuilderEndpoint = async ({ config, method, url, body }) => { .expect(403) } -/** - * Raw DB insert utility. - */ -exports.insertDocument = async (databaseId, document) => { - const { id, ...documentFields } = document - return await new CouchDB(databaseId).put({ _id: id, ...documentFields }) -} +exports.checkPermissionsEndpoint = async ({ + config, + method, + url, + body, + passRole, + failRole, +}) => { + const password = "PASSWORD" + await config.createUser("passUser@budibase.com", password, passRole) + const passHeader = await config.login("passUser@budibase.com", password) -/** - * Raw DB delete utility. - */ -exports.destroyDocument = async (databaseId, documentId) => { - return await new CouchDB(databaseId).destroy(documentId) -} + await exports + .createRequest(config.request, method, url, body) + .set(passHeader) + .expect(200) -/** - * Raw DB get utility. - */ -exports.getDocument = async (databaseId, documentId) => { - return await new CouchDB(databaseId).get(documentId) + await config.createUser("failUser@budibase.com", password, failRole) + const failHeader = await config.login("failUser@budibase.com", password) + + await exports + .createRequest(config.request, method, url, body) + .set(failHeader) + .expect(403) }