From 70532da4f16c2e1aed0576d77623a549cd960845 Mon Sep 17 00:00:00 2001 From: chen <23054115+cwkang1998@users.noreply.github.com> Date: Mon, 14 Oct 2024 13:18:19 +0800 Subject: [PATCH] Ammend tests for verification urls --- .../starknet_commands/verification/voyager.rs | 2 - .../starknet_commands/verification/walnut.rs | 1 - crates/sncast/tests/e2e/mod.rs | 2 +- .../sncast/tests/e2e/verification/helpers.rs | 24 +++ .../tests/e2e/{verify => verification}/mod.rs | 1 + .../e2e/{verify => verification}/voyager.rs | 150 +++++++++++++----- .../e2e/{verify => verification}/walnut.rs | 138 ++++++++++++---- 7 files changed, 238 insertions(+), 80 deletions(-) create mode 100644 crates/sncast/tests/e2e/verification/helpers.rs rename crates/sncast/tests/e2e/{verify => verification}/mod.rs (65%) rename crates/sncast/tests/e2e/{verify => verification}/voyager.rs (75%) rename crates/sncast/tests/e2e/{verify => verification}/walnut.rs (76%) diff --git a/crates/sncast/src/starknet_commands/verification/voyager.rs b/crates/sncast/src/starknet_commands/verification/voyager.rs index 24bf02d436..0cba26cf05 100644 --- a/crates/sncast/src/starknet_commands/verification/voyager.rs +++ b/crates/sncast/src/starknet_commands/verification/voyager.rs @@ -1,9 +1,7 @@ use super::base::VerificationInterface; use async_trait::async_trait; -use blockifier::blockifier::config; use camino::Utf8PathBuf; use sncast::{helpers::configuration::CastConfig, Network}; -use std::env; pub struct VoyagerVerificationInterface { pub network: Network, diff --git a/crates/sncast/src/starknet_commands/verification/walnut.rs b/crates/sncast/src/starknet_commands/verification/walnut.rs index d8c7285cf6..94221e074b 100644 --- a/crates/sncast/src/starknet_commands/verification/walnut.rs +++ b/crates/sncast/src/starknet_commands/verification/walnut.rs @@ -2,7 +2,6 @@ use super::base::VerificationInterface; use async_trait::async_trait; use camino::Utf8PathBuf; use sncast::{helpers::configuration::CastConfig, Network}; -use std::env; pub struct WalnutVerificationInterface { pub network: Network, diff --git a/crates/sncast/tests/e2e/mod.rs b/crates/sncast/tests/e2e/mod.rs index 573cc5d827..c1ffd3d8f4 100644 --- a/crates/sncast/tests/e2e/mod.rs +++ b/crates/sncast/tests/e2e/mod.rs @@ -8,4 +8,4 @@ mod multicall; mod script; mod show_config; mod tx_status; -mod verify; +mod verification; diff --git a/crates/sncast/tests/e2e/verification/helpers.rs b/crates/sncast/tests/e2e/verification/helpers.rs new file mode 100644 index 0000000000..e9ba9ba7b1 --- /dev/null +++ b/crates/sncast/tests/e2e/verification/helpers.rs @@ -0,0 +1,24 @@ +use std::{fs::File, io::Write}; + +use anyhow::Result; +use tempfile::TempDir; + +use crate::helpers::fixtures::copy_directory_to_tempdir; +use configuration::CONFIG_FILENAME; + +pub fn copy_directory_to_tempdir_with_config( + src_dir: String, + cast_config_content: String, +) -> Result { + let temp_dir = copy_directory_to_tempdir(src_dir); + + let mut file = File::create(temp_dir.path().join(CONFIG_FILENAME)) + .expect("Unable to create a temporary accounts file"); + + file.write_all(cast_config_content.as_bytes()) + .expect("Unable to write test data to a temporary file"); + + file.flush().expect("Unable to flush a temporary file"); + + Ok(temp_dir) +} diff --git a/crates/sncast/tests/e2e/verify/mod.rs b/crates/sncast/tests/e2e/verification/mod.rs similarity index 65% rename from crates/sncast/tests/e2e/verify/mod.rs rename to crates/sncast/tests/e2e/verification/mod.rs index c2c30141a3..e55afdcb96 100644 --- a/crates/sncast/tests/e2e/verify/mod.rs +++ b/crates/sncast/tests/e2e/verification/mod.rs @@ -1,2 +1,3 @@ +mod helpers; mod voyager; mod walnut; diff --git a/crates/sncast/tests/e2e/verify/voyager.rs b/crates/sncast/tests/e2e/verification/voyager.rs similarity index 75% rename from crates/sncast/tests/e2e/verify/voyager.rs rename to crates/sncast/tests/e2e/verification/voyager.rs index 57c3573b05..621990875d 100644 --- a/crates/sncast/tests/e2e/verify/voyager.rs +++ b/crates/sncast/tests/e2e/verification/voyager.rs @@ -1,3 +1,4 @@ +use super::helpers::copy_directory_to_tempdir_with_config; use crate::helpers::constants::{ ACCOUNT_FILE_PATH, CONTRACTS_DIR, MAP_CONTRACT_ADDRESS_SEPOLIA, MAP_CONTRACT_CLASS_HASH_SEPOLIA, }; @@ -10,14 +11,12 @@ use wiremock::{Mock, MockServer, ResponseTemplate}; #[tokio::test] async fn test_happy_case() { - let contract_path = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/map"); - let mock_server = MockServer::start().await; let verifier_response = "Contract successfully verified"; Mock::given(method("POST")) - .and(path("/v1/sn_sepolia/verify")) + .and(path("/class-verify-v2")) .respond_with( ResponseTemplate::new(200) .append_header("content-type", "text/plain") @@ -26,6 +25,22 @@ async fn test_happy_case() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let contract_path = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/map", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -40,10 +55,7 @@ async fn test_happy_case() { "sepolia", ]; - let snapbox = runner(&args) - .env("VOYAGER_API_URL", mock_server.uri()) - .current_dir(contract_path.path()) - .stdin("Y"); + let snapbox = runner(&args).current_dir(contract_path.path()).stdin("Y"); let output = snapbox.assert().success(); @@ -61,14 +73,12 @@ async fn test_happy_case() { #[tokio::test] async fn test_happy_case_class_hash() { - let contract_path = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/map"); - let mock_server = MockServer::start().await; let verifier_response = "Contract successfully verified"; Mock::given(method("POST")) - .and(path("/v1/sn_sepolia/verify")) + .and(path("/class-verify-v2")) .respond_with( ResponseTemplate::new(200) .append_header("content-type", "text/plain") @@ -77,6 +87,22 @@ async fn test_happy_case_class_hash() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let contract_path = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/map", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -91,10 +117,7 @@ async fn test_happy_case_class_hash() { "sepolia", ]; - let snapbox = runner(&args) - .env("VOYAGER_API_URL", mock_server.uri()) - .current_dir(contract_path.path()) - .stdin("Y"); + let snapbox = runner(&args).current_dir(contract_path.path()).stdin("Y"); let output = snapbox.assert().success(); @@ -112,14 +135,12 @@ async fn test_happy_case_class_hash() { #[tokio::test] async fn test_failed_verification() { - let contract_path = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/map"); - let mock_server = MockServer::start().await; let verifier_response = "An error occurred during verification: contract class isn't declared"; Mock::given(method("POST")) - .and(path("/v1/sn_sepolia/verify")) + .and(path("/class-verify-v2")) .respond_with( ResponseTemplate::new(400) .append_header("content-type", "text/plain") @@ -128,6 +149,22 @@ async fn test_failed_verification() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let contract_path = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/map", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -142,10 +179,7 @@ async fn test_failed_verification() { "sepolia", ]; - let snapbox = runner(&args) - .env("VOYAGER_API_URL", mock_server.uri()) - .current_dir(contract_path.path()) - .stdin("Y"); + let snapbox = runner(&args).current_dir(contract_path.path()).stdin("Y"); let output = snapbox.assert().success(); @@ -260,14 +294,12 @@ async fn test_no_class_hash_or_contract_address_provided() { #[tokio::test] async fn test_happy_case_with_confirm_verification_flag() { - let contract_path = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/map"); - let mock_server = MockServer::start().await; let verifier_response = "Contract successfully verified"; Mock::given(method("POST")) - .and(path("/v1/sn_sepolia/verify")) + .and(path("/class-verify-v2")) .respond_with( ResponseTemplate::new(200) .append_header("content-type", "text/plain") @@ -276,6 +308,22 @@ async fn test_happy_case_with_confirm_verification_flag() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let contract_path = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/map", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -291,9 +339,7 @@ async fn test_happy_case_with_confirm_verification_flag() { "--confirm-verification", ]; - let snapbox = runner(&args) - .env("VOYAGER_API_URL", mock_server.uri()) - .current_dir(contract_path.path()); + let snapbox = runner(&args).current_dir(contract_path.path()); let output = snapbox.assert().success(); @@ -311,14 +357,12 @@ async fn test_happy_case_with_confirm_verification_flag() { #[tokio::test] async fn test_happy_case_specify_package() { - let tempdir = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/multiple_packages"); - let mock_server = MockServer::start().await; let verifier_response = "Contract successfully verified"; Mock::given(method("POST")) - .and(path("/v1/sn_sepolia/verify")) + .and(path("/class-verify-v2")) .respond_with( ResponseTemplate::new(200) .append_header("content-type", "text/plain") @@ -327,6 +371,22 @@ async fn test_happy_case_specify_package() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let tempdir = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/multiple_packages", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -343,10 +403,7 @@ async fn test_happy_case_specify_package() { "main_workspace", ]; - let snapbox = runner(&args) - .env("VOYAGER_API_URL", mock_server.uri()) - .current_dir(tempdir.path()) - .stdin("Y"); + let snapbox = runner(&args).current_dir(tempdir.path()).stdin("Y"); let output = snapbox.assert().success(); @@ -364,14 +421,12 @@ async fn test_happy_case_specify_package() { #[tokio::test] async fn test_worskpaces_package_specified_virtual_fibonacci() { - let tempdir = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/virtual_workspace"); - let mock_server = MockServer::start().await; let verifier_response = "Contract successfully verified"; Mock::given(method("POST")) - .and(path("/v1/sn_sepolia/verify")) + .and(path("/class-verify-v2")) .respond_with( ResponseTemplate::new(200) .append_header("content-type", "text/plain") @@ -380,6 +435,22 @@ async fn test_worskpaces_package_specified_virtual_fibonacci() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let tempdir = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/virtual_workspace", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -396,10 +467,7 @@ async fn test_worskpaces_package_specified_virtual_fibonacci() { "cast_fibonacci", ]; - let snapbox = runner(&args) - .env("VOYAGER_API_URL", mock_server.uri()) - .current_dir(tempdir.path()) - .stdin("Y"); + let snapbox = runner(&args).current_dir(tempdir.path()).stdin("Y"); let output = snapbox.assert().success(); diff --git a/crates/sncast/tests/e2e/verify/walnut.rs b/crates/sncast/tests/e2e/verification/walnut.rs similarity index 76% rename from crates/sncast/tests/e2e/verify/walnut.rs rename to crates/sncast/tests/e2e/verification/walnut.rs index 97f62a5215..4a7289ba36 100644 --- a/crates/sncast/tests/e2e/verify/walnut.rs +++ b/crates/sncast/tests/e2e/verification/walnut.rs @@ -1,3 +1,4 @@ +use super::helpers::copy_directory_to_tempdir_with_config; use crate::helpers::constants::{ ACCOUNT_FILE_PATH, CONTRACTS_DIR, MAP_CONTRACT_ADDRESS_SEPOLIA, MAP_CONTRACT_CLASS_HASH_SEPOLIA, }; @@ -10,8 +11,6 @@ use wiremock::{Mock, MockServer, ResponseTemplate}; #[tokio::test] async fn test_happy_case() { - let contract_path = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/map"); - let mock_server = MockServer::start().await; let verifier_response = "Contract successfully verified"; @@ -26,6 +25,22 @@ async fn test_happy_case() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let contract_path = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/map", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -40,10 +55,7 @@ async fn test_happy_case() { "sepolia", ]; - let snapbox = runner(&args) - .env("WALNUT_API_URL", mock_server.uri()) - .current_dir(contract_path.path()) - .stdin("Y"); + let snapbox = runner(&args).current_dir(contract_path.path()).stdin("Y"); let output = snapbox.assert().success(); @@ -61,8 +73,6 @@ async fn test_happy_case() { #[tokio::test] async fn test_happy_case_class_hash() { - let contract_path = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/map"); - let mock_server = MockServer::start().await; let verifier_response = "Contract successfully verified"; @@ -77,6 +87,22 @@ async fn test_happy_case_class_hash() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let contract_path = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/map", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -91,10 +117,7 @@ async fn test_happy_case_class_hash() { "sepolia", ]; - let snapbox = runner(&args) - .env("WALNUT_API_URL", mock_server.uri()) - .current_dir(contract_path.path()) - .stdin("Y"); + let snapbox = runner(&args).current_dir(contract_path.path()).stdin("Y"); let output = snapbox.assert().success(); @@ -112,8 +135,6 @@ async fn test_happy_case_class_hash() { #[tokio::test] async fn test_failed_verification() { - let contract_path = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/map"); - let mock_server = MockServer::start().await; let verifier_response = "An error occurred during verification: contract class isn't declared"; @@ -128,6 +149,22 @@ async fn test_failed_verification() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let contract_path = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/map", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -142,10 +179,7 @@ async fn test_failed_verification() { "sepolia", ]; - let snapbox = runner(&args) - .env("WALNUT_API_URL", mock_server.uri()) - .current_dir(contract_path.path()) - .stdin("Y"); + let snapbox = runner(&args).current_dir(contract_path.path()).stdin("Y"); let output = snapbox.assert().success(); @@ -229,8 +263,6 @@ async fn test_wrong_contract_name_passed() { #[tokio::test] async fn test_happy_case_with_confirm_verification_flag() { - let contract_path = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/map"); - let mock_server = MockServer::start().await; let verifier_response = "Contract successfully verified"; @@ -245,6 +277,22 @@ async fn test_happy_case_with_confirm_verification_flag() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let contract_path = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/map", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -260,9 +308,7 @@ async fn test_happy_case_with_confirm_verification_flag() { "--confirm-verification", ]; - let snapbox = runner(&args) - .env("WALNUT_API_URL", mock_server.uri()) - .current_dir(contract_path.path()); + let snapbox = runner(&args).current_dir(contract_path.path()); let output = snapbox.assert().success(); @@ -280,8 +326,6 @@ async fn test_happy_case_with_confirm_verification_flag() { #[tokio::test] async fn test_happy_case_specify_package() { - let tempdir = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/multiple_packages"); - let mock_server = MockServer::start().await; let verifier_response = "Contract successfully verified"; @@ -296,6 +340,22 @@ async fn test_happy_case_specify_package() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let tempdir = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/multiple_packages", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -312,10 +372,7 @@ async fn test_happy_case_specify_package() { "main_workspace", ]; - let snapbox = runner(&args) - .env("WALNUT_API_URL", mock_server.uri()) - .current_dir(tempdir.path()) - .stdin("Y"); + let snapbox = runner(&args).current_dir(tempdir.path()).stdin("Y"); let output = snapbox.assert().success(); @@ -333,8 +390,6 @@ async fn test_happy_case_specify_package() { #[tokio::test] async fn test_worskpaces_package_specified_virtual_fibonacci() { - let tempdir = copy_directory_to_tempdir(CONTRACTS_DIR.to_string() + "/virtual_workspace"); - let mock_server = MockServer::start().await; let verifier_response = "Contract successfully verified"; @@ -349,6 +404,22 @@ async fn test_worskpaces_package_specified_virtual_fibonacci() { .mount(&mock_server) .await; + let cast_config = format!( + r#" + [sncast.default] + url = "http://127.0.0.1:5055/rpc" + account = "user1" + verification-base-url = "{}" + "#, + mock_server.uri() + ); + + let tempdir = copy_directory_to_tempdir_with_config( + CONTRACTS_DIR.to_string() + "/virtual_workspace", + cast_config.to_string(), + ) + .unwrap(); + let args = vec![ "--accounts-file", ACCOUNT_FILE_PATH, @@ -365,10 +436,7 @@ async fn test_worskpaces_package_specified_virtual_fibonacci() { "cast_fibonacci", ]; - let snapbox = runner(&args) - .env("WALNUT_API_URL", mock_server.uri()) - .current_dir(tempdir.path()) - .stdin("Y"); + let snapbox = runner(&args).current_dir(tempdir.path()).stdin("Y"); let output = snapbox.assert().success();