mirror of
https://github.com/floor-licker/polyfill-rs.git
synced 2026-08-19 07:28:10 +00:00
tests: unit tests for client.rs and auth.rs
This commit is contained in:
+147
@@ -188,6 +188,7 @@ where
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::client::ApiCreds;
|
||||
|
||||
#[test]
|
||||
fn test_unix_timestamp() {
|
||||
@@ -206,4 +207,150 @@ mod tests {
|
||||
);
|
||||
assert!(result.is_ok());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hmac_signature_with_body() {
|
||||
let body = r#"{"test": "data"}"#;
|
||||
let result = build_hmac_signature(
|
||||
"test_secret",
|
||||
1234567890,
|
||||
"POST",
|
||||
"/orders",
|
||||
Some(body),
|
||||
);
|
||||
assert!(result.is_ok());
|
||||
let signature = result.unwrap();
|
||||
assert!(!signature.is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hmac_signature_consistency() {
|
||||
let secret = "test_secret";
|
||||
let timestamp = 1234567890;
|
||||
let method = "GET";
|
||||
let path = "/test";
|
||||
|
||||
let sig1 = build_hmac_signature::<String>(secret, timestamp, method, path, None).unwrap();
|
||||
let sig2 = build_hmac_signature::<String>(secret, timestamp, method, path, None).unwrap();
|
||||
|
||||
// Same inputs should produce same signature
|
||||
assert_eq!(sig1, sig2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_hmac_signature_different_inputs() {
|
||||
let secret = "test_secret";
|
||||
let timestamp = 1234567890;
|
||||
|
||||
let sig1 = build_hmac_signature::<String>(secret, timestamp, "GET", "/test", None).unwrap();
|
||||
let sig2 = build_hmac_signature::<String>(secret, timestamp, "POST", "/test", None).unwrap();
|
||||
let sig3 = build_hmac_signature::<String>(secret, timestamp, "GET", "/other", None).unwrap();
|
||||
|
||||
// Different inputs should produce different signatures
|
||||
assert_ne!(sig1, sig2);
|
||||
assert_ne!(sig1, sig3);
|
||||
assert_ne!(sig2, sig3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_l1_headers() {
|
||||
use alloy_signer_local::PrivateKeySigner;
|
||||
use alloy_primitives::U256;
|
||||
|
||||
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
|
||||
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
|
||||
|
||||
let result = create_l1_headers(&signer, Some(U256::from(12345)));
|
||||
assert!(result.is_ok());
|
||||
|
||||
let headers = result.unwrap();
|
||||
assert!(headers.contains_key("POLY_ADDRESS"));
|
||||
assert!(headers.contains_key("POLY_SIGNATURE"));
|
||||
assert!(headers.contains_key("POLY_TIMESTAMP"));
|
||||
assert!(headers.contains_key("POLY_NONCE"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_l1_headers_different_nonces() {
|
||||
use alloy_signer_local::PrivateKeySigner;
|
||||
use alloy_primitives::U256;
|
||||
|
||||
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
|
||||
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
|
||||
|
||||
let headers_1 = create_l1_headers(&signer, Some(U256::from(12345))).unwrap();
|
||||
let headers_2 = create_l1_headers(&signer, Some(U256::from(54321))).unwrap();
|
||||
|
||||
// Different nonces should produce different signatures
|
||||
assert_ne!(
|
||||
headers_1.get("POLY_SIGNATURE"),
|
||||
headers_2.get("POLY_SIGNATURE")
|
||||
);
|
||||
|
||||
// But same address
|
||||
assert_eq!(
|
||||
headers_1.get("POLY_ADDRESS"),
|
||||
headers_2.get("POLY_ADDRESS")
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_l2_headers() {
|
||||
use alloy_signer_local::PrivateKeySigner;
|
||||
|
||||
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
|
||||
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
|
||||
|
||||
let api_creds = ApiCredentials {
|
||||
api_key: "test_key".to_string(),
|
||||
secret: "test_secret".to_string(),
|
||||
passphrase: "test_passphrase".to_string(),
|
||||
};
|
||||
|
||||
let result = create_l2_headers(&signer, &api_creds, "/test", "GET", None);
|
||||
assert!(result.is_ok());
|
||||
|
||||
let headers = result.unwrap();
|
||||
assert!(headers.contains_key("POLY_API_KEY"));
|
||||
assert!(headers.contains_key("POLY_SIGNATURE"));
|
||||
assert!(headers.contains_key("POLY_TIMESTAMP"));
|
||||
assert!(headers.contains_key("POLY_PASSPHRASE"));
|
||||
|
||||
assert_eq!(headers.get("POLY_API_KEY").unwrap(), "test_key");
|
||||
assert_eq!(headers.get("POLY_PASSPHRASE").unwrap(), "test_passphrase");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_eip712_signature_format() {
|
||||
use alloy_signer_local::PrivateKeySigner;
|
||||
use alloy_primitives::U256;
|
||||
|
||||
let private_key = "0x1234567890123456789012345678901234567890123456789012345678901234";
|
||||
let signer: PrivateKeySigner = private_key.parse().expect("Valid private key");
|
||||
|
||||
// Test that we can create and sign EIP-712 messages
|
||||
let result = create_l1_headers(&signer, Some(U256::from(12345)));
|
||||
assert!(result.is_ok());
|
||||
|
||||
let headers = result.unwrap();
|
||||
let signature = headers.get("POLY_SIGNATURE").unwrap();
|
||||
|
||||
// EIP-712 signatures should be hex strings of specific length
|
||||
assert!(signature.starts_with("0x"));
|
||||
assert_eq!(signature.len(), 132); // 0x + 130 hex chars = 132 total
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_timestamp_generation() {
|
||||
let ts1 = get_current_unix_time_secs();
|
||||
std::thread::sleep(std::time::Duration::from_millis(1));
|
||||
let ts2 = get_current_unix_time_secs();
|
||||
|
||||
// Timestamps should be increasing
|
||||
assert!(ts2 >= ts1);
|
||||
|
||||
// Should be reasonable current time (after 2020, before 2030)
|
||||
assert!(ts1 > 1_600_000_000);
|
||||
assert!(ts1 < 1_900_000_000);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user