Skip to content

Commit 3aa3346

Browse files
authored
Merge pull request #479 from AmbireTech/channel-list-tests
Tests for GET /v5/channel/list and multichain support
2 parents d5199e6 + 02e6317 commit 3aa3346

File tree

4 files changed

+250
-67
lines changed

4 files changed

+250
-67
lines changed

adapter/src/ethereum/test_util.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ pub static KEYSTORES: Lazy<HashMap<Address, Options>> = Lazy::new(|| {
9292
pub const GANACHE_URL: &str = "http://localhost:8545";
9393

9494
// /// [`Chain`] of the locally running `ganache-cli` chain with id #1
95-
// pub static GANACHE_1: Lazy<Chain> = Lazy::new(|| GANACHE_INFO_1.chain.clone());
96-
97-
// /// [`ChainInfo`] of the locally running `ganache-cli` chain with id #1
98-
// pub static GANACHE_INFO_1: Lazy<ChainInfo> = Lazy::new(|| {
99-
// GANACHE_CONFIG
100-
// .chains
101-
// .get("Ganache #1")
102-
// .expect("Ganache Local chain 1 not found")
103-
// .clone()
104-
// });
95+
pub static GANACHE_1: Lazy<Chain> = Lazy::new(|| GANACHE_INFO_1.chain.clone());
96+
97+
/// [`ChainInfo`] of the locally running `ganache-cli` chain with id #1
98+
pub static GANACHE_INFO_1: Lazy<ChainInfo> = Lazy::new(|| {
99+
GANACHE_CONFIG
100+
.chains
101+
.get("Ganache #1")
102+
.expect("Ganache Local chain 1 not found")
103+
.clone()
104+
});
105105

106106
/// [`Chain`] of the locally running `ganache-cli` chain with id #1337
107107
pub static GANACHE_1337: Lazy<Chain> = Lazy::new(|| GANACHE_INFO_1337.chain.clone());

adview-manager/serve/src/main.rs

+57-53
Original file line numberDiff line numberDiff line change
@@ -151,63 +151,67 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
151151
});
152152

153153
// GET /preview/video
154-
let get_preview_of_video = warp::get().and(warp::path!("preview" / "video" )).then(move || {
155-
let tera = tera.clone();
154+
let get_preview_of_video = warp::get()
155+
.and(warp::path!("preview" / "video"))
156+
.then(move || {
157+
let tera = tera.clone();
156158

157-
async move {
158-
let whitelisted_tokens = vec!["0x6B175474E89094C44Da98b954EedeAC495271d0F"
159-
.parse()
160-
.expect("Valid token Address")];
161-
let disabled_video = false;
162-
let publisher_addr = "0x0000000000000000626f62627973686d75726461"
163-
.parse()
164-
.unwrap();
165-
166-
let options = Options {
167-
market_url: "http://placeholder.com".parse().unwrap(),
168-
market_slot: DUMMY_IPFS[0],
169-
publisher_addr,
170-
// All passed tokens must be of the same price and decimals, so that the amounts can be accurately compared
171-
whitelisted_tokens,
172-
size: Some(Size::new(728, 90)),
173-
// TODO: Check this value
174-
navigator_language: Some("bg".into()),
175-
/// Defaulted
176-
disabled_video,
177-
disabled_sticky: false,
178-
};
179-
180-
// legacy_728x90
181-
let video_ad_unit = adex_primitives::supermarket::units_for_slot::response::AdUnit {
182-
/// Same as `ipfs`
183-
id: "QmShJ6tsJ7LHLiYGX4EAmPyCPWJuCnvm6AKjweQPnw9qfh"
159+
async move {
160+
let whitelisted_tokens = vec!["0x6B175474E89094C44Da98b954EedeAC495271d0F"
184161
.parse()
185-
.expect("Valid IPFS"),
186-
media_url: "ipfs://Qmevmms1AZgYXS27A9ghSjHn4DSaHMfgYcD2SoGW14RYGy".to_string(),
187-
media_mime: "video/mp4".to_string(),
188-
target_url: "https://www.stremio.com/downloads".to_string(),
189-
};
190-
191-
let video_html = get_unit_html_with_events(
192-
&options,
193-
&video_ad_unit,
194-
"localhost",
195-
DUMMY_CAMPAIGN.id,
196-
&DUMMY_CAMPAIGN.validators,
197-
false,
198-
);
199-
200-
// let video_html = get_unit_html_with_events(&options, );
201-
let html = {
202-
let mut context = Context::new();
203-
context.insert("ad_code", &video_html);
162+
.expect("Valid token Address")];
163+
let disabled_video = false;
164+
let publisher_addr = "0x0000000000000000626f62627973686d75726461"
165+
.parse()
166+
.unwrap();
167+
168+
let options = Options {
169+
market_url: "http://placeholder.com".parse().unwrap(),
170+
market_slot: DUMMY_IPFS[0],
171+
publisher_addr,
172+
// All passed tokens must be of the same price and decimals, so that the amounts can be accurately compared
173+
whitelisted_tokens,
174+
size: Some(Size::new(728, 90)),
175+
// TODO: Check this value
176+
navigator_language: Some("bg".into()),
177+
/// Defaulted
178+
disabled_video,
179+
disabled_sticky: false,
180+
};
204181

205-
tera.render("ad.html", &context).expect("Should render")
206-
};
182+
// legacy_728x90
183+
let video_ad_unit =
184+
adex_primitives::supermarket::units_for_slot::response::AdUnit {
185+
/// Same as `ipfs`
186+
id: "QmShJ6tsJ7LHLiYGX4EAmPyCPWJuCnvm6AKjweQPnw9qfh"
187+
.parse()
188+
.expect("Valid IPFS"),
189+
media_url: "ipfs://Qmevmms1AZgYXS27A9ghSjHn4DSaHMfgYcD2SoGW14RYGy"
190+
.to_string(),
191+
media_mime: "video/mp4".to_string(),
192+
target_url: "https://www.stremio.com/downloads".to_string(),
193+
};
194+
195+
let video_html = get_unit_html_with_events(
196+
&options,
197+
&video_ad_unit,
198+
"localhost",
199+
DUMMY_CAMPAIGN.id,
200+
&DUMMY_CAMPAIGN.validators,
201+
false,
202+
);
203+
204+
// let video_html = get_unit_html_with_events(&options, );
205+
let html = {
206+
let mut context = Context::new();
207+
context.insert("ad_code", &video_html);
208+
209+
tera.render("ad.html", &context).expect("Should render")
210+
};
207211

208-
warp::reply::html(html)
209-
}
210-
});
212+
warp::reply::html(html)
213+
}
214+
});
211215

212216
warp::serve(get_ad.or(get_preview_of_video))
213217
.run(([127, 0, 0, 1], 3030))

docs/config/ganache.toml

+20
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,23 @@ sweeper = '0x7dD57C0324284102A757153E18F2Cb1ACdB7d2bD'
6767
# min_validator_fee = 1 * 10^-6 = 0.000_001
6868
min_validator_fee = '1000000000000'
6969

70+
71+
[chain."Ganache #1"]
72+
# Ganache default chain id for testing
73+
chain_id = 1
74+
rpc = 'http://localhost:8546'
75+
# Ganache Snapshot address
76+
outpace = '0x0000000000000000000000000000000000000001'
77+
# Ganache Snapshot address
78+
sweeper = '0x0000000000000000000000000000000000000001'
79+
80+
[chain."Ganache #1".token."Mocked TOKEN 2"]
81+
address = '0x0000000000000000000000000000000000000001' # checked
82+
precision = 18
83+
# Minimum token units for the Create2 deposits to count
84+
# 1 * 10^18 = 1.0000 TOKEN
85+
min_token_units_for_deposit = '1000000000000000000'
86+
# multiplier = 10^12 - 10^18 (token precision) = 10^-6
87+
# min_validator_fee = 1 * 10^-6 = 0.000_001
88+
min_validator_fee = '1000000000000'
89+

sentry/src/routes/channel.rs

+163-4
Original file line numberDiff line numberDiff line change
@@ -668,10 +668,18 @@ mod test {
668668
use crate::db::{insert_campaign, insert_channel};
669669
use crate::test_util::setup_dummy_app;
670670
use crate::CampaignRemaining;
671+
use adapter::{
672+
ethereum::test_util::{GANACHE_INFO_1, GANACHE_INFO_1337},
673+
primitives::Deposit as AdapterDeposit,
674+
};
671675
use hyper::StatusCode;
672676
use primitives::{
673-
test_util::{ADVERTISER, CREATOR, GUARDIAN, PUBLISHER, PUBLISHER_2},
674-
test_util::{DUMMY_CAMPAIGN, IDS},
677+
channel::Nonce,
678+
sentry::channel_list::ChannelListResponse,
679+
test_util::{
680+
ADVERTISER, CREATOR, DUMMY_CAMPAIGN, FOLLOWER, GUARDIAN, IDS, LEADER, LEADER_2,
681+
PUBLISHER, PUBLISHER_2,
682+
},
675683
BigNum, Deposit, UnifiedMap, ValidatorId,
676684
};
677685

@@ -690,7 +698,7 @@ mod test {
690698
};
691699

692700
let precision: u8 = channel_context.token.precision.into();
693-
let deposit = Deposit {
701+
let deposit = AdapterDeposit {
694702
total: BigNum::from_str("100000000000000000000").expect("should convert"), // 100 DAI
695703
still_on_create2: BigNum::from_str("1000000000000000000").expect("should convert"), // 1 DAI
696704
};
@@ -731,7 +739,7 @@ mod test {
731739
.expect("should return a spendable");
732740
assert!(spendable.is_some());
733741

734-
let updated_deposit = Deposit {
742+
let updated_deposit = AdapterDeposit {
735743
total: BigNum::from_str("110000000000000000000").expect("should convert"), // 110 DAI
736744
still_on_create2: BigNum::from_str("1100000000000000000").expect("should convert"), // 1.1 DAI
737745
};
@@ -771,6 +779,14 @@ mod test {
771779
accounting_response
772780
}
773781

782+
async fn res_to_channel_list_response(res: Response<Body>) -> ChannelListResponse {
783+
let json = hyper::body::to_bytes(res.into_body())
784+
.await
785+
.expect("Should get json");
786+
787+
serde_json::from_slice(&json).expect("Should deserialize ChannelListResponse")
788+
}
789+
774790
#[tokio::test]
775791
async fn get_accountings_for_channel() {
776792
let app = setup_dummy_app().await;
@@ -976,6 +992,149 @@ mod test {
976992
);
977993
}
978994

995+
#[tokio::test]
996+
async fn get_channels_list() {
997+
let mut app = setup_dummy_app().await;
998+
app.config.channels_find_limit = 2;
999+
1000+
let channel = Channel {
1001+
leader: IDS[&LEADER],
1002+
follower: IDS[&FOLLOWER],
1003+
guardian: *GUARDIAN,
1004+
token: GANACHE_INFO_1337.tokens["Mocked TOKEN"].address,
1005+
nonce: Nonce::from(987_654_321_u32),
1006+
};
1007+
insert_channel(&app.pool, channel)
1008+
.await
1009+
.expect("should insert");
1010+
let channel_other_token = Channel {
1011+
leader: IDS[&LEADER],
1012+
follower: IDS[&FOLLOWER],
1013+
guardian: *GUARDIAN,
1014+
token: GANACHE_INFO_1.tokens["Mocked TOKEN 2"].address,
1015+
nonce: Nonce::from(987_654_322_u32),
1016+
};
1017+
insert_channel(&app.pool, channel_other_token)
1018+
.await
1019+
.expect("should insert");
1020+
1021+
let channel_other_leader = Channel {
1022+
leader: IDS[&LEADER_2],
1023+
follower: IDS[&FOLLOWER],
1024+
guardian: *GUARDIAN,
1025+
token: GANACHE_INFO_1337.tokens["Mocked TOKEN"].address,
1026+
nonce: Nonce::from(987_654_323_u32),
1027+
};
1028+
insert_channel(&app.pool, channel_other_leader)
1029+
.await
1030+
.expect("should insert");
1031+
1032+
let build_request = |query: ChannelListQuery| {
1033+
let query = serde_urlencoded::to_string(query).expect("should parse query");
1034+
Request::builder()
1035+
.uri(format!("http://127.0.0.1/v5/channel/list?{}", query))
1036+
.extension(DUMMY_CAMPAIGN.channel.clone())
1037+
.body(Body::empty())
1038+
.expect("Should build Request")
1039+
};
1040+
1041+
// Test query page - page 0, page 1
1042+
{
1043+
let query = ChannelListQuery {
1044+
page: 0,
1045+
validator: None,
1046+
};
1047+
let res = channel_list(build_request(query), &app)
1048+
.await
1049+
.expect("should get channels");
1050+
let channels_list = res_to_channel_list_response(res).await;
1051+
1052+
assert_eq!(
1053+
channels_list.channels,
1054+
vec![channel, channel_other_token],
1055+
"The channels should be listed by ascending order of their creation"
1056+
);
1057+
assert_eq!(
1058+
channels_list.pagination.total_pages, 2,
1059+
"There should be 2 pages in total"
1060+
);
1061+
1062+
let query = ChannelListQuery {
1063+
page: 1,
1064+
validator: None,
1065+
};
1066+
let res = channel_list(build_request(query), &app)
1067+
.await
1068+
.expect("should get channels");
1069+
let channels_list = res_to_channel_list_response(res).await;
1070+
assert_eq!(
1071+
channels_list.channels,
1072+
vec![channel_other_leader],
1073+
"The channels should be listed by ascending order of their creation"
1074+
);
1075+
}
1076+
1077+
// Test query validator - query with validator ID
1078+
{
1079+
let query = ChannelListQuery {
1080+
page: 0,
1081+
validator: Some(IDS[&LEADER_2]),
1082+
};
1083+
let res = channel_list(build_request(query), &app)
1084+
.await
1085+
.expect("should get channels");
1086+
let channels_list = res_to_channel_list_response(res).await;
1087+
assert_eq!(
1088+
channels_list.channels,
1089+
vec![channel_other_leader],
1090+
"Response returns the correct channel"
1091+
);
1092+
assert_eq!(
1093+
channels_list.pagination.total_pages, 1,
1094+
"There should be 1 page in total"
1095+
);
1096+
}
1097+
// Test query with both pagination and validator by querying for the follower validator
1098+
{
1099+
let query = ChannelListQuery {
1100+
page: 0,
1101+
validator: Some(IDS[&FOLLOWER]),
1102+
};
1103+
let res = channel_list(build_request(query), &app)
1104+
.await
1105+
.expect("should get channels");
1106+
let channels_list = res_to_channel_list_response(res).await;
1107+
1108+
assert_eq!(
1109+
channels_list.pagination.total_pages, 2,
1110+
"There should be 2 pages in total"
1111+
);
1112+
assert_eq!(
1113+
channels_list.channels,
1114+
vec![channel, channel_other_token],
1115+
"The channels should be listed by ascending order of their creation"
1116+
);
1117+
1118+
let query = ChannelListQuery {
1119+
page: 1,
1120+
validator: Some(IDS[&FOLLOWER]),
1121+
};
1122+
let res = channel_list(build_request(query), &app)
1123+
.await
1124+
.expect("should get channels");
1125+
let channels_list = res_to_channel_list_response(res).await;
1126+
assert_eq!(
1127+
channels_list.channels,
1128+
vec![channel_other_leader],
1129+
"The channels should be listed by ascending order of their creation"
1130+
);
1131+
assert_eq!(
1132+
channels_list.pagination.total_pages, 2,
1133+
"There should be 2 pages in total"
1134+
);
1135+
}
1136+
}
1137+
9791138
#[tokio::test]
9801139
async fn payouts_for_earners_test() {
9811140
let app = setup_dummy_app().await;

0 commit comments

Comments
 (0)