finish authenticated tests

This commit is contained in:
Martin McKeaveney 2021-03-09 17:31:52 +00:00
parent 67c4a5ef6c
commit 6263300a68
3 changed files with 72 additions and 14 deletions

View File

@ -31,6 +31,7 @@ module.exports = async (ctx, next) => {
token = ctx.cookies.get(getCookieName()) token = ctx.cookies.get(getCookieName())
authType = AuthTypes.BUILDER authType = AuthTypes.BUILDER
} }
if (!token && appId) { if (!token && appId) {
token = ctx.cookies.get(getCookieName(appId)) token = ctx.cookies.get(getCookieName(appId))
authType = AuthTypes.APP authType = AuthTypes.APP
@ -58,6 +59,7 @@ module.exports = async (ctx, next) => {
role: await getRole(appId, jwtPayload.roleId), role: await getRole(appId, jwtPayload.roleId),
} }
} catch (err) { } catch (err) {
console.log(err)
if (authType === AuthTypes.BUILDER) { if (authType === AuthTypes.BUILDER) {
clearCookie(ctx) clearCookie(ctx)
ctx.status = 200 ctx.status = 200

View File

@ -0,0 +1,28 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`Authenticated middleware sets the correct APP auth type information when the user is not in the builder 1`] = `
Object {
"apiKey": "1234",
"appId": "budibase:app:local",
"role": Role {
"_id": "ADMIN",
"inherits": "POWER",
"name": "Admin",
"permissionId": "admin",
},
"roleId": "ADMIN",
}
`;
exports[`Authenticated middleware sets the correct BUILDER auth type information when the x-budibase-type header is not 'client' 1`] = `
Object {
"apiKey": "1234",
"appId": "budibase:builder:local",
"role": Role {
"_id": "BUILDER",
"name": "Builder",
"permissionId": "admin",
},
"roleId": "BUILDER",
}
`;

View File

@ -1,10 +1,13 @@
const { AuthTypes } = require("../../constants") const { AuthTypes } = require("../../constants")
const authenticatedMiddleware = require("../authenticated") const authenticatedMiddleware = require("../authenticated")
const jwt = require("jsonwebtoken")
jest.mock("jsonwebtoken")
class TestConfiguration { class TestConfiguration {
constructor(middleware) { constructor(middleware) {
this.middleware = authenticatedMiddleware this.middleware = authenticatedMiddleware
this.ctx = { this.ctx = {
config: {},
auth: {}, auth: {},
request: {}, request: {},
cookies: { cookies: {
@ -16,7 +19,8 @@ class TestConfiguration {
path: "", path: "",
request: { request: {
headers: {} headers: {}
} },
throw: jest.fn()
} }
this.next = jest.fn() this.next = jest.fn()
} }
@ -28,6 +32,10 @@ class TestConfiguration {
executeMiddleware() { executeMiddleware() {
return this.middleware(this.ctx, this.next) return this.middleware(this.ctx, this.next)
} }
afterEach() {
jest.resetAllMocks()
}
} }
describe("Authenticated middleware", () => { describe("Authenticated middleware", () => {
@ -37,6 +45,10 @@ describe("Authenticated middleware", () => {
config = new TestConfiguration() config = new TestConfiguration()
}) })
afterEach(() => {
config.afterEach()
})
it("calls next() when on the builder path", async () => { it("calls next() when on the builder path", async () => {
config.ctx.path = "/_builder" config.ctx.path = "/_builder"
@ -47,10 +59,10 @@ describe("Authenticated middleware", () => {
it("sets a new cookie when the current cookie does not match the app id from context", async () => { it("sets a new cookie when the current cookie does not match the app id from context", async () => {
const appId = "app_123" const appId = "app_123"
config.ctx.cookies.get.mockImplementationOnce(() => "cookieAppId")
config.setHeaders({ config.setHeaders({
"x-budibase-app-id": appId "x-budibase-app-id": appId
}) })
config.ctx.cookies.get.mockImplementation(() => "cookieAppId")
await config.executeMiddleware() await config.executeMiddleware()
@ -62,37 +74,53 @@ describe("Authenticated middleware", () => {
}) })
fit("sets a BUILDER auth type when the x-budibase-type header is not 'client'", async () => { it("sets the correct BUILDER auth type information when the x-budibase-type header is not 'client'", async () => {
config.ctx.cookies.get.mockImplementationOnce(() => `budibase:builder:local`) config.ctx.cookies.get.mockImplementation(() => "budibase:builder:local")
jwt.verify.mockImplementationOnce(() => ({
apiKey: "1234",
roleId: "BUILDER"
}))
await config.executeMiddleware() await config.executeMiddleware()
expect(config.ctx.auth.authenticated).toEqual(AuthTypes.BUILDER) expect(config.ctx.auth.authenticated).toEqual(AuthTypes.BUILDER)
expect(config.ctx.user).toMatchSnapshot()
}) })
it("assigns an APP auth type when the user is not in the builder", async () => { it("sets the correct APP auth type information when the user is not in the builder", async () => {
config.setHeaders({ config.setHeaders({
"x-budibase-type": "client" "x-budibase-type": "client"
}) })
config.ctx.cookies.get.mockImplementationOnce(() => `budibase:builder:local`) config.ctx.cookies.get.mockImplementation(() => `budibase:app:local`)
jwt.verify.mockImplementationOnce(() => ({
apiKey: "1234",
roleId: "ADMIN"
}))
await config.executeMiddleware() await config.executeMiddleware()
expect(config.ctx.auth.authenticated).toEqual(AuthTypes.APP) expect(config.ctx.auth.authenticated).toEqual(AuthTypes.APP)
expect(config.ctx.user).toMatchSnapshot()
}) })
it("marks the user as unauthenticated when a token cannot be determined from the users cookie", async () => { it("marks the user as unauthenticated when a token cannot be determined from the users cookie", async () => {
config.executeMiddleware() config.executeMiddleware()
expect() expect(config.ctx.auth.authenticated).toBe(false)
}) expect(config.ctx.user.role).toEqual({
_id: "PUBLIC",
it("verifies the users JWT token and sets the user information in context when successful", async () => { name: "Public",
config.executeMiddleware() permissionId: "public"
expect() })
}) })
it("clears the cookie when there is an error authenticating in the builder", async () => { it("clears the cookie when there is an error authenticating in the builder", async () => {
config.executeMiddleware() config.ctx.cookies.get.mockImplementation(() => "budibase:builder:local")
expect() jwt.verify.mockImplementationOnce(() => {
throw new Error()
})
await config.executeMiddleware()
expect(config.ctx.cookies.set).toBeCalledWith("budibase:builder:local")
}) })
}) })