Skip to content

Commit

Permalink
Add base tests back I was drunk
Browse files Browse the repository at this point in the history
  • Loading branch information
lsunsi committed Dec 3, 2023
1 parent 7bfe64f commit eabaa91
Show file tree
Hide file tree
Showing 6 changed files with 323 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: mozilla-actions/sccache-action@v0.0.3
- run: cargo clippy --all-features
- run: cargo test --all-features
- run: cargo clippy --all-features --all-targets
- run: cargo test --all-features --all-targets
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ This library works as a toolkit so you can serve and make your LNURL requests wi

## Current support

- [LUD-01](https://github.com/lnurl/luds/blob/luds/01.md): ⚠️ core ⚠️ client ⚠️ server 🆘 tests
- [LUD-01](https://github.com/lnurl/luds/blob/luds/01.md): core client ⚠️ server ⚠️ tests
- [LUD-02](https://github.com/lnurl/luds/blob/luds/02.md): ⚠️ core ⚠️ client 🆘 server 🆘 tests
- [LUD-03](https://github.com/lnurl/luds/blob/luds/03.md): ⚠️ core ⚠️ client 🆘 server 🆘 tests
- [LUD-04](https://github.com/lnurl/luds/blob/luds/04.md): 🆘 core 🆘 client 🆘 server 🆘 tests
Expand All @@ -25,7 +25,7 @@ This library works as a toolkit so you can serve and make your LNURL requests wi
- [LUD-14](https://github.com/lnurl/luds/blob/luds/14.md): 🆘 core 🆘 client 🆘 server 🆘 tests
- [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-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-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
Expand Down
55 changes: 54 additions & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub mod withdrawal_request;
///
/// Returns error in case `s` cannot be understood.
pub fn resolve(s: &str) -> Result<url::Url, &'static str> {
if s.starts_with("lnurl1") {
if s.starts_with("lnurl1") | s.starts_with("LNURL1") {
resolve_bech32(s)
} else if s.starts_with("lnurl") || s.starts_with("keyauth") {
resolve_scheme(s)
Expand Down Expand Up @@ -100,3 +100,56 @@ impl std::str::FromStr for Query {
}
}
}

#[cfg(test)]
mod tests {
#[test]
fn resolve_bech32() {
let input = "lnurl1dp68gurn8ghj7argv4ex2tnfwvhkumelwv7hqmm0dc6p3ztw";
assert_eq!(
super::resolve(input).unwrap().to_string(),
"https://there.is/no?s=poon"
);

let input = "LNURL1DP68GURN8GHJ7ARGV4EX2TNFWVHKUMELWV7HQMM0DC6P3ZTW";
assert_eq!(
super::resolve(input).unwrap().to_string(),
"https://there.is/no?s=poon"
);
}

#[test]
fn resolve_address() {
assert_eq!(
super::resolve("no-spoon@there.is").unwrap().to_string(),
"https://there.is/.well-known/lnurlp/no-spoon"
);
}

#[test]
fn resolve_schemes() {
let input = "lnurlc://there.is/no?s=poon";
assert_eq!(
super::resolve(input).unwrap().to_string(),
"https://there.is/no?s=poon"
);

let input = "lnurlw://there.is/no?s=poon";
assert_eq!(
super::resolve(input).unwrap().to_string(),
"https://there.is/no?s=poon"
);

let input = "lnurlp://there.is/no?s=poon";
assert_eq!(
super::resolve(input).unwrap().to_string(),
"https://there.is/no?s=poon"
);

let input = "keyauth://there.is/no?s=poon";
assert_eq!(
super::resolve(input).unwrap().to_string(),
"https://there.is/no?s=poon"
);
}
}
77 changes: 71 additions & 6 deletions src/core/channel_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub const TAG: &str = "channelRequest";

#[derive(Clone, Debug)]
pub struct ChannelRequest {
callback: crate::serde::Url,
callback: url::Url,
pub uri: String,
k1: String,
}
Expand All @@ -13,27 +13,27 @@ impl ChannelRequest {
/// Returns errors on network or deserialization failures.
#[must_use]
pub fn callback_accept(mut self, remoteid: &str, private: bool) -> url::Url {
self.callback.0.query_pairs_mut().extend_pairs([
self.callback.query_pairs_mut().extend_pairs([
("k1", &self.k1 as &str),
("remoteid", remoteid),
("private", if private { "1" } else { "0" }),
]);

self.callback.0
self.callback
}

/// # Errors
///
/// Returns errors on network or deserialization failures.
#[must_use]
pub fn callback_cancel(mut self, remoteid: &str) -> url::Url {
self.callback.0.query_pairs_mut().extend_pairs([
self.callback.query_pairs_mut().extend_pairs([
("k1", &self.k1 as &str),
("remoteid", remoteid),
("cancel", "1"),
]);

self.callback.0
self.callback
}
}

Expand All @@ -45,7 +45,7 @@ impl std::str::FromStr for ChannelRequest {
miniserde::json::from_str(s).map_err(|_| "deserialize failed")?;

Ok(ChannelRequest {
callback: d.callback,
callback: d.callback.0,
uri: d.uri,
k1: d.k1,
})
Expand All @@ -63,3 +63,68 @@ mod serde {
pub k1: String,
}
}

#[cfg(test)]
mod tests {
#[test]
fn parse() {
let input = r#"
{
"uri": "noh@ipe:porta",
"callback": "https://yuri?o=callback",
"k1": "caum"
}
"#;

let parsed = input.parse::<super::ChannelRequest>().expect("parse");

assert_eq!(parsed.callback.to_string(), "https://yuri/?o=callback");
assert_eq!(parsed.uri, "noh@ipe:porta");
assert_eq!(parsed.k1, "caum");
}

#[test]
fn callback_accept() {
let input = r#"
{
"uri": "noh@ipe:porta",
"callback": "https://yuri?o=callback",
"k1": "caum"
}
"#;

let parsed = input.parse::<super::ChannelRequest>().expect("parse");
let url = parsed.clone().callback_accept("idremoto", true);

assert_eq!(
url.to_string(),
"https://yuri/?o=callback&k1=caum&remoteid=idremoto&private=1"
);

let url = parsed.callback_accept("idremoto", false);

assert_eq!(
url.to_string(),
"https://yuri/?o=callback&k1=caum&remoteid=idremoto&private=0"
);
}

#[test]
fn callback_cancel() {
let input = r#"
{
"uri": "noh@ipe:porta",
"callback": "https://yuri?o=callback",
"k1": "caum"
}
"#;

let parsed = input.parse::<super::ChannelRequest>().expect("parse");
let url = parsed.callback_cancel("idremoto");

assert_eq!(
url.to_string(),
"https://yuri/?o=callback&k1=caum&remoteid=idremoto&cancel=1"
);
}
}
147 changes: 147 additions & 0 deletions src/core/pay_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,150 @@ mod de {
pub disposable: Option<bool>,
}
}

#[cfg(test)]
mod tests {
#[test]
fn parse_base() {
let input = r#"
{
"callback": "https://yuri?o=callback",
"metadata": "[[\"text/plain\", \"boneco do steve magal\"]]",
"maxSendable": 315,
"minSendable": 314
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");

assert_eq!(parsed.callback.to_string(), "https://yuri/?o=callback");
assert_eq!(parsed.short_description, "boneco do steve magal");
assert_eq!(parsed.min, 314);
assert_eq!(parsed.max, 315);

assert_eq!(parsed.comment_size, 0);
assert!(parsed.long_description.is_none());
assert!(parsed.success_action.is_none());
assert!(parsed.jpeg.is_none());
assert!(parsed.png.is_none());
}

#[test]
fn parse_comment_size() {
let input = r#"
{
"callback": "https://yuri?o=callback",
"metadata": "[[\"text/plain\", \"boneco do steve magal\"]]",
"commentAllowed": 140,
"maxSendable": 315,
"minSendable": 314
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");
assert_eq!(parsed.comment_size, 140);
}

#[test]
fn parse_long_description() {
let input = r#"
{
"callback": "https://yuri?o=callback",
"metadata": "[[\"text/plain\", \"boneco do steve magal\"],[\"text/long-desc\", \"mochila a jato brutal incluida\"]]",
"maxSendable": 315,
"minSendable": 314
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");
assert_eq!(
parsed.long_description.unwrap(),
"mochila a jato brutal incluida"
);
}

#[test]
fn parse_images() {
let input = r#"
{
"callback": "https://yuri?o=callback",
"metadata": "[[\"text/plain\", \"boneco do steve magal\"],[\"image/png;base64\", \"Zm90b2JydXRhbA==\"],[\"image/jpeg;base64\", \"aW1hZ2VtYnJ1dGFs\"]]",
"maxSendable": 315,
"minSendable": 314
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");
assert_eq!(parsed.jpeg.unwrap(), b"imagembrutal");
assert_eq!(parsed.png.unwrap(), b"fotobrutal");
}

#[test]
fn parse_success_actions() {
let input = r#"
{
"callback": "https://yuri?o=callback",
"metadata": "[[\"text/plain\", \"boneco do steve magal\"]]",
"maxSendable": 315,
"minSendable": 314,
"successAction": {
"tag": "message",
"message": "obrigado!"
}
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");
let Some(super::SuccessAction::Message(m)) = parsed.success_action else {
panic!("bad success action");
};

assert_eq!(m, "obrigado!");

let input = r#"
{
"callback": "https://yuri?o=callback",
"metadata": "[[\"text/plain\", \"boneco do steve magal\"]]",
"maxSendable": 315,
"minSendable": 314,
"successAction": {
"tag": "url",
"description": "valeu demais",
"url": "http://uerre.ele"
}
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");
let Some(super::SuccessAction::Url(u, d)) = parsed.success_action else {
panic!("bad success action");
};

assert_eq!(u.to_string(), "http://uerre.ele/");
assert_eq!(d, "valeu demais");
}

#[test]
fn callback() {
let input = r#"
{
"callback": "https://yuri?o=callback",
"metadata": "[[\"text/plain\", \"boneco do steve magal\"]]",
"maxSendable": 315,
"minSendable": 314
}
"#;

let parsed = input.parse::<super::PayRequest>().expect("parse");

assert_eq!(
parsed.clone().callback("comentario", 314).to_string(),
"https://yuri/?o=callback&comment=comentario&amount=314"
);

assert_eq!(
parsed.callback("", 314).to_string(),
"https://yuri/?o=callback&amount=314"
);
}
}
Loading

0 comments on commit eabaa91

Please sign in to comment.