Skip to content

Commit 9634bf2

Browse files
committed
tests: test that root endpoint and node state
1 parent f8e506b commit 9634bf2

File tree

3 files changed

+30
-23
lines changed

3 files changed

+30
-23
lines changed

src/api/root.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use crate::{node::sync_progress::SyncProgress, BlockfrostError, NodePool};
22
use axum::{response::IntoResponse, Extension, Json};
3-
use serde::Serialize;
3+
use serde::{Deserialize, Serialize};
44

5-
#[derive(Serialize)]
6-
pub struct Response {
5+
#[derive(Serialize, Deserialize)]
6+
pub struct RootResponse {
77
pub name: String,
88
pub version: String,
99
pub sync_progress: SyncProgress,
@@ -18,7 +18,7 @@ pub async fn route(
1818
let mut node = node.get().await?;
1919
let sync_progress = node.sync_progress().await?;
2020

21-
let response = Response {
21+
let response = RootResponse {
2222
name: "blockfrost-platform".to_string(),
2323
version: env!("CARGO_PKG_VERSION").to_string(),
2424
sync_progress,

src/node/sync_progress.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ use crate::BlockfrostError;
33
use chrono::{Duration, TimeZone, Utc};
44
use pallas_network::{miniprotocols, miniprotocols::localstate};
55
use pallas_traverse::wellknown;
6+
use serde::{Deserialize, Serialize};
67
use std::boxed::Box;
78

8-
#[derive(serde::Serialize)]
9+
#[derive(Serialize, Deserialize, Debug)]
910
pub struct SyncProgress {
10-
percentage: f64,
11+
pub percentage: f64,
1112
era: u16,
1213
epoch: u32,
1314
slot: u64,

tests/integration_routes_test.rs

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ mod tests {
44

55
use axum::body::{to_bytes, Body};
66
use axum::http::Request;
7+
use blockfrost_platform::api::root::RootResponse;
78
use blockfrost_platform::{
89
cli::{Config, LogLevel, Mode, Network},
910
server::build,
1011
};
1112
use lazy_static::lazy_static;
12-
use serde_json::Value;
13+
use pretty_assertions::assert_eq;
14+
use reqwest::StatusCode;
1315
use tower::ServiceExt;
1416

1517
lazy_static! {
@@ -27,12 +29,12 @@ mod tests {
2729
server_address: "0.0.0.0".into(),
2830
server_port: 8080,
2931
log_level: LogLevel::Info.into(),
30-
network_magic: 1,
31-
mode: Mode::Full,
32+
network_magic: 2,
33+
mode: Mode::Compact,
3234
node_socket_path: "/run/cardano-node/node.socket".into(),
3335
icebreakers_config: None,
3436
max_pool_connections: 10,
35-
network: Network::Preprod,
37+
network: Network::Preview,
3638
}
3739
}
3840

@@ -51,25 +53,29 @@ mod tests {
5153
.await
5254
.expect("Failed to send request");
5355

54-
println!("response:{:?}", response.body());
55-
56-
// assert_eq!(
57-
// response.status(),
58-
// StatusCode::OK,
59-
// "Root route should return 200"
60-
// );
56+
// Root route should return 200
57+
assert_eq!(
58+
response.status(),
59+
StatusCode::OK,
60+
"Root route should return 200"
61+
);
6162

6263
let body_bytes = to_bytes(response.into_body(), usize::MAX)
6364
.await
6465
.expect("Failed to read response body");
6566

66-
let json_body: Value =
67-
serde_json::from_slice(&body_bytes).expect("Response body is not valid JSON");
68-
69-
let body_text = String::from_utf8_lossy(&body_bytes);
67+
let root_response: RootResponse = serde_json::from_slice(&body_bytes)
68+
.expect("Response body is not valid JSON or doesn't match RootResponse");
7069

71-
println!("body_text:{:?}", body_text);
70+
// Root data
71+
assert!(
72+
root_response.errors.is_empty(),
73+
"Expected no errors in the response"
74+
);
75+
assert_eq!(root_response.name, "blockfrost-platform");
76+
assert!(root_response.healthy);
7277

73-
assert_eq!(json_body["name"], "blockfrost-platform");
78+
// Node should be 100% synced
79+
assert_eq!(root_response.sync_progress.percentage, 100.00);
7480
}
7581
}

0 commit comments

Comments
 (0)