|
| 1 | +import { expect } from "chai"; |
| 2 | +import { describe, it, beforeEach, afterEach } from "mocha"; |
| 3 | +import sinon from "sinon"; |
| 4 | + |
| 5 | +import { AppsEngineValidator } from "../src/compiler/AppsEngineValidator"; |
| 6 | +import logger from "../src/misc/logger"; |
| 7 | + |
| 8 | +const OLD_PERMISSIONS_PATH = |
| 9 | + "@rocket.chat/apps-engine/server/permissions/AppPermissions"; |
| 10 | +const NEW_PERMISSIONS_PATH = |
| 11 | + "@rocket.chat/apps-engine/definition/metadata/AppPermissions"; |
| 12 | +const OLD_INTERFACE_PATH = "@rocket.chat/apps-engine/definition/metadata"; |
| 13 | +const NEW_INTERFACE_PATH = |
| 14 | + "@rocket.chat/apps-engine/server/compiler/AppImplements"; |
| 15 | + |
| 16 | +const mockAppPermissions = { |
| 17 | + network: { |
| 18 | + write: { name: "networking.write" }, |
| 19 | + read: { name: "networking.read" }, |
| 20 | + }, |
| 21 | + env: { |
| 22 | + read: { name: "env.read" }, |
| 23 | + }, |
| 24 | +}; |
| 25 | + |
| 26 | +function makeRequire(moduleMap: Record<string, any>): NodeJS.Require { |
| 27 | + const fn = (id: string) => { |
| 28 | + if (id in moduleMap) { |
| 29 | + return moduleMap[id]; |
| 30 | + } |
| 31 | + throw new Error(`Cannot find module '${id}'`); |
| 32 | + }; |
| 33 | + fn.resolve = () => { |
| 34 | + throw new Error("not implemented"); |
| 35 | + }; |
| 36 | + fn.cache = {}; |
| 37 | + fn.extensions = {}; |
| 38 | + fn.main = undefined; |
| 39 | + return fn as unknown as NodeJS.Require; |
| 40 | +} |
| 41 | + |
| 42 | +describe("AppsEngineValidator", () => { |
| 43 | + let warnStub: sinon.SinonStub; |
| 44 | + |
| 45 | + beforeEach(() => { |
| 46 | + warnStub = sinon.stub(logger, "warn"); |
| 47 | + }); |
| 48 | + |
| 49 | + afterEach(() => { |
| 50 | + sinon.restore(); |
| 51 | + }); |
| 52 | + |
| 53 | + describe("validateAppPermissionsSchema", () => { |
| 54 | + it("returns early when permissions is falsy", () => { |
| 55 | + const validator = new AppsEngineValidator(makeRequire({})); |
| 56 | + expect(() => |
| 57 | + validator.validateAppPermissionsSchema(null as any), |
| 58 | + ).not.to.throw(); |
| 59 | + }); |
| 60 | + |
| 61 | + it("throws when permissions is not an array", () => { |
| 62 | + const validator = new AppsEngineValidator(makeRequire({})); |
| 63 | + expect(() => |
| 64 | + validator.validateAppPermissionsSchema({} as any), |
| 65 | + ).to.throw("Invalid permission definition"); |
| 66 | + }); |
| 67 | + |
| 68 | + it("logs a warning and skips validation when neither permissions module path resolves", () => { |
| 69 | + const validator = new AppsEngineValidator(makeRequire({})); |
| 70 | + validator.validateAppPermissionsSchema([ |
| 71 | + { name: "networking.write" }, |
| 72 | + ]); |
| 73 | + expect(warnStub.calledOnce).to.be.true; |
| 74 | + expect(warnStub.firstCall.args[0]).to.include( |
| 75 | + "Couldn't find permissions in @rocket.chat/apps-engine version", |
| 76 | + ); |
| 77 | + }); |
| 78 | + |
| 79 | + it("validates permissions using the old module path", () => { |
| 80 | + const require = makeRequire({ |
| 81 | + [OLD_PERMISSIONS_PATH]: { AppPermissions: mockAppPermissions }, |
| 82 | + }); |
| 83 | + const validator = new AppsEngineValidator(require); |
| 84 | + expect(() => |
| 85 | + validator.validateAppPermissionsSchema([ |
| 86 | + { name: "networking.write" }, |
| 87 | + ]), |
| 88 | + ).not.to.throw(); |
| 89 | + }); |
| 90 | + |
| 91 | + it("falls back to new module path when old path is not found", () => { |
| 92 | + const require = makeRequire({ |
| 93 | + [NEW_PERMISSIONS_PATH]: { AppPermissions: mockAppPermissions }, |
| 94 | + }); |
| 95 | + const validator = new AppsEngineValidator(require); |
| 96 | + expect(() => |
| 97 | + validator.validateAppPermissionsSchema([{ name: "env.read" }]), |
| 98 | + ).not.to.throw(); |
| 99 | + }); |
| 100 | + |
| 101 | + it("throws for an invalid permission name", () => { |
| 102 | + const require = makeRequire({ |
| 103 | + [OLD_PERMISSIONS_PATH]: { AppPermissions: mockAppPermissions }, |
| 104 | + }); |
| 105 | + const validator = new AppsEngineValidator(require); |
| 106 | + expect(() => |
| 107 | + validator.validateAppPermissionsSchema([ |
| 108 | + { name: "not.a.real.permission" }, |
| 109 | + ]), |
| 110 | + ).to.throw('Invalid permission "not.a.real.permission"'); |
| 111 | + }); |
| 112 | + |
| 113 | + it("skips null/undefined entries in the permissions array", () => { |
| 114 | + const require = makeRequire({ |
| 115 | + [OLD_PERMISSIONS_PATH]: { AppPermissions: mockAppPermissions }, |
| 116 | + }); |
| 117 | + const validator = new AppsEngineValidator(require); |
| 118 | + expect(() => |
| 119 | + validator.validateAppPermissionsSchema([ |
| 120 | + null as any, |
| 121 | + undefined as any, |
| 122 | + { name: "networking.write" }, |
| 123 | + ]), |
| 124 | + ).not.to.throw(); |
| 125 | + }); |
| 126 | + }); |
| 127 | + |
| 128 | + describe("isValidAppInterface", () => { |
| 129 | + const mockAppInterface = { |
| 130 | + IPreMessageSentPrevent: "IPreMessageSentPrevent", |
| 131 | + IPostMessageSent: "IPostMessageSent", |
| 132 | + }; |
| 133 | + |
| 134 | + it("returns true for a known interface using the primary module path", () => { |
| 135 | + const require = makeRequire({ |
| 136 | + [OLD_INTERFACE_PATH]: { AppInterface: mockAppInterface }, |
| 137 | + }); |
| 138 | + const validator = new AppsEngineValidator(require); |
| 139 | + expect(validator.isValidAppInterface("IPreMessageSentPrevent")).to |
| 140 | + .be.true; |
| 141 | + }); |
| 142 | + |
| 143 | + it("returns false for an unknown interface", () => { |
| 144 | + const require = makeRequire({ |
| 145 | + [OLD_INTERFACE_PATH]: { AppInterface: mockAppInterface }, |
| 146 | + }); |
| 147 | + const validator = new AppsEngineValidator(require); |
| 148 | + expect(validator.isValidAppInterface("IDoesNotExist")).to.be.false; |
| 149 | + }); |
| 150 | + |
| 151 | + it("falls back to the legacy module path when primary path is not found", () => { |
| 152 | + const require = makeRequire({ |
| 153 | + [NEW_INTERFACE_PATH]: { AppInterface: mockAppInterface }, |
| 154 | + }); |
| 155 | + const validator = new AppsEngineValidator(require); |
| 156 | + expect(validator.isValidAppInterface("IPostMessageSent")).to.be |
| 157 | + .true; |
| 158 | + }); |
| 159 | + |
| 160 | + it("returns false for falsy interface value (not just key presence)", () => { |
| 161 | + const require = makeRequire({ |
| 162 | + [OLD_INTERFACE_PATH]: { |
| 163 | + AppInterface: { IFalsyInterface: "" }, |
| 164 | + }, |
| 165 | + }); |
| 166 | + const validator = new AppsEngineValidator(require); |
| 167 | + expect(validator.isValidAppInterface("IFalsyInterface")).to.be |
| 168 | + .false; |
| 169 | + }); |
| 170 | + }); |
| 171 | +}); |
0 commit comments