From f1a229002f4d643e4776f68e85135f8f8cab87a6 Mon Sep 17 00:00:00 2001 From: appflowy Date: Tue, 6 Jul 2021 21:20:53 +0800 Subject: [PATCH] enable test target parse response if need --- .githook/pre-push | 12 ++++ .idea/libraries/Dart_Packages.xml | 16 ----- rust-lib/flowy-sys/src/data.rs | 28 ++++++--- rust-lib/flowy-test/src/lib.rs | 44 ++++++++------ .../src/domain/user/user_password.rs | 35 +++++++---- rust-lib/flowy-user/tests/sign_in.rs | 58 ++++++++++--------- 6 files changed, 115 insertions(+), 78 deletions(-) diff --git a/.githook/pre-push b/.githook/pre-push index 72c666bcb0..113d1e14bd 100755 --- a/.githook/pre-push +++ b/.githook/pre-push @@ -13,6 +13,18 @@ fi printf "\e[33;1m%s\e[0m\n" 'Finished running the Flutter analyzer' printf "\e[33;1m%s\e[0m\n" 'Running unit tests' +#current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,') +#if [ "$current_branch" = "main" ]; then +# echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" +# echo "" +# echo " Should not push to main directly. Use Pull Request instead" +# echo "" +# echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" +# exit 1 +#fi + + + #flutter test #if [ $? -ne 0 ]; then # printf "\e[31;1m%s\e[0m\n" 'Unit tests error' diff --git a/.idea/libraries/Dart_Packages.xml b/.idea/libraries/Dart_Packages.xml index f30e73c4f6..1f41b04ded 100644 --- a/.idea/libraries/Dart_Packages.xml +++ b/.idea/libraries/Dart_Packages.xml @@ -113,7 +113,6 @@ @@ -121,7 +120,6 @@ @@ -247,7 +245,6 @@ @@ -528,7 +525,6 @@ @@ -553,13 +549,6 @@ - - - - - - @@ -773,9 +762,7 @@ - - @@ -799,7 +786,6 @@ - @@ -841,12 +827,10 @@ - - diff --git a/rust-lib/flowy-sys/src/data.rs b/rust-lib/flowy-sys/src/data.rs index e8f6cdf0a4..2294b04224 100644 --- a/rust-lib/flowy-sys/src/data.rs +++ b/rust-lib/flowy-sys/src/data.rs @@ -95,15 +95,27 @@ where T: FromBytes, { type Error = String; + fn try_from(payload: &Payload) -> Result, Self::Error> { parse_payload(payload) } +} - fn try_from(payload: &Payload) -> Result, Self::Error> { - match payload { - Payload::None => Err(format!("Expected payload")), - Payload::Bytes(bytes) => match T::parse_from_bytes(bytes) { - Ok(data) => Ok(Data(data)), - Err(e) => Err(e), - }, - } +impl std::convert::TryFrom for Data +where + T: FromBytes, +{ + type Error = String; + fn try_from(payload: Payload) -> Result, Self::Error> { parse_payload(&payload) } +} + +fn parse_payload(payload: &Payload) -> Result, String> +where + T: FromBytes, +{ + match payload { + Payload::None => Err(format!("Expected payload")), + Payload::Bytes(bytes) => match T::parse_from_bytes(&bytes) { + Ok(data) => Ok(Data(data)), + Err(e) => Err(e), + }, } } diff --git a/rust-lib/flowy-test/src/lib.rs b/rust-lib/flowy-test/src/lib.rs index 92c5162ac9..255337db5b 100644 --- a/rust-lib/flowy-test/src/lib.rs +++ b/rust-lib/flowy-test/src/lib.rs @@ -42,8 +42,9 @@ fn root_dir() -> String { } pub struct EventTester { - request: DispatchRequest, + request: Option, assert_status_code: Option, + response: Option, } impl EventTester { @@ -54,8 +55,9 @@ impl EventTester { init_sdk(); let request = DispatchRequest::new(event); Self { - request, + request: Some(request), assert_status_code: None, + response: None, } } @@ -63,7 +65,9 @@ impl EventTester { where P: ToBytes, { - self.request = self.request.payload(Data(payload)); + let mut request = self.request.take().unwrap(); + request = request.payload(Data(payload)); + self.request = Some(request); self } @@ -73,26 +77,32 @@ impl EventTester { } #[allow(dead_code)] - pub async fn async_send(self) -> R - where - R: FromBytes, - { - let resp = async_send(self.request).await; + pub async fn async_send(mut self) -> Self { + let resp = async_send(self.request.take().unwrap()).await; + if let Some(ref status_code) = self.assert_status_code { + assert_eq!(&resp.status_code, status_code) + } dbg!(&resp); - data_from_response(&resp) + self.response = Some(resp); + self } - pub fn sync_send(self) -> R + pub fn sync_send(mut self) -> Self { + let resp = sync_send(self.request.take().unwrap()); + if let Some(ref status_code) = self.assert_status_code { + assert_eq!(&resp.status_code, status_code) + } + dbg!(&resp); + self.response = Some(resp); + self + } + + pub fn parse(self) -> R where R: FromBytes, { - let resp = sync_send(self.request); - if let Some(status_code) = self.assert_status_code { - assert_eq!(resp.status_code, status_code) - } - - dbg!(&resp); - data_from_response(&resp) + let response = self.response.unwrap(); + >::try_from(response.payload).unwrap().into_inner() } } diff --git a/rust-lib/flowy-user/src/domain/user/user_password.rs b/rust-lib/flowy-user/src/domain/user/user_password.rs index e29b167978..99e0695cc2 100644 --- a/rust-lib/flowy-user/src/domain/user/user_password.rs +++ b/rust-lib/flowy-user/src/domain/user/user_password.rs @@ -6,20 +6,25 @@ pub struct UserPassword(pub String); impl UserPassword { pub fn parse(s: String) -> Result { - let is_empty_or_whitespace = s.trim().is_empty(); - if is_empty_or_whitespace { + if s.trim().is_empty() { return Err(format!("Password can not be empty or whitespace.")); } - let is_too_long = s.graphemes(true).count() > 100; + + if s.graphemes(true).count() > 100 { + return Err(format!("Password too long.")); + } + let forbidden_characters = ['/', '(', ')', '"', '<', '>', '\\', '{', '}']; let contains_forbidden_characters = s.chars().any(|g| forbidden_characters.contains(&g)); - let is_invalid_password = !validate_password(&s); - - if is_too_long || contains_forbidden_characters || is_invalid_password { - Err(format!("{} is not a valid password.", s)) - } else { - Ok(Self(s)) + if contains_forbidden_characters { + return Err(format!("Password contains forbidden characters.")); } + + if !validate_password(&s) { + return Err(format!("Password should contain a minimum of 6 characters with 1 special 1 letter and 1 numeric")); + } + + Ok(Self(s)) } } @@ -27,7 +32,7 @@ lazy_static! { // Test it in https://regex101.com/ // https://stackoverflow.com/questions/2370015/regular-expression-for-password-validation/2370045 // Hell1! - // [invalid, less than 6] + // [invalid, greater or equal to 6] // Hel1! // // Hello1! @@ -40,4 +45,12 @@ lazy_static! { static ref PASSWORD: Regex = Regex::new("((?=.*\\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\\W]).{6,20})").unwrap(); } -pub fn validate_password(password: &str) -> bool { PASSWORD.is_match(password).is_ok() } +pub fn validate_password(password: &str) -> bool { + match PASSWORD.is_match(password) { + Ok(is_match) => is_match, + Err(e) => { + log::error!("validate_password fail: {:?}", e); + false + }, + } +} diff --git a/rust-lib/flowy-user/tests/sign_in.rs b/rust-lib/flowy-user/tests/sign_in.rs index 4d886d7565..8407d014bf 100644 --- a/rust-lib/flowy-user/tests/sign_in.rs +++ b/rust-lib/flowy-user/tests/sign_in.rs @@ -2,46 +2,52 @@ use flowy_test::prelude::*; use flowy_user::prelude::*; #[test] -#[should_panic] -fn sign_in_without_password() { - let params = UserSignInParams { - email: "annie@appflowy.io".to_string(), - password: "".to_string(), - }; +fn sign_in_with_invalid_email() { + let test_cases = vec!["", "annie@", "annie@gmail@"]; + let password = "Appflowy!123".to_string(); - let result = EventTester::new(SignIn) - .payload(params) - .assert_status_code(StatusCode::Err) - .sync_send::(); - dbg!(&result); + for email in test_cases { + let params = UserSignInParams { + email: email.to_string(), + password: password.clone(), + }; + + let _ = EventTester::new(SignIn) + .payload(params) + .assert_status_code(StatusCode::Err) + .sync_send(); + } } #[test] -#[should_panic] fn sign_in_with_invalid_password() { - let params = UserSignInParams { - email: "annie@appflowy.io".to_string(), - password: "123".to_string(), - }; + let test_cases = vec!["".to_string(), "123456".to_owned(), "1234".repeat(100)]; + let email = "annie@appflowy.io".to_string(); - let result = EventTester::new(SignIn) - .payload(params) - .assert_status_code(StatusCode::Err) - .sync_send::(); - dbg!(&result); + for password in test_cases { + let params = UserSignInParams { + email: email.clone(), + password, + }; + + let _ = EventTester::new(SignIn) + .payload(params) + .assert_status_code(StatusCode::Err) + .sync_send(); + } } #[test] -#[should_panic] -fn sign_in_without_email() { +fn sign_in_success() { let params = UserSignInParams { - email: "".to_string(), + email: "annie@appflowy.io".to_string(), password: "HelloWorld!123".to_string(), }; let result = EventTester::new(SignIn) .payload(params) - .assert_status_code(StatusCode::Err) - .sync_send::(); + .assert_status_code(StatusCode::Ok) + .sync_send() + .parse::(); dbg!(&result); }