From 1e0018cb66110407122d31d783f565a4451f3840 Mon Sep 17 00:00:00 2001 From: tipogi Date: Fri, 6 Dec 2024 18:07:19 +0100 Subject: [PATCH 1/5] tag sanitation and validation tests --- Cargo.lock | 85 +++++++++++++++++++++++++++++++++++- Cargo.toml | 3 ++ src/tag.rs | 125 ++++++++++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 200 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6c3e753..b74b7d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,6 +1,21 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -version = 3 +version = 4 + +[[package]] +name = "addr2line" +version = "0.24.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfbe277e56a376000877090da837660b4427aad530e3028d44e0bffe4f89a1c1" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "512761e0bb2578dd7380c6baaa0f4ce03e84f95e960231d1dec8bf4d7d6e2627" [[package]] name = "aead" @@ -77,6 +92,21 @@ version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" +[[package]] +name = "backtrace" +version = "0.3.74" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d82cb332cdfaed17ae235a638438ac4d4839913cc2af585c3c6746e8f8bee1a" +dependencies = [ + "addr2line", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", + "windows-targets", +] + [[package]] name = "base32" version = "0.5.1" @@ -520,6 +550,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "gimli" +version = "0.31.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" + [[package]] name = "hash32" version = "0.2.1" @@ -818,6 +854,15 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "miniz_oxide" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2d80299ef12ff69b16a84bb182e3b9df68b5a91574d3d4fa6e41b65deec4df1" +dependencies = [ + "adler2", +] + [[package]] name = "nanorand" version = "0.7.0" @@ -836,6 +881,15 @@ dependencies = [ "autocfg", ] +[[package]] +name = "object" +version = "0.36.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aedf0a2d09c573ed1d8d85b30c119153926a2b36dce0ab28322c09a117a4683e" +dependencies = [ + "memchr", +] + [[package]] name = "once_cell" version = "1.20.2" @@ -968,6 +1022,7 @@ dependencies = [ "pubky-common", "serde", "serde_json", + "tokio", "url", "utoipa", ] @@ -1047,6 +1102,12 @@ dependencies = [ "getrandom", ] +[[package]] +name = "rustc-demangle" +version = "0.1.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" + [[package]] name = "rustc_version" version = "0.4.1" @@ -1279,6 +1340,28 @@ dependencies = [ "zerovec", ] +[[package]] +name = "tokio" +version = "1.42.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" +dependencies = [ + "backtrace", + "pin-project-lite", + "tokio-macros", +] + +[[package]] +name = "tokio-macros" +version = "2.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing" version = "0.1.40" diff --git a/Cargo.toml b/Cargo.toml index dce5752..cc6a3bb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,3 +19,6 @@ url = "2.5.4" base32 = "0.5.1" blake3 = "1.5.4" chrono = "0.4.38" + +[dev-dependencies] +tokio = { version = "1", features = ["macros", "rt"] } diff --git a/src/tag.rs b/src/tag.rs index d52b83a..abf7374 100644 --- a/src/tag.rs +++ b/src/tag.rs @@ -15,7 +15,7 @@ const MAX_TAG_LABEL_LENGTH: usize = 20; /// `/pub/pubky.app/tags/FPB0AM9S93Q3M1GFY1KV09GMQM` /// /// Where tag_id is Crockford-base32(Blake3("{uri_tagged}:{label}")[:half]) -#[derive(Serialize, Deserialize, Default)] +#[derive(Serialize, Deserialize, Default, Debug)] pub struct PubkyAppTag { pub uri: String, pub label: String, @@ -33,11 +33,17 @@ impl HashId for PubkyAppTag { #[async_trait] impl Validatable for PubkyAppTag { async fn sanitize(self) -> Result { + // Remove spaces from the tag and keep it as one word + let mut label = self + .label + .chars() + .filter(|c| !c.is_whitespace()) + .collect::(); // Convert label to lowercase and trim - let label = self.label.trim().to_lowercase(); + label = label.trim().to_lowercase(); // Enforce maximum label length safely - let label = label.chars().take(MAX_TAG_LABEL_LENGTH).collect::(); + label = label.chars().take(MAX_TAG_LABEL_LENGTH).collect::(); // Sanitize URI let uri = match Url::parse(&self.uri) { @@ -66,14 +72,109 @@ impl Validatable for PubkyAppTag { } } -#[test] -fn test_create_id() { - let tag = PubkyAppTag { - uri: "user_id/pub/pubky.app/posts/post_id".to_string(), - created_at: 1627849723, - label: "cool".to_string(), - }; +#[cfg(test)] +mod tests { + use super::*; + use tokio; - let tag_id = tag.create_id(); - println!("Generated Tag ID: {}", tag_id); + #[test] + fn test_label_id() { + // Precomputed earlier + let tag_id = "CBYS8P6VJPHC5XXT4WDW26662W"; + // Create new tag + let tag = PubkyAppTag { + uri: "pubky://user_id/pub/pubky.app/posts/post_id".to_string(), + created_at: 1627849723, + label: "cool".to_string(), + }; + // Check if the tag ID is correct + assert_eq!( + tag.create_id(), + tag_id + ); + + let wrong_tag = PubkyAppTag { + uri: "pubky://user_id/pub/pubky.app/posts/post_id".to_string(), + created_at: 1627849723, + label: "co0l".to_string(), + }; + + // Assure that the new tag has wrong ID + assert_ne!( + wrong_tag.create_id(), + tag_id + ); + } + + #[tokio::test] + async fn test_incorrect_label() -> Result<(), DynError> { + let tag = PubkyAppTag { + uri: "user_id/pub/pubky.app/posts/post_id".to_string(), + created_at: 1627849723, + label: "cool".to_string(), + }; + + match tag.sanitize().await { + Err(e) => assert_eq!(e.to_string(), "Invalid URI in tag".to_string(), "The error message is not related URI or the message description is wrong"), + _ => () + }; + + let tag = PubkyAppTag { + uri: "pubky://user_id/pub/pubky.app/posts/post_id".to_string(), + created_at: 1627849723, + label: "coolc00lcolaca0g00llooll".to_string(), + }; + + // Precomputed earlier + let label_id = "8WXXXXHK028RH8AWBZZNHJYDN4"; + + match tag.validate(label_id).await { + Err(e) => assert_eq!(e.to_string(), "Tag label exceeds maximum length".to_string(), "The error message is not related tag length or the message description is wrong"), + _ => () + }; + + Ok(()) + + + } + + #[tokio::test] + async fn test_white_space_tag() -> Result<(), DynError> { + // All the tags has to be that label after sanitation + let label = "cool"; + + let leading_whitespace = PubkyAppTag { + uri: "pubky://user_id/pub/pubky.app/posts/post_id".to_string(), + created_at: 1627849723, + label: " cool".to_string(), + }; + let mut sanitazed_label = leading_whitespace.sanitize().await?; + assert_eq!(sanitazed_label.label, label); + + let trailing_whitespace = PubkyAppTag { + uri: "pubky://user_id/pub/pubky.app/posts/post_id".to_string(), + created_at: 1627849723, + label: " cool".to_string(), + }; + sanitazed_label = trailing_whitespace.sanitize().await?; + assert_eq!(sanitazed_label.label, label); + + let space_between = PubkyAppTag { + uri: "pubky://user_id/pub/pubky.app/posts/post_id".to_string(), + created_at: 1627849723, + label: "co ol".to_string(), + }; + sanitazed_label = space_between.sanitize().await?; + assert_eq!(sanitazed_label.label, label); + + let space_between = PubkyAppTag { + uri: "pubky://user_id/pub/pubky.app/posts/post_id".to_string(), + created_at: 1627849723, + label: " co ol ".to_string(), + }; + sanitazed_label = space_between.sanitize().await?; + assert_eq!(sanitazed_label.label, label); + + Ok(()) + } } From ec5c5dc666931ad8aba076cb6f786998ac72cf73 Mon Sep 17 00:00:00 2001 From: tipogi Date: Wed, 18 Dec 2024 10:03:28 +0100 Subject: [PATCH 2/5] rollback cargo.toml and .lock --- Cargo.lock | 118 ++++++++++++++++++++++++++++------------------------- Cargo.toml | 2 +- 2 files changed, 63 insertions(+), 57 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 316328f..eb16c7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -29,9 +29,9 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.94" +version = "1.0.93" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c1fd03a028ef38ba2276dce7e33fcd6369c158a1bca17946c4b1b701891c1ff7" +checksum = "4c95c10ba0b00a02636238b814946408b1322d5ac4760326e6fb8ec956d85775" [[package]] name = "argon2" @@ -122,9 +122,9 @@ dependencies = [ [[package]] name = "blake3" -version = "1.5.5" +version = "1.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b8ee0c1824c4dea5b5f81736aff91bae041d2c07ee1192bec91054e10e3e601e" +checksum = "d82033247fd8e890df8f740e407ad4d038debb9eb1f40533fffb32e7d17dc6f7" dependencies = [ "arrayref", "arrayvec", @@ -162,9 +162,9 @@ checksum = "325918d6fe32f23b19878fe4b34794ae41fc19ddbe53b10571a4874d44ffd39b" [[package]] name = "cc" -version = "1.2.2" +version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f34d93e62b03caf570cccc334cbc6c2fceca82f39211051345108adcba3eebdc" +checksum = "fd9de9f2205d5ef3fd67e685b0df337994ddd4495e2a28d185500d0e1edfea47" dependencies = [ "shlex", ] @@ -603,11 +603,17 @@ dependencies = [ "stable_deref_trait", ] +[[package]] +name = "hermit-abi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" + [[package]] name = "http" -version = "1.2.0" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f16ca2af56261c99fba8bac40a10251ce8188205a4c448fbb745a2e4daa76fea" +checksum = "21b9ddb458710bc376481b842f5da65cdf31522de232c1ca8146abce2a358258" dependencies = [ "bytes", "fnv", @@ -846,9 +852,9 @@ dependencies = [ [[package]] name = "indexmap" -version = "2.7.0" +version = "2.6.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f" +checksum = "707907fe3c25f5424cce2cb7e1cbcafee6bdbe735ca90ef77c29e84591e5b9da" dependencies = [ "equivalent", "hashbrown", @@ -878,19 +884,18 @@ checksum = "d75a2a4b1b190afb6f5425f10f6a8f959d2ea0b9c2b1d79553551850539e4674" [[package]] name = "js-sys" -version = "0.3.74" +version = "0.3.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a865e038f7f6ed956f788f0d7d60c541fff74c7bd74272c5d4cf15c63743e705" +checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" dependencies = [ - "once_cell", "wasm-bindgen", ] [[package]] name = "libc" -version = "0.2.167" +version = "0.2.166" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "09d6582e104315a817dff97f75133544b2e094ee22447d2acf4a74e189ba06fc" +checksum = "c2ccc108bbc0b1331bd061864e7cd823c0cab660bbe6970e66e2c0614decde36" [[package]] name = "litemap" @@ -969,10 +974,11 @@ dependencies = [ [[package]] name = "mio" -version = "1.0.3" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2886843bf800fba2e3377cff24abf6379b4c4d5c6681eaf9ea5b0d15090450bd" +checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" dependencies = [ + "hermit-abi", "libc", "wasi", "windows-sys 0.52.0", @@ -1116,9 +1122,9 @@ dependencies = [ [[package]] name = "postcard" -version = "1.1.1" +version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "170a2601f67cc9dba8edd8c4870b15f71a6a2dc196daec8c83f72b59dff628a8" +checksum = "f63d01def49fc815900a83e7a4a5083d2abc81b7ddd569a3fa0477778ae9b3ec" dependencies = [ "cobs", "embedded-io 0.4.0", @@ -1251,7 +1257,7 @@ dependencies = [ "rustc-hash", "rustls", "socket2", - "thiserror 2.0.4", + "thiserror 2.0.3", "tokio", "tracing", ] @@ -1270,7 +1276,7 @@ dependencies = [ "rustls", "rustls-pki-types", "slab", - "thiserror 2.0.4", + "thiserror 2.0.3", "tinyvec", "tracing", "web-time", @@ -1405,9 +1411,9 @@ checksum = "719b953e2095829ee67db738b3bfa9fa368c94900df327b3f07fe6e794d2fe1f" [[package]] name = "rustc-hash" -version = "2.1.0" +version = "2.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7fb8039b3032c191086b10f11f319a6e99e1e82889c5cc6046f515c9db1d497" +checksum = "583034fd73374156e66797ed8e5b0d5690409c9226b22d87cb7f19821c05d152" [[package]] name = "rustc_version" @@ -1484,9 +1490,9 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49" [[package]] name = "self_cell" -version = "1.1.0" +version = "1.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c2fdfc24bc566f839a2da4c4295b82db7d25a24253867d5c64355abb5799bdbe" +checksum = "d369a96f978623eb3dc28807c4852d6cc617fed53da5d3c400feff1ef34a714a" [[package]] name = "semver" @@ -1665,9 +1671,9 @@ checksum = "13c2bddecc57b384dee18652358fb23172facb8a2c51ccc10d74c157bdea3292" [[package]] name = "syn" -version = "2.0.90" +version = "2.0.89" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "919d3b74a5dd0ccd15aeb8f93e7006bd9e14c295087c9896a110f490752bcf31" +checksum = "44d46482f1c1c87acd84dea20c1bf5ebff4c757009ed6bf19cfd36fb10e92c4e" dependencies = [ "proc-macro2", "quote", @@ -1705,11 +1711,11 @@ dependencies = [ [[package]] name = "thiserror" -version = "2.0.4" +version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2f49a1853cf82743e3b7950f77e0f4d622ca36cf4317cba00c767838bac8d490" +checksum = "c006c85c7651b3cf2ada4584faa36773bd07bac24acfb39f3c431b36d7e667aa" dependencies = [ - "thiserror-impl 2.0.4", + "thiserror-impl 2.0.3", ] [[package]] @@ -1725,9 +1731,9 @@ dependencies = [ [[package]] name = "thiserror-impl" -version = "2.0.4" +version = "2.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8381894bb3efe0c4acac3ded651301ceee58a15d47c2e34885ed1908ad667061" +checksum = "f077553d607adc1caf65430528a576c757a71ed73944b66ebb58ef2bbd243568" dependencies = [ "proc-macro2", "quote", @@ -1736,9 +1742,9 @@ dependencies = [ [[package]] name = "time" -version = "0.3.37" +version = "0.3.36" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "35e7868883861bd0e56d9ac6efcaaca0d6d5d82a2a7ec8209ff492c07cf37b21" +checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ "deranged", "itoa", @@ -1757,9 +1763,9 @@ checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" [[package]] name = "time-macros" -version = "0.2.19" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2834e6017e3e5e4b9834939793b282bc03b37a3336245fa820e35e233e2a85de" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" dependencies = [ "num-conv", "time-core", @@ -1792,9 +1798,9 @@ checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" [[package]] name = "tokio" -version = "1.42.0" +version = "1.41.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5cec9b21b0450273377fc97bd4c33a8acffc8c996c987a7c5b319a0083707551" +checksum = "22cfb5bee7a6a52939ca9224d6ac897bb669134078daa8735560897f69de4d33" dependencies = [ "backtrace", "bytes", @@ -1821,11 +1827,12 @@ dependencies = [ [[package]] name = "tokio-rustls" -version = "0.26.1" +version = "0.26.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5f6d0975eaace0cf0fcadee4e4aaa5da15b5c079146f2cffb67c113be122bf37" +checksum = "0c7bc40d0e5a97695bb96e27995cd3a08538541b0a846f65bba7a359f36700d4" dependencies = [ "rustls", + "rustls-pki-types", "tokio", ] @@ -1969,9 +1976,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" [[package]] name = "wasm-bindgen" -version = "0.2.97" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d15e63b4482863c109d70a7b8706c1e364eb6ea449b201a76c5b89cedcec2d5c" +checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" dependencies = [ "cfg-if", "once_cell", @@ -1980,9 +1987,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-backend" -version = "0.2.97" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8d36ef12e3aaca16ddd3f67922bc63e48e953f126de60bd33ccc0101ef9998cd" +checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" dependencies = [ "bumpalo", "log", @@ -1995,22 +2002,21 @@ dependencies = [ [[package]] name = "wasm-bindgen-futures" -version = "0.4.47" +version = "0.4.45" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9dfaf8f50e5f293737ee323940c7d8b08a66a95a419223d9f41610ca08b0833d" +checksum = "cc7ec4f8827a71586374db3e87abdb5a2bb3a15afed140221307c3ec06b1f63b" dependencies = [ "cfg-if", "js-sys", - "once_cell", "wasm-bindgen", "web-sys", ] [[package]] name = "wasm-bindgen-macro" -version = "0.2.97" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "705440e08b42d3e4b36de7d66c944be628d579796b8090bfa3471478a2260051" +checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -2018,9 +2024,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.97" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "98c9ae5a76e46f4deecd0f0255cc223cfa18dc9b261213b8aa0c7b36f61b3f1d" +checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" dependencies = [ "proc-macro2", "quote", @@ -2031,15 +2037,15 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.97" +version = "0.2.95" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6ee99da9c5ba11bd675621338ef6fa52296b76b83305e9b6e5c77d4c286d6d49" +checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" [[package]] name = "web-sys" -version = "0.3.74" +version = "0.3.72" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a98bc3c33f0fe7e59ad7cd041b89034fa82a7c2d4365ca538dda6cdaf513863c" +checksum = "f6488b90108c040df0fe62fa815cbdee25124641df01814dd7282749234c6112" dependencies = [ "js-sys", "wasm-bindgen", @@ -2280,4 +2286,4 @@ dependencies = [ "proc-macro2", "quote", "syn", -] +] \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 056d50f..5d1b585 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,10 +17,10 @@ blake3 = "1.5.4" utoipa = { version = "5.2.0", optional = true } [dev-dependencies] -tokio = { version = "1.41.1", features = ["full"] } pubky = "0.3.0" pubky-common = "0.1.0" anyhow = "1.0.93" +tokio = { version = "1.41.1", features = ["full"] } [features] openapi = ["utoipa"] From 203c648e9c31bf48f1a651277c248a365283d220 Mon Sep 17 00:00:00 2001 From: tipogi Date: Wed, 18 Dec 2024 10:17:04 +0100 Subject: [PATCH 3/5] test pass --- src/tag.rs | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/tag.rs b/src/tag.rs index 0e6a9cf..03f0522 100644 --- a/src/tag.rs +++ b/src/tag.rs @@ -55,15 +55,15 @@ impl HashId for PubkyAppTag { impl Validatable for PubkyAppTag { fn sanitize(self) -> Self { - // Remove spaces from the tag and keep it as one word - // let mut label = self - // .label - // .chars() - // .filter(|c| !c.is_whitespace()) - // .collect::(); - // Convert label to lowercase and trim - let mut label = self.label.trim().to_lowercase(); + //let mut label = self.label.trim().to_lowercase(); + // Remove spaces from the tag and keep it as one word + let mut label = self + .label + .chars() + .filter(|c| !c.is_whitespace()) + .collect::() + .to_lowercase(); // Enforce maximum label length safely label = label.chars().take(MAX_TAG_LABEL_LENGTH).collect::(); @@ -171,8 +171,6 @@ mod tests { assert_eq!(tag.label, label); // Check that created_at is recent let now = timestamp(); - println!("TIMESTAMP {}", tag.created_at); - println!("TIMESTAMP {}", now); assert!(tag.created_at <= now && tag.created_at >= now - 1_000_000); // within 1 second } @@ -267,7 +265,7 @@ mod tests { let blob = tag_json.as_bytes(); let tag = ::try_from(&blob, &id).unwrap(); assert_eq!(tag.uri, "pubky://user_pubky_id/pub/pubky.app/profile.json"); - assert_eq!(tag.label, "cool tag"); // After sanitization + assert_eq!(tag.label, "cooltag"); // After sanitization } #[test] @@ -280,7 +278,7 @@ mod tests { } "#; - let id = "B55PGPFV1E5E0HQ2PB76EQGXPR"; + let id = "D2DV4EZDA03Q3KCRMVGMDYZ8C0"; let blob = tag_json.as_bytes(); let result = ::try_from(&blob, &id); assert!(result.is_err()); @@ -346,6 +344,6 @@ mod tests { label: " co ol ".to_string(), }; sanitazed_label = space_between.sanitize(); - assert_eq!(sanitazed_label.label, "co ol"); + assert_eq!(sanitazed_label.label, "cool"); } } From 2cf0b86d1685e2afe625185582f5e19f4b3081c6 Mon Sep 17 00:00:00 2001 From: tipogi Date: Wed, 18 Dec 2024 10:18:42 +0100 Subject: [PATCH 4/5] forget to delete a comment --- src/tag.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/tag.rs b/src/tag.rs index 03f0522..502d871 100644 --- a/src/tag.rs +++ b/src/tag.rs @@ -55,9 +55,8 @@ impl HashId for PubkyAppTag { impl Validatable for PubkyAppTag { fn sanitize(self) -> Self { - // Convert label to lowercase and trim - //let mut label = self.label.trim().to_lowercase(); // Remove spaces from the tag and keep it as one word + // Returns a lowercase tag let mut label = self .label .chars() From ae2d76a814462f2720761971b4ccff64445c9fa1 Mon Sep 17 00:00:00 2001 From: tipogi Date: Wed, 18 Dec 2024 10:25:29 +0100 Subject: [PATCH 5/5] cargo.lock new line --- Cargo.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index eb16c7d..867f0d9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2286,4 +2286,4 @@ dependencies = [ "proc-macro2", "quote", "syn", -] \ No newline at end of file +]