From 4e9097862d74051a7b0ba0ce71b6a95ee6332b7b Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Wed, 19 May 2021 16:24:20 +0100 Subject: [PATCH] Updating user test cases. --- .../server/src/api/routes/tests/auth.spec.js | 16 +----- .../server/src/api/routes/tests/user.spec.js | 56 +------------------ .../src/api/routes/tests/utilities/index.js | 3 +- .../src/automations/tests/createUser.spec.js | 42 -------------- .../src/tests/utilities/TestConfiguration.js | 19 +++---- packages/server/src/utilities/users.js | 6 +- 6 files changed, 15 insertions(+), 127 deletions(-) delete mode 100644 packages/server/src/automations/tests/createUser.spec.js diff --git a/packages/server/src/api/routes/tests/auth.spec.js b/packages/server/src/api/routes/tests/auth.spec.js index fa307bf96f..e8abc2abe8 100644 --- a/packages/server/src/api/routes/tests/auth.spec.js +++ b/packages/server/src/api/routes/tests/auth.spec.js @@ -1,20 +1,6 @@ const setup = require("./utilities") const { generateUserMetadataID } = require("../../../db/utils") -require("../../../utilities/workerRequests") -jest.mock("../../../utilities/workerRequests", () => ({ - getGlobalUsers: jest.fn(() => { - return { - _id: "us_uuid1", - } - }), - saveGlobalUser: jest.fn(() => { - return { - _id: "us_uuid1", - } - }), -})) - describe("/authenticate", () => { let request = setup.getRequest() let config = setup.getConfig() @@ -27,7 +13,7 @@ describe("/authenticate", () => { describe("fetch self", () => { it("should be able to fetch self", async () => { - const user = await config.createUser("test@test.com", "p4ssw0rd") + await config.createUser("test@test.com", "p4ssw0rd") const headers = await config.login("test@test.com", "p4ssw0rd", { userId: "us_uuid1" }) const res = await request .get(`/api/self`) diff --git a/packages/server/src/api/routes/tests/user.spec.js b/packages/server/src/api/routes/tests/user.spec.js index 0259b2bc87..129a5f44fa 100644 --- a/packages/server/src/api/routes/tests/user.spec.js +++ b/packages/server/src/api/routes/tests/user.spec.js @@ -8,12 +8,7 @@ jest.mock("../../../utilities/workerRequests", () => ({ getGlobalUsers: jest.fn(() => { return {} }), - saveGlobalUser: jest.fn(() => { - const uuid = require("uuid/v4") - return { - _id: `us_${uuid()}` - } - }), + addAppRoleToUser: jest.fn(), deleteGlobalUser: jest.fn(), })) @@ -67,54 +62,6 @@ describe("/users", () => { }) }) - describe("create", () => { - beforeEach(() => { - workerRequests.getGlobalUsers.mockImplementationOnce(() => ([ - { - _id: "us_uuid1", - }, - { - _id: "us_uuid2", - } - ] - )) - }) - - async function create(user, status = 200) { - return request - .post(`/api/users/metadata`) - .set(config.defaultHeaders()) - .send(user) - .expect(status) - .expect("Content-Type", /json/) - } - - it("returns a success message when a user is successfully created", async () => { - const body = basicUser(BUILTIN_ROLE_IDS.POWER) - const res = await create(body) - - expect(res.res.statusMessage).toEqual("OK") - expect(res.body._id).toBeDefined() - }) - - it("should apply authorization to endpoint", async () => { - const body = basicUser(BUILTIN_ROLE_IDS.POWER) - await checkPermissionsEndpoint({ - config, - method: "POST", - body, - url: `/api/users/metadata`, - passRole: BUILTIN_ROLE_IDS.ADMIN, - failRole: BUILTIN_ROLE_IDS.PUBLIC, - }) - }) - - it("should error if no role provided", async () => { - const user = basicUser(null) - await create(user, 400) - }) - }) - describe("update", () => { beforeEach(() => { }) @@ -141,7 +88,6 @@ describe("/users", () => { .expect(200) .expect("Content-Type", /json/) expect(res.body.message).toBeDefined() - expect(workerRequests.deleteGlobalUser).toHaveBeenCalled() }) }) diff --git a/packages/server/src/api/routes/tests/utilities/index.js b/packages/server/src/api/routes/tests/utilities/index.js index e9361aa67d..6a9f982bcb 100644 --- a/packages/server/src/api/routes/tests/utilities/index.js +++ b/packages/server/src/api/routes/tests/utilities/index.js @@ -3,8 +3,7 @@ const structures = require("../../../../tests/utilities/structures") const env = require("../../../../environment") jest.mock("../../../../utilities/workerRequests", () => ({ - getGlobalUsers: jest.fn(), - saveGlobalUser: jest.fn(() => { + getGlobalUsers: jest.fn(() => { return { _id: "us_uuid1", } diff --git a/packages/server/src/automations/tests/createUser.spec.js b/packages/server/src/automations/tests/createUser.spec.js deleted file mode 100644 index 3adfa637dd..0000000000 --- a/packages/server/src/automations/tests/createUser.spec.js +++ /dev/null @@ -1,42 +0,0 @@ -const usageQuota = require("../../utilities/usageQuota") -const setup = require("./utilities") -const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles") -const { InternalTables } = require("../../db/utils") - -jest.mock("../../utilities/usageQuota") - -describe("test the create user action", () => { - let config = setup.getConfig() - let user - - beforeEach(async () => { - await config.init() - user = { - email: "test@test.com", - password: "password", - roleId: BUILTIN_ROLE_IDS.POWER - } - }) - - afterAll(setup.afterAll) - - it("should be able to run the action", async () => { - const res = await setup.runStep(setup.actions.CREATE_USER.stepId, user) - expect(res.id).toBeDefined() - expect(res.revision).toBeDefined() - const userDoc = await config.getRow(InternalTables.USER_METADATA, res.id) - expect(userDoc).toBeDefined() - }) - - it("should return an error if no inputs provided", async () => { - const res = await setup.runStep(setup.actions.CREATE_USER.stepId, {}) - expect(res.success).toEqual(false) - }) - - it("check usage quota attempts", async () => { - await setup.runInProd(async () => { - await setup.runStep(setup.actions.CREATE_USER.stepId, user) - expect(usageQuota.update).toHaveBeenCalledWith(setup.apiKey, "users", 1) - }) - }) -}) diff --git a/packages/server/src/tests/utilities/TestConfiguration.js b/packages/server/src/tests/utilities/TestConfiguration.js index cd33f14cd1..2a3ea8ca70 100644 --- a/packages/server/src/tests/utilities/TestConfiguration.js +++ b/packages/server/src/tests/utilities/TestConfiguration.js @@ -77,7 +77,11 @@ class TestConfiguration { if (builder) { user.builder = { global: true } } - await db.put(user) + const resp = await db.put(user) + return { + _rev: resp._rev, + ...user, + } } async init(appName = "test_application") { @@ -308,18 +312,9 @@ class TestConfiguration { roleId = BUILTIN_ROLE_IDS.POWER ) { const globalId = `us_${Math.random()}` - await this.globalUser(globalId, roleId === BUILTIN_ROLE_IDS.BUILDER) - const user = await this._req( - { - email, - password, - roleId, - }, - null, - controllers.user.createMetadata - ) + const resp = await this.globalUser(globalId, roleId === BUILTIN_ROLE_IDS.BUILDER) return { - ...user, + ...resp, globalId, } } diff --git a/packages/server/src/utilities/users.js b/packages/server/src/utilities/users.js index 4c637c1865..997d68c1c9 100644 --- a/packages/server/src/utilities/users.js +++ b/packages/server/src/utilities/users.js @@ -1,5 +1,8 @@ const CouchDB = require("../db") -const { getGlobalIDFromUserMetadataID } = require("../db/utils") +const { + getGlobalIDFromUserMetadataID, + InternalTables +} = require("../db/utils") const { getGlobalUsers } = require("../utilities/workerRequests") exports.getFullUser = async (ctx, userId) => { @@ -21,6 +24,7 @@ exports.getFullUser = async (ctx, userId) => { return { ...global, ...metadata, + tableId: InternalTables.USER_METADATA, // make sure the ID is always a local ID, not a global one _id: userId, }