Skip to content

Commit 8e004c2

Browse files
AshwinGajbhiyeAbhishek
authored andcommitted
fix: add background color to TLP badges and include TLP in job API. C… (intelowlproject#3284)
* fix: add background color to TLP badges and include TLP in job API. Closes intelowlproject#3150 - Added backgroundColor property to TLP badge component styling - Implemented accessible text color logic (black for CLEAR/GREEN/AMBER, white for RED) - Added tlp field to JobSerializer to return TLP value in job details API response - Fixed issue where TLP badges showed only borders without background colors on job report page * test: add tlp field assertion in job retrieve test * fix: use unique IDs for TLPTag badges and add unit tests - Replace simple value-based IDs with counter-based unique IDs to prevent duplicate DOM IDs when multiple badges with same TLP value are rendered - Add comprehensive Jest tests for TLPTag component covering: * Background and text color rendering for all TLP levels (CLEAR, RED, GREEN, AMBER) * Unique ID generation for multiple instances - Addresses Copilot review feedback for duplicate ID issue and missing test coverage * fix: resolve ESLint no-plusplus error in TLPTag unique ID generation - Replace postfix increment (idCounter++) with prefix increment (idCounter += 1) - Maintains unique ID generation for TLP badges without ESLint violations * fix: resolve Prettier formatting issues in TLPTag tests
1 parent ec062ea commit 8e004c2

File tree

4 files changed

+95
-1
lines changed

4 files changed

+95
-1
lines changed

api_app/serializers/job.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ class Meta:
507507
"tags",
508508
"comments",
509509
"status",
510+
"tlp",
510511
"pivots_to_execute",
511512
"analyzers_to_execute",
512513
"analyzers_requested",

frontend/src/components/common/TLPTag.jsx

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,21 @@ import { TLPColors } from "../../constants/colorConst";
55
import { TLPDescriptions } from "../../constants/miscConst";
66
import { TlpChoices } from "../../constants/advancedSettingsConst";
77

8+
let idCounter = 0;
9+
810
export function TLPTag(props) {
911
const { value, ...rest } = props;
10-
const badgeId = `tlptag-badge__${value}`;
12+
const uniqueId = React.useRef(idCounter).current;
13+
idCounter += 1;
14+
const badgeId = `tlptag-badge__${value}__${uniqueId}`;
1115
const color = TLPColors?.[value] || "#dfe1e2";
1216
const tooltipText = TLPDescriptions?.[value] || "invalid";
17+
const textColorMap = {
18+
CLEAR: "#000000",
19+
GREEN: "#000000",
20+
AMBER: "#000000",
21+
};
22+
const textColor = textColorMap[value] || "#FFFFFF";
1323

1424
return value ? (
1525
<Badge
@@ -18,7 +28,9 @@ export function TLPTag(props) {
1828
style={{
1929
borderRadius: 5,
2030
userSelect: "none",
31+
backgroundColor: color,
2132
border: `1px solid ${color}`,
33+
color: textColor,
2234
}}
2335
{...rest}
2436
>
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import React from "react";
2+
import "@testing-library/jest-dom";
3+
import { render } from "@testing-library/react";
4+
import { TLPTag } from "../../../src/components/common/TLPTag";
5+
import { TLPColors } from "../../../src/constants/colorConst";
6+
7+
describe("TLPTag component", () => {
8+
test("CLEAR TLP renders with correct background and text color", () => {
9+
const { container } = render(<TLPTag value="CLEAR" />);
10+
11+
const badge = container.querySelector('[id^="tlptag-badge__CLEAR"]');
12+
expect(badge).toBeInTheDocument();
13+
expect(badge).toHaveTextContent("CLEAR");
14+
15+
// CLEAR should have white background and black text
16+
expect(badge).toHaveStyle({
17+
backgroundColor: TLPColors.CLEAR,
18+
color: "#000000",
19+
});
20+
});
21+
22+
test("RED TLP renders with correct background and text color", () => {
23+
const { container } = render(<TLPTag value="RED" />);
24+
25+
const badge = container.querySelector('[id^="tlptag-badge__RED"]');
26+
expect(badge).toBeInTheDocument();
27+
expect(badge).toHaveTextContent("RED");
28+
29+
// RED should have red background and white text
30+
expect(badge).toHaveStyle({
31+
backgroundColor: TLPColors.RED,
32+
color: "#FFFFFF",
33+
});
34+
});
35+
36+
test("GREEN TLP renders with correct background and text color", () => {
37+
const { container } = render(<TLPTag value="GREEN" />);
38+
39+
const badge = container.querySelector('[id^="tlptag-badge__GREEN"]');
40+
expect(badge).toBeInTheDocument();
41+
expect(badge).toHaveTextContent("GREEN");
42+
43+
// GREEN should have green background and black text
44+
expect(badge).toHaveStyle({
45+
backgroundColor: TLPColors.GREEN,
46+
color: "#000000",
47+
});
48+
});
49+
50+
test("AMBER TLP renders with correct background and text color", () => {
51+
const { container } = render(<TLPTag value="AMBER" />);
52+
53+
const badge = container.querySelector('[id^="tlptag-badge__AMBER"]');
54+
expect(badge).toBeInTheDocument();
55+
expect(badge).toHaveTextContent("AMBER");
56+
57+
// AMBER should have amber background and black text
58+
expect(badge).toHaveStyle({
59+
backgroundColor: TLPColors.AMBER,
60+
color: "#000000",
61+
});
62+
});
63+
64+
test("Multiple TLPTag instances with same value have unique IDs", () => {
65+
const { container } = render(
66+
<>
67+
<TLPTag value="RED" />
68+
<TLPTag value="RED" />
69+
</>,
70+
);
71+
72+
const badges = container.querySelectorAll('[id^="tlptag-badge__RED"]');
73+
expect(badges).toHaveLength(2);
74+
75+
// Ensure each badge has a unique ID
76+
const id1 = badges[0].getAttribute("id");
77+
const id2 = badges[1].getAttribute("id");
78+
expect(id1).not.toEqual(id2);
79+
});
80+
});

tests/api_app/test_views.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ def test_retrieve_200(self):
290290
self.assertEqual(response.status_code, 200)
291291
self.assertEqual(content["id"], self.job.id, msg=msg)
292292
self.assertEqual(content["status"], self.job.status, msg=msg)
293+
self.assertEqual(content["tlp"], self.job.tlp, msg=msg)
293294
self.assertEqual(content["investigation_id"], self.investigation1.pk)
294295
self.assertEqual(content["investigation_name"], self.investigation1.name)
295296
self.assertEqual(content["analyzable_id"], self.analyzable.pk)

0 commit comments

Comments
 (0)