|
| 1 | +import { describe, it, expect, vi } from "vitest" |
| 2 | +import { renderHook, waitFor } from "@testing-library/react" |
| 3 | +import { QueryClient, QueryClientProvider } from "@tanstack/react-query" |
| 4 | +import { |
| 5 | + K8sClient, |
| 6 | + K8sProvider, |
| 7 | + useSelfSubjectAccessReview, |
| 8 | + type SelfSubjectAccessReview, |
| 9 | +} from "@cozystack/k8s-client" |
| 10 | +import type { ReactNode } from "react" |
| 11 | + |
| 12 | +function makeWrapper(client: K8sClient) { |
| 13 | + const queryClient = new QueryClient({ |
| 14 | + defaultOptions: { queries: { retry: false, gcTime: 0 } }, |
| 15 | + }) |
| 16 | + return function Wrapper({ children }: { children: ReactNode }) { |
| 17 | + return ( |
| 18 | + <QueryClientProvider client={queryClient}> |
| 19 | + <K8sProvider client={client} queryClient={queryClient}> |
| 20 | + {children} |
| 21 | + </K8sProvider> |
| 22 | + </QueryClientProvider> |
| 23 | + ) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +function ssarResult(allowed: boolean): SelfSubjectAccessReview { |
| 28 | + return { |
| 29 | + apiVersion: "authorization.k8s.io/v1", |
| 30 | + kind: "SelfSubjectAccessReview", |
| 31 | + metadata: { name: "" }, |
| 32 | + spec: { resourceAttributes: { resource: "nodes", verb: "list" } }, |
| 33 | + status: { allowed }, |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +describe("useSelfSubjectAccessReview", () => { |
| 38 | + it("starts in loading state with allowed=false", () => { |
| 39 | + const client = new K8sClient() |
| 40 | + vi.spyOn(client, "create").mockImplementation(() => new Promise(() => {})) |
| 41 | + const { result } = renderHook( |
| 42 | + () => |
| 43 | + useSelfSubjectAccessReview({ |
| 44 | + resourceAttributes: { resource: "nodes", verb: "list" }, |
| 45 | + }), |
| 46 | + { wrapper: makeWrapper(client) }, |
| 47 | + ) |
| 48 | + expect(result.current.isLoading).toBe(true) |
| 49 | + expect(result.current.allowed).toBe(false) |
| 50 | + }) |
| 51 | + |
| 52 | + it("reports allowed=true when the API responds with status.allowed=true", async () => { |
| 53 | + const client = new K8sClient() |
| 54 | + vi.spyOn(client, "create").mockResolvedValue(ssarResult(true)) |
| 55 | + const { result } = renderHook( |
| 56 | + () => |
| 57 | + useSelfSubjectAccessReview({ |
| 58 | + resourceAttributes: { resource: "nodes", verb: "list" }, |
| 59 | + }), |
| 60 | + { wrapper: makeWrapper(client) }, |
| 61 | + ) |
| 62 | + await waitFor(() => expect(result.current.isLoading).toBe(false)) |
| 63 | + expect(result.current.allowed).toBe(true) |
| 64 | + }) |
| 65 | + |
| 66 | + it("reports allowed=false explicitly when status.allowed=false", async () => { |
| 67 | + const client = new K8sClient() |
| 68 | + vi.spyOn(client, "create").mockResolvedValue(ssarResult(false)) |
| 69 | + const { result } = renderHook( |
| 70 | + () => |
| 71 | + useSelfSubjectAccessReview({ |
| 72 | + resourceAttributes: { resource: "nodes", verb: "list" }, |
| 73 | + }), |
| 74 | + { wrapper: makeWrapper(client) }, |
| 75 | + ) |
| 76 | + await waitFor(() => expect(result.current.isLoading).toBe(false)) |
| 77 | + expect(result.current.allowed).toBe(false) |
| 78 | + }) |
| 79 | + |
| 80 | + it("POSTs once for two consumers asking the same question", async () => { |
| 81 | + const client = new K8sClient() |
| 82 | + const spy = vi.spyOn(client, "create").mockResolvedValue(ssarResult(true)) |
| 83 | + const Wrapper = makeWrapper(client) |
| 84 | + const { result: a } = renderHook( |
| 85 | + () => |
| 86 | + useSelfSubjectAccessReview({ |
| 87 | + resourceAttributes: { resource: "nodes", verb: "list" }, |
| 88 | + }), |
| 89 | + { wrapper: Wrapper }, |
| 90 | + ) |
| 91 | + const { result: b } = renderHook( |
| 92 | + () => |
| 93 | + useSelfSubjectAccessReview({ |
| 94 | + resourceAttributes: { resource: "nodes", verb: "list" }, |
| 95 | + }), |
| 96 | + { wrapper: Wrapper }, |
| 97 | + ) |
| 98 | + await waitFor(() => expect(a.current.isLoading).toBe(false)) |
| 99 | + await waitFor(() => expect(b.current.isLoading).toBe(false)) |
| 100 | + expect(spy).toHaveBeenCalledTimes(1) |
| 101 | + }) |
| 102 | + |
| 103 | + it("POSTs twice when two consumers ask different questions", async () => { |
| 104 | + const client = new K8sClient() |
| 105 | + const spy = vi.spyOn(client, "create").mockResolvedValue(ssarResult(true)) |
| 106 | + const Wrapper = makeWrapper(client) |
| 107 | + const { result: a } = renderHook( |
| 108 | + () => |
| 109 | + useSelfSubjectAccessReview({ |
| 110 | + resourceAttributes: { resource: "nodes", verb: "list" }, |
| 111 | + }), |
| 112 | + { wrapper: Wrapper }, |
| 113 | + ) |
| 114 | + const { result: b } = renderHook( |
| 115 | + () => |
| 116 | + useSelfSubjectAccessReview({ |
| 117 | + resourceAttributes: { resource: "pods", verb: "list" }, |
| 118 | + }), |
| 119 | + { wrapper: Wrapper }, |
| 120 | + ) |
| 121 | + await waitFor(() => expect(a.current.isLoading).toBe(false)) |
| 122 | + await waitFor(() => expect(b.current.isLoading).toBe(false)) |
| 123 | + expect(spy).toHaveBeenCalledTimes(2) |
| 124 | + }) |
| 125 | + |
| 126 | + it("surfaces the error and reports allowed=false on API failure", async () => { |
| 127 | + const client = new K8sClient() |
| 128 | + const err = new Error("server error") |
| 129 | + vi.spyOn(client, "create").mockRejectedValue(err) |
| 130 | + const { result } = renderHook( |
| 131 | + () => |
| 132 | + useSelfSubjectAccessReview({ |
| 133 | + resourceAttributes: { resource: "nodes", verb: "list" }, |
| 134 | + }), |
| 135 | + { wrapper: makeWrapper(client) }, |
| 136 | + ) |
| 137 | + await waitFor(() => expect(result.current.isLoading).toBe(false)) |
| 138 | + expect(result.current.allowed).toBe(false) |
| 139 | + expect(result.current.error).toBeTruthy() |
| 140 | + }) |
| 141 | + |
| 142 | + it("sends the spec verbatim in the POST body", async () => { |
| 143 | + const client = new K8sClient() |
| 144 | + const spy = vi.spyOn(client, "create").mockResolvedValue(ssarResult(true)) |
| 145 | + const { result } = renderHook( |
| 146 | + () => |
| 147 | + useSelfSubjectAccessReview({ |
| 148 | + resourceAttributes: { |
| 149 | + group: "metrics.k8s.io", |
| 150 | + resource: "nodes", |
| 151 | + verb: "list", |
| 152 | + }, |
| 153 | + }), |
| 154 | + { wrapper: makeWrapper(client) }, |
| 155 | + ) |
| 156 | + await waitFor(() => expect(result.current.isLoading).toBe(false)) |
| 157 | + expect(spy).toHaveBeenCalledWith( |
| 158 | + "authorization.k8s.io", |
| 159 | + "v1", |
| 160 | + "selfsubjectaccessreviews", |
| 161 | + expect.objectContaining({ |
| 162 | + kind: "SelfSubjectAccessReview", |
| 163 | + apiVersion: "authorization.k8s.io/v1", |
| 164 | + spec: { |
| 165 | + resourceAttributes: { |
| 166 | + group: "metrics.k8s.io", |
| 167 | + resource: "nodes", |
| 168 | + verb: "list", |
| 169 | + }, |
| 170 | + }, |
| 171 | + }), |
| 172 | + ) |
| 173 | + }) |
| 174 | +}) |
0 commit comments