Skip to content

Commit

Permalink
Merge pull request #5 from pubky/bug/label-whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
tipogi authored Dec 18, 2024
2 parents 9173187 + ae2d76a commit 2b5371e
Showing 1 changed file with 103 additions and 7 deletions.
110 changes: 103 additions & 7 deletions src/tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<String>()
.to_lowercase();

// Enforce maximum label length safely
let label = label.chars().take(MAX_TAG_LABEL_LENGTH).collect::<String>();
label = label.chars().take(MAX_TAG_LABEL_LENGTH).collect::<String>();

// Sanitize URI
let uri = match Url::parse(&self.uri) {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -227,7 +264,7 @@ mod tests {
let blob = tag_json.as_bytes();
let tag = <PubkyAppTag as Validatable>::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]
Expand All @@ -240,7 +277,7 @@ mod tests {
}
"#;

let id = "B55PGPFV1E5E0HQ2PB76EQGXPR";
let id = "D2DV4EZDA03Q3KCRMVGMDYZ8C0";
let blob = tag_json.as_bytes();
let result = <PubkyAppTag as Validatable>::try_from(&blob, &id);
assert!(result.is_err());
Expand All @@ -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");
}
}

0 comments on commit 2b5371e

Please sign in to comment.