|
1 | 1 | import { render, screen, fireEvent } from "@testing-library/react";
|
| 2 | +import "@testing-library/jest-dom"; |
2 | 3 | import { describe, it, beforeEach, jest } from "@jest/globals";
|
3 | 4 | import Sidebar from "../Sidebar";
|
4 | 5 | import { DEFAULT_INSPECTOR_CONFIG } from "@/lib/constants";
|
@@ -108,6 +109,157 @@ describe("Sidebar Environment Variables", () => {
|
108 | 109 | });
|
109 | 110 | });
|
110 | 111 |
|
| 112 | + describe("Authentication", () => { |
| 113 | + const openAuthSection = () => { |
| 114 | + const button = screen.getByTestId("auth-button"); |
| 115 | + fireEvent.click(button); |
| 116 | + }; |
| 117 | + |
| 118 | + it("should update bearer token", () => { |
| 119 | + const setBearerToken = jest.fn(); |
| 120 | + renderSidebar({ |
| 121 | + bearerToken: "", |
| 122 | + setBearerToken, |
| 123 | + transportType: "sse", // Set transport type to SSE |
| 124 | + }); |
| 125 | + |
| 126 | + openAuthSection(); |
| 127 | + |
| 128 | + const tokenInput = screen.getByTestId("bearer-token-input"); |
| 129 | + fireEvent.change(tokenInput, { target: { value: "new_token" } }); |
| 130 | + |
| 131 | + expect(setBearerToken).toHaveBeenCalledWith("new_token"); |
| 132 | + }); |
| 133 | + |
| 134 | + it("should update header name", () => { |
| 135 | + const setHeaderName = jest.fn(); |
| 136 | + renderSidebar({ |
| 137 | + headerName: "Authorization", |
| 138 | + setHeaderName, |
| 139 | + transportType: "sse", |
| 140 | + }); |
| 141 | + |
| 142 | + openAuthSection(); |
| 143 | + |
| 144 | + const headerInput = screen.getByTestId("header-input"); |
| 145 | + fireEvent.change(headerInput, { target: { value: "X-Custom-Auth" } }); |
| 146 | + |
| 147 | + expect(setHeaderName).toHaveBeenCalledWith("X-Custom-Auth"); |
| 148 | + }); |
| 149 | + |
| 150 | + it("should clear bearer token", () => { |
| 151 | + const setBearerToken = jest.fn(); |
| 152 | + renderSidebar({ |
| 153 | + bearerToken: "existing_token", |
| 154 | + setBearerToken, |
| 155 | + transportType: "sse", // Set transport type to SSE |
| 156 | + }); |
| 157 | + |
| 158 | + openAuthSection(); |
| 159 | + |
| 160 | + const tokenInput = screen.getByTestId("bearer-token-input"); |
| 161 | + fireEvent.change(tokenInput, { target: { value: "" } }); |
| 162 | + |
| 163 | + expect(setBearerToken).toHaveBeenCalledWith(""); |
| 164 | + }); |
| 165 | + |
| 166 | + it("should properly render bearer token input", () => { |
| 167 | + const { rerender } = renderSidebar({ |
| 168 | + bearerToken: "existing_token", |
| 169 | + transportType: "sse", // Set transport type to SSE |
| 170 | + }); |
| 171 | + |
| 172 | + openAuthSection(); |
| 173 | + |
| 174 | + // Token input should be a password field |
| 175 | + const tokenInput = screen.getByTestId("bearer-token-input"); |
| 176 | + expect(tokenInput).toHaveProperty("type", "password"); |
| 177 | + |
| 178 | + // Update the token |
| 179 | + fireEvent.change(tokenInput, { target: { value: "new_token" } }); |
| 180 | + |
| 181 | + // Rerender with updated token |
| 182 | + rerender( |
| 183 | + <TooltipProvider> |
| 184 | + <Sidebar |
| 185 | + {...defaultProps} |
| 186 | + bearerToken="new_token" |
| 187 | + transportType="sse" |
| 188 | + /> |
| 189 | + </TooltipProvider>, |
| 190 | + ); |
| 191 | + |
| 192 | + // Token input should still exist after update |
| 193 | + expect(screen.getByTestId("bearer-token-input")).toBeInTheDocument(); |
| 194 | + }); |
| 195 | + |
| 196 | + it("should maintain token visibility state after update", () => { |
| 197 | + const { rerender } = renderSidebar({ |
| 198 | + bearerToken: "existing_token", |
| 199 | + transportType: "sse", // Set transport type to SSE |
| 200 | + }); |
| 201 | + |
| 202 | + openAuthSection(); |
| 203 | + |
| 204 | + // Token input should be a password field |
| 205 | + const tokenInput = screen.getByTestId("bearer-token-input"); |
| 206 | + expect(tokenInput).toHaveProperty("type", "password"); |
| 207 | + |
| 208 | + // Update the token |
| 209 | + fireEvent.change(tokenInput, { target: { value: "new_token" } }); |
| 210 | + |
| 211 | + // Rerender with updated token |
| 212 | + rerender( |
| 213 | + <TooltipProvider> |
| 214 | + <Sidebar |
| 215 | + {...defaultProps} |
| 216 | + bearerToken="new_token" |
| 217 | + transportType="sse" |
| 218 | + /> |
| 219 | + </TooltipProvider>, |
| 220 | + ); |
| 221 | + |
| 222 | + // Token input should still exist after update |
| 223 | + expect(screen.getByTestId("bearer-token-input")).toBeInTheDocument(); |
| 224 | + }); |
| 225 | + |
| 226 | + it("should maintain header name when toggling auth section", () => { |
| 227 | + renderSidebar({ |
| 228 | + headerName: "X-API-Key", |
| 229 | + transportType: "sse", |
| 230 | + }); |
| 231 | + |
| 232 | + // Open auth section |
| 233 | + openAuthSection(); |
| 234 | + |
| 235 | + // Verify header name is displayed |
| 236 | + const headerInput = screen.getByTestId("header-input"); |
| 237 | + expect(headerInput).toHaveValue("X-API-Key"); |
| 238 | + |
| 239 | + // Close auth section |
| 240 | + const authButton = screen.getByTestId("auth-button"); |
| 241 | + fireEvent.click(authButton); |
| 242 | + |
| 243 | + // Reopen auth section |
| 244 | + fireEvent.click(authButton); |
| 245 | + |
| 246 | + // Verify header name is still preserved |
| 247 | + expect(screen.getByTestId("header-input")).toHaveValue("X-API-Key"); |
| 248 | + }); |
| 249 | + |
| 250 | + it("should display default header name when not specified", () => { |
| 251 | + renderSidebar({ |
| 252 | + headerName: undefined, |
| 253 | + transportType: "sse", |
| 254 | + }); |
| 255 | + |
| 256 | + openAuthSection(); |
| 257 | + |
| 258 | + const headerInput = screen.getByTestId("header-input"); |
| 259 | + expect(headerInput).toHaveAttribute("placeholder", "Authorization"); |
| 260 | + }); |
| 261 | + }); |
| 262 | + |
111 | 263 | describe("Key Editing", () => {
|
112 | 264 | it("should maintain order when editing first key", () => {
|
113 | 265 | const setEnv = jest.fn();
|
|
0 commit comments