Skip to content

Commit 72b8751

Browse files
author
Damien LACHAUME / PALO-IT
committed
Adapt tests for the fake aggregator
1 parent a44c6b5 commit 72b8751

File tree

1 file changed

+42
-90
lines changed

1 file changed

+42
-90
lines changed

mithril-client-wasm/src/client_wasm.rs

Lines changed: 42 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,6 @@ impl MithrilClient {
199199
}
200200
}
201201

202-
// The tests are commented for now, as we don't want to run them on a testnet aggregator.
203-
/*
204202
#[cfg(test)]
205203
mod tests {
206204
use super::*;
@@ -210,60 +208,63 @@ mod tests {
210208
};
211209
use wasm_bindgen_test::*;
212210

213-
const AGGREGATOR_ENDPOINT: &str =
214-
"https://aggregator.testing-preview.api.mithril.network/aggregator";
215211
const GENESIS_VERIFICATION_KEY: &str = "5b33322c3235332c3138362c3230312c3137372c31312c3131372c3133352c3138372c3136372c3138312c3138382c32322c35392c3230362c3130352c3233312c3135302c3231352c33302c37382c3231322c37362c31362c3235322c3138302c37322c3133342c3133372c3234372c3136312c36385d";
212+
const FAKE_AGGREGATOR_IP: &str = "127.0.0.1";
213+
const FAKE_AGGREGATOR_PORT: &str = "8000";
214+
const FAKE_AGGREGATOR_SNAPSHOT_DIGEST: &str =
215+
"000ee4c84c7b64a62dc30ec78a765a1f3bb81cd9dd4bd1eccf9f2da785e70877";
216+
const FAKE_AGGREGATOR_MSD_HASH: &str =
217+
"03ebb00e6626037f2e58eb7cc50d308fd57c253baa1fe2b04eb5945ced16b5bd";
218+
const FAKE_CERTIFICATE_HASH: &str =
219+
"05bf6740e781e649dd2fe7e3319818747d8038ca759c67711c90cf24cdade8a9";
220+
221+
fn get_mithril_client() -> MithrilClient {
222+
MithrilClient::new(
223+
&format!(
224+
"http://{}:{}/aggregator",
225+
FAKE_AGGREGATOR_IP, FAKE_AGGREGATOR_PORT
226+
),
227+
GENESIS_VERIFICATION_KEY,
228+
)
229+
}
216230

217231
wasm_bindgen_test_configure!(run_in_browser);
218232
#[wasm_bindgen_test]
219233
async fn list_snapshots_should_return_value_convertible_in_rust_type() {
220-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
221-
222-
let snapshots_list_js_value = client
234+
let snapshots_list_js_value = get_mithril_client()
223235
.list_snapshots()
224236
.await
225237
.expect("list_snapshots should not fail");
226238
let snapshots_list =
227239
serde_wasm_bindgen::from_value::<Vec<SnapshotListItem>>(snapshots_list_js_value)
228240
.expect("conversion should not fail");
229241

230-
assert_eq!(snapshots_list.len(), 20);
242+
assert_eq!(snapshots_list.len(), 3);
231243
}
232244

233245
#[wasm_bindgen_test]
234246
async fn get_snapshot_should_return_value_convertible_in_rust_type() {
235-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
236-
let snapshots_list_js_value = client.list_snapshots().await.unwrap();
237-
let snapshots_list =
238-
serde_wasm_bindgen::from_value::<Vec<SnapshotListItem>>(snapshots_list_js_value)
239-
.unwrap();
240-
let last_digest = &snapshots_list.first().unwrap().digest;
241-
242-
let snapshot_js_value = client
243-
.get_snapshot(last_digest)
247+
let snapshot_js_value = get_mithril_client()
248+
.get_snapshot(FAKE_AGGREGATOR_SNAPSHOT_DIGEST)
244249
.await
245250
.expect("get_snapshot should not fail");
246251
let snapshot = serde_wasm_bindgen::from_value::<Snapshot>(snapshot_js_value)
247252
.expect("conversion should not fail");
248253

249-
assert_eq!(snapshot.digest, last_digest.to_string());
254+
assert_eq!(snapshot.digest, FAKE_AGGREGATOR_SNAPSHOT_DIGEST);
250255
}
251256

252257
#[wasm_bindgen_test]
253258
async fn get_snapshot_should_fail_with_unknown_digest() {
254-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
255-
256-
client
259+
get_mithril_client()
257260
.get_snapshot("whatever")
258261
.await
259262
.expect_err("get_snapshot should fail");
260263
}
261264

262265
#[wasm_bindgen_test]
263266
async fn list_mithril_stake_distributions_should_return_value_convertible_in_rust_type() {
264-
let wasm_client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
265-
266-
let msd_list_js_value = wasm_client
267+
let msd_list_js_value = get_mithril_client()
267268
.list_mithril_stake_distributions()
268269
.await
269270
.expect("list_mithril_stake_distributions should not fail");
@@ -272,44 +273,32 @@ mod tests {
272273
)
273274
.expect("conversion should not fail");
274275

275-
assert_eq!(msd_list.len(), 20);
276+
assert_eq!(msd_list.len(), 3);
276277
}
277278

278279
#[wasm_bindgen_test]
279280
async fn get_mithril_stake_distribution_should_return_value_convertible_in_rust_type() {
280-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
281-
let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
282-
let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
283-
msd_list_js_value,
284-
)
285-
.unwrap();
286-
let last_hash = &msd_list.first().unwrap().hash;
287-
288-
let msd_js_value = client
289-
.get_mithril_stake_distribution(last_hash)
281+
let msd_js_value = get_mithril_client()
282+
.get_mithril_stake_distribution(FAKE_AGGREGATOR_MSD_HASH)
290283
.await
291284
.expect("get_mithril_stake_distribution should not fail");
292285
let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value)
293286
.expect("conversion should not fail");
294287

295-
assert_eq!(msd.hash, last_hash.to_string());
288+
assert_eq!(msd.hash, FAKE_AGGREGATOR_MSD_HASH);
296289
}
297290

298291
#[wasm_bindgen_test]
299292
async fn get_mithril_stake_distribution_should_fail_with_unknown_hash() {
300-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
301-
302-
client
293+
get_mithril_client()
303294
.get_mithril_stake_distribution("whatever")
304295
.await
305296
.expect_err("get_mithril_stake_distribution should fail");
306297
}
307298

308299
#[wasm_bindgen_test]
309300
async fn list_mithril_certificates_should_return_value_convertible_in_rust_type() {
310-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
311-
312-
let certificates_list_js_value = client
301+
let certificates_list_js_value = get_mithril_client()
313302
.list_mithril_certificates()
314303
.await
315304
.expect("list_mithril_certificates should not fail");
@@ -318,35 +307,25 @@ mod tests {
318307
)
319308
.expect("conversion should not fail");
320309

321-
assert_eq!(certificates_list.len(), 20);
310+
assert_eq!(certificates_list.len(), 7);
322311
}
323312

324313
#[wasm_bindgen_test]
325314
async fn get_mithril_certificate_should_return_value_convertible_in_rust_type() {
326-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
327-
let certificates_list_js_value = client.list_mithril_certificates().await.unwrap();
328-
let certificates_list = serde_wasm_bindgen::from_value::<Vec<MithrilCertificateListItem>>(
329-
certificates_list_js_value,
330-
)
331-
.unwrap();
332-
let last_hash = &certificates_list.first().unwrap().hash;
333-
334-
let certificate_js_value = client
335-
.get_mithril_certificate(last_hash)
315+
let certificate_js_value = get_mithril_client()
316+
.get_mithril_certificate(FAKE_CERTIFICATE_HASH)
336317
.await
337318
.expect("get_mithril_certificate should not fail");
338319
let certificate =
339320
serde_wasm_bindgen::from_value::<MithrilCertificate>(certificate_js_value)
340321
.expect("conversion should not fail");
341322

342-
assert_eq!(certificate.hash, last_hash.to_string());
323+
assert_eq!(certificate.hash, FAKE_CERTIFICATE_HASH);
343324
}
344325

345326
#[wasm_bindgen_test]
346327
async fn get_mithril_certificate_should_fail_with_unknown_hash() {
347-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
348-
349-
client
328+
get_mithril_client()
350329
.get_mithril_certificate("whatever")
351330
.await
352331
.expect_err("get_mithril_certificate should fail");
@@ -355,15 +334,9 @@ mod tests {
355334
#[wasm_bindgen_test]
356335
async fn compute_mithril_stake_distribution_message_should_return_value_convertible_in_rust_type(
357336
) {
358-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
359-
let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
360-
let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
361-
msd_list_js_value,
362-
)
363-
.unwrap();
364-
let last_hash = &msd_list.first().unwrap().hash;
337+
let client = get_mithril_client();
365338
let msd_js_value = client
366-
.get_mithril_stake_distribution(last_hash)
339+
.get_mithril_stake_distribution(FAKE_AGGREGATOR_MSD_HASH)
367340
.await
368341
.unwrap();
369342

@@ -375,20 +348,11 @@ mod tests {
375348
.expect("conversion should not fail");
376349
}
377350

378-
// This test is commented for now as it's execution takes too long with a real testnet aggregator
379-
// Timeout in tests is 20s
380-
/*
381351
#[wasm_bindgen_test]
382352
async fn verify_certificate_chain_should_return_value_convertible_in_rust_type() {
383-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
384-
let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
385-
let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
386-
msd_list_js_value,
387-
)
388-
.unwrap();
389-
let last_hash = &msd_list.first().unwrap().hash;
353+
let client = get_mithril_client();
390354
let msd_js_value = client
391-
.get_mithril_stake_distribution(last_hash)
355+
.get_mithril_stake_distribution(FAKE_AGGREGATOR_MSD_HASH)
392356
.await
393357
.unwrap();
394358
let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value).unwrap();
@@ -400,22 +364,12 @@ mod tests {
400364
serde_wasm_bindgen::from_value::<MithrilCertificate>(certificate_js_value)
401365
.expect("conversion should not fail");
402366
}
403-
*/
404367

405-
// This test is commented for now as it's execution takes too long with a real testnet aggregator
406-
// Timeout in tests is 20s
407-
/*
408368
#[wasm_bindgen_test]
409369
async fn verify_message_match_certificate_should_return_true() {
410-
let client = MithrilClient::new(AGGREGATOR_ENDPOINT, GENESIS_VERIFICATION_KEY);
411-
let msd_list_js_value = client.list_mithril_stake_distributions().await.unwrap();
412-
let msd_list = serde_wasm_bindgen::from_value::<Vec<MithrilStakeDistributionListItem>>(
413-
msd_list_js_value,
414-
)
415-
.unwrap();
416-
let last_hash = &msd_list.first().unwrap().hash;
370+
let client = get_mithril_client();
417371
let msd_js_value = client
418-
.get_mithril_stake_distribution(last_hash)
372+
.get_mithril_stake_distribution(FAKE_AGGREGATOR_MSD_HASH)
419373
.await
420374
.unwrap();
421375
let msd = serde_wasm_bindgen::from_value::<MithrilStakeDistribution>(msd_js_value.clone())
@@ -434,6 +388,4 @@ mod tests {
434388
.await
435389
.expect("verify_message_match_certificate should not fail");
436390
}
437-
*/
438391
}
439-
*/

0 commit comments

Comments
 (0)