|
1 | | -import { screen, waitFor } from "@testing-library/react"; |
2 | | -import { describe, expect, it, vi } from "vitest"; |
| 1 | +import { fireEvent, screen, waitFor } from "@testing-library/react"; |
| 2 | +import { afterEach, describe, expect, it, vi } from "vitest"; |
3 | 3 |
|
4 | 4 | import { Profile } from "@/pages/Profile"; |
5 | 5 | import { renderWithProviders } from "@/test/renderWithProviders"; |
@@ -52,6 +52,110 @@ describe("Profile (public view)", () => { |
52 | 52 | }); |
53 | 53 | }); |
54 | 54 |
|
| 55 | +describe("Profile (admin edit)", () => { |
| 56 | + afterEach(() => { |
| 57 | + window.__APP_CONFIG__ = makeConfig(); |
| 58 | + }); |
| 59 | + |
| 60 | + function setAdminConfig() { |
| 61 | + const config = makeConfig({ |
| 62 | + oidc_enabled: true, |
| 63 | + user: { sub: "admin-user", name: "Admin" }, |
| 64 | + roles: ["admin"], |
| 65 | + role_names: { admin: "admin", operator: "operator", member: "member" }, |
| 66 | + }); |
| 67 | + window.__APP_CONFIG__ = config; |
| 68 | + return config; |
| 69 | + } |
| 70 | + |
| 71 | + function setMemberConfig() { |
| 72 | + const config = makeConfig({ |
| 73 | + oidc_enabled: true, |
| 74 | + user: { sub: "other-user", name: "Member" }, |
| 75 | + roles: ["member"], |
| 76 | + role_names: { admin: "admin", operator: "operator", member: "member" }, |
| 77 | + }); |
| 78 | + window.__APP_CONFIG__ = config; |
| 79 | + return config; |
| 80 | + } |
| 81 | + |
| 82 | + it("admin sees edit button on another user's profile", async () => { |
| 83 | + vi.spyOn(api, "apiGet").mockResolvedValue(PROFILE_DATA); |
| 84 | + const config = setAdminConfig(); |
| 85 | + renderWithProviders(<Profile />, { |
| 86 | + route: "/profile/p1", |
| 87 | + routePath: "/profile/:id", |
| 88 | + config, |
| 89 | + }); |
| 90 | + await waitFor(() => { |
| 91 | + expect(screen.getByTestId("profile-admin-edit")).toBeInTheDocument(); |
| 92 | + }); |
| 93 | + }); |
| 94 | + |
| 95 | + it("non-admin does not see edit button on another user's profile", async () => { |
| 96 | + vi.spyOn(api, "apiGet").mockResolvedValue(PROFILE_DATA); |
| 97 | + const config = setMemberConfig(); |
| 98 | + renderWithProviders(<Profile />, { |
| 99 | + route: "/profile/p1", |
| 100 | + routePath: "/profile/:id", |
| 101 | + config, |
| 102 | + }); |
| 103 | + await waitFor(() => { |
| 104 | + expect(screen.getAllByText("Jane Operator").length).toBeGreaterThanOrEqual(1); |
| 105 | + }); |
| 106 | + expect(screen.queryByTestId("profile-admin-edit")).toBeNull(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("owner sees edit link, not admin edit button", async () => { |
| 110 | + vi.spyOn(api, "apiGet").mockResolvedValue(PROFILE_DATA); |
| 111 | + const config = makeConfig({ |
| 112 | + oidc_enabled: true, |
| 113 | + user: { sub: "user-123", name: "Jane" }, |
| 114 | + roles: ["admin"], |
| 115 | + role_names: { admin: "admin", operator: "operator", member: "member" }, |
| 116 | + }); |
| 117 | + window.__APP_CONFIG__ = config; |
| 118 | + renderWithProviders(<Profile />, { |
| 119 | + route: "/profile/p1", |
| 120 | + routePath: "/profile/:id", |
| 121 | + config, |
| 122 | + }); |
| 123 | + await waitFor(() => { |
| 124 | + expect(screen.getAllByText("Jane Operator").length).toBeGreaterThanOrEqual(1); |
| 125 | + }); |
| 126 | + expect(screen.queryByTestId("profile-admin-edit")).toBeNull(); |
| 127 | + }); |
| 128 | + |
| 129 | + it("admin edit form submits to correct endpoint", async () => { |
| 130 | + vi.spyOn(api, "apiGet").mockResolvedValue(PROFILE_DATA); |
| 131 | + const apiPutSpy = vi.spyOn(api, "apiPut").mockResolvedValue(undefined); |
| 132 | + const config = setAdminConfig(); |
| 133 | + renderWithProviders(<Profile />, { |
| 134 | + route: "/profile/p1", |
| 135 | + routePath: "/profile/:id", |
| 136 | + config, |
| 137 | + }); |
| 138 | + await waitFor(() => { |
| 139 | + expect(screen.getByTestId("profile-admin-edit")).toBeInTheDocument(); |
| 140 | + }); |
| 141 | + |
| 142 | + fireEvent.click(screen.getByTestId("profile-admin-edit")); |
| 143 | + |
| 144 | + const nameInput = await screen.findByTestId("profile-name"); |
| 145 | + fireEvent.change(nameInput, { target: { value: "Admin Set Name" } }); |
| 146 | + fireEvent.click(screen.getByTestId("profile-save")); |
| 147 | + |
| 148 | + await waitFor(() => { |
| 149 | + expect(apiPutSpy).toHaveBeenCalledWith("/api/v1/user/profile/p1", { |
| 150 | + name: "Admin Set Name", |
| 151 | + callsign: "AB1CDE", |
| 152 | + description: "Mesh enthusiast", |
| 153 | + url: "https://example.com", |
| 154 | + }); |
| 155 | + }); |
| 156 | + }); |
| 157 | +}); |
| 158 | + |
55 | 159 | describe("Profile (own view)", () => { |
56 | 160 | it("shows a login prompt when OIDC is disabled", async () => { |
57 | 161 | renderWithProviders(<Profile />, { route: "/profile" }); |
|
0 commit comments