2022-10-27 09:24:07 +00:00
use crate ::utils ::{
is_valid_actor_name ,
is_valid_display_name ,
is_valid_matrix_id ,
is_valid_post_title ,
remove_slurs ,
scrape_text_for_mentions ,
slur_check ,
slurs_vec_to_str ,
2020-09-14 15:29:50 +00:00
} ;
2022-11-02 19:18:22 +00:00
use regex ::RegexBuilder ;
2020-09-14 15:29:50 +00:00
#[ test ]
fn test_mentions_regex ( ) {
let text = " Just read a great blog post by [@tedu@honk.teduangst.com](/u/test). And another by !test_community@fish.teduangst.com . Another [@lemmy@lemmy-alpha:8540](/u/fish) " ;
let mentions = scrape_text_for_mentions ( text ) ;
assert_eq! ( mentions [ 0 ] . name , " tedu " . to_string ( ) ) ;
assert_eq! ( mentions [ 0 ] . domain , " honk.teduangst.com " . to_string ( ) ) ;
assert_eq! ( mentions [ 1 ] . domain , " lemmy-alpha:8540 " . to_string ( ) ) ;
}
#[ test ]
2021-07-23 01:53:44 +00:00
fn test_valid_actor_name ( ) {
2022-10-27 09:24:07 +00:00
let actor_name_max_length = 20 ;
2021-09-22 15:57:09 +00:00
assert! ( is_valid_actor_name ( " Hello_98 " , actor_name_max_length ) ) ;
assert! ( is_valid_actor_name ( " ten " , actor_name_max_length ) ) ;
assert! ( ! is_valid_actor_name ( " Hello-98 " , actor_name_max_length ) ) ;
assert! ( ! is_valid_actor_name ( " a " , actor_name_max_length ) ) ;
assert! ( ! is_valid_actor_name ( " " , actor_name_max_length ) ) ;
2020-09-14 15:29:50 +00:00
}
#[ test ]
2021-04-01 17:57:45 +00:00
fn test_valid_display_name ( ) {
2022-10-27 09:24:07 +00:00
let actor_name_max_length = 20 ;
2021-09-22 15:57:09 +00:00
assert! ( is_valid_display_name ( " hello @there " , actor_name_max_length ) ) ;
assert! ( ! is_valid_display_name (
" @hello there " ,
actor_name_max_length
) ) ;
2021-04-01 18:09:53 +00:00
// Make sure zero-space with an @ doesn't work
2021-09-22 15:57:09 +00:00
assert! ( ! is_valid_display_name (
& format! ( " {} @my name is " , '\u{200b}' ) ,
actor_name_max_length
) ) ;
2020-09-14 15:29:50 +00:00
}
#[ test ]
fn test_valid_post_title ( ) {
assert! ( is_valid_post_title ( " Post Title " ) ) ;
assert! ( is_valid_post_title ( " POST TITLE 😃😃😃😃😃 " ) ) ;
assert! ( ! is_valid_post_title ( " \n \n \n \n " ) ) ; // tabs/spaces/newlines
}
2021-04-07 11:38:00 +00:00
#[ test ]
fn test_valid_matrix_id ( ) {
assert! ( is_valid_matrix_id ( " @dess:matrix.org " ) ) ;
assert! ( ! is_valid_matrix_id ( " dess:matrix.org " ) ) ;
assert! ( ! is_valid_matrix_id ( " @dess:matrix.org " ) ) ;
assert! ( ! is_valid_matrix_id ( " @dess:matrix.org t " ) ) ;
}
2020-09-14 15:29:50 +00:00
#[ test ]
fn test_slur_filter ( ) {
2022-10-27 09:24:07 +00:00
let slur_regex = Some ( RegexBuilder ::new ( r "(fag(g|got|tard)?\b|cock\s?sucker(s|ing)?|ni((g{2,}|q)+|[gq]{2,})[e3r]+(s|z)?|mudslime?s?|kikes?|\bspi(c|k)s?\b|\bchinks?|gooks?|bitch(es|ing|y)?|whor(es?|ing)|\btr(a|@)nn?(y|ies?)|\b(b|re|r)tard(ed)?s?)" ) . case_insensitive ( true ) . build ( ) . unwrap ( ) ) ;
2020-09-14 15:29:50 +00:00
let test =
2021-01-12 15:32:38 +00:00
" faggot test kike tranny cocksucker retardeds. Capitalized Niggerz. This is a bunch of other safe text. " ;
2020-09-14 15:29:50 +00:00
let slur_free = " No slurs here " ;
assert_eq! (
2021-09-22 15:57:09 +00:00
remove_slurs ( test , & slur_regex ) ,
2020-09-14 15:29:50 +00:00
" *removed* test *removed* *removed* *removed* *removed*. Capitalized *removed*. This is a bunch of other safe text. "
. to_string ( )
) ;
let has_slurs_vec = vec! [
" Niggerz " ,
2021-01-12 15:32:38 +00:00
" cocksucker " ,
" faggot " ,
" kike " ,
2020-09-14 15:29:50 +00:00
" retardeds " ,
" tranny " ,
] ;
2021-01-12 15:32:38 +00:00
let has_slurs_err_str = " No slurs - Niggerz, cocksucker, faggot, kike, retardeds, tranny " ;
2020-09-14 15:29:50 +00:00
2021-09-22 15:57:09 +00:00
assert_eq! ( slur_check ( test , & slur_regex ) , Err ( has_slurs_vec ) ) ;
assert_eq! ( slur_check ( slur_free , & slur_regex ) , Ok ( ( ) ) ) ;
if let Err ( slur_vec ) = slur_check ( test , & slur_regex ) {
2020-09-14 15:29:50 +00:00
assert_eq! ( & slurs_vec_to_str ( slur_vec ) , has_slurs_err_str ) ;
}
}
// These helped with testing
// #[test]
// fn test_send_email() {
// let result = send_email("not a subject", "test_email@gmail.com", "ur user", "<h1>HI there</h1>");
// assert!(result.is_ok());
// }