Skip to content

Commit

Permalink
replace test consts
Browse files Browse the repository at this point in the history
  • Loading branch information
AmbientTea committed Jan 29, 2025
1 parent 6ffeb68 commit d8d5f1c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
31 changes: 16 additions & 15 deletions toolkit/partner-chains-cli/src/generate_keys/tests.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
use super::*;
use crate::config::RESOURCES_CONFIG_FILE_PATH;
use crate::tests::*;
use crate::CmdRun;
use scenarios::key_file_content;
use scenarios::resources_file_content;

const DATA_PATH: &str = "/path/to/data";
const RESOURCES_CONFIG_PATH: &str = "pc-resources-config.json";
const KEYS_FILE: &str = "partner-chains-public-keys.json";
const CHAIN_NAME: &str = "partner_chains_template";

const GRANDPA_PREFIX: &str = "6772616e"; // "gran" in hex
const CROSS_CHAIN_PREFIX: &str = "63726368"; // "crch" in hex
const AURA_PREFIX: &str = "61757261"; // "aura" in hex

fn default_config() -> GenerateKeysConfig {
GenerateKeysConfig { chain_name: CHAIN_NAME.into(), substrate_node_base_path: DATA_PATH.into() }
GenerateKeysConfig {
chain_name: DEFAULT_CHAIN_NAME.into(),
substrate_node_base_path: DATA_PATH.into(),
}
}

fn network_key_file() -> String {
format!("{DATA_PATH}/chains/{CHAIN_NAME}/network/secret_ed25519")
format!("{DATA_PATH}/chains/{DEFAULT_CHAIN_NAME}/network/secret_ed25519")
}

fn keystore_path() -> String {
format!("{DATA_PATH}/chains/{CHAIN_NAME}/keystore")
format!("{DATA_PATH}/chains/{DEFAULT_CHAIN_NAME}/keystore")
}

pub mod scenarios {
Expand Down Expand Up @@ -167,7 +168,7 @@ fn happy_path() {
"partner-chains-public-keys.json",
key_file_content("aura-pub-key", "grandpa-pub-key", "cross-chain-pub-key")
);
verify_json!(mock_context, RESOURCES_CONFIG_PATH, resources_file_content());
verify_json!(mock_context, RESOURCES_CONFIG_FILE_PATH, resources_file_content());
}

mod config_read {
Expand All @@ -181,26 +182,26 @@ mod config_read {

let result = GenerateKeysConfig::load(&context);

assert_eq!(result.chain_name, CHAIN_NAME);
assert_eq!(result.chain_name, DEFAULT_CHAIN_NAME);
assert_eq!(result.substrate_node_base_path, DATA_PATH);
}

#[test]
fn reads_all_when_present() {
let context = MockIOContext::new()
.with_json_file(
RESOURCES_CONFIG_PATH,
RESOURCES_CONFIG_FILE_PATH,
serde_json::json!({
"substrate_node_base_path": DATA_PATH,
}),
)
.with_expected_io(vec![MockIO::eprint(&format!(
"🛠️ Loaded node base path from config ({RESOURCES_CONFIG_PATH}): {DATA_PATH}"
"🛠️ Loaded node base path from config ({RESOURCES_CONFIG_FILE_PATH}): {DATA_PATH}"
))]);

let result = GenerateKeysConfig::load(&context);

assert_eq!(result.chain_name, CHAIN_NAME);
assert_eq!(result.chain_name, DEFAULT_CHAIN_NAME);
assert_eq!(result.substrate_node_base_path, DATA_PATH);
}
}
Expand All @@ -217,7 +218,7 @@ mod generate_spo_keys {
];
let mock_context = MockIOContext::new()
.with_json_file(
RESOURCES_CONFIG_PATH,
RESOURCES_CONFIG_FILE_PATH,
serde_json::json!({
"substrate_node_base_path": DATA_PATH,
}),
Expand Down Expand Up @@ -260,16 +261,16 @@ mod generate_spo_keys {
#[test]
fn skips_the_step_if_user_declines_keys_file_overwrite() {
let mock_context = MockIOContext::new()
.with_file(KEYS_FILE, "irrelevant")
.with_file(KEYS_FILE_PATH, "irrelevant")
.with_json_file(
RESOURCES_CONFIG_PATH,
RESOURCES_CONFIG_FILE_PATH,
serde_json::json!({
"substrate_node_base_path": DATA_PATH,
}),
)
.with_expected_io(vec![
MockIO::prompt_yes_no(
&format! {"keys file {KEYS_FILE} exists - overwrite it?"},
&format! {"keys file {KEYS_FILE_PATH} exists - overwrite it?"},
false,
false,
),
Expand Down
34 changes: 16 additions & 18 deletions toolkit/partner-chains-cli/src/register/register1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ fn derive_address<C: IOContext>(
mod tests {
use super::*;
use crate::tests::{MockIO, MockIOContext};
use config::{CHAIN_CONFIG_FILE_PATH, RESOURCES_CONFIG_FILE_PATH};
use ogmios::{
config::tests::{
default_ogmios_config_json, default_ogmios_service_config,
Expand All @@ -184,8 +185,8 @@ mod tests {
});

let mock_context = MockIOContext::new()
.with_json_file(CHAIN_CONFIG_PATH, chain_config_content())
.with_json_file(RESOURCE_CONFIG_PATH, resource_config_without_cardano_fields)
.with_json_file(CHAIN_CONFIG_FILE_PATH, chain_config_content())
.with_json_file(RESOURCES_CONFIG_FILE_PATH, resource_config_without_cardano_fields)
.with_json_file(KEYS_FILE_PATH, generated_keys_file_content())
.with_file(ECDSA_KEY_PATH, ECDSA_KEY_FILE_CONTENT)
.with_file(PAYMENT_VKEY_PATH, PAYMENT_VKEY_CONTENT)
Expand All @@ -206,7 +207,7 @@ mod tests {
result.expect("should succeed");
verify_json!(
mock_context,
RESOURCE_CONFIG_PATH,
RESOURCES_CONFIG_FILE_PATH,
json!({
"substrate_node_base_path": "/path/to/data",
"cardano_payment_verification_key_file": PAYMENT_VKEY_PATH,
Expand Down Expand Up @@ -246,8 +247,8 @@ mod tests {
#[test]
fn saved_prompt_fields_are_loaded_without_prompting() {
let mock_context = MockIOContext::new()
.with_json_file(CHAIN_CONFIG_PATH, chain_config_content())
.with_json_file(RESOURCE_CONFIG_PATH, resource_config_content())
.with_json_file(CHAIN_CONFIG_FILE_PATH, chain_config_content())
.with_json_file(RESOURCES_CONFIG_FILE_PATH, resource_config_content())
.with_json_file(KEYS_FILE_PATH, generated_keys_file_content())
.with_file(PAYMENT_VKEY_PATH, PAYMENT_VKEY_CONTENT)
.with_file(ECDSA_KEY_PATH, ECDSA_KEY_FILE_CONTENT)
Expand All @@ -271,8 +272,8 @@ mod tests {
#[test]
fn report_error_if_payment_file_is_invalid() {
let mock_context = MockIOContext::new()
.with_json_file(CHAIN_CONFIG_PATH, chain_config_content())
.with_json_file(RESOURCE_CONFIG_PATH, resource_config_content())
.with_json_file(CHAIN_CONFIG_FILE_PATH, chain_config_content())
.with_json_file(RESOURCES_CONFIG_FILE_PATH, resource_config_content())
.with_json_file(KEYS_FILE_PATH, generated_keys_file_content())
.with_file(PAYMENT_VKEY_PATH, "invalid content")
.with_expected_io(
Expand All @@ -293,8 +294,8 @@ mod tests {
#[test]
fn utxo_query_error() {
let mock_context = MockIOContext::new()
.with_json_file(CHAIN_CONFIG_PATH, chain_config_content())
.with_json_file(RESOURCE_CONFIG_PATH, resource_config_content())
.with_json_file(CHAIN_CONFIG_FILE_PATH, chain_config_content())
.with_json_file(RESOURCES_CONFIG_FILE_PATH, resource_config_content())
.with_json_file(KEYS_FILE_PATH, generated_keys_file_content())
.with_file(PAYMENT_VKEY_PATH, PAYMENT_VKEY_CONTENT)
.with_expected_io(
Expand Down Expand Up @@ -327,8 +328,8 @@ mod tests {
#[test]
fn should_error_with_missing_public_keys_file() {
let mock_context = MockIOContext::new()
.with_json_file(CHAIN_CONFIG_PATH, chain_config_content())
.with_json_file(RESOURCE_CONFIG_PATH, resource_config_content())
.with_json_file(CHAIN_CONFIG_FILE_PATH, chain_config_content())
.with_json_file(RESOURCES_CONFIG_FILE_PATH, resource_config_content())
.with_expected_io(
vec![
intro_msg_io(),
Expand All @@ -346,8 +347,8 @@ mod tests {
#[test]
fn should_error_with_missing_private_keys_in_storage() {
let mock_context = MockIOContext::new()
.with_json_file(CHAIN_CONFIG_PATH, chain_config_content())
.with_json_file(RESOURCE_CONFIG_PATH, resource_config_content())
.with_json_file(CHAIN_CONFIG_FILE_PATH, chain_config_content())
.with_json_file(RESOURCES_CONFIG_FILE_PATH, resource_config_content())
.with_file(PAYMENT_VKEY_PATH, PAYMENT_VKEY_CONTENT)
.with_json_file(KEYS_FILE_PATH, generated_keys_file_content())
.with_expected_io(
Expand All @@ -372,8 +373,8 @@ mod tests {
#[test]
fn should_error_on_invalid_seed_phrase() {
let mock_context = MockIOContext::new()
.with_json_file(CHAIN_CONFIG_PATH, chain_config_content())
.with_json_file(RESOURCE_CONFIG_PATH, resource_config_content())
.with_json_file(CHAIN_CONFIG_FILE_PATH, chain_config_content())
.with_json_file(RESOURCES_CONFIG_FILE_PATH, resource_config_content())
.with_json_file(KEYS_FILE_PATH, generated_keys_file_content())
.with_file(PAYMENT_VKEY_PATH, PAYMENT_VKEY_CONTENT)
.with_file(ECDSA_KEY_PATH, "invalid seed phrase")
Expand All @@ -396,9 +397,6 @@ mod tests {
assert!(result.is_err());
}

const CHAIN_CONFIG_PATH: &str = "pc-chain-config.json";
const RESOURCE_CONFIG_PATH: &str = "pc-resources-config.json";

fn chain_config_content() -> serde_json::Value {
serde_json::json!({
"chain_parameters": {
Expand Down

0 comments on commit d8d5f1c

Please sign in to comment.