Fixing test cases.

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

View File

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

View File

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

View File

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

View File

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

View File

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