Skip to content

Commit

Permalink
feat(pay): add lud18 base test
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 18, 2023
1 parent df5fa64 commit 8e81afc
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 3 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion tests/lud16.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
105 changes: 105 additions & 0 deletions tests/lud18.rs
Original file line number Diff line number Diff line change
@@ -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] }) })");
}
2 changes: 1 addition & 1 deletion tests/lud21.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 8e81afc

Please sign in to comment.