|
| 1 | +import { describe, it, expect } from "@jest/globals" |
| 2 | +import { transformMarkdown } from "@lib/markdown/transformMarkdown.js" |
| 3 | +import { REQUIRED_ROLE_PRODUCTS, describeRequiredRole } from "./roles.js" |
| 4 | + |
| 5 | +describe("describeRequiredRole", () => { |
| 6 | + it("names the role with its product and links to the product-scoped RBAC section", () => { |
| 7 | + expect(describeRequiredRole("ace", "operator")).toEqual({ |
| 8 | + name: "ACE Operator", |
| 9 | + orHigher: true, |
| 10 | + url: "/chainlink-platform/rbac?parent=ace#ace", |
| 11 | + }) |
| 12 | + }) |
| 13 | + |
| 14 | + it("does not say 'or higher' for the most privileged role", () => { |
| 15 | + expect(describeRequiredRole("ace", "admin")?.orHigher).toBe(false) |
| 16 | + expect(describeRequiredRole("cre", "admin")?.orHigher).toBe(false) |
| 17 | + }) |
| 18 | + |
| 19 | + it("uses the roles of each product", () => { |
| 20 | + expect(describeRequiredRole("cre", "developer")?.name).toBe("CRE Developer") |
| 21 | + expect(describeRequiredRole("crec", "operator")?.name).toBe("CRE Connect Operator") |
| 22 | + expect(describeRequiredRole("crec", "viewer")?.url).toBe("/chainlink-platform/rbac?parent=crec#cre-connect") |
| 23 | + }) |
| 24 | + |
| 25 | + it("returns null for an unknown product or role", () => { |
| 26 | + expect(describeRequiredRole("ace", "developer")).toBeNull() |
| 27 | + expect(describeRequiredRole("dataStreams", "admin")).toBeNull() |
| 28 | + expect(describeRequiredRole("toString", "admin")).toBeNull() |
| 29 | + expect(describeRequiredRole(undefined, "admin")).toBeNull() |
| 30 | + expect(describeRequiredRole("ace", 1)).toBeNull() |
| 31 | + }) |
| 32 | + |
| 33 | + it("lists the most privileged role first for every product", () => { |
| 34 | + for (const config of Object.values(REQUIRED_ROLE_PRODUCTS)) { |
| 35 | + expect(Object.keys(config.roles)[0]).toBe("admin") |
| 36 | + } |
| 37 | + }) |
| 38 | +}) |
| 39 | + |
| 40 | +describe("RequiredRole in the Markdown export", () => { |
| 41 | + it("projects the role and the link as a sentence", async () => { |
| 42 | + const result = await transformMarkdown('<RequiredRole product="ace" role="operator" />', "/fake/path.mdx") |
| 43 | + expect(result).toContain("**Required role:** ACE Operator or higher.") |
| 44 | + expect(result).toContain("[What each role can do](/chainlink-platform/rbac?parent=ace#ace)") |
| 45 | + }) |
| 46 | + |
| 47 | + it("keeps the optional body after the sentence", async () => { |
| 48 | + const markdown = `<RequiredRole product="ace" role="admin"> |
| 49 | + Creating a wallet needs the \`ace:core:wallet:write\` permission. |
| 50 | +</RequiredRole>` |
| 51 | + const result = await transformMarkdown(markdown, "/fake/path.mdx") |
| 52 | + expect(result).toContain("**Required role:** ACE Admin.") |
| 53 | + expect(result).not.toContain("ACE Admin or higher") |
| 54 | + expect(result).toContain("Creating a wallet needs the `ace:core:wallet:write` permission.") |
| 55 | + }) |
| 56 | + |
| 57 | + it("drops a component with an unknown role instead of leaking JSX", async () => { |
| 58 | + const result = await transformMarkdown( |
| 59 | + 'Before\n\n<RequiredRole product="ace" role="owner" />\n\nAfter', |
| 60 | + "/fake/path.mdx" |
| 61 | + ) |
| 62 | + expect(result).toContain("Before") |
| 63 | + expect(result).toContain("After") |
| 64 | + expect(result).not.toContain("RequiredRole") |
| 65 | + expect(result).not.toContain("Required role") |
| 66 | + }) |
| 67 | +}) |
0 commit comments