Skip to content

Commit 27a92d8

Browse files
committed
refactor: makes changes requested from PR review
1 parent f158b94 commit 27a92d8

File tree

2 files changed

+44
-35
lines changed

2 files changed

+44
-35
lines changed

modules/analysis/src/endpoints/tests/latest_filters.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@ use trustify_test_context::{TrustifyContext, subset::ContainsSubset};
88
#[test_context(TrustifyContext)]
99
#[rstest]
1010
#[case( // cpe search
11-
Req { loc: Loc::Id("cpe:/a:redhat:quay:3::el8"), ..Req::default() },
11+
Req { what: What::Id("cpe:/a:redhat:quay:3::el8"), ..Req::default() },
1212
2
1313
)]
1414
#[case( // cpe latest search
15-
Req { loc: Loc::Id("cpe:/a:redhat:quay:3::el8"), latest: true, ..Req::default() },
15+
Req { what: What::Id("cpe:/a:redhat:quay:3::el8"), latest: true, ..Req::default() },
1616
1
1717
)]
1818
#[case( // purl partial search
19-
Req { loc: Loc::Q("pkg:oci/quay-builder-qemu-rhcos-rhel8"), ancestors: Some(10), ..Req::default() },
19+
Req { what: What::Q("pkg:oci/quay-builder-qemu-rhcos-rhel8"), ancestors: Some(10), ..Req::default() },
2020
18
2121
)]
2222
#[case( // purl partial search latest
23-
Req { loc: Loc::Q("pkg:oci/quay-builder-qemu-rhcos-rhel8"), ancestors: Some(10), latest: true, ..Req::default() },
23+
Req { what: What::Q("pkg:oci/quay-builder-qemu-rhcos-rhel8"), ancestors: Some(10), latest: true, ..Req::default() },
2424
2
2525
)]
2626
#[case( // purl partial search latest
27-
Req { loc: Loc::Q("purl:name~quay-builder-qemu-rhcos-rhel8&purl:ty=oci"), ancestors: Some(10), latest: true, ..Req::default() },
27+
Req { what: What::Q("purl:name~quay-builder-qemu-rhcos-rhel8&purl:ty=oci"), ancestors: Some(10), latest: true, ..Req::default() },
2828
7
2929
)]
3030
#[test_log::test(actix_web::test)]
@@ -61,31 +61,31 @@ async fn resolve_rh_variant_latest_filter_container_cdx(
6161
#[test_context(TrustifyContext)]
6262
#[rstest]
6363
#[case( // cpe search
64-
Req { loc: Loc::Id("cpe:/a:redhat:rhel_eus:9.4::crb"), ..Req::default() },
64+
Req { what: What::Id("cpe:/a:redhat:rhel_eus:9.4::crb"), ..Req::default() },
6565
2
6666
)]
6767
#[case( // cpe latest search
68-
Req { loc: Loc::Id("cpe:/a:redhat:rhel_eus:9.4::crb"), latest: true, ..Req::default() },
68+
Req { what: What::Id("cpe:/a:redhat:rhel_eus:9.4::crb"), latest: true, ..Req::default() },
6969
1
7070
)]
7171
#[case( // purl partial search
72-
Req { loc: Loc::Q("pkg:rpm/redhat/NetworkManager-libnm"), ancestors: Some(10), ..Req::default() },
72+
Req { what: What::Q("pkg:rpm/redhat/NetworkManager-libnm"), ancestors: Some(10), ..Req::default() },
7373
30
7474
)]
7575
#[case( // purl partial latest search
76-
Req { loc: Loc::Q("pkg:rpm/redhat/NetworkManager-libnm"), ancestors: Some(10), latest: true, ..Req::default() },
76+
Req { what: What::Q("pkg:rpm/redhat/NetworkManager-libnm"), ancestors: Some(10), latest: true, ..Req::default() },
7777
15
7878
)]
7979
#[case( // purl more specific latest q search
80-
Req { loc: Loc::Q("pkg:rpm/redhat/NetworkManager-libnm-devel@"), latest: true, ..Req::default() },
80+
Req { what: What::Q("pkg:rpm/redhat/NetworkManager-libnm-devel@"), latest: true, ..Req::default() },
8181
5
8282
)]
8383
#[case( // name exact search
84-
Req { loc: Loc::Id("NetworkManager-libnm-devel"), ..Req::default() },
84+
Req { what: What::Id("NetworkManager-libnm-devel"), ..Req::default() },
8585
10
8686
)]
8787
#[case( // latest name exact search
88-
Req { loc: Loc::Id("NetworkManager-libnm-devel"), latest: true, ..Req::default() },
88+
Req { what: What::Id("NetworkManager-libnm-devel"), latest: true, ..Req::default() },
8989
5
9090
)]
9191
#[test_log::test(actix_web::test)]
@@ -120,27 +120,27 @@ async fn resolve_rh_variant_latest_filter_rpms_cdx(
120120
#[test_context(TrustifyContext)]
121121
#[rstest]
122122
#[case( // cpe search
123-
Req { loc: Loc::Id("cpe:/a:redhat:camel_quarkus:3"), ..Req::default() },
123+
Req { what: What::Id("cpe:/a:redhat:camel_quarkus:3"), ..Req::default() },
124124
2
125125
)]
126126
#[case( // cpe latest search
127-
Req { loc: Loc::Id("cpe:/a:redhat:camel_quarkus:3"), latest: true, ..Req::default() },
127+
Req { what: What::Id("cpe:/a:redhat:camel_quarkus:3"), latest: true, ..Req::default() },
128128
1
129129
)]
130130
#[case( // purl partial search
131-
Req { loc: Loc::Q("pkg:maven/io.vertx/vertx-core@"), ancestors: Some(10), ..Req::default() },
131+
Req { what: What::Q("pkg:maven/io.vertx/vertx-core@"), ancestors: Some(10), ..Req::default() },
132132
6
133133
)]
134134
#[case( // purl partial latest search
135-
Req { loc: Loc::Q("pkg:maven/io.vertx/vertx-core@"), latest: true, ..Req::default() },
135+
Req { what: What::Q("pkg:maven/io.vertx/vertx-core@"), latest: true, ..Req::default() },
136136
2
137137
)]
138138
#[case( // name exact search
139-
Req { loc: Loc::Id("vertx-core"), ..Req::default() },
139+
Req { what: What::Id("vertx-core"), ..Req::default() },
140140
6
141141
)]
142142
#[case( // latest name exact search
143-
Req { loc: Loc::Id("vertx-core"), latest: true, ..Req::default() },
143+
Req { what: What::Id("vertx-core"), latest: true, ..Req::default() },
144144
2
145145
)]
146146
#[test_log::test(actix_web::test)]
@@ -201,7 +201,7 @@ async fn test_tc2606(
201201
let response = app
202202
.req(Req {
203203
latest: true,
204-
loc: Loc::Id("cpe:/a:redhat:rhel_eus:9.4::appstream"),
204+
what: What::Id("cpe:/a:redhat:rhel_eus:9.4::appstream"),
205205
descendants: Some(1),
206206
..Req::default()
207207
})
@@ -300,7 +300,7 @@ async fn test_tc2677(
300300
let response = app
301301
.req(Req {
302302
latest: true,
303-
loc: Loc::Id("cpe:/a:redhat:3scale:2.15::el9"),
303+
what: What::Id("cpe:/a:redhat:3scale:2.15::el9"),
304304
descendants: Some(10),
305305
..Req::default()
306306
})
@@ -364,11 +364,11 @@ async fn test_tc2677(
364364
#[test_context(TrustifyContext)]
365365
#[rstest]
366366
#[case( // non-latest
367-
Req { loc: Loc::Id("pkg:maven/io.vertx/vertx-core"), ..Req::default() },
367+
Req { what: What::Id("pkg:maven/io.vertx/vertx-core"), ..Req::default() },
368368
0
369369
)]
370370
#[case( // latest
371-
Req { loc: Loc::Id("pkg:maven/io.vertx/vertx-core"), latest: true, ..Req::default() },
371+
Req { what: What::Id("pkg:maven/io.vertx/vertx-core"), latest: true, ..Req::default() },
372372
2
373373
)]
374374
#[test_log::test(actix_web::test)]
@@ -425,7 +425,7 @@ async fn test_tc2578(
425425
let response = app
426426
.req(Req {
427427
latest: true,
428-
loc: Loc::Id("cpe:/a:redhat:jboss_enterprise_application_platform:7.4"),
428+
what: What::Id("cpe:/a:redhat:jboss_enterprise_application_platform:7.4"),
429429
descendants: Some(100),
430430
..Req::default()
431431
})

modules/analysis/src/endpoints/tests/req.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,41 @@ use actix_http::{Request, StatusCode};
33
use actix_web::test::TestRequest;
44
use serde_json::Value;
55

6+
/// Request on the analysis API
67
#[derive(Default, Copy, Clone)]
78
pub struct Req<'a> {
9+
/// request against "latest" API
810
pub latest: bool,
11+
/// Level of ancestors to request
912
pub ancestors: Option<u64>,
13+
/// Level of descendants to request
1014
pub descendants: Option<u64>,
11-
pub loc: Loc<'a>,
15+
/// What is being requested
16+
pub what: What<'a>,
1217
}
1318

19+
/// Indication of what is being requested
1420
#[derive(Default, Copy, Clone)]
15-
pub enum Loc<'a> {
21+
pub enum What<'a> {
22+
/// Everything
1623
#[default]
1724
None,
25+
/// Search by `q` parameter
1826
Q(&'a str),
27+
/// By ID
1928
Id(&'a str),
2029
}
2130

2231
pub trait ReqExt {
32+
/// Process request
2333
async fn req(&self, req: Req<'_>) -> anyhow::Result<Value>;
2434
}
2535

2636
impl<C: CallService> ReqExt for C {
2737
async fn req(&self, req: Req<'_>) -> anyhow::Result<Value> {
2838
let Req {
2939
latest,
30-
loc,
40+
what: loc,
3141
ancestors,
3242
descendants,
3343
} = req;
@@ -37,19 +47,18 @@ impl<C: CallService> ReqExt for C {
3747
false => "",
3848
};
3949

50+
const BASE: &str = "/api/v2/analysis/";
51+
4052
let mut uri = match loc {
41-
Loc::None => {
42-
format!("/api/v2/analysis/{latest}component?",)
53+
What::None => {
54+
format!("{BASE}{latest}component?",)
4355
}
44-
Loc::Q(q) => {
45-
format!(
46-
"/api/v2/analysis/{latest}component?q={q}&",
47-
q = urlencoding::encode(q),
48-
)
56+
What::Q(q) => {
57+
format!("{BASE}{latest}component?q={q}&", q = urlencoding::encode(q),)
4958
}
50-
Loc::Id(id) => {
59+
What::Id(id) => {
5160
format!(
52-
"/api/v2/analysis/{latest}component/{id}?",
61+
"{BASE}{latest}component/{id}?",
5362
id = urlencoding::encode(id),
5463
)
5564
}

0 commit comments

Comments
 (0)