Elffuss BraidHub

kikocis/continue public

main · 7.6 KiBRawPermalink
1import { fireEvent, render, screen, waitFor } from "@testing-library/react";
2import { beforeEach, describe, expect, it, vi } from "vitest";
3import { SecureImageComponent } from "./SecureImageComponent";
4
5describe("SecureImageComponent", () => {
6 beforeEach(() => {
7 vi.clearAllMocks();
8 });
9
10 describe("Default blocking behavior", () => {
11 it("should block images by default and show warning message", () => {
12 render(<SecureImageComponent src="https://example.com/image.jpg" />);
13
14 expect(
15 screen.getByText(
16 /Image blocked for security.*External images can leak data/,
17 ),
18 ).toBeInTheDocument();
19 expect(screen.getByText("Load Image")).toBeInTheDocument();
20 expect(screen.queryByRole("img")).not.toBeInTheDocument();
21 });
22
23 it("should display the image URL", () => {
24 const testUrl = "https://example.com/test-image.png";
25 render(<SecureImageComponent src={testUrl} />);
26
27 expect(screen.getByText(testUrl)).toBeInTheDocument();
28 });
29
30 it("should handle invalid src prop", () => {
31 render(<SecureImageComponent src={undefined} />);
32
33 expect(
34 screen.getByText("[Invalid image: no source]"),
35 ).toBeInTheDocument();
36 expect(screen.queryByText("Load Image")).not.toBeInTheDocument();
37 });
38 });
39
40 describe("Query parameter detection", () => {
41 it("should detect and display query parameters", () => {
42 render(
43 <SecureImageComponent src="https://example.com/image.jpg?user=123&token=abc" />,
44 );
45
46 expect(
47 screen.getByText(/Warning: URL contains query parameters/),
48 ).toBeInTheDocument();
49 expect(screen.getByText(/"user": "123"/)).toBeInTheDocument();
50 expect(screen.getByText(/"token": "abc"/)).toBeInTheDocument();
51 });
52
53 it("should not show query parameter warning for URLs without parameters", () => {
54 render(<SecureImageComponent src="https://example.com/image.jpg" />);
55
56 expect(
57 screen.queryByText(/Warning: URL contains query parameters/),
58 ).not.toBeInTheDocument();
59 });
60
61 it("should handle relative URLs with query parameters", () => {
62 render(
63 <SecureImageComponent src="/images/test.jpg?id=456&session=xyz" />,
64 );
65
66 expect(
67 screen.getByText(/Warning: URL contains query parameters/),
68 ).toBeInTheDocument();
69 expect(screen.getByText(/"id": "456"/)).toBeInTheDocument();
70 expect(screen.getByText(/"session": "xyz"/)).toBeInTheDocument();
71 });
72
73 it("should handle malformed URLs gracefully", () => {
74 render(<SecureImageComponent src="not-a-valid-url://image" />);
75
76 // Should still display the URL even if it can't be parsed
77 expect(screen.getByText("not-a-valid-url://image")).toBeInTheDocument();
78 expect(screen.getByText("Load Image")).toBeInTheDocument();
79 });
80 });
81
82 describe("User interaction", () => {
83 it("should show image when Load Image button is clicked", () => {
84 const testUrl = "https://example.com/image.jpg";
85 render(<SecureImageComponent src={testUrl} alt="Test image" />);
86
87 // Initially no image
88 expect(screen.queryByRole("img")).not.toBeInTheDocument();
89
90 // Click load button
91 const loadButton = screen.getByText("Load Image");
92 fireEvent.click(loadButton);
93
94 // Image should now be displayed (query by tag since it might be role="presentation")
95 const image = screen.getByAltText("Test image");
96 expect(image).toBeInTheDocument();
97 expect(image).toHaveAttribute("src", testUrl);
98 expect(image).toHaveAttribute("alt", "Test image");
99
100 // Warning message should be gone
101 expect(
102 screen.queryByText(
103 /Image blocked for security.*External images can leak data/,
104 ),
105 ).not.toBeInTheDocument();
106 });
107
108 it("should handle image load errors", async () => {
109 const testUrl = "https://example.com/broken-image.jpg";
110 render(<SecureImageComponent src={testUrl} alt="broken image" />);
111
112 // Click load button
113 const loadButton = screen.getByText("Load Image");
114 fireEvent.click(loadButton);
115
116 // Simulate image error (query by alt text since role might be presentation)
117 const image = screen.getByAltText("broken image");
118 fireEvent.error(image);
119
120 // Should show error message and hide image
121 await waitFor(() => {
122 expect(screen.getByText(/Failed to load image/)).toBeInTheDocument();
123 expect(screen.queryByAltText("broken image")).not.toBeInTheDocument();
124 });
125
126 // Load button should be available again
127 expect(screen.getByText("Load Image")).toBeInTheDocument();
128 });
129
130 it("should pass through title and className props", () => {
131 render(
132 <SecureImageComponent
133 src="https://example.com/image.jpg"
134 alt="test image"
135 title="Image title"
136 className="custom-class"
137 />,
138 );
139
140 // Click load button
141 fireEvent.click(screen.getByText("Load Image"));
142
143 // Check image has title (query by alt text)
144 const image = screen.getByAltText("test image");
145 expect(image).toHaveAttribute("title", "Image title");
146
147 // Check container has className
148 const container = image.parentElement;
149 expect(container).toHaveClass("custom-class");
150 });
151 });
152
153 describe("Security features", () => {
154 it("should display query parameters as JSON for transparency", () => {
155 render(
156 <SecureImageComponent src="https://malicious.com/track.gif?email=user@example.com&id=12345&action=view" />,
157 );
158
159 // Should show all parameters clearly
160 expect(
161 screen.getByText(/Warning: URL contains query parameters/),
162 ).toBeInTheDocument();
163
164 // Check JSON is properly formatted
165 const preElement = screen.getByText(/"email": "user@example.com"/);
166 expect(preElement).toBeInTheDocument();
167 expect(screen.getByText(/"id": "12345"/)).toBeInTheDocument();
168 expect(screen.getByText(/"action": "view"/)).toBeInTheDocument();
169 });
170
171 it("should handle encoded query parameters", () => {
172 render(
173 <SecureImageComponent src="https://example.com/img.png?data=%7B%22user%22%3A%22test%22%7D" />,
174 );
175
176 // Should decode and display the parameter
177 expect(
178 screen.getByText(/Warning: URL contains query parameters/),
179 ).toBeInTheDocument();
180 // The decoded value should be shown in the pre element
181 const preElement = document.querySelector("pre");
182 expect(preElement).toBeTruthy();
183 // Check that the JSON contains the decoded data
184 expect(preElement?.textContent).toContain('"data"');
185 // The value is decoded as a string containing JSON
186 expect(preElement?.textContent).toContain('"{');
187 expect(preElement?.textContent).toContain("user");
188 expect(preElement?.textContent).toContain("test");
189 });
190 });
191
192 describe("Alt text handling", () => {
193 it("should use empty string for alt when not provided", () => {
194 render(<SecureImageComponent src="https://example.com/image.jpg" />);
195
196 fireEvent.click(screen.getByText("Load Image"));
197
198 // Query by tag name since empty alt makes it role="presentation"
199 const images = document.querySelectorAll("img");
200 expect(images.length).toBe(1);
201 expect(images[0]).toHaveAttribute("alt", "");
202 });
203
204 it("should use provided alt text", () => {
205 render(
206 <SecureImageComponent
207 src="https://example.com/image.jpg"
208 alt="Description of image"
209 />,
210 );
211
212 fireEvent.click(screen.getByText("Load Image"));
213
214 const image = screen.getByAltText("Description of image");
215 expect(image).toHaveAttribute("alt", "Description of image");
216 });
217 });
218});