use std::{env, fs, path::Path};
use tarpc::context;
use crate::{
commands::verify::{verify_token_address, verify_token_using_url},
constants::{ADDRESS, DEFAULT_ENVIRONMENT, SUB_FOLDER},
errors::TokenGenErrors,
tests::common::setup_test_client,
utils::{
client::{responses::RpcResponseErrors, rpc_client::TokenGenClient},
generation::ContractGenerator,
helpers::sanitize_name,
},
Result,
};
async fn test_initiate_client() -> Result<TokenGenClient> {
setup_test_client(ADDRESS).await
}
#[tokio::test]
async fn create_command() -> Result<()> {
let decimals: u8 = 6;
let symbol: String = "SAMPLE".to_string();
let name: &str = "sampletoken";
let description: String = "This is a sample token for testing.".to_string();
let is_frozen: bool = false;
let environment: String = DEFAULT_ENVIRONMENT.to_string();
let base_folder = sanitize_name(name);
let client: TokenGenClient = test_initiate_client().await?;
if Path::new(&base_folder).exists() {
fs::remove_dir_all(&base_folder).expect("Failed to delete test base folder");
}
let (token_content, move_toml, _test_token_content) = client
.create(
context::current(),
decimals,
name.to_owned(),
symbol.to_owned(),
description.to_owned(),
is_frozen,
environment,
)
.await
.map_err(TokenGenErrors::RpcError)?
.map_err(|e| TokenGenErrors::FailedToCreateTokenContract(e.to_string()))?;
let contract_generator = ContractGenerator::new(base_folder.to_string());
contract_generator.create_base_folder()?;
contract_generator.create_move_toml(&move_toml)?;
contract_generator.create_contract_file(name, &token_content, SUB_FOLDER)?;
let sources_folder = format!("{}/{}", base_folder, SUB_FOLDER);
let toml_file: String = format!("{}/Move.toml", base_folder);
let move_file: String = format!(
"{}/{}.move",
sources_folder,
sanitize_name(name).to_lowercase()
);
assert!(
Path::new(&sources_folder).exists(),
"Sources folder not created"
);
assert!(Path::new(&toml_file).exists(), "Move.toml file not created");
assert!(
Path::new(&move_file).exists(),
"Move contract file not created"
);
let toml_content = fs::read_to_string(&toml_file).expect("Failed to read Move.toml file");
assert!(
toml_content.contains("0.0.1"),
"Move.toml file does not contain the correct version"
);
assert!(
toml_content.contains(&base_folder.to_lowercase()),
"Move.toml file does not contain the correct package name"
);
let move_content = fs::read_to_string(&move_file).expect("Failed to read contract file");
assert!(
move_content.contains(&symbol),
"Contract does not contain the correct symbol"
);
assert!(
move_content.contains(name),
"Contract does not contain the correct name"
);
assert!(
move_content.contains(&description),
"Contract does not contain the correct description"
);
fs::remove_dir_all(base_folder).expect("Failed to delete test base folder");
Ok(())
}
#[tokio::test]
async fn verify_command_valid_file() -> Result<()> {
let current_dir = env::current_dir().expect("Failed to get current directory");
let templates_path = format!(
"{}/src/tests/tokens/valid_token.move",
current_dir.display()
);
let toml_path = format!("{}/src/tests/tokens/valid_toml.toml", current_dir.display());
let client: TokenGenClient = test_initiate_client().await?;
let valid_content =
fs::read_to_string(templates_path).expect("Failed to read valid token file");
let valid_toml_content = fs::read_to_string(toml_path).expect("Failed to read valid toml file");
let response = client
.verify_content(context::current(), valid_content, valid_toml_content)
.await;
match response {
Ok(Ok(())) => {
println!("Verification successful");
}
_ => panic!("Expected Content match, but got {:?}", response),
}
Ok(())
}
#[tokio::test]
async fn verify_command_invalid_file() -> Result<()> {
let current_dir = env::current_dir().expect("Failed to get current directory");
let templates_path = format!(
"{}/src/tests/tokens/invalid_token.move",
current_dir.display()
);
let toml_path = format!(
"{}/src/tests/tokens/invalid_toml.toml",
current_dir.display()
);
let client: TokenGenClient = test_initiate_client().await?;
let invalid_content =
fs::read_to_string(templates_path).expect("Failed to read invalid token file");
let invalid_toml_content =
fs::read_to_string(toml_path).expect("Failed to read invalid toml file");
let response = client
.verify_content(context::current(), invalid_content, invalid_toml_content)
.await;
match response {
Ok(Err(RpcResponseErrors::VerifyResultError(msg))) => {
assert_eq!(msg, "Content mismatch detected", "Unexpected error message");
}
_ => panic!("Expected Content mismatch error, but got {:?}", response),
}
Ok(())
}
#[tokio::test]
async fn verify_command_valid_github() -> Result<()> {
let valid_url = "https://github.com/meumar-osec/test-sui-token";
let client: TokenGenClient = test_initiate_client().await?;
let response = verify_token_using_url(valid_url, client).await;
assert!(response.is_ok(), "Failed to verify URL");
Ok(())
}
#[tokio::test]
async fn verify_command_invalid_github() -> Result<()> {
let invalid_url = "https://github.com/meumar-osec/sui-token1";
let client: TokenGenClient = test_initiate_client().await?;
let response = verify_token_using_url(invalid_url, client).await;
assert!(response.is_err(), "Failed to verify URL");
Ok(())
}
#[tokio::test]
async fn verify_command_valid_gitlab() -> Result<()> {
let valid_url = "https://gitlab.com/osec/test-sui-token";
let client: TokenGenClient = test_initiate_client().await?;
let response = verify_token_using_url(valid_url, client).await;
assert!(response.is_ok(), "Failed to verify URL");
Ok(())
}
#[tokio::test]
async fn verify_token_address_invalid_cases() -> Result<()> {
let client = setup_test_client(ADDRESS).await?;
let empty_address = "";
let result = verify_token_address(empty_address, "testnet", client.to_owned()).await;
assert!(result.is_err());
let invalid_address = "invalid_token_address";
let result = verify_token_address(invalid_address, "testnet", client.to_owned()).await;
assert!(result.is_err());
Ok(())
}
#[tokio::test]
async fn verify_token_address_successful_case() -> Result<()> {
let client = setup_test_client(ADDRESS).await?;
let valid_address = "0xd808a18c3b508f6d80f7bd21fbc0faa20d5f69fab237cf073df29cfff199a440";
let result = verify_token_address(valid_address, "devnet", client.to_owned()).await;
assert!(result.is_ok());
Ok(())
}