From 8e81afc52a2f4aabf4b16187c7a64a33235df197 Mon Sep 17 00:00:00 2001 From: Lucas Sunsi Abreu Date: Mon, 18 Dec 2023 07:00:42 -0300 Subject: [PATCH] feat(pay): add lud18 base test --- Cargo.toml | 4 ++ README.md | 2 +- tests/lud16.rs | 2 +- tests/lud18.rs | 105 +++++++++++++++++++++++++++++++++++++++++++++++++ tests/lud21.rs | 2 +- 5 files changed, 112 insertions(+), 3 deletions(-) create mode 100644 tests/lud18.rs diff --git a/Cargo.toml b/Cargo.toml index d6b89c2..c584841 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -85,6 +85,10 @@ required-features = ["client", "server"] name = "lud16" required-features = ["client", "server"] +[[test]] +name = "lud18" +required-features = ["client", "server"] + [[test]] name = "lud21" required-features = ["client", "server"] diff --git a/README.md b/README.md index c9862ac..a1d0ff3 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ This library works as a toolkit so you can serve and make your LNURL requests wi - [LUD-15](https://github.com/lnurl/luds/blob/luds/15.md): 🆘 core 🆘 client 🆘 server 🆘 tests - [LUD-16](https://github.com/lnurl/luds/blob/luds/16.md): ✅ core ✅ client ✅ server ✅ tests - [LUD-17](https://github.com/lnurl/luds/blob/luds/17.md): ✅ core ⚠️ client ⚠️ server ⚠️ tests -- [LUD-18](https://github.com/lnurl/luds/blob/luds/18.md): 🆘 core 🆘 client 🆘 server 🆘 tests +- [LUD-18](https://github.com/lnurl/luds/blob/luds/18.md): ✅ core ⚠️ client ⚠️ server ⚠️ tests - [LUD-19](https://github.com/lnurl/luds/blob/luds/19.md): 🆘 core 🆘 client 🆘 server 🆘 tests - [LUD-20](https://github.com/lnurl/luds/blob/luds/20.md): ✅ core ✅ client ✅ server ⚠️ tests - [LUD-21 *proposal*](https://github.com/lnurl/luds/blob/8580e3c8cbfd8fc95a6c0e5f7fcb5b048a0d5b61/21.md): ✅ core ✅ client ✅ server ✅ tests diff --git a/tests/lud16.rs b/tests/lud16.rs index e31ace7..6f50076 100644 --- a/tests/lud16.rs +++ b/tests/lud16.rs @@ -16,7 +16,7 @@ async fn test() { Ok(lnurlkit::pay::server::Entrypoint { callback, short_description: String::from("today i become death"), - long_description: Some(String::from("the destroyer of worlds")), + long_description: None, jpeg: None, png: None, comment_size: None, diff --git a/tests/lud18.rs b/tests/lud18.rs new file mode 100644 index 0000000..7aefd1f --- /dev/null +++ b/tests/lud18.rs @@ -0,0 +1,105 @@ +#[tokio::test] +async fn test() { + let listener = tokio::net::TcpListener::bind("0.0.0.0:0") + .await + .expect("net"); + + let addr = listener.local_addr().expect("addr"); + + let query_url = format!("http://{addr}/lnurlp"); + let callback_url = url::Url::parse(&format!("http://{addr}/lnurlp/callback")).expect("url"); + + let router = lnurlkit::Server::default() + .pay_request( + move |_| { + let callback = callback_url.clone(); + async { + Ok(lnurlkit::pay::server::Entrypoint { + callback, + short_description: String::from("today i become death"), + long_description: None, + jpeg: None, + png: None, + comment_size: None, + min: 314, + max: 315, + identifier: None, + email: None, + currencies: None, + payer: Some(lnurlkit::pay::PayerRequirements { + name: None, + pubkey: None, + identifier: Some(lnurlkit::pay::PayerRequirement { mandatory: true }), + email: None, + auth: Some(lnurlkit::pay::PayerRequirementAuth { + mandatory: false, + k1: *b"12312312312312312312321312312312", + }), + others: std::collections::HashMap::new(), + }), + }) + } + }, + |req: lnurlkit::pay::server::Callback| async move { + Ok(lnurlkit::pay::server::CallbackResponse { + pr: format!("pierre:{:?}", req.payer), + disposable: false, + success_action: None, + }) + }, + ) + .build(); + + tokio::spawn(async move { + axum::serve(listener, router).await.expect("serve"); + }); + + let client = lnurlkit::Client::default(); + + let lnurl = bech32::encode( + "lnurl", + bech32::ToBase32::to_base32(&query_url), + bech32::Variant::Bech32, + ) + .expect("lnurl"); + + let queried = client.entrypoint(&lnurl).await.expect("query"); + let lnurlkit::client::Entrypoint::Pay(pr) = queried else { + panic!("not pay request"); + }; + + let payer = pr.core.payer.as_ref().unwrap(); + + assert!(payer.name.is_none()); + assert!(payer.email.is_none()); + assert!(payer.pubkey.is_none()); + assert!(matches!( + payer.auth.as_ref().unwrap(), + lnurlkit::pay::PayerRequirementAuth { mandatory, k1 } if !*mandatory && k1 == b"12312312312312312312321312312312" + )); + assert!( + matches!(payer.identifier.as_ref().unwrap(), lnurlkit::pay::PayerRequirement { mandatory } if *mandatory) + ); + + let invoice = pr + .invoice( + &lnurlkit::pay::Amount::Millisatoshis(314), + Some("comment"), + None, + Some(lnurlkit::pay::PayerInformations { + name: None, + pubkey: None, + identifier: Some(String::from("senhor")), + email: None, + auth: Some(lnurlkit::pay::PayerInformationAuth { + key: b"linkinpark".to_vec(), + k1: *b"12312312312312312312321312312312", + sig: *b"1231231231231231231232131231231212312312312312312312321312312312", + }), + }), + ) + .await + .expect("callback"); + + assert_eq!(&invoice.pr as &str, "pierre:Some(PayerInformations { name: None, pubkey: None, identifier: Some(\"senhor\"), email: None, auth: Some(PayerInformationAuth { key: [108, 105, 110, 107, 105, 110, 112, 97, 114, 107], k1: [49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 50, 49, 51, 49, 50, 51, 49, 50, 51, 49, 50], sig: [49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 50, 49, 51, 49, 50, 51, 49, 50, 51, 49, 50, 49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 49, 50, 51, 50, 49, 51, 49, 50, 51, 49, 50, 51, 49, 50] }) })"); +} diff --git a/tests/lud21.rs b/tests/lud21.rs index db8e6ad..b077e70 100644 --- a/tests/lud21.rs +++ b/tests/lud21.rs @@ -17,7 +17,7 @@ async fn test() { Ok(lnurlkit::pay::server::Entrypoint { callback, short_description: String::from("today i become death"), - long_description: Some(String::from("the destroyer of worlds")), + long_description: None, jpeg: None, png: None, comment_size: None,