Skip to content

Commit ba98252

Browse files
authored
Merge pull request #527 from nttcom/topic/delete-threat-impact
chage topic sort key
2 parents 9d55f3b + 9f401a8 commit ba98252

File tree

5 files changed

+40
-38
lines changed

5 files changed

+40
-38
lines changed

api/app/business/topic_business.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77

88
def get_sorted_topics(topics: Sequence[models.Topic]) -> Sequence[models.Topic]:
99
"""
10-
Sort topics with standard sort rules -- (threat_impact ASC, updated_at DESC)
10+
Sort topics with standard sort rules -- (cvss_v3_score DESC, updated_at DESC)
1111
"""
1212
return sorted(
1313
topics,
1414
key=lambda topic: (
15-
topic.threat_impact,
16-
-(dt.timestamp() if (dt := topic.updated_at) else 0),
15+
topic.cvss_v3_score if topic.cvss_v3_score is not None else -1,
16+
dt.timestamp() if (dt := topic.updated_at) else 0,
1717
),
18+
reverse=True,
1819
)
1920

2021

api/app/command.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,21 @@
1717
from app import models, schemas
1818

1919
sortkey2orderby: dict[schemas.TopicSortKey, list] = {
20-
schemas.TopicSortKey.THREAT_IMPACT: [
21-
models.Topic.threat_impact,
20+
schemas.TopicSortKey.CVSS_V3_SCORE: [
21+
models.Topic.cvss_v3_score.nulls_first(),
2222
models.Topic.updated_at.desc(),
2323
],
24-
schemas.TopicSortKey.THREAT_IMPACT_DESC: [
25-
models.Topic.threat_impact.desc(),
24+
schemas.TopicSortKey.CVSS_V3_SCORE_DESC: [
25+
models.Topic.cvss_v3_score.desc().nullslast(),
2626
models.Topic.updated_at.desc(),
2727
],
2828
schemas.TopicSortKey.UPDATED_AT: [
2929
models.Topic.updated_at,
30-
models.Topic.threat_impact,
30+
models.Topic.cvss_v3_score.desc().nullslast(),
3131
],
3232
schemas.TopicSortKey.UPDATED_AT_DESC: [
3333
models.Topic.updated_at.desc(),
34-
models.Topic.threat_impact,
34+
models.Topic.cvss_v3_score.desc().nullslast(),
3535
],
3636
}
3737

@@ -94,7 +94,7 @@ def search_topics_internal(
9494
db: Session,
9595
offset: int = 0,
9696
limit: int = 10,
97-
sort_key: schemas.TopicSortKey = schemas.TopicSortKey.THREAT_IMPACT,
97+
sort_key: schemas.TopicSortKey = schemas.TopicSortKey.CVSS_V3_SCORE_DESC,
9898
threat_impacts: list[int] | None = None,
9999
title_words: list[str | None] | None = None,
100100
abstract_words: list[str | None] | None = None,

api/app/routers/topics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def get_topics(
5151
def search_topics(
5252
offset: int = Query(0, ge=0),
5353
limit: int = Query(10, ge=1, le=100), # 10 is default in web/src/pages/TopicManagement.jsx
54-
sort_key: schemas.TopicSortKey = Query(schemas.TopicSortKey.THREAT_IMPACT),
54+
sort_key: schemas.TopicSortKey = Query(schemas.TopicSortKey.CVSS_V3_SCORE_DESC),
5555
threat_impacts: list[int] | None = Query(None),
5656
topic_ids: list[str] | None = Query(None),
5757
title_words: list[str] | None = Query(None),

api/app/schemas.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ class ORMModel(BaseModel):
2323

2424

2525
class TopicSortKey(str, Enum):
26-
THREAT_IMPACT = "threat_impact"
27-
THREAT_IMPACT_DESC = "threat_impact_desc"
26+
CVSS_V3_SCORE = "cvss_v3_score"
27+
CVSS_V3_SCORE_DESC = "cvss_v3_score_desc"
2828
UPDATED_AT = "updated_at"
2929
UPDATED_AT_DESC = "updated_at_desc"
3030

api/app/tests/medium/routers/test_topics.py

+26-25
Original file line numberDiff line numberDiff line change
@@ -263,11 +263,11 @@ def test_get_all_topics():
263263
create_user(USER2)
264264
create_pteam(USER1, PTEAM1)
265265

266-
topic1 = create_topic(USER1, {**TOPIC1, "threat_impact": 1}, actions=[ACTION1, ACTION2])
267-
topic2 = create_topic(USER1, {**TOPIC2, "threat_impact": 2}, actions=[ACTION3])
268-
topic3 = create_topic(USER1, {**TOPIC3, "threat_impact": 3})
269-
topic4 = create_topic(USER1, {**TOPIC4, "threat_impact": 2})
270-
topic5 = create_topic(USER2, {**TOPIC1, "threat_impact": 1, "topic_id": str(uuid4())})
266+
topic1 = create_topic(USER1, {**TOPIC1, "cvss_v3_score": 10}, actions=[ACTION1, ACTION2])
267+
topic2 = create_topic(USER1, {**TOPIC2, "cvss_v3_score": 2}, actions=[ACTION3])
268+
topic3 = create_topic(USER1, {**TOPIC3, "cvss_v3_score": None})
269+
topic4 = create_topic(USER1, {**TOPIC4, "cvss_v3_score": 2})
270+
topic5 = create_topic(USER2, {**TOPIC1, "cvss_v3_score": 10, "topic_id": str(uuid4())})
271271

272272
data = assert_200(client.get("/topics", headers=headers(USER1)))
273273
assert len(data) == 5
@@ -985,6 +985,7 @@ def create_minimal_topic(user: dict, params: dict) -> schemas.TopicCreateRespons
985985
"title": "",
986986
"abstract": "",
987987
"threat_impact": 1,
988+
"cvss_v3_score": 10.0,
988989
**params,
989990
"exploitation": "active",
990991
"automatable": "yes",
@@ -1464,16 +1465,16 @@ def try_search_topics(user, topics_dict, search_params, expected):
14641465
class TestSearchResultSlice(ExtCommonForResultSlice_):
14651466
@pytest.fixture(scope="function", autouse=True)
14661467
def setup_for_result_slice(self):
1467-
self.topic1 = self.create_minimal_topic(USER1, {"threat_impact": 1})
1468-
self.topic2 = self.create_minimal_topic(USER1, {"threat_impact": 2})
1469-
self.topic3 = self.create_minimal_topic(USER1, {"threat_impact": 3})
1470-
self.topic4 = self.create_minimal_topic(USER1, {"threat_impact": 4})
1471-
self.topic5 = self.create_minimal_topic(USER1, {"threat_impact": 1})
1472-
self.topic6 = self.create_minimal_topic(USER1, {"threat_impact": 2})
1473-
self.topic7 = self.create_minimal_topic(USER1, {"threat_impact": 3})
1474-
update_topic(USER1, self.topic5, {"threat_impact": 2})
1475-
update_topic(USER1, self.topic2, {"threat_impact": 1})
1476-
update_topic(USER1, self.topic6, {"threat_impact": 3})
1468+
self.topic1 = self.create_minimal_topic(USER1, {"cvss_v3_score": 10})
1469+
self.topic2 = self.create_minimal_topic(USER1, {"cvss_v3_score": 8})
1470+
self.topic3 = self.create_minimal_topic(USER1, {"cvss_v3_score": 3})
1471+
self.topic4 = self.create_minimal_topic(USER1, {"cvss_v3_score": None})
1472+
self.topic5 = self.create_minimal_topic(USER1, {"cvss_v3_score": 10})
1473+
self.topic6 = self.create_minimal_topic(USER1, {"cvss_v3_score": 8})
1474+
self.topic7 = self.create_minimal_topic(USER1, {"cvss_v3_score": 3})
1475+
update_topic(USER1, self.topic5, {"cvss_v3_score": 8})
1476+
update_topic(USER1, self.topic2, {"cvss_v3_score": 10})
1477+
update_topic(USER1, self.topic6, {"cvss_v3_score": 3})
14771478
self.topics = {
14781479
1: self.topic1,
14791480
2: self.topic2,
@@ -1496,16 +1497,16 @@ def setup_for_result_slice(self):
14961497
# sort_key
14971498
(
14981499
(None, None, None), # check defaults
1499-
([2, 1, 5, 6, 7, 3, 4], 7, 0, 10, "threat_impact"),
1500+
([2, 1, 5, 6, 7, 3, 4], 7, 0, 10, "cvss_v3_score_desc"),
15001501
),
15011502
((None, None, "my_sort_key"), "422: Unprocessable Entity: "),
15021503
(
1503-
(None, None, "threat_impact"), # implicit 2nd key is updated_at_desc
1504-
([2, 1, 5, 6, 7, 3, 4], 7, 0, 10, "threat_impact"),
1504+
(None, None, "cvss_v3_score_desc"), # implicit 2nd key is updated_at_desc
1505+
([2, 1, 5, 6, 7, 3, 4], 7, 0, 10, "cvss_v3_score_desc"),
15051506
),
15061507
(
1507-
(None, None, "threat_impact_desc"), # implicit 2nd key is updated_at_desc
1508-
([4, 6, 7, 3, 5, 2, 1], 7, 0, 10, "threat_impact_desc"),
1508+
(None, None, "cvss_v3_score"), # implicit 2nd key is updated_at_desc
1509+
([4, 6, 7, 3, 5, 2, 1], 7, 0, 10, "cvss_v3_score"),
15091510
),
15101511
(
15111512
(None, None, "updated_at"), # implicit 2nd key is threat_impact
@@ -1518,29 +1519,29 @@ def setup_for_result_slice(self):
15181519
# offset
15191520
(
15201521
(0, None, None),
1521-
([2, 1, 5, 6, 7, 3, 4], 7, 0, 10, "threat_impact"),
1522+
([2, 1, 5, 6, 7, 3, 4], 7, 0, 10, "cvss_v3_score_desc"),
15221523
),
15231524
(("xxx", None, None), "422: Unprocessable Entity: "),
15241525
((-1, None, None), "422: Unprocessable Entity: "), # offset should be >=0
15251526
(
15261527
(5, None, None),
1527-
([3, 4], 7, 5, 10, "threat_impact"),
1528+
([3, 4], 7, 5, 10, "cvss_v3_score_desc"),
15281529
),
15291530
(
15301531
(10, None, None),
1531-
([], 7, 10, 10, "threat_impact"),
1532+
([], 7, 10, 10, "cvss_v3_score_desc"),
15321533
),
15331534
# limit
15341535
(
15351536
(None, 10, None),
1536-
([2, 1, 5, 6, 7, 3, 4], 7, 0, 10, "threat_impact"),
1537+
([2, 1, 5, 6, 7, 3, 4], 7, 0, 10, "cvss_v3_score_desc"),
15371538
),
15381539
((None, "xxx", None), "422: Unprocessable Entity: "),
15391540
((None, 0, None), "422: Unprocessable Entity: "), # limit should be >= 1
15401541
((None, 101, None), "422: Unprocessable Entity: "), # limit should be <= 100
15411542
(
15421543
(None, 5, None),
1543-
([2, 1, 5, 6, 7], 7, 0, 5, "threat_impact"),
1544+
([2, 1, 5, 6, 7], 7, 0, 5, "cvss_v3_score_desc"),
15441545
),
15451546
# complex
15461547
(

0 commit comments

Comments
 (0)