Skip to content

Commit b627148

Browse files
committed
validator_worker - SentryApi - improve test
1 parent d731242 commit b627148

File tree

4 files changed

+42
-40
lines changed

4 files changed

+42
-40
lines changed

sentry/src/routes/campaign.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1470,11 +1470,9 @@ mod test {
14701470

14711471
let chain_1_token = GANACHE_CONFIG
14721472
.chains
1473-
.get("Ganache #1")
1474-
.unwrap()
1473+
["Ganache #1"]
14751474
.tokens
1476-
.get("Mocked TOKEN 2")
1477-
.unwrap()
1475+
["Mocked TOKEN 1"]
14781476
.address;
14791477
// Setting up new follower and a channel and campaign which use it on Ganache #1
14801478
let dummy_follower_2 = ValidatorDesc {

sentry/src/routes/channel.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -938,9 +938,11 @@ mod test {
938938
total: BigNum::from_str("100000000000000000000").expect("should convert"), // 100 DAI
939939
still_on_create2: BigNum::from_str("1000000000000000000").expect("should convert"), // 1 DAI
940940
};
941-
app.adapter
942-
.client
943-
.add_deposit_call(channel_context.context.id(), *CREATOR, deposit.clone());
941+
app.adapter.client.add_deposit_call(
942+
channel_context.context.id(),
943+
*CREATOR,
944+
deposit.clone(),
945+
);
944946

945947
insert_channel(&app.pool, channel_context.context)
946948
.await

test_harness/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub static SNAPSHOT_CONTRACTS_1337: Lazy<Contracts> = Lazy::new(|| {
3636
let token_info = ganache_chain_info
3737
.tokens
3838
.get("Mocked TOKEN 1337")
39-
.expect("Ganache config should contain for Chain #1337 the Mocked TOKEN");
39+
.expect("Ganache config should contain for Chain #1337 the Mocked TOKEN 1337");
4040
let chain = ganache_chain_info.chain.clone();
4141

4242
let token = Erc20Token::new(&web3, token_info.clone());
@@ -68,7 +68,7 @@ pub static SNAPSHOT_CONTRACTS_1: Lazy<Contracts> = Lazy::new(|| {
6868
let token_info = ganache_chain_info
6969
.tokens
7070
.get("Mocked TOKEN 1")
71-
.expect("Ganache config should contain for Chain #1 the Mocked TOKEN");
71+
.expect("Ganache config should contain for Chain #1 the Mocked TOKEN 1");
7272

7373
let token = Erc20Token::new(&web3, token_info.clone());
7474

validator_worker/src/sentry_interface.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -619,15 +619,18 @@ pub mod campaigns {
619619
#[cfg(test)]
620620
mod test {
621621
use super::*;
622-
use adapter::dummy::{Adapter, Dummy, Options};
622+
use adapter::{
623+
dummy::{Adapter, Dummy, Options},
624+
ethereum::test_util::GANACHE_INFO_1,
625+
};
623626
use primitives::{
624627
campaign::validators::Validators as CampaignValidators,
625628
config::{configuration, Environment, GANACHE_CONFIG},
626629
sentry::{
627630
campaign_list::CampaignListResponse, channel_list::ChannelListResponse, Pagination,
628631
},
629632
test_util::{
630-
discard_logger, ADVERTISER, ADVERTISER_2, CREATOR, DUMMY_CAMPAIGN,
633+
discard_logger, ADVERTISER, ADVERTISER_2, CREATOR, DUMMY_AUTH, DUMMY_CAMPAIGN,
631634
DUMMY_VALIDATOR_FOLLOWER, DUMMY_VALIDATOR_LEADER, FOLLOWER, GUARDIAN, IDS, LEADER,
632635
LEADER_2, PUBLISHER, PUBLISHER_2,
633636
},
@@ -639,6 +642,22 @@ mod test {
639642
Mock, MockServer, ResponseTemplate,
640643
};
641644

645+
/// Uses the [`Dummy`] adapter with [`DUMMY_AUTH`] as the authentication tokens.
646+
/// Sentry url can be provided, for `wiremock` to be able to mock the calls in [`SentryApi`].
647+
pub fn setup_dummy_sentry(
648+
whoami: ValidatorId,
649+
config: Config,
650+
sentry_url: ApiUrl,
651+
) -> SentryApi<Dummy, ()> {
652+
let adapter = Adapter::with_unlocked(Dummy::init(Options {
653+
dummy_identity: whoami,
654+
dummy_auth_tokens: DUMMY_AUTH.clone(),
655+
}));
656+
let logger = discard_logger();
657+
658+
SentryApi::new(adapter, logger, config, sentry_url).expect("Should build sentry")
659+
}
660+
642661
#[tokio::test]
643662
async fn test_get_all_spenders() {
644663
let server = MockServer::start().await;
@@ -744,11 +763,9 @@ mod test {
744763
let mut config = configuration(Environment::Development, None).expect("Should get Config");
745764
config.spendable_find_limit = 2;
746765

747-
let adapter = Adapter::with_unlocked(Dummy::init(Options {
766+
let leader_adapter = Adapter::with_unlocked(Dummy::init(Options {
748767
dummy_identity: IDS[&LEADER],
749-
dummy_auth_tokens: vec![(IDS[&LEADER].to_address(), "AUTH_Leader".into())]
750-
.into_iter()
751-
.collect(),
768+
dummy_auth_tokens: DUMMY_AUTH.clone(),
752769
}));
753770
let logger = discard_logger();
754771

@@ -757,8 +774,8 @@ mod test {
757774
.expect("Should find Dummy campaign token in config")
758775
.with_channel(DUMMY_CAMPAIGN.channel);
759776

760-
let sentry =
761-
SentryApi::new(adapter, logger, config, sentry_url).expect("Should build sentry");
777+
let sentry = SentryApi::new(leader_adapter, logger, config, sentry_url)
778+
.expect("Should build sentry");
762779

763780
let mut res = sentry
764781
.get_all_spenders(&channel_context)
@@ -788,14 +805,7 @@ mod test {
788805
#[tokio::test]
789806
async fn test_collecting_and_channels_and_campaigns() {
790807
let server = MockServer::start().await;
791-
let chain_1_token = GANACHE_CONFIG
792-
.chains
793-
.get("Ganache #1")
794-
.unwrap()
795-
.tokens
796-
.get("Mocked TOKEN 2")
797-
.unwrap()
798-
.address;
808+
let chain_1_token = GANACHE_INFO_1.tokens["Mocked TOKEN 1"].address;
799809

800810
// Setting up new leader and a channel and campaign which use it on Ganache #1337
801811
let dummy_leader_2 = ValidatorDesc {
@@ -851,21 +861,13 @@ mod test {
851861
campaign_new_leader_and_follower.validators =
852862
CampaignValidators::new((dummy_leader_2.clone(), dummy_follower_2.clone()));
853863

854-
// Initializing SentryApi instance
855864
let sentry_url = ApiUrl::from_str(&server.uri()).expect("Should parse");
856865

857-
let mut config = configuration(Environment::Development, None).expect("Should get Config");
858-
config.spendable_find_limit = 2;
859-
let adapter = Adapter::with_unlocked(Dummy::init(Options {
860-
dummy_identity: IDS[&LEADER],
861-
dummy_auth_tokens: vec![(IDS[&LEADER].to_address(), "AUTH_Leader".into())]
862-
.into_iter()
863-
.collect(),
864-
}));
865-
let logger = discard_logger();
866+
let mut config = GANACHE_CONFIG.clone();
867+
config.campaigns_find_limit = 2;
866868

867-
let sentry = SentryApi::new(adapter, logger, config.clone(), sentry_url.clone())
868-
.expect("Should build sentry");
869+
// Initializing SentryApi instance
870+
let leader_sentry = setup_dummy_sentry(IDS[&LEADER], config.clone(), sentry_url.clone());
869871

870872
// Getting Wiremock to return the campaigns when called
871873
let first_page_response = CampaignListResponse {
@@ -929,7 +931,7 @@ mod test {
929931
.with_channel(channel_new_leader_and_follower),
930932
);
931933

932-
let (channels, chains_validators) = sentry
934+
let (channels, chains_validators) = leader_sentry
933935
.collect_channels()
934936
.await
935937
.expect("Should collect channels");
@@ -986,7 +988,7 @@ mod test {
986988
campaign_new_follower.clone(),
987989
campaign_new_leader_and_follower.clone(),
988990
];
989-
let res = campaigns::all_campaigns(sentry.client.clone(), &sentry_url.clone(), None)
991+
let res = campaigns::all_campaigns(leader_sentry.client.clone(), &sentry_url.clone(), None)
990992
.await
991993
.expect("Should get all campaigns");
992994
assert_eq!(res, all_campaigns, "All campaigns are present");
@@ -1033,7 +1035,7 @@ mod test {
10331035
channel_new_leader_and_follower,
10341036
];
10351037
let res = channels::all_channels(
1036-
sentry.client.clone(),
1038+
leader_sentry.client.clone(),
10371039
&sentry_url.clone(),
10381040
DUMMY_VALIDATOR_LEADER.id,
10391041
)

0 commit comments

Comments
 (0)