2022-11-15 09:25:05 +13:00
|
|
|
require("../../db").init()
|
2021-04-14 00:32:09 +12:00
|
|
|
mockAuthWithNoCookie()
|
|
|
|
mockWorker()
|
2022-07-22 21:12:16 +12:00
|
|
|
mockUserGroups()
|
2021-04-14 00:32:09 +12:00
|
|
|
|
|
|
|
function mockWorker() {
|
|
|
|
jest.mock("../../utilities/workerRequests", () => ({
|
2021-05-28 02:40:55 +12:00
|
|
|
getGlobalSelf: () => {
|
2021-04-14 00:32:09 +12:00
|
|
|
return {
|
2021-04-24 05:07:39 +12:00
|
|
|
_id: "us_uuid1",
|
2021-04-14 00:32:09 +12:00
|
|
|
roles: {
|
|
|
|
"app_test": "BASIC",
|
2021-05-15 02:43:41 +12:00
|
|
|
},
|
|
|
|
roleId: "BASIC",
|
2021-04-14 00:32:09 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
function mockReset() {
|
|
|
|
jest.resetModules()
|
|
|
|
mockWorker()
|
|
|
|
}
|
|
|
|
|
2022-07-22 21:12:16 +12:00
|
|
|
function mockUserGroups() {
|
|
|
|
jest.mock("@budibase/pro", () => ({
|
|
|
|
groups: {
|
|
|
|
getGroupRoleId: () => {
|
|
|
|
return "BASIC"
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2021-04-14 00:32:09 +12:00
|
|
|
function mockAuthWithNoCookie() {
|
|
|
|
jest.resetModules()
|
|
|
|
mockWorker()
|
2022-11-15 09:25:05 +13:00
|
|
|
jest.mock("@budibase/backend-core", () => {
|
|
|
|
const core = jest.requireActual("@budibase/backend-core")
|
|
|
|
return {
|
|
|
|
...core,
|
|
|
|
db: {
|
|
|
|
dbExists: () => true,
|
2021-07-08 11:30:55 +12:00
|
|
|
},
|
2022-11-15 09:25:05 +13:00
|
|
|
cache: {
|
|
|
|
user: {
|
|
|
|
getUser: () => {
|
|
|
|
return {
|
|
|
|
_id: "us_uuid1",
|
|
|
|
}
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
utils: {
|
|
|
|
getAppIdFromCtx: jest.fn(),
|
|
|
|
setCookie: jest.fn(),
|
|
|
|
getCookie: jest.fn(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2021-04-14 00:32:09 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
function mockAuthWithCookie() {
|
|
|
|
jest.resetModules()
|
|
|
|
mockWorker()
|
2022-11-15 09:25:05 +13:00
|
|
|
jest.mock("@budibase/backend-core", () => {
|
|
|
|
const core = jest.requireActual("@budibase/backend-core")
|
|
|
|
return {
|
|
|
|
...core,
|
|
|
|
db: {
|
|
|
|
dbExists: () => true,
|
|
|
|
},
|
|
|
|
utils: {
|
|
|
|
getAppIdFromCtx: () => {
|
|
|
|
return "app_test"
|
|
|
|
},
|
|
|
|
setCookie: jest.fn(),
|
|
|
|
clearCookie: jest.fn(),
|
|
|
|
getCookie: () => ({ appId: "app_different", roleId: "PUBLIC" }),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2021-04-14 00:32:09 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
class TestConfiguration {
|
|
|
|
constructor() {
|
|
|
|
this.next = jest.fn()
|
|
|
|
this.throw = jest.fn()
|
|
|
|
|
|
|
|
this.ctx = {
|
|
|
|
next: this.next,
|
2022-11-15 09:25:05 +13:00
|
|
|
throw: this.throw,
|
|
|
|
request: {
|
|
|
|
body: {},
|
|
|
|
headers: {},
|
|
|
|
},
|
|
|
|
headers: {},
|
|
|
|
path: "",
|
|
|
|
cookies: {
|
|
|
|
set: jest.fn(),
|
|
|
|
}
|
2021-04-14 00:32:09 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setUser() {
|
|
|
|
this.ctx.user = {
|
2021-04-24 05:07:39 +12:00
|
|
|
userId: "us_uuid1",
|
|
|
|
_id: "us_uuid1",
|
2021-04-14 00:32:09 +12:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
executeMiddleware() {
|
|
|
|
// import as late as possible for mocks
|
2022-11-15 09:25:05 +13:00
|
|
|
jest.resetModules()
|
|
|
|
require("../../db").init()
|
2021-04-14 00:32:09 +12:00
|
|
|
const currentAppMiddleware = require("../currentapp")
|
|
|
|
return currentAppMiddleware(this.ctx, this.next)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
describe("Current app middleware", () => {
|
|
|
|
let config
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
config = new TestConfiguration()
|
|
|
|
})
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
jest.clearAllMocks()
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("test having no cookies or app ID", () => {
|
|
|
|
it("should be able to proceed with nothing setup", async () => {
|
|
|
|
await config.executeMiddleware()
|
|
|
|
expect(config.next).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("check get public for app when not logged in", () => {
|
|
|
|
it("should be able to proceed with no login, but cookies configured", async () => {
|
|
|
|
mockAuthWithCookie()
|
|
|
|
await config.executeMiddleware()
|
|
|
|
expect(config.ctx.roleId).toEqual("PUBLIC")
|
|
|
|
expect(config.ctx.appId).toEqual("app_test")
|
|
|
|
expect(config.next).toHaveBeenCalled()
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
describe("check functionality when logged in", () => {
|
|
|
|
async function checkExpected(setCookie) {
|
|
|
|
config.setUser()
|
|
|
|
await config.executeMiddleware()
|
2022-11-15 09:25:05 +13:00
|
|
|
let { utils } = require("@budibase/backend-core")
|
2021-04-14 00:32:09 +12:00
|
|
|
if (setCookie) {
|
2022-11-15 09:25:05 +13:00
|
|
|
expect(utils.setCookie).toHaveBeenCalled()
|
2021-04-14 00:32:09 +12:00
|
|
|
} else {
|
2022-11-15 09:25:05 +13:00
|
|
|
expect(utils.setCookie).not.toHaveBeenCalled()
|
2021-04-14 00:32:09 +12:00
|
|
|
}
|
2021-10-13 02:03:47 +13:00
|
|
|
expect(config.ctx.roleId).toEqual("PUBLIC")
|
|
|
|
expect(config.ctx.user.role._id).toEqual("PUBLIC")
|
2021-04-14 00:32:09 +12:00
|
|
|
expect(config.ctx.appId).toEqual("app_test")
|
|
|
|
expect(config.next).toHaveBeenCalled()
|
|
|
|
}
|
|
|
|
|
|
|
|
it("should be able to setup an app token when cookie not setup", async () => {
|
|
|
|
mockAuthWithCookie()
|
|
|
|
await checkExpected(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("should perform correct when no cookie exists", async () => {
|
|
|
|
mockReset()
|
2022-11-15 09:25:05 +13:00
|
|
|
jest.mock("@budibase/backend-core", () => {
|
|
|
|
const core = jest.requireActual("@budibase/backend-core")
|
|
|
|
return {
|
|
|
|
...core,
|
|
|
|
db: {
|
|
|
|
dbExists: () => true,
|
|
|
|
},
|
|
|
|
utils: {
|
|
|
|
getAppIdFromCtx: () => {
|
|
|
|
return "app_test"
|
|
|
|
},
|
|
|
|
setCookie: jest.fn(),
|
|
|
|
getCookie: jest.fn(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2021-04-14 00:32:09 +12:00
|
|
|
await checkExpected(true)
|
|
|
|
})
|
|
|
|
|
|
|
|
it("lastly check what occurs when cookie doesn't need updated", async () => {
|
|
|
|
mockReset()
|
2022-11-15 09:25:05 +13:00
|
|
|
jest.mock("@budibase/backend-core", () => {
|
|
|
|
const core = jest.requireActual("@budibase/backend-core")
|
|
|
|
return {
|
|
|
|
...core,
|
|
|
|
db: {
|
|
|
|
dbExists: () => true,
|
|
|
|
},
|
|
|
|
utils: {
|
|
|
|
getAppIdFromCtx: () => {
|
|
|
|
return "app_test"
|
|
|
|
},
|
|
|
|
setCookie: jest.fn(),
|
|
|
|
getCookie: () => ({ appId: "app_test", roleId: "PUBLIC" }),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
2021-04-14 00:32:09 +12:00
|
|
|
await checkExpected(false)
|
|
|
|
})
|
|
|
|
})
|
2021-04-24 05:07:39 +12:00
|
|
|
})
|