basic test setup
modified migration fake grpc server in tokio fake db migration for testing test utilities for signing grpc req
This commit is contained in:
		
							parent
							
								
									2d108a53b5
								
							
						
					
					
						commit
						77efe3ad63
					
				
							
								
								
									
										1
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										1
									
								
								Cargo.lock
									
									
									
										generated
									
									
									
								
							| @ -3771,6 +3771,7 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" | |||||||
| name = "surreal-brain" | name = "surreal-brain" | ||||||
| version = "0.1.0" | version = "0.1.0" | ||||||
| dependencies = [ | dependencies = [ | ||||||
|  |  "anyhow", | ||||||
|  "bs58", |  "bs58", | ||||||
|  "chrono", |  "chrono", | ||||||
|  "dashmap 6.1.0", |  "dashmap 6.1.0", | ||||||
|  | |||||||
| @ -28,3 +28,8 @@ strip = true | |||||||
| opt-level = 3 | opt-level = 3 | ||||||
| panic = 'abort' | panic = 'abort' | ||||||
| codegen-units = 1 | codegen-units = 1 | ||||||
|  | 
 | ||||||
|  | [dev-dependencies] | ||||||
|  | anyhow = "1.0.98" | ||||||
|  | bs58 = "0.5.1" | ||||||
|  | ed25519-dalek = "2.1.1" | ||||||
|  | |||||||
| @ -1,6 +1,8 @@ | |||||||
| // After deleting this migration, also delete old_brain structs
 | // After deleting this migration, also delete old_brain structs
 | ||||||
| // and dangling impls from the model
 | // and dangling impls from the model
 | ||||||
| use std::error::Error; | use std::error::Error; | ||||||
|  | use surreal_brain::constants::{DB_ADDRESS, DB_NAME, DB_NS}; | ||||||
|  | use surreal_brain::db::init; | ||||||
| use surreal_brain::{db, old_brain}; | use surreal_brain::{db, old_brain}; | ||||||
| 
 | 
 | ||||||
| #[tokio::main] | #[tokio::main] | ||||||
| @ -8,6 +10,8 @@ async fn main() -> Result<(), Box<dyn Error>> { | |||||||
|     let old_brain_data = old_brain::BrainData::load_from_disk()?; |     let old_brain_data = old_brain::BrainData::load_from_disk()?; | ||||||
|     // println!("{}", serde_yaml::to_string(&old_brain_data)?);
 |     // println!("{}", serde_yaml::to_string(&old_brain_data)?);
 | ||||||
| 
 | 
 | ||||||
|  |     init(DB_ADDRESS, DB_NS, DB_NAME).await?; | ||||||
|  | 
 | ||||||
|     let result = db::migration0(&old_brain_data).await?; |     let result = db::migration0(&old_brain_data).await?; | ||||||
| 
 | 
 | ||||||
|     println!("{result:?}"); |     println!("{result:?}"); | ||||||
|  | |||||||
| @ -15,7 +15,7 @@ use surrealdb::{ | |||||||
| use tokio::sync::mpsc::Sender; | use tokio::sync::mpsc::Sender; | ||||||
| use tokio_stream::StreamExt as _; | use tokio_stream::StreamExt as _; | ||||||
| 
 | 
 | ||||||
| static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init); | pub static DB: LazyLock<Surreal<Client>> = LazyLock::new(Surreal::init); | ||||||
| 
 | 
 | ||||||
| #[derive(thiserror::Error, Debug)] | #[derive(thiserror::Error, Debug)] | ||||||
| pub enum Error { | pub enum Error { | ||||||
| @ -50,8 +50,6 @@ pub async fn migration0(old_data: &old_brain::BrainData) -> surrealdb::Result<() | |||||||
|     let app_nodes: Vec<AppNode> = old_data.into(); |     let app_nodes: Vec<AppNode> = old_data.into(); | ||||||
|     let vm_contracts: Vec<ActiveVm> = old_data.into(); |     let vm_contracts: Vec<ActiveVm> = old_data.into(); | ||||||
| 
 | 
 | ||||||
|     init(DB_ADDRESS, DB_NS, DB_NAME).await?; |  | ||||||
| 
 |  | ||||||
|     println!("Inserting accounts..."); |     println!("Inserting accounts..."); | ||||||
|     let _: Vec<Account> = DB.insert(()).content(accounts).await?; |     let _: Vec<Account> = DB.insert(()).content(accounts).await?; | ||||||
|     println!("Inserting vm nodes..."); |     println!("Inserting vm nodes..."); | ||||||
|  | |||||||
							
								
								
									
										1
									
								
								tests/fixtures/secret_detee_wallet_key
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										1
									
								
								tests/fixtures/secret_detee_wallet_key
									
									
									
									
										vendored
									
									
										Normal file
									
								
							| @ -0,0 +1 @@ | |||||||
|  | 9RBoFzqSfMVjQmmCbnMhfNGxGEdRmTyb9eF4wDdRVX6f | ||||||
							
								
								
									
										80
									
								
								tests/grpcs_test.rs
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										80
									
								
								tests/grpcs_test.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,80 @@ | |||||||
|  | use detee_shared::{ | ||||||
|  |     common_proto::Pubkey, | ||||||
|  |     general_proto::{ | ||||||
|  |         brain_general_cli_client::BrainGeneralCliClient, | ||||||
|  |         brain_general_cli_server::BrainGeneralCliServer, | ||||||
|  |     }, | ||||||
|  |     vm_proto::brain_vm_cli_server::BrainVmCliServer, | ||||||
|  | }; | ||||||
|  | use surreal_brain::grpc::{BrainGeneralCliMock, BrainVmCliMock}; | ||||||
|  | use test_utils::{get_pub_key, sign_request}; | ||||||
|  | use tokio::sync::OnceCell; | ||||||
|  | use tonic::transport::Channel; | ||||||
|  | 
 | ||||||
|  | mod test_utils; | ||||||
|  | 
 | ||||||
|  | const DB_URL: &str = "localhost:8000"; | ||||||
|  | const DB_NS: &str = "test_brain"; | ||||||
|  | const DB_NAME: &str = "test_migration_db"; | ||||||
|  | 
 | ||||||
|  | const GRPC_ADDR: &str = "127.0.0.1:31337"; | ||||||
|  | 
 | ||||||
|  | static TEST_STATE: OnceCell<Channel> = OnceCell::const_new(); | ||||||
|  | 
 | ||||||
|  | async fn prepare_test_db() { | ||||||
|  |     surreal_brain::db::init(DB_URL, DB_NS, DB_NAME) | ||||||
|  |         .await | ||||||
|  |         .expect("Failed to initialize the database"); | ||||||
|  | 
 | ||||||
|  |     let old_brain_data = surreal_brain::old_brain::BrainData::load_from_disk().unwrap(); | ||||||
|  |     // cleanup old brain data
 | ||||||
|  |     surreal_brain::db::migration0(&old_brain_data).await.unwrap(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async fn fake_grpc_server() { | ||||||
|  |     tonic::transport::Server::builder() | ||||||
|  |         .add_service(BrainGeneralCliServer::new(BrainGeneralCliMock {})) | ||||||
|  |         .add_service(BrainVmCliServer::new(BrainVmCliMock {})) | ||||||
|  |         .serve(GRPC_ADDR.parse().unwrap()) | ||||||
|  |         .await | ||||||
|  |         .unwrap(); | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async fn fake_grpc_client() -> Channel { | ||||||
|  |     let url = format!("http://{GRPC_ADDR}"); | ||||||
|  |     Channel::from_shared(url).unwrap().connect().await.unwrap() | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | async fn prepare_test_setup() { | ||||||
|  |     TEST_STATE | ||||||
|  |         .get_or_init(|| async { | ||||||
|  |             prepare_test_db().await; | ||||||
|  | 
 | ||||||
|  |             tokio::spawn(async { | ||||||
|  |                 fake_grpc_server().await; | ||||||
|  |             }); | ||||||
|  | 
 | ||||||
|  |             tokio::time::sleep(std::time::Duration::from_secs(1)).await; | ||||||
|  |             fake_grpc_client().await | ||||||
|  |         }) | ||||||
|  |         .await; | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | #[tokio::test] | ||||||
|  | async fn test_general_balance() { | ||||||
|  |     prepare_test_setup().await; | ||||||
|  |     let grpc_channel = TEST_STATE.get().unwrap().clone(); | ||||||
|  | 
 | ||||||
|  |     let mut brain_general_cli_client = BrainGeneralCliClient::new(grpc_channel.clone()); | ||||||
|  | 
 | ||||||
|  |     let req_data = Pubkey { pubkey: get_pub_key().unwrap() }; | ||||||
|  | 
 | ||||||
|  |     let acc_bal = brain_general_cli_client | ||||||
|  |         .get_balance(sign_request(req_data).unwrap()) | ||||||
|  |         .await | ||||||
|  |         .unwrap() | ||||||
|  |         .into_inner(); | ||||||
|  | 
 | ||||||
|  |     assert_eq!(acc_bal.balance, 0); | ||||||
|  |     assert_eq!(acc_bal.tmp_locked, 0); | ||||||
|  | } | ||||||
							
								
								
									
										41
									
								
								tests/test_utils.rs
									
									
									
									
									
										Normal file
									
								
							
							
								
								
								
								
								
									
									
								
							
						
						
									
										41
									
								
								tests/test_utils.rs
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,41 @@ | |||||||
|  | use anyhow::Result; | ||||||
|  | use ed25519_dalek::Signer; | ||||||
|  | use ed25519_dalek::SigningKey; | ||||||
|  | use tonic::metadata::AsciiMetadataValue; | ||||||
|  | use tonic::Request; | ||||||
|  | 
 | ||||||
|  | pub const WALLET_KEY_PATH: &str = "tests/fixtures/secret_detee_wallet_key"; | ||||||
|  | 
 | ||||||
|  | pub fn sign_request<T: std::fmt::Debug>(req: T) -> Result<Request<T>> { | ||||||
|  |     let pubkey = get_pub_key()?; | ||||||
|  |     let timestamp = chrono::Utc::now().to_rfc3339(); | ||||||
|  |     let signature = try_sign_message(&format!("{timestamp}{req:?}"))?; | ||||||
|  |     let timestamp: AsciiMetadataValue = timestamp.parse()?; | ||||||
|  |     let pubkey: AsciiMetadataValue = pubkey.parse()?; | ||||||
|  |     let signature: AsciiMetadataValue = signature.parse()?; | ||||||
|  |     let mut req = Request::new(req); | ||||||
|  |     req.metadata_mut().insert("timestamp", timestamp); | ||||||
|  |     req.metadata_mut().insert("pubkey", pubkey); | ||||||
|  |     req.metadata_mut().insert("request-signature", signature); | ||||||
|  | 
 | ||||||
|  |     Ok(req) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | fn get_signing_key() -> Result<SigningKey> { | ||||||
|  |     let key = bs58::decode(std::fs::read_to_string(WALLET_KEY_PATH)?.trim()) | ||||||
|  |         .into_vec()? | ||||||
|  |         .try_into() | ||||||
|  |         .map_err(|e: Vec<u8>| anyhow::anyhow!("Invalid key length: {}", e.len()))?; | ||||||
|  |     let key = SigningKey::from_bytes(&key); | ||||||
|  |     Ok(key) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn get_pub_key() -> Result<String> { | ||||||
|  |     let key = get_signing_key()?; | ||||||
|  |     Ok(bs58::encode(key.verifying_key().to_bytes()).into_string()) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn try_sign_message(message: &str) -> Result<String> { | ||||||
|  |     let key = get_signing_key()?; | ||||||
|  |     Ok(bs58::encode(key.sign(message.as_bytes()).to_bytes()).into_string()) | ||||||
|  | } | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user