Fixing test cases.

This commit is contained in:
mike12345567 2021-07-08 00:30:55 +01:00
parent 93302cb667
commit b0fb7ae991
5 changed files with 32 additions and 12 deletions

View File

@ -2,7 +2,6 @@ const setup = require("./utilities")
const { basicScreen } = setup.structures
const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
const { BUILTIN_ROLE_IDS } = require("@budibase/auth/roles")
const workerRequests = require("../../../utilities/workerRequests")
const route = "/test"

View File

@ -56,7 +56,11 @@ module.exports = async (ctx, next) => {
role: await getRole(appId, roleId),
}
}
if (requestAppId !== appId) {
if (
requestAppId !== appId ||
appCookie == null ||
appCookie.appId !== requestAppId
) {
setCookie(ctx, { appId }, Cookies.CurrentApp)
}
return next()

View File

@ -23,6 +23,15 @@ function mockReset() {
function mockAuthWithNoCookie() {
jest.resetModules()
mockWorker()
jest.mock("@budibase/auth/cache", () => ({
user: {
getUser: () => {
return {
_id: "us_uuid1",
}
},
},
}))
jest.mock("@budibase/auth", () => ({
utils: {
getAppId: jest.fn(),

View File

@ -17,6 +17,8 @@ const { cleanup } = require("../../utilities/fileSystem")
const { Cookies } = require("@budibase/auth").constants
const { jwt } = require("@budibase/auth").auth
const { StaticDatabases } = require("@budibase/auth/db")
const { createASession } = require("@budibase/auth/sessions")
const { user: userCache } = require("@budibase/auth/cache")
const CouchDB = require("../../db")
const GLOBAL_USER_ID = "us_uuid1"
@ -62,7 +64,7 @@ class TestConfiguration {
return request.body
}
async globalUser(id = GLOBAL_USER_ID, builder = true) {
async globalUser(id = GLOBAL_USER_ID, builder = true, roles) {
const db = new CouchDB(StaticDatabases.GLOBAL.name)
let existing
try {
@ -73,8 +75,9 @@ class TestConfiguration {
const user = {
_id: id,
...existing,
roles: {},
roles: roles || {},
}
await createASession(id, "sessionid")
if (builder) {
user.builder = { global: true }
}
@ -103,6 +106,7 @@ class TestConfiguration {
defaultHeaders() {
const auth = {
userId: GLOBAL_USER_ID,
sessionId: "sessionid",
}
const app = {
roleId: BUILTIN_ROLE_IDS.ADMIN,
@ -138,13 +142,7 @@ class TestConfiguration {
roleId = BUILTIN_ROLE_IDS.ADMIN,
builder = false,
}) {
let user
try {
user = await this.createUser(email, PASSWORD, roleId)
} catch (err) {
// allow errors here
}
return this.login(email, PASSWORD, { roleId, userId: user._id, builder })
return this.login(email, PASSWORD, { roleId, builder })
}
async createApp(appName) {
@ -313,6 +311,7 @@ class TestConfiguration {
async createUser(id = null) {
const globalId = !id ? `us_${Math.random()}` : `us_${id}`
const resp = await this.globalUser(globalId)
await userCache.invalidateUser(globalId)
return {
...resp,
globalId,
@ -326,14 +325,19 @@ class TestConfiguration {
}
// make sure the user exists in the global DB
if (roleId !== BUILTIN_ROLE_IDS.PUBLIC) {
await this.globalUser(userId, builder)
const appId = `app${this.getAppId().split("app_dev")[1]}`
await this.globalUser(userId, builder, {
[appId]: roleId,
})
}
if (!email || !password) {
await this.createUser()
}
await createASession(userId, "sessionid")
// have to fake this
const auth = {
userId,
sessionId: "sessionid",
}
const app = {
roleId: roleId,
@ -343,6 +347,7 @@ class TestConfiguration {
const appToken = jwt.sign(app, env.JWT_SECRET)
// returning necessary request headers
await userCache.invalidateUser(userId)
return {
Accept: "application/json",
Cookie: [

View File

@ -5,6 +5,7 @@ const { jwt } = require("@budibase/auth").auth
const { Cookies } = require("@budibase/auth").constants
const { Configs, LOGO_URL } = require("../../../../constants")
const { getGlobalUserByEmail } = require("@budibase/auth").utils
const { createASession } = require("@budibase/auth/sessions")
class TestConfiguration {
constructor(openServer = true) {
@ -56,6 +57,7 @@ class TestConfiguration {
null,
controllers.users.save
)
await createASession("us_uuid1", "sessionid")
}
}
@ -69,6 +71,7 @@ class TestConfiguration {
const user = {
_id: "us_uuid1",
userId: "us_uuid1",
sessionId: "sessionid",
}
const authToken = jwt.sign(user, env.JWT_SECRET)
return {