Skip to content

Commit

Permalink
Test payment signature (in middle of the work)
Browse files Browse the repository at this point in the history
  • Loading branch information
nieznanysprawiciel committed Sep 5, 2024
1 parent d8dff86 commit c12fee9
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 45 deletions.
67 changes: 25 additions & 42 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ ya-client = { git = "https://github.com/golemfactory/ya-client.git", rev = "258d
#ya-client = { path = "../ya-client" }
ya-client-model = { git = "https://github.com/golemfactory/ya-client.git", rev = "258d5e085e591cab33858e8d1b19eff01b64687d" }
#ya-client-model = { path = "../ya-client/model" }
golem-certificate = { git = "https://github.com/golemfactory/golem-certificate.git", rev = "f2d7514c18fc066e9cfb796090b90f5b27cfe1c6" }
golem-certificate = { git = "https://github.com/golemfactory/golem-certificate.git", rev = "800d710331cdf3bf82f2be5de8345b90b6982af1" }

## RELAY and networking stack

Expand Down
101 changes: 101 additions & 0 deletions core/payment/tests/test_payment_signature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
use bigdecimal::BigDecimal;
use chrono::Utc;
use std::time::Duration;
use test_context::test_context;

use ya_client_model::payment::NewInvoice;
use ya_framework_basic::async_drop::DroppableTestContext;
use ya_framework_basic::log::enable_logs;
use ya_framework_basic::{resource, temp_dir};
use ya_framework_mocks::market::FakeMarket;
use ya_framework_mocks::net::MockNet;
use ya_framework_mocks::node::MockNode;
use ya_framework_mocks::payment::fake_payment::FakePayment;
use ya_framework_mocks::payment::{Driver, PaymentRestExt};

#[cfg_attr(not(feature = "framework-test"), ignore)]
#[test_context(DroppableTestContext)]
#[serial_test::serial]
async fn test_payment_signature(ctx: &mut DroppableTestContext) -> anyhow::Result<()> {
enable_logs(true);

let dir = temp_dir!("test_payment_signature")?;
let dir = dir.path();

let net = MockNet::new().bind();
let node1 = MockNode::new(net.clone(), "node-1", dir)
.with_identity()
.with_payment(None)
.with_fake_market();
node1.bind_gsb().await?;
node1.start_server(ctx).await?;

let identity = node1.get_identity()?;
let appkey_prov = identity.create_identity_key("provider").await?;
let appkey_req = identity
.create_from_private_key(&resource!("ci-requestor-1.key.priv"))
.await?;

node1
.get_payment()?
.fund_account(Driver::Erc20, &appkey_req.identity.to_string())
.await?;

let requestor = node1.rest_payments(&appkey_req.key)?;
let provider = node1.rest_payments(&appkey_prov.key)?;

log::info!("Creating mock Agreement...");
let agreement =
FakeMarket::create_fake_agreement(appkey_req.identity, appkey_prov.identity).unwrap();
node1.get_market()?.add_agreement(agreement.clone()).await;

log::info!("Creating allocation...");
let new_allocation = FakePayment::default_allocation(&agreement, BigDecimal::from(10u64))?;
let allocation = requestor.create_allocation(&new_allocation).await?;
log::info!(
"Allocation created. ({}) Issuing invoice...",
allocation.allocation_id
);

let invoice = provider
.issue_invoice(&NewInvoice {
agreement_id: agreement.agreement_id.to_string(),
activity_ids: None,
amount: BigDecimal::from(2u64),
payment_due_date: Utc::now(),
})
.await?;

log::info!(
"Invoice issued ({}). Sending invoice...",
invoice.invoice_id
);
provider.send_invoice(&invoice.invoice_id).await?;

log::info!(
"Invoice sent. Accepting Invoice ({})...",
invoice.invoice_id
);
requestor.get_invoice(&invoice.invoice_id).await.unwrap();
requestor
.simple_accept_invoice(&invoice, &allocation)
.await
.unwrap();

// Payments are processed, and we don't want payment confirmation to reach Provider.
// This is hack which will block communication between Requestor and Provider despite them
// using the same node.
// We want to send payment confirmation manually later. This way we will be able to modify
// the message and check more different conditions.
net.break_network_for(appkey_prov.identity);

let payments = requestor
.wait_for_invoice_payment::<Utc>(&invoice.invoice_id, Duration::from_secs(5 * 60), None)
.await?;
assert_eq!(payments.len(), 1);
let _payment = requestor
.get_signed_payment(&payments[0].payment_id)
.await?;

Ok(())
}
2 changes: 0 additions & 2 deletions core/payment/tests/test_payment_sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,6 @@ async fn test_payment_sync_fallback(ctx: &mut DroppableTestContext) -> anyhow::R
.unwrap()
.unwrap();

println!("{:?}", sync);
if from != appkey_req.identity {
continue;
}
Expand Down Expand Up @@ -457,6 +456,5 @@ async fn test_payment_sync_fallback(ctx: &mut DroppableTestContext) -> anyhow::R

break;
}

Ok(())
}

0 comments on commit c12fee9

Please sign in to comment.