-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3a878e9
commit 7acd1b9
Showing
9 changed files
with
369 additions
and
361 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
pub mod deposit; | ||
pub mod transfer; | ||
pub mod withdraw; | ||
|
||
pub use deposit::deposit_handler; | ||
pub use transfer::transfer_handler; | ||
pub use withdraw::withdraw_handler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
use std::sync::OnceLock; | ||
|
||
use axum_extra::routing::TypedPath; | ||
use axum_test::{TestServer, TestServerConfig}; | ||
use kairos_server::{ | ||
routes::{ | ||
deposit::{Deposit, DepositPath}, | ||
transfer::{Transfer, TransferPath}, | ||
withdraw::{WithdrawPath, Withdrawal}, | ||
}, | ||
state::BatchState, | ||
}; | ||
use tracing_subscriber::{prelude::*, EnvFilter}; | ||
|
||
static TEST_ENVIRONMENT: OnceLock<()> = OnceLock::new(); | ||
|
||
fn new_test_app() -> TestServer { | ||
TEST_ENVIRONMENT.get_or_init(|| { | ||
tracing_subscriber::registry() | ||
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| "trace".into())) | ||
.with(tracing_subscriber::fmt::layer()) | ||
.init(); | ||
}); | ||
let config = TestServerConfig::builder().mock_transport().build(); | ||
|
||
TestServer::new_with_config(kairos_server::app_router(BatchState::new()), config).unwrap() | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_deposit_withdraw() { | ||
let server = new_test_app(); | ||
|
||
let deposit = Deposit { | ||
public_key: "alice_key".into(), | ||
amount: 100, | ||
}; | ||
|
||
// no arguments | ||
server | ||
.post(DepositPath.to_uri().path()) | ||
.await | ||
.assert_status_failure(); | ||
|
||
// deposit | ||
server | ||
.post(DepositPath.to_uri().path()) | ||
.json(&deposit) | ||
.await | ||
.assert_status_success(); | ||
|
||
// wrong arguments | ||
server | ||
.post(WithdrawPath.to_uri().path()) | ||
.json(&deposit) | ||
.await | ||
.assert_status_failure(); | ||
|
||
let withdrawal = Withdrawal { | ||
public_key: "alice_key".into(), | ||
signature: "TODO".into(), | ||
amount: 50, | ||
}; | ||
|
||
// first withdrawal | ||
server | ||
.post(WithdrawPath.to_uri().path()) | ||
.json(&withdrawal) | ||
.await | ||
.assert_status_success(); | ||
|
||
// withdrawal with insufficient funds | ||
server | ||
.post(WithdrawPath.to_uri().path()) | ||
.json(&Withdrawal { | ||
amount: 51, | ||
..withdrawal.clone() | ||
}) | ||
.await | ||
.assert_status_failure(); | ||
|
||
// second withdrawal | ||
server | ||
.post(WithdrawPath.to_uri().path()) | ||
.json(&Withdrawal { | ||
amount: 50, | ||
..withdrawal.clone() | ||
}) | ||
.await | ||
.assert_status_success(); | ||
|
||
server | ||
.post(WithdrawPath.to_uri().path()) | ||
.json(&withdrawal) | ||
.await | ||
.assert_status_failure(); | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_deposit_transfer_withdraw() { | ||
let server = new_test_app(); | ||
|
||
let deposit = Deposit { | ||
public_key: "alice_key".into(), | ||
amount: 100, | ||
}; | ||
|
||
let transfer = Transfer { | ||
from: "alice_key".into(), | ||
signature: "TODO".into(), | ||
to: "bob_key".into(), | ||
amount: 50, | ||
}; | ||
|
||
let withdrawal = Withdrawal { | ||
public_key: "bob_key".into(), | ||
signature: "TODO".into(), | ||
amount: 50, | ||
}; | ||
|
||
// deposit | ||
server | ||
.post(DepositPath.to_uri().path()) | ||
.json(&deposit) | ||
.await | ||
.assert_status_success(); | ||
|
||
// transfer | ||
server | ||
.post(TransferPath.to_uri().path()) | ||
.json(&transfer) | ||
.await | ||
.assert_status_success(); | ||
|
||
// withdraw | ||
server | ||
.post(WithdrawPath.to_uri().path()) | ||
.json(&withdrawal) | ||
.await | ||
.assert_status_success(); | ||
} |