diff --git a/src/tag.rs b/src/tag.rs index 46d1431..502d871 100644 --- a/src/tag.rs +++ b/src/tag.rs @@ -55,11 +55,17 @@ impl HashId for PubkyAppTag { impl Validatable for PubkyAppTag { fn sanitize(self) -> Self { - // Convert label to lowercase and trim - let 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() + .filter(|c| !c.is_whitespace()) + .collect::() + .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) { @@ -105,6 +111,39 @@ mod tests { use super::*; use crate::{traits::Validatable, APP_PATH}; + #[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(), + }; + + let new_tag_id = tag.create_id(); + assert!(!tag_id.is_empty()); + + // Check if the tag ID is correct + assert_eq!( + new_tag_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 + ); + } + #[test] fn test_create_id() { let tag = PubkyAppTag { @@ -131,8 +170,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 } @@ -227,7 +264,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] @@ -240,7 +277,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()); @@ -249,4 +286,63 @@ mod tests { "Validation Error: Invalid URI format: invalid_uri" ); } + + #[test] + fn test_incorrect_label() { + let tag = PubkyAppTag { + uri: "user_id/pub/pubky.app/posts/post_id".to_string(), + created_at: 1627849723, + label: "cool".to_string(), + }; + let tag_id = tag.create_id(); + + match tag.validate(&tag_id) { + Err(e) => assert_eq!(e.to_string(), format!("Validation Error: Invalid URI format: {}", tag.uri), "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 = tag.create_id(); + + match tag.validate(&label_id) { + Err(e) => assert_eq!(e.to_string(), "Validation Error: Tag label exceeds maximum length".to_string(), "The error message is not related tag length or the message description is wrong"), + _ => () + }; + } + + #[test] + fn test_white_space_tag() { + // 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(); + 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(); + 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(); + assert_eq!(sanitazed_label.label, "cool"); + } }