Skip to content

Commit 3e7f70b

Browse files
committed
test: add mock escrow subgraph server with sample response
1 parent 863894f commit 3e7f70b

File tree

2 files changed

+103
-28
lines changed

2 files changed

+103
-28
lines changed

tap-agent/src/agent/sender_account.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,7 +1059,7 @@ pub mod tests {
10591059
use std::time::{Duration, SystemTime, UNIX_EPOCH};
10601060
use tokio::sync::watch;
10611061
use wiremock::matchers::{body_string_contains, method};
1062-
use wiremock::{Mock, MockServer, ResponseTemplate};
1062+
use wiremock::{Mock, MockGuard, MockServer, ResponseTemplate};
10631063

10641064
// we implement the PartialEq and Eq traits for SenderAccountMessage to be able to compare
10651065
impl Eq for SenderAccountMessage {}
@@ -1110,6 +1110,23 @@ pub mod tests {
11101110
const BUFFER_MS: u64 = 100;
11111111
const RECEIPT_LIMIT: u64 = 10000;
11121112

1113+
async fn mock_escrow_subgraph() -> (MockServer, MockGuard) {
1114+
let mock_ecrow_subgraph_server: MockServer = MockServer::start().await;
1115+
let _mock_ecrow_subgraph = mock_ecrow_subgraph_server
1116+
.register_as_scoped(
1117+
Mock::given(method("POST"))
1118+
.and(body_string_contains("TapTransactions"))
1119+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": {
1120+
"transactions": [{
1121+
"id": "0x00224ee6ad4ae77b817b4e509dc29d644da9004ad0c44005a7f34481d421256409000000"
1122+
}],
1123+
}
1124+
}))),
1125+
)
1126+
.await;
1127+
(mock_ecrow_subgraph_server, _mock_ecrow_subgraph)
1128+
}
1129+
11131130
async fn create_sender_account(
11141131
pgpool: PgPool,
11151132
initial_allocation: HashSet<Address>,
@@ -1201,12 +1218,14 @@ pub mod tests {
12011218
)
12021219
.await;
12031220

1221+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1222+
12041223
let (sender_account, handle, prefix, _) = create_sender_account(
12051224
pgpool,
12061225
HashSet::new(),
12071226
TRIGGER_VALUE,
12081227
TRIGGER_VALUE,
1209-
DUMMY_URL,
1228+
&mock_escrow_subgraph_server.uri(),
12101229
&mock_server.uri(),
12111230
RECEIPT_LIMIT,
12121231
)
@@ -1295,12 +1314,14 @@ pub mod tests {
12951314
)
12961315
.await;
12971316

1317+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1318+
12981319
let (sender_account, handle, prefix, _) = create_sender_account(
12991320
pgpool,
13001321
HashSet::new(),
13011322
TRIGGER_VALUE,
13021323
TRIGGER_VALUE,
1303-
DUMMY_URL,
1324+
&mock_escrow_subgraph_server.uri(),
13041325
&mock_server.uri(),
13051326
RECEIPT_LIMIT,
13061327
)
@@ -1711,12 +1732,13 @@ pub mod tests {
17111732

17121733
#[sqlx::test(migrations = "../migrations")]
17131734
async fn test_remove_sender_account(pgpool: PgPool) {
1735+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
17141736
let (sender_account, handle, prefix, _) = create_sender_account(
17151737
pgpool,
17161738
vec![*ALLOCATION_ID_0].into_iter().collect(),
17171739
TRIGGER_VALUE,
17181740
TRIGGER_VALUE,
1719-
DUMMY_URL,
1741+
&mock_escrow_subgraph_server.uri(),
17201742
DUMMY_URL,
17211743
RECEIPT_LIMIT,
17221744
)

tap-agent/src/agent/sender_allocation.rs

Lines changed: 77 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -907,7 +907,7 @@ pub mod tests {
907907
use tokio::sync::mpsc;
908908
use wiremock::{
909909
matchers::{body_string_contains, method},
910-
Mock, MockServer, Respond, ResponseTemplate,
910+
Mock, MockGuard, MockServer, Respond, ResponseTemplate,
911911
};
912912

913913
const DUMMY_URL: &str = "http://localhost:1234";
@@ -916,6 +916,23 @@ pub mod tests {
916916
pub last_message_emitted: tokio::sync::mpsc::Sender<SenderAccountMessage>,
917917
}
918918

919+
async fn mock_escrow_subgraph() -> (MockServer, MockGuard) {
920+
let mock_ecrow_subgraph_server: MockServer = MockServer::start().await;
921+
let _mock_ecrow_subgraph = mock_ecrow_subgraph_server
922+
.register_as_scoped(
923+
Mock::given(method("POST"))
924+
.and(body_string_contains("TapTransactions"))
925+
.respond_with(ResponseTemplate::new(200).set_body_json(json!({ "data": {
926+
"transactions": [{
927+
"id": "0x00224ee6ad4ae77b817b4e509dc29d644da9004ad0c44005a7f34481d421256409000000"
928+
}],
929+
}
930+
}))),
931+
)
932+
.await;
933+
(mock_ecrow_subgraph_server, _mock_ecrow_subgraph)
934+
}
935+
919936
#[async_trait::async_trait]
920937
impl Actor for MockSenderAccount {
921938
type Msg = SenderAccountMessage;
@@ -1029,6 +1046,7 @@ pub mod tests {
10291046

10301047
#[sqlx::test(migrations = "../migrations")]
10311048
async fn should_update_unaggregated_fees_on_start(pgpool: PgPool) {
1049+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
10321050
let (mut last_message_emitted, sender_account, _join_handle) =
10331051
create_mock_sender_account().await;
10341052
// Add receipts to the database.
@@ -1042,7 +1060,7 @@ pub mod tests {
10421060
let sender_allocation = create_sender_allocation(
10431061
pgpool.clone(),
10441062
DUMMY_URL.to_string(),
1045-
DUMMY_URL,
1063+
&mock_escrow_subgraph_server.uri(),
10461064
Some(sender_account),
10471065
)
10481066
.await;
@@ -1072,6 +1090,7 @@ pub mod tests {
10721090

10731091
#[sqlx::test(migrations = "../migrations")]
10741092
async fn should_return_invalid_receipts_on_startup(pgpool: PgPool) {
1093+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
10751094
let (mut message_receiver, sender_account, _join_handle) =
10761095
create_mock_sender_account().await;
10771096
// Add receipts to the database.
@@ -1085,7 +1104,7 @@ pub mod tests {
10851104
let sender_allocation = create_sender_allocation(
10861105
pgpool.clone(),
10871106
DUMMY_URL.to_string(),
1088-
DUMMY_URL,
1107+
&mock_escrow_subgraph_server.uri(),
10891108
Some(sender_account),
10901109
)
10911110
.await;
@@ -1123,13 +1142,14 @@ pub mod tests {
11231142

11241143
#[sqlx::test(migrations = "../migrations")]
11251144
async fn test_receive_new_receipt(pgpool: PgPool) {
1145+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
11261146
let (mut message_receiver, sender_account, _join_handle) =
11271147
create_mock_sender_account().await;
11281148

11291149
let sender_allocation = create_sender_allocation(
11301150
pgpool.clone(),
11311151
DUMMY_URL.to_string(),
1132-
DUMMY_URL,
1152+
&mock_escrow_subgraph_server.uri(),
11331153
Some(sender_account),
11341154
)
11351155
.await;
@@ -1294,14 +1314,15 @@ pub mod tests {
12941314

12951315
#[sqlx::test(migrations = "../migrations")]
12961316
async fn test_close_allocation_no_pending_fees(pgpool: PgPool) {
1317+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
12971318
let (mut message_receiver, sender_account, _join_handle) =
12981319
create_mock_sender_account().await;
12991320

13001321
// create allocation
13011322
let sender_allocation = create_sender_allocation(
13021323
pgpool.clone(),
13031324
DUMMY_URL.to_string(),
1304-
DUMMY_URL,
1325+
&mock_escrow_subgraph_server.uri(),
13051326
Some(sender_account),
13061327
)
13071328
.await;
@@ -1415,9 +1436,14 @@ pub mod tests {
14151436

14161437
#[sqlx::test(migrations = "../migrations")]
14171438
async fn should_return_unaggregated_fees_without_rav(pgpool: PgPool) {
1418-
let args =
1419-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1420-
.await;
1439+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1440+
let args = create_sender_allocation_args(
1441+
pgpool.clone(),
1442+
DUMMY_URL.to_string(),
1443+
&mock_escrow_subgraph_server.uri(),
1444+
None,
1445+
)
1446+
.await;
14211447
let state = SenderAllocationState::new(args).await.unwrap();
14221448

14231449
// Add receipts to the database.
@@ -1437,9 +1463,14 @@ pub mod tests {
14371463

14381464
#[sqlx::test(migrations = "../migrations")]
14391465
async fn should_calculate_invalid_receipts_fee(pgpool: PgPool) {
1440-
let args =
1441-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1442-
.await;
1466+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1467+
let args = create_sender_allocation_args(
1468+
pgpool.clone(),
1469+
DUMMY_URL.to_string(),
1470+
&mock_escrow_subgraph_server.uri(),
1471+
None,
1472+
)
1473+
.await;
14431474
let state = SenderAllocationState::new(args).await.unwrap();
14441475

14451476
// Add receipts to the database.
@@ -1465,9 +1496,14 @@ pub mod tests {
14651496
/// than the RAV's timestamp.
14661497
#[sqlx::test(migrations = "../migrations")]
14671498
async fn should_return_unaggregated_fees_with_rav(pgpool: PgPool) {
1468-
let args =
1469-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1470-
.await;
1499+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1500+
let args = create_sender_allocation_args(
1501+
pgpool.clone(),
1502+
DUMMY_URL.to_string(),
1503+
&mock_escrow_subgraph_server.uri(),
1504+
None,
1505+
)
1506+
.await;
14711507
let state = SenderAllocationState::new(args).await.unwrap();
14721508

14731509
// Add the RAV to the database.
@@ -1492,9 +1528,14 @@ pub mod tests {
14921528

14931529
#[sqlx::test(migrations = "../migrations")]
14941530
async fn test_store_failed_rav(pgpool: PgPool) {
1495-
let args =
1496-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1497-
.await;
1531+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1532+
let args = create_sender_allocation_args(
1533+
pgpool.clone(),
1534+
DUMMY_URL.to_string(),
1535+
&mock_escrow_subgraph_server.uri(),
1536+
None,
1537+
)
1538+
.await;
14981539
let state = SenderAllocationState::new(args).await.unwrap();
14991540

15001541
let signed_rav = create_rav(*ALLOCATION_ID_0, SIGNER.0.clone(), 4, 10);
@@ -1522,9 +1563,14 @@ pub mod tests {
15221563
}
15231564
}
15241565

1525-
let args =
1526-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1527-
.await;
1566+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1567+
let args = create_sender_allocation_args(
1568+
pgpool.clone(),
1569+
DUMMY_URL.to_string(),
1570+
&mock_escrow_subgraph_server.uri(),
1571+
None,
1572+
)
1573+
.await;
15281574
let mut state = SenderAllocationState::new(args).await.unwrap();
15291575

15301576
let checks = CheckList::new(vec![Arc::new(FailingCheck)]);
@@ -1556,12 +1602,17 @@ pub mod tests {
15561602

15571603
#[sqlx::test(migrations = "../migrations")]
15581604
async fn test_mark_rav_last(pgpool: PgPool) {
1605+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
15591606
let signed_rav = create_rav(*ALLOCATION_ID_0, SIGNER.0.clone(), 4, 10);
15601607
store_rav(&pgpool, signed_rav, SENDER.1).await.unwrap();
15611608

1562-
let args =
1563-
create_sender_allocation_args(pgpool.clone(), DUMMY_URL.to_string(), DUMMY_URL, None)
1564-
.await;
1609+
let args = create_sender_allocation_args(
1610+
pgpool.clone(),
1611+
DUMMY_URL.to_string(),
1612+
&mock_escrow_subgraph_server.uri(),
1613+
None,
1614+
)
1615+
.await;
15651616
let state = SenderAllocationState::new(args).await.unwrap();
15661617

15671618
// mark rav as final
@@ -1573,6 +1624,8 @@ pub mod tests {
15731624

15741625
#[sqlx::test(migrations = "../migrations")]
15751626
async fn test_failed_rav_request(pgpool: PgPool) {
1627+
let (mock_escrow_subgraph_server, _mock_ecrow_subgraph) = mock_escrow_subgraph().await;
1628+
15761629
// Add receipts to the database.
15771630
for i in 0..10 {
15781631
let receipt =
@@ -1589,7 +1642,7 @@ pub mod tests {
15891642
let sender_allocation = create_sender_allocation(
15901643
pgpool.clone(),
15911644
DUMMY_URL.to_string(),
1592-
DUMMY_URL,
1645+
&mock_escrow_subgraph_server.uri(),
15931646
Some(sender_account),
15941647
)
15951648
.await;

0 commit comments

Comments
 (0)