Skip to content

Commit 2f7565f

Browse files
committed
Added test cases for analytics with another chain
1 parent 6d3de12 commit 2f7565f

File tree

4 files changed

+151
-13
lines changed

4 files changed

+151
-13
lines changed

Cargo.lock

Lines changed: 13 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sentry/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ once_cell = "1.5.2"
6161
woothee = "0.13"
6262
# Making requests to the platform
6363
reqwest = { version = "0.11", features = ["json", "cookies"] }
64+
serde_qs = "0.9.2"
6465

6566
[dev-dependencies]
6667
pretty_assertions = "1"

sentry/src/routes/analytics.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ pub async fn analytics<C: Locked + 'static>(
1919
request_allowed: Option<HashSet<AllowedKey>>,
2020
authenticate_as: Option<AuthenticateAs>,
2121
) -> Result<Response<Body>, ResponseError> {
22-
let mut query = serde_urlencoded::from_str::<AnalyticsQuery>(req.uri().query().unwrap_or(""))?;
23-
22+
let mut query = serde_qs::from_str::<AnalyticsQuery>(req.uri().query().unwrap_or(""))?;
2423
// If we have a route that requires authentication the Chain will be extracted
2524
// from the sentry's authentication, which guarantees the value will exist
2625
// This will also override a query parameter for the chain if it is provided

sentry/src/routes/routers.rs

Lines changed: 136 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,26 @@ mod analytics_router_test {
266266
update_analytics(pool, analytics_two_years_ago)
267267
.await
268268
.expect("Should update analytics");
269+
270+
let analytics_other_chain = UpdateAnalytics {
271+
time: base_datehour,
272+
campaign_id: DUMMY_CAMPAIGN.id,
273+
ad_unit: DUMMY_IPFS[0],
274+
ad_slot: DUMMY_IPFS[1],
275+
ad_slot_type: None,
276+
advertiser: *ADVERTISER,
277+
publisher: *PUBLISHER,
278+
hostname: None,
279+
country: Some("Bulgaria".to_string()),
280+
os_name: OperatingSystem::map_os("Windows"),
281+
chain_id: ChainId::new(2),
282+
event_type: CLICK,
283+
amount_to_add: UnifiedNum::from_u64(1_000_000),
284+
count_to_add: 1,
285+
};
286+
update_analytics(pool, analytics_other_chain)
287+
.await
288+
.expect("Should update analytics");
269289
}
270290

271291
#[tokio::test]
@@ -411,9 +431,9 @@ mod analytics_router_test {
411431
hostname: None,
412432
country: None,
413433
os_name: None,
414-
chains: None,
434+
chains: Some(vec![ChainId::new(1)]),
415435
};
416-
let query = serde_urlencoded::to_string(query).expect("should parse query");
436+
let query = serde_qs::to_string(&query).expect("should parse query");
417437
let req = Request::builder()
418438
.uri(format!("http://127.0.0.1/v5/analytics?{}", query))
419439
.body(Body::empty())
@@ -458,9 +478,9 @@ mod analytics_router_test {
458478
hostname: None,
459479
country: None,
460480
os_name: None,
461-
chains: None,
481+
chains: Some(vec![ChainId::new(1)]),
462482
};
463-
let query = serde_urlencoded::to_string(query).expect("should parse query");
483+
let query = serde_qs::to_string(&query).expect("should parse query");
464484
let req = Request::builder()
465485
.uri(format!("http://127.0.0.1/v5/analytics?{}", query))
466486
.body(Body::empty())
@@ -509,9 +529,9 @@ mod analytics_router_test {
509529
hostname: None,
510530
country: None,
511531
os_name: None,
512-
chains: None,
532+
chains: Some(vec![ChainId::new(1)]),
513533
};
514-
let query = serde_urlencoded::to_string(query).expect("should serialize query");
534+
let query = serde_qs::to_string(&query).expect("should parse query");
515535
let req = Request::builder()
516536
.uri(format!("http://127.0.0.1/v5/analytics?{}", query))
517537
.body(Body::empty())
@@ -561,9 +581,9 @@ mod analytics_router_test {
561581
hostname: None,
562582
country: Some("Bulgaria".into()),
563583
os_name: None,
564-
chains: None,
584+
chains: Some(vec![ChainId::new(1)]),
565585
};
566-
let query = serde_urlencoded::to_string(query).expect("should parse query");
586+
let query = serde_qs::to_string(&query).expect("should parse query");
567587
let req = Request::builder()
568588
.uri(format!("http://127.0.0.1/v5/analytics?{}", query))
569589
.body(Body::empty())
@@ -859,6 +879,56 @@ mod analytics_router_test {
859879
.collect::<Vec<_>>(),
860880
);
861881
}
882+
883+
// Test with a different chain
884+
// with base date hour
885+
// event type: CLICK
886+
{
887+
let query = AnalyticsQuery {
888+
limit: 1000,
889+
event_type: CLICK,
890+
metric: Metric::Count,
891+
segment_by: None,
892+
time: Time {
893+
timeframe: Timeframe::Day,
894+
start: base_datehour - 1,
895+
end: None,
896+
},
897+
campaign_id: None,
898+
ad_unit: None,
899+
ad_slot: None,
900+
ad_slot_type: None,
901+
advertiser: None,
902+
publisher: None,
903+
hostname: None,
904+
country: None,
905+
os_name: None,
906+
chains: Some(vec![ChainId::new(2)]),
907+
};
908+
let query = serde_qs::to_string(&query).expect("should parse query");
909+
let req = Request::builder()
910+
.uri(format!("http://127.0.0.1/v5/analytics?{}", query))
911+
.body(Body::empty())
912+
.expect("Should build Request");
913+
914+
let analytics_response = analytics_router(req, &app)
915+
.await
916+
.expect("Should get analytics data");
917+
let json = hyper::body::to_bytes(analytics_response.into_body())
918+
.await
919+
.expect("Should get json");
920+
921+
let fetched_analytics: Vec<FetchedAnalytics> =
922+
serde_json::from_slice(&json).expect("Should get analytics response");
923+
assert_eq!(
924+
vec![FetchedMetric::Count(1)],
925+
fetched_analytics
926+
.iter()
927+
.map(|analytics| analytics.value)
928+
.collect::<Vec<_>>(),
929+
);
930+
}
931+
862932
// Test with timeframe=day and start_date= 2 or more days ago to check if the results vec is split properly
863933
}
864934

@@ -1004,6 +1074,25 @@ mod analytics_router_test {
10041074
update_analytics(pool, analytics_different_publisher_advertiser)
10051075
.await
10061076
.expect("Should update analytics");
1077+
let analytics_different_chain = UpdateAnalytics {
1078+
time: base_datehour,
1079+
campaign_id: DUMMY_CAMPAIGN.id,
1080+
ad_unit: DUMMY_IPFS[0],
1081+
ad_slot: DUMMY_IPFS[1],
1082+
ad_slot_type: None,
1083+
advertiser: *ADVERTISER,
1084+
publisher: *PUBLISHER,
1085+
hostname: None,
1086+
country: Some("Bulgaria".to_string()),
1087+
os_name: OperatingSystem::map_os("Windows"),
1088+
chain_id: ChainId::new(2),
1089+
event_type: CLICK,
1090+
amount_to_add: UnifiedNum::from_u64(1_000_000),
1091+
count_to_add: 1,
1092+
};
1093+
update_analytics(pool, analytics_different_chain)
1094+
.await
1095+
.expect("Should update analytics");
10071096
}
10081097

10091098
#[tokio::test]
@@ -1052,6 +1141,13 @@ mod analytics_router_test {
10521141
uid: IDS[&LEADER],
10531142
chain: DUMMY_CHAIN.clone(),
10541143
};
1144+
let mut different_chain = DUMMY_CHAIN.clone();
1145+
different_chain.chain_id = ChainId::new(2);
1146+
let admin_auth_other_chain = Auth {
1147+
era: 0,
1148+
uid: IDS[&LEADER],
1149+
chain: different_chain,
1150+
};
10551151

10561152
// test for publisher
10571153
{
@@ -1163,9 +1259,9 @@ mod analytics_router_test {
11631259
hostname: Some("localhost".into()),
11641260
country: Some("Bulgaria".into()),
11651261
os_name: Some(OperatingSystem::map_os("Windows")),
1166-
chains: None,
1262+
chains: Some(vec![ChainId::new(1)]),
11671263
};
1168-
let query = serde_urlencoded::to_string(query).expect("should parse query");
1264+
let query = serde_qs::to_string(&query).expect("should parse query");
11691265
let req = Request::builder()
11701266
.extension(admin_auth.clone())
11711267
.uri(format!("http://127.0.0.1/v5/analytics/for-admin?{}", query))
@@ -1188,6 +1284,36 @@ mod analytics_router_test {
11881284
);
11891285
}
11901286

1287+
// test for admin with a different chain
1288+
{
1289+
let req = Request::builder()
1290+
.extension(admin_auth_other_chain.clone())
1291+
.uri(format!(
1292+
"http://127.0.0.1/v5/analytics/for-admin?{}",
1293+
base_query
1294+
))
1295+
.body(Body::empty())
1296+
.expect("Should build Request");
1297+
1298+
let analytics_response = analytics_router(req, &app)
1299+
.await
1300+
.expect("Should get analytics data");
1301+
let json = hyper::body::to_bytes(analytics_response.into_body())
1302+
.await
1303+
.expect("Should get json");
1304+
1305+
let fetched_analytics: Vec<FetchedAnalytics> =
1306+
serde_json::from_slice(&json).expect("Should get analytics response");
1307+
assert_eq!(
1308+
vec![FetchedMetric::Count(1)],
1309+
fetched_analytics
1310+
.iter()
1311+
.map(|fetched| fetched.value)
1312+
.collect::<Vec<_>>(),
1313+
);
1314+
}
1315+
1316+
11911317
// TODO: Move test to a analytics_router test
11921318
// test with no authUid
11931319
// let req = Request::builder()

0 commit comments

Comments
 (0)