Format payment-method and interrupted-delivery regressions

This commit is contained in:
archipelago
2026-10-06 07:10:48 -04:00
parent 607f54260e
commit d46f6ceeeb
2 changed files with 78 additions and 16 deletions
+50 -8
View File
@@ -201,14 +201,30 @@ async fn onchain_delivery_streams_to_owned_cache_and_preserves_incomplete_recove
.body(hyper::Body::from(vec![71u8; 2 * 1024 * 1024])) .body(hyper::Body::from(vec![71u8; 2 * 1024 * 1024]))
.unwrap() .unwrap()
.into(); .into();
let item = cache_peer_response(dir.path(), "seller.onion", "film", "film.mp4", "video/mp4", 1, "onchain", response).await.unwrap(); let item = cache_peer_response(
dir.path(),
"seller.onion",
"film",
"film.mp4",
"video/mp4",
1,
"onchain",
response,
)
.await
.unwrap();
assert!(item.download_complete); assert!(item.download_complete);
assert_eq!(item.ecash_backend, "onchain"); assert_eq!(item.ecash_backend, "onchain");
let (_, mut file) = crate::content_owned::open_owned(dir.path(), "seller.onion", "film").await.unwrap().unwrap(); let (_, mut file) = crate::content_owned::open_owned(dir.path(), "seller.onion", "film")
.await
.unwrap()
.unwrap();
let mut bytes = Vec::new(); let mut bytes = Vec::new();
file.read_to_end(&mut bytes).await.unwrap(); file.read_to_end(&mut bytes).await.unwrap();
assert_eq!(bytes, vec![71u8; 2 * 1024 * 1024]); assert_eq!(bytes, vec![71u8; 2 * 1024 * 1024]);
let reply = cached_purchase_response(dir.path(), "seller.onion", "film", true, 0).await.unwrap(); let reply = cached_purchase_response(dir.path(), "seller.onion", "film", true, 0)
.await
.unwrap();
assert_eq!(reply["owned"], true); assert_eq!(reply["owned"], true);
assert!(reply.get("data").is_none()); assert!(reply.get("data").is_none());
@@ -216,12 +232,38 @@ async fn onchain_delivery_streams_to_owned_cache_and_preserves_incomplete_recove
.header("content-length", "100") .header("content-length", "100")
.body(hyper::Body::wrap_stream(futures_util::stream::iter([ .body(hyper::Body::wrap_stream(futures_util::stream::iter([
Ok(bytes::Bytes::from_static(b"short")), Ok(bytes::Bytes::from_static(b"short")),
Err(std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "connection closed before delivery completed")), Err(std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"connection closed before delivery completed",
)),
]))) ])))
.unwrap() .unwrap()
.into(); .into();
assert!(cache_peer_response(dir.path(), "seller.onion", "interrupted", "film.mp4", "video/mp4", 1, "onchain", truncated).await.is_err()); assert!(cache_peer_response(
let entries = crate::content_owned::list_owned_checked(dir.path()).await.unwrap(); dir.path(),
assert!(!entries.iter().find(|item| item.content_id == "interrupted").unwrap().download_complete); "seller.onion",
assert!(existing_paid_content(dir.path(), "seller.onion", "interrupted", None, true).await.is_err()); "interrupted",
"film.mp4",
"video/mp4",
1,
"onchain",
truncated
)
.await
.is_err());
let entries = crate::content_owned::list_owned_checked(dir.path())
.await
.unwrap();
assert!(
!entries
.iter()
.find(|item| item.content_id == "interrupted")
.unwrap()
.download_complete
);
assert!(
existing_paid_content(dir.path(), "seller.onion", "interrupted", None, true)
.await
.is_err()
);
} }
+28 -8
View File
@@ -436,7 +436,9 @@ where
} }
if !authorized { if !authorized {
if let Some(hash) = invoice_hash { if let Some(hash) = invoice_hash {
if let Some(method) = crate::content_invoice::paid_method_for(data_dir, hash, id).await { if let Some(method) =
crate::content_invoice::paid_method_for(data_dir, hash, id).await
{
authorized = method_accepted(&item.access, method.as_str()); authorized = method_accepted(&item.access, method.as_str());
} }
} }
@@ -1316,7 +1318,7 @@ mod paid_read_order_tests {
#[tokio::test] #[tokio::test]
async fn durable_payment_uses_its_recorded_method_for_seller_acceptance() { async fn durable_payment_uses_its_recorded_method_for_seller_acceptance() {
use crate::content_invoice::{record_pending_method, mark_paid, PaymentMethod}; use crate::content_invoice::{mark_paid, record_pending_method, PaymentMethod};
for (paid_with, accepted, expected) in [ for (paid_with, accepted, expected) in [
(PaymentMethod::Onchain, "onchain", true), (PaymentMethod::Onchain, "onchain", true),
(PaymentMethod::Onchain, "lightning", false), (PaymentMethod::Onchain, "lightning", false),
@@ -1325,15 +1327,33 @@ mod paid_read_order_tests {
] { ] {
let dir = fixture(b"paid bytes").await; let dir = fixture(b"paid bytes").await;
let mut catalog = load_catalog(dir.path()).await.unwrap(); let mut catalog = load_catalog(dir.path()).await.unwrap();
catalog.items[0].access = AccessControl::Paid { price_sats: 10, accepted: vec![accepted.into()] }; catalog.items[0].access = AccessControl::Paid {
price_sats: 10,
accepted: vec![accepted.into()],
};
save_catalog(dir.path(), &catalog).await.unwrap(); save_catalog(dir.path(), &catalog).await.unwrap();
record_pending_method(dir.path(), "receipt", "paid", 10, paid_with).await.unwrap(); record_pending_method(dir.path(), "receipt", "paid", 10, paid_with)
.await
.unwrap();
mark_paid(dir.path(), "receipt").await.unwrap(); mark_paid(dir.path(), "receipt").await.unwrap();
let result = serve_content_with(dir.path(), "paid", None, Some("receipt"), None, None, false, let result = serve_content_with(
dir.path(),
"paid",
None,
Some("receipt"),
None,
None,
false,
|path, range, mime| prepare_content(dir.path(), path, range, mime), |path, range, mime| prepare_content(dir.path(), path, range, mime),
|_, _| async { panic!("must not redeem another payment") }).await.unwrap(); |_, _| async { panic!("must not redeem another payment") },
if expected { assert!(matches!(result, ServeResult::Ok(bytes, _) if bytes == b"paid bytes")); } )
else { assert!(matches!(result, ServeResult::PaymentRequired(10))); } .await
.unwrap();
if expected {
assert!(matches!(result, ServeResult::Ok(bytes, _) if bytes == b"paid bytes"));
} else {
assert!(matches!(result, ServeResult::PaymentRequired(10)));
}
} }
} }