Skip to content

Commit d495d40

Browse files
committed
test: add integration test
Signed-off-by: Gustavo Inacio <[email protected]>
1 parent 87d7a4e commit d495d40

File tree

10 files changed

+190
-255
lines changed

10 files changed

+190
-255
lines changed

Cargo.lock

Lines changed: 50 additions & 248 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,4 @@ thegraph-graphql-http = "0.2.0"
7676
graphql_client = { version = "0.14.0", features = ["reqwest-rustls"] }
7777
bip39 = "2.0.0"
7878
rstest = "0.23.0"
79+
wiremock = "0.6.1"

crates/monitor/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,5 @@ bip39.workspace = true
2424
[dev-dependencies]
2525
env_logger = { version = "0.11.0", default-features = false }
2626
test-log = { version = "0.2.12", default-features = false }
27-
wiremock = "0.5.19"
27+
wiremock.workspace = true
2828
test-assets = { path = "../test-assets" }

crates/service/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ typed-builder = "0.20.0"
6161
[dev-dependencies]
6262
hex-literal = "0.4.1"
6363
test-assets = { path = "../test-assets" }
64+
sqlx = { workspace = true, features = ["migrate"] }
6465
rstest.workspace = true
6566
tower-test = "0.4.0"
6667
tower-service = "0.3.3"
6768
tokio-test = "0.4.4"
69+
wiremock.workspace = true
6870

6971
[build-dependencies]
7072
build-info-build = { version = "0.0.39", default-features = false }

crates/service/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ mod routes;
1010
pub mod service;
1111
mod tap;
1212
mod wallet;
13+
14+
pub use middleware::QueryBody;

crates/service/src/middleware.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ pub use deployment::deployment_middleware;
1919
pub use labels::labels_middleware;
2020
pub use prometheus_metrics::PrometheusMetricsMiddlewareLayer;
2121
pub use sender::{sender_middleware, SenderState};
22-
pub use tap_context::context_middleware;
22+
pub use tap_context::{context_middleware, QueryBody};
2323
pub use tap_receipt::receipt_middleware;

crates/service/src/middleware/tap_context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ use crate::{error::IndexerServiceError, tap::AgoraQuery};
2222

2323
/// Graphql query body to be decoded and passed to agora context
2424
#[derive(Debug, serde::Deserialize, serde::Serialize)]
25-
struct QueryBody {
26-
query: String,
27-
variables: Option<Box<RawValue>>,
25+
pub struct QueryBody {
26+
pub query: String,
27+
pub variables: Option<Box<RawValue>>,
2828
}
2929

3030
/// Injects tap context in the extensions to be used by tap_receipt_authorize
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
use std::time::{Duration, SystemTime};
2+
3+
use alloy::primitives::Address;
4+
use axum::http::Request;
5+
use axum_extra::headers::Header;
6+
use indexer_config::{BlockchainConfig, GraphNodeConfig, IndexerConfig, NonZeroGRT};
7+
use indexer_monitor::EscrowAccounts;
8+
use indexer_service_rs::{
9+
service::{ServiceRouter, TapReceipt},
10+
QueryBody,
11+
};
12+
use reqwest::{StatusCode, Url};
13+
use sqlx::PgPool;
14+
use test_assets::{create_signed_receipt, INDEXER_ALLOCATIONS, TAP_EIP712_DOMAIN};
15+
use tokio::sync::watch;
16+
use tower::ServiceExt;
17+
use wiremock::{
18+
matchers::{method, path},
19+
Mock, MockServer, ResponseTemplate,
20+
};
21+
22+
#[sqlx::test(migrations = "../../migrations")]
23+
async fn full_integration_test(database: PgPool) {
24+
let http_client = reqwest::Client::builder()
25+
.tcp_nodelay(true)
26+
.build()
27+
.expect("Failed to init HTTP client");
28+
29+
let allocation = INDEXER_ALLOCATIONS
30+
.values()
31+
.collect::<Vec<_>>()
32+
.pop()
33+
.unwrap()
34+
.clone();
35+
let deployment = allocation.subgraph_deployment.id;
36+
37+
let mock_server = MockServer::start().await;
38+
39+
let mock = Mock::given(method("POST"))
40+
.and(path(format!("/subgraphs/id/{deployment}")))
41+
.respond_with(ResponseTemplate::new(200).set_body_raw(
42+
r#"
43+
{
44+
"data": {
45+
"graphNetwork": {
46+
"currentEpoch": 960
47+
}
48+
}
49+
}
50+
"#,
51+
"application/json",
52+
));
53+
mock_server.register(mock).await;
54+
55+
let (_escrow_tx, escrow_accounts) = watch::channel(EscrowAccounts::new(
56+
test_assets::ESCROW_ACCOUNTS_BALANCES.clone(),
57+
test_assets::ESCROW_ACCOUNTS_SENDERS_TO_SIGNERS.clone(),
58+
));
59+
let (_dispute_tx, dispute_manager) = watch::channel(Address::ZERO);
60+
61+
let (_allocations_tx, allocations) = watch::channel(test_assets::INDEXER_ALLOCATIONS.clone());
62+
63+
let graph_node_url = Url::parse(&mock_server.uri()).unwrap();
64+
65+
let router = ServiceRouter::builder()
66+
.database(database)
67+
.domain_separator(TAP_EIP712_DOMAIN.clone())
68+
.http_client(http_client)
69+
.graph_node(leak(GraphNodeConfig {
70+
query_url: graph_node_url.clone(),
71+
status_url: graph_node_url.clone(),
72+
}))
73+
.indexer(leak(IndexerConfig {
74+
indexer_address: *test_assets::INDEXER_ADDRESS,
75+
operator_mnemonic: test_assets::INDEXER_MNEMONIC.clone(),
76+
}))
77+
.service(leak(indexer_config::ServiceConfig {
78+
serve_network_subgraph: false,
79+
serve_escrow_subgraph: false,
80+
serve_auth_token: None,
81+
host_and_port: "0.0.0.0:0".parse().unwrap(),
82+
url_prefix: "/".into(),
83+
tap: indexer_config::ServiceTapConfig {
84+
max_receipt_value_grt: NonZeroGRT::new(1000000000000).unwrap(),
85+
},
86+
free_query_auth_token: None,
87+
}))
88+
.blockchain(leak(BlockchainConfig {
89+
chain_id: indexer_config::TheGraphChainId::Test,
90+
receipts_verifier_address: *test_assets::VERIFIER_ADDRESS,
91+
}))
92+
.timestamp_buffer_secs(Duration::from_secs(10))
93+
.escrow_accounts(escrow_accounts)
94+
.dispute_manager(dispute_manager)
95+
.allocations(allocations)
96+
.build();
97+
98+
let app = router.create_router().await.unwrap();
99+
100+
let timestamp = SystemTime::now()
101+
.duration_since(SystemTime::UNIX_EPOCH)
102+
.expect("Time went backwards")
103+
.as_nanos();
104+
105+
let receipt = create_signed_receipt(allocation.id, 0, timestamp as u64, 100).await;
106+
107+
let query = QueryBody {
108+
query: "query".into(),
109+
variables: None,
110+
};
111+
112+
let request = Request::builder()
113+
.uri(format!("/subgraphs/id/{deployment}"))
114+
.header(TapReceipt::name(), serde_json::to_string(&receipt).unwrap())
115+
.body(serde_json::to_string(&query).unwrap())
116+
.unwrap();
117+
118+
// with deployment
119+
let res = app.oneshot(request).await.unwrap();
120+
assert_eq!(res.status(), StatusCode::OK);
121+
}
122+
123+
fn leak<T>(thing: T) -> &'static T {
124+
Box::leak(Box::new(thing))
125+
}

crates/tap-agent/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,4 @@ futures = { version = "0.3.30", default-features = false }
4848

4949
[dev-dependencies]
5050
tempfile = "3.8.0"
51-
wiremock = "0.6.1"
51+
wiremock.workspace = true

crates/test-assets/src/lib.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,12 @@ lazy_static! {
256256
(wallet, address)
257257
};
258258

259+
pub static ref VERIFIER_ADDRESS: Address = Address::from([0x11u8; 20]);
260+
261+
259262
pub static ref TAP_EIP712_DOMAIN: Eip712Domain = tap_eip712_domain(
260263
1,
261-
Address::from([0x11u8; 20])
264+
*VERIFIER_ADDRESS
262265
);
263266
}
264267

0 commit comments

Comments
 (0)