Skip to content

Commit f9e15be

Browse files
committed
Add tests for all current gateway v1 APIs
1 parent ce157db commit f9e15be

File tree

1 file changed

+151
-6
lines changed

1 file changed

+151
-6
lines changed

iot_config/tests/integrations/gateway_service.rs

Lines changed: 151 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ use crate::common::{
33
make_keypair, spawn_gateway_service,
44
};
55
use chrono::{DateTime, Utc};
6+
use futures::StreamExt;
67
use h3o::{LatLng, Resolution};
78
use helium_crypto::{Keypair, PublicKey, PublicKeyBinary, Sign};
89
use helium_proto::{
9-
services::iot_config::{self as proto, GatewayClient},
10+
services::iot_config::{self as proto, GatewayClient, GatewayInfo},
1011
BlockchainRegionParamsV1, Region,
1112
};
1213
use hextree::Cell;
@@ -18,8 +19,10 @@ use std::io::Write;
1819
use std::vec;
1920
use tonic::Code;
2021

22+
const DEFAULT_REGION: Region = Region::Us915;
23+
2124
#[sqlx::test]
22-
async fn gateway_info_authorization_errors(pool: PgPool) -> anyhow::Result<()> {
25+
async fn gateway_info_v1_authorization_errors(pool: PgPool) -> anyhow::Result<()> {
2326
// NOTE: The information we're requesting does not exist in the DB for
2427
// this test. But we're only interested in Authization Errors.
2528

@@ -66,7 +69,7 @@ async fn gateway_info_authorization_errors(pool: PgPool) -> anyhow::Result<()> {
6669
}
6770

6871
#[sqlx::test]
69-
async fn gateway_info(pool: PgPool) -> anyhow::Result<()> {
72+
async fn gateway_location_v1(pool: PgPool) -> anyhow::Result<()> {
7073
let admin_key = make_keypair();
7174
let pub_key = make_keypair().public_key().clone();
7275
let now = Utc::now();
@@ -79,7 +82,58 @@ async fn gateway_info(pool: PgPool) -> anyhow::Result<()> {
7982

8083
let mut client = GatewayClient::connect(addr).await?;
8184

82-
let res = gateway_info_v1(&mut client, &pub_key, &admin_key).await?;
85+
let res = req_gateway_location_v1(&mut client, &pub_key, &admin_key).await?;
86+
87+
let cell = Cell::from_raw(gateway.location.unwrap() as u64)?;
88+
assert_eq!(res.location, cell.to_string());
89+
90+
Ok(())
91+
}
92+
93+
#[sqlx::test]
94+
async fn gateway_region_params_v1(pool: PgPool) -> anyhow::Result<()> {
95+
let admin_key = make_keypair();
96+
let keypair = make_keypair();
97+
let pub_key = keypair.public_key().clone();
98+
let now = Utc::now();
99+
100+
create_tables(&pool).await?;
101+
102+
let gateway = insert_gateway(&pool, now, pub_key.clone().into()).await?;
103+
104+
let (addr, _) = spawn_gateway_service(pool.clone(), admin_key.public_key().clone()).await?;
105+
106+
let mut client = GatewayClient::connect(addr).await?;
107+
108+
let res = req_gateway_region_params_v1(&mut client, &pub_key, &keypair).await?;
109+
110+
assert_eq!(res.region, DEFAULT_REGION as i32);
111+
assert_eq!(
112+
res.params,
113+
Some(BlockchainRegionParamsV1 {
114+
region_params: vec![],
115+
})
116+
);
117+
assert_eq!(res.gain, gateway.gain.unwrap() as u64);
118+
119+
Ok(())
120+
}
121+
122+
#[sqlx::test]
123+
async fn gateway_info_v1(pool: PgPool) -> anyhow::Result<()> {
124+
let admin_key = make_keypair();
125+
let pub_key = make_keypair().public_key().clone();
126+
let now = Utc::now();
127+
128+
create_tables(&pool).await?;
129+
130+
let gateway = insert_gateway(&pool, now, pub_key.clone().into()).await?;
131+
132+
let (addr, _) = spawn_gateway_service(pool.clone(), admin_key.public_key().clone()).await?;
133+
134+
let mut client = GatewayClient::connect(addr).await?;
135+
136+
let res = req_gateway_info_v1(&mut client, &pub_key, &admin_key).await?;
83137

84138
assert!(res.info.is_some());
85139

@@ -98,6 +152,40 @@ async fn gateway_info(pool: PgPool) -> anyhow::Result<()> {
98152
Ok(())
99153
}
100154

155+
#[sqlx::test]
156+
async fn gateway_info_stream_v1(pool: PgPool) -> anyhow::Result<()> {
157+
let admin_key = make_keypair();
158+
let pub_key = make_keypair().public_key().clone();
159+
let now = Utc::now();
160+
161+
create_tables(&pool).await?;
162+
163+
let gateway = insert_gateway(&pool, now, pub_key.clone().into()).await?;
164+
165+
let (addr, _) = spawn_gateway_service(pool.clone(), admin_key.public_key().clone()).await?;
166+
167+
let mut client = GatewayClient::connect(addr).await?;
168+
169+
let res = req_gateway_info_stream_v1(&mut client, &admin_key).await?;
170+
171+
assert_eq!(res.gateways.len(), 1);
172+
173+
let info: &GatewayInfo = res.gateways.first().unwrap();
174+
175+
assert_eq!(info.address, pub_key.to_vec());
176+
assert_eq!(info.is_full_hotspot, gateway.is_full_hotspot.unwrap());
177+
assert!(info.metadata.is_some());
178+
179+
let metadata = info.metadata.clone().unwrap();
180+
let cell = Cell::from_raw(gateway.location.unwrap() as u64)?;
181+
assert_eq!(metadata.location, cell.to_string());
182+
assert_eq!(metadata.region, Region::Us915 as i32);
183+
assert_eq!(metadata.gain, gateway.gain.unwrap() as i32);
184+
assert_eq!(metadata.elevation, gateway.elevation.unwrap() as i32);
185+
186+
Ok(())
187+
}
188+
101189
fn make_signed_info_request(address: &PublicKey, signer: &Keypair) -> proto::GatewayInfoReqV1 {
102190
let mut req = proto::GatewayInfoReqV1 {
103191
address: address.to_vec(),
@@ -108,7 +196,41 @@ fn make_signed_info_request(address: &PublicKey, signer: &Keypair) -> proto::Gat
108196
req
109197
}
110198

111-
async fn gateway_info_v1(
199+
async fn req_gateway_location_v1(
200+
client: &mut GatewayClient<tonic::transport::Channel>,
201+
address: &PublicKey,
202+
signer: &Keypair,
203+
) -> anyhow::Result<proto::GatewayLocationResV1> {
204+
let mut req = proto::GatewayLocationReqV1 {
205+
gateway: address.to_vec(),
206+
signer: signer.public_key().to_vec(),
207+
signature: vec![],
208+
};
209+
210+
req.signature = signer.sign(&req.encode_to_vec()).unwrap();
211+
212+
let res = client.location(req).await?.into_inner();
213+
Ok(res)
214+
}
215+
216+
async fn req_gateway_region_params_v1(
217+
client: &mut GatewayClient<tonic::transport::Channel>,
218+
address: &PublicKey,
219+
signer: &Keypair,
220+
) -> anyhow::Result<proto::GatewayRegionParamsResV1> {
221+
let mut req = proto::GatewayRegionParamsReqV1 {
222+
region: 0,
223+
address: address.to_vec(),
224+
signature: vec![],
225+
};
226+
227+
req.signature = signer.sign(&req.encode_to_vec()).unwrap();
228+
229+
let res = client.region_params(req).await?.into_inner();
230+
Ok(res)
231+
}
232+
233+
async fn req_gateway_info_v1(
112234
client: &mut GatewayClient<tonic::transport::Channel>,
113235
address: &PublicKey,
114236
signer: &Keypair,
@@ -125,6 +247,29 @@ async fn gateway_info_v1(
125247
Ok(res)
126248
}
127249

250+
async fn req_gateway_info_stream_v1(
251+
client: &mut GatewayClient<tonic::transport::Channel>,
252+
signer: &Keypair,
253+
) -> anyhow::Result<proto::GatewayInfoStreamResV1> {
254+
let mut req = proto::GatewayInfoStreamReqV1 {
255+
batch_size: 10_000,
256+
signer: signer.public_key().to_vec(),
257+
signature: vec![],
258+
};
259+
260+
req.signature = signer.sign(&req.encode_to_vec()).unwrap();
261+
262+
let mut stream = client.info_stream(req).await?.into_inner();
263+
264+
let first = stream
265+
.next()
266+
.await
267+
.transpose()? // map tonic Status into Err
268+
.ok_or_else(|| anyhow::Error::msg("no response"))?;
269+
270+
Ok(first)
271+
}
272+
128273
async fn insert_gateway(
129274
pool: &PgPool,
130275
now: DateTime<Utc>,
@@ -175,7 +320,7 @@ async fn insert_gateway(
175320
let compressed = encoder.finish().into_result()?;
176321

177322
region_map::update_region(
178-
Region::Us915,
323+
DEFAULT_REGION,
179324
&BlockchainRegionParamsV1 {
180325
region_params: vec![],
181326
},

0 commit comments

Comments
 (0)