Updating user test cases.

This commit is contained in:
mike12345567 2021-05-19 16:24:20 +01:00
parent d62280ae83
commit 4e9097862d
6 changed files with 15 additions and 127 deletions

View File

@ -1,20 +1,6 @@
const setup = require("./utilities") const setup = require("./utilities")
const { generateUserMetadataID } = require("../../../db/utils") 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", () => { describe("/authenticate", () => {
let request = setup.getRequest() let request = setup.getRequest()
let config = setup.getConfig() let config = setup.getConfig()
@ -27,7 +13,7 @@ describe("/authenticate", () => {
describe("fetch self", () => { describe("fetch self", () => {
it("should be able to fetch self", async () => { 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 headers = await config.login("test@test.com", "p4ssw0rd", { userId: "us_uuid1" })
const res = await request const res = await request
.get(`/api/self`) .get(`/api/self`)

View File

@ -8,12 +8,7 @@ jest.mock("../../../utilities/workerRequests", () => ({
getGlobalUsers: jest.fn(() => { getGlobalUsers: jest.fn(() => {
return {} return {}
}), }),
saveGlobalUser: jest.fn(() => { addAppRoleToUser: jest.fn(),
const uuid = require("uuid/v4")
return {
_id: `us_${uuid()}`
}
}),
deleteGlobalUser: 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", () => { describe("update", () => {
beforeEach(() => { beforeEach(() => {
}) })
@ -141,7 +88,6 @@ describe("/users", () => {
.expect(200) .expect(200)
.expect("Content-Type", /json/) .expect("Content-Type", /json/)
expect(res.body.message).toBeDefined() expect(res.body.message).toBeDefined()
expect(workerRequests.deleteGlobalUser).toHaveBeenCalled()
}) })
}) })

View File

@ -3,8 +3,7 @@ const structures = require("../../../../tests/utilities/structures")
const env = require("../../../../environment") const env = require("../../../../environment")
jest.mock("../../../../utilities/workerRequests", () => ({ jest.mock("../../../../utilities/workerRequests", () => ({
getGlobalUsers: jest.fn(), getGlobalUsers: jest.fn(() => {
saveGlobalUser: jest.fn(() => {
return { return {
_id: "us_uuid1", _id: "us_uuid1",
} }

View File

@ -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)
})
})
})

View File

@ -77,7 +77,11 @@ class TestConfiguration {
if (builder) { if (builder) {
user.builder = { global: true } user.builder = { global: true }
} }
await db.put(user) const resp = await db.put(user)
return {
_rev: resp._rev,
...user,
}
} }
async init(appName = "test_application") { async init(appName = "test_application") {
@ -308,18 +312,9 @@ class TestConfiguration {
roleId = BUILTIN_ROLE_IDS.POWER roleId = BUILTIN_ROLE_IDS.POWER
) { ) {
const globalId = `us_${Math.random()}` const globalId = `us_${Math.random()}`
await this.globalUser(globalId, roleId === BUILTIN_ROLE_IDS.BUILDER) const resp = await this.globalUser(globalId, roleId === BUILTIN_ROLE_IDS.BUILDER)
const user = await this._req(
{
email,
password,
roleId,
},
null,
controllers.user.createMetadata
)
return { return {
...user, ...resp,
globalId, globalId,
} }
} }

View File

@ -1,5 +1,8 @@
const CouchDB = require("../db") const CouchDB = require("../db")
const { getGlobalIDFromUserMetadataID } = require("../db/utils") const {
getGlobalIDFromUserMetadataID,
InternalTables
} = require("../db/utils")
const { getGlobalUsers } = require("../utilities/workerRequests") const { getGlobalUsers } = require("../utilities/workerRequests")
exports.getFullUser = async (ctx, userId) => { exports.getFullUser = async (ctx, userId) => {
@ -21,6 +24,7 @@ exports.getFullUser = async (ctx, userId) => {
return { return {
...global, ...global,
...metadata, ...metadata,
tableId: InternalTables.USER_METADATA,
// make sure the ID is always a local ID, not a global one // make sure the ID is always a local ID, not a global one
_id: userId, _id: userId,
} }