Skip to content

Commit a98db77

Browse files
committed
add auth tests
1 parent eb9b2dd commit a98db77

File tree

3 files changed

+159
-3
lines changed

3 files changed

+159
-3
lines changed

client/src/App.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,12 @@ const App = () => {
108108
const [bearerToken, setBearerToken] = useState<string>(() => {
109109
return localStorage.getItem("lastBearerToken") || "";
110110
});
111-
111+
112112
const [headerName, setHeaderName] = useState<string>(() => {
113113
return localStorage.getItem("lastHeaderName") || "Authorization";
114114
});
115115

116116
const [pendingSampleRequests, setPendingSampleRequests] = useState<
117-
118117
Array<
119118
PendingRequest & {
120119
resolve: (result: CreateMessageResult) => void;

client/src/components/Sidebar.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ const Sidebar = ({
161161
variant="outline"
162162
onClick={() => setShowBearerToken(!showBearerToken)}
163163
className="flex items-center w-full"
164+
data-testid="auth-button"
164165
>
165166
{showBearerToken ? (
166167
<ChevronDown className="w-4 h-4 mr-2" />
@@ -174,7 +175,10 @@ const Sidebar = ({
174175
<label className="text-sm font-medium">Header Name</label>
175176
<Input
176177
placeholder="Authorization"
177-
onChange={(e) => setHeaderName && setHeaderName(e.target.value)}
178+
onChange={(e) =>
179+
setHeaderName && setHeaderName(e.target.value)
180+
}
181+
data-testid="header-input"
178182
className="font-mono"
179183
value={headerName}
180184
/>
@@ -183,6 +187,7 @@ const Sidebar = ({
183187
placeholder="Bearer Token"
184188
value={bearerToken}
185189
onChange={(e) => setBearerToken(e.target.value)}
190+
data-testid="bearer-token-input"
186191
className="font-mono"
187192
type="password"
188193
/>

client/src/components/__tests__/Sidebar.test.tsx

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { render, screen, fireEvent } from "@testing-library/react";
2+
import "@testing-library/jest-dom";
23
import { describe, it, beforeEach, jest } from "@jest/globals";
34
import Sidebar from "../Sidebar";
45
import { DEFAULT_INSPECTOR_CONFIG } from "@/lib/constants";
@@ -108,6 +109,157 @@ describe("Sidebar Environment Variables", () => {
108109
});
109110
});
110111

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+
111263
describe("Key Editing", () => {
112264
it("should maintain order when editing first key", () => {
113265
const setEnv = jest.fn();

0 commit comments

Comments
 (0)