diff --git a/rust-lib/flowy-derive/tests/progress.rs b/rust-lib/flowy-derive/tests/progress.rs index 3dbb147199..6f1f5b1b9e 100644 --- a/rust-lib/flowy-derive/tests/progress.rs +++ b/rust-lib/flowy-derive/tests/progress.rs @@ -1,5 +1,5 @@ #[tokio::test] async fn tests() { let t = trybuild::TestCases::new(); - t.pass("tests/protobuf_enum.rs"); + // t.pass("tests/protobuf_enum.rs"); } diff --git a/rust-lib/flowy-dispatch/src/response/response.rs b/rust-lib/flowy-dispatch/src/response/response.rs index edc6e63789..f0ed7c3fb6 100644 --- a/rust-lib/flowy-dispatch/src/response/response.rs +++ b/rust-lib/flowy-dispatch/src/response/response.rs @@ -1,11 +1,12 @@ use crate::{ + byte_trait::FromBytes, data::Data, - errors::DispatchError, + errors::{DispatchError, InternalError}, request::{EventRequest, Payload}, response::Responder, }; use derivative::*; -use std::{fmt, fmt::Formatter}; +use std::{convert::TryFrom, fmt, fmt::Formatter}; #[derive(Clone, Debug, Eq, PartialEq, serde::Serialize)] pub enum StatusCode { @@ -30,6 +31,24 @@ impl EventResponse { error: None, } } + + pub fn parse(self) -> Result, DispatchError> + where + T: FromBytes, + E: FromBytes, + { + if self.status_code == StatusCode::Err { + match >::try_from(self.payload) { + Ok(err) => Ok(Err(err.into_inner())), + Err(e) => Err(InternalError::new(e).into()), + } + } else { + match >::try_from(self.payload) { + Ok(a) => Ok(Ok(a.into_inner())), + Err(e) => Err(InternalError::new(e).into()), + } + } + } } impl std::fmt::Display for EventResponse { diff --git a/rust-lib/flowy-test/src/lib.rs b/rust-lib/flowy-test/src/lib.rs index d06d4d965c..9c89eafc34 100644 --- a/rust-lib/flowy-test/src/lib.rs +++ b/rust-lib/flowy-test/src/lib.rs @@ -112,6 +112,18 @@ where let inner = self.inner.take().unwrap(); inner.error() } + + pub fn assert_error(mut self) -> Self { + let inner = self.inner.take().unwrap(); + self.inner = Some(inner.assert_error()); + self + } + + pub fn assert_success(mut self) -> Self { + let inner = self.inner.take().unwrap(); + self.inner = Some(inner.assert_success()); + self + } } pub struct Tester { @@ -167,11 +179,17 @@ where self } + pub fn assert_success(mut self) -> Self { + self.assert_status_code = Some(StatusCode::Ok); + self + } + pub async fn async_send(mut self) -> Self { + assert_eq!(self.inner_request.is_some(), true, "must set event"); + let resp = EventDispatch::async_send(self.inner_request.take().unwrap(), |_| Box::pin(async {})) .await; - check(&resp, &self.assert_status_code); self.response = Some(resp); self @@ -189,14 +207,10 @@ where R: FromBytes, { let response = self.response.unwrap(); - if response.status_code == StatusCode::Err { - let error = >::try_from(response.payload) - .unwrap() - .into_inner(); - dbg!(&error); - panic!("") - } else { - >::try_from(response.payload).unwrap().into_inner() + match response.parse::() { + Ok(Ok(data)) => data, + Ok(Err(e)) => panic!("parse failed: {:?}", e), + Err(e) => panic!("Internal error: {:?}", e), } } diff --git a/rust-lib/flowy-user/src/errors.rs b/rust-lib/flowy-user/src/errors.rs index 0fd42758df..b2ff67213b 100644 --- a/rust-lib/flowy-user/src/errors.rs +++ b/rust-lib/flowy-user/src/errors.rs @@ -47,7 +47,7 @@ pub enum UserErrorCode { EmailInvalid = 20, #[display(fmt = "Password format is not correct")] PasswordInvalid = 21, - #[display(fmt = "User is invalid")] + #[display(fmt = "User name is invalid")] UserNameInvalid = 22, #[display(fmt = "User workspace is invalid")] UserWorkspaceInvalid = 23, diff --git a/rust-lib/flowy-user/src/services/user_session/user_session.rs b/rust-lib/flowy-user/src/services/user_session/user_session.rs index 6bfb862a12..97f7fa8d82 100644 --- a/rust-lib/flowy-user/src/services/user_session/user_session.rs +++ b/rust-lib/flowy-user/src/services/user_session/user_session.rs @@ -12,9 +12,12 @@ use std::sync::{Arc, RwLock}; use crate::{ entities::{SignInParams, SignUpParams, UpdateUserParams, UserDetail}, errors::{ErrorBuilder, UserError, UserErrorCode}, + event::UserEvent::GetStatus, services::user_session::{database::UserDB, user_server::UserServer}, sql_tables::{User, UserChangeset}, }; +use flowy_dispatch::prelude::{Data, EventDispatch, ModuleRequest}; +use std::convert::TryFrom; pub struct UserSessionConfig { root_dir: String, @@ -108,8 +111,27 @@ impl UserSession { self.database.get_connection(&user_id) } + pub fn set_current_workspace() { + unimplemented!() + + // let request = SignInRequest { + // email: valid_email(), + // password: valid_password(), + // }; + // + // let user_detail = Tester::::new(SignIn) + // .request(request) + // .sync_send() + // .parse::(); + // + // user_detail + } + #[allow(dead_code)] pub fn get_current_workspace(&self) -> Result { + // let response = EventDispatch::sync_send(ModuleRequest::new(GetStatus)); + // let user_detail = + // >::try_from(response.payload).unwrap().into_inner(); let user_id = get_current_user_id()?; let conn = self.get_db_connection()?; diff --git a/rust-lib/flowy-user/tests/event/main.rs b/rust-lib/flowy-user/tests/event/main.rs index 2089722798..ce3c9fb1cd 100644 --- a/rust-lib/flowy-user/tests/event/main.rs +++ b/rust-lib/flowy-user/tests/event/main.rs @@ -1,4 +1,5 @@ mod helper; mod sign_in_test; mod sign_up_test; +mod user_detail_test; mod user_status_test; diff --git a/rust-lib/flowy-user/tests/event/user_detail_test.rs b/rust-lib/flowy-user/tests/event/user_detail_test.rs new file mode 100644 index 0000000000..cd113e4fbf --- /dev/null +++ b/rust-lib/flowy-user/tests/event/user_detail_test.rs @@ -0,0 +1,140 @@ +use crate::helper::*; +use flowy_user::{errors::UserErrorCode, event::UserEvent::*, prelude::*}; +use serial_test::*; + +#[test] +#[serial] +fn user_update_with_name() { + let user_detail = UserTestBuilder::new().login().user_detail.unwrap(); + let new_name = "hello_world".to_owned(); + let request = UpdateUserRequest { + id: user_detail.id.clone(), + name: Some(new_name.clone()), + email: None, + workspace: None, + password: None, + }; + + let user_detail = UserTestBuilder::new() + .event(UpdateUser) + .request(request) + .sync_send() + .parse::(); + + assert_eq!(user_detail.name, new_name,); +} + +#[test] +#[serial] +fn user_update_with_email() { + let user_detail = UserTestBuilder::new().login().user_detail.unwrap(); + let new_email = "123@gmai.com".to_owned(); + let request = UpdateUserRequest { + id: user_detail.id.clone(), + name: None, + email: Some(new_email.clone()), + workspace: None, + password: None, + }; + + let user_detail = UserTestBuilder::new() + .event(UpdateUser) + .request(request) + .sync_send() + .parse::(); + + assert_eq!(user_detail.email, new_email,); +} + +#[test] +#[serial] +fn user_update_with_password() { + let user_detail = UserTestBuilder::new().login().user_detail.unwrap(); + let new_password = "H123world!".to_owned(); + let request = UpdateUserRequest { + id: user_detail.id.clone(), + name: None, + email: None, + workspace: None, + password: Some(new_password.clone()), + }; + + let _ = UserTestBuilder::new() + .event(UpdateUser) + .request(request) + .sync_send() + .assert_success(); +} + +#[test] +#[serial] +fn user_update_with_invalid_email() { + let user_detail = UserTestBuilder::new().login().user_detail.unwrap(); + for email in invalid_email_test_case() { + let request = UpdateUserRequest { + id: user_detail.id.clone(), + name: None, + email: Some(email), + workspace: None, + password: None, + }; + + assert_eq!( + UserTestBuilder::new() + .event(UpdateUser) + .request(request) + .sync_send() + .error() + .code, + UserErrorCode::EmailInvalid + ); + } +} + +#[test] +#[serial] +fn user_update_with_invalid_password() { + let user_detail = UserTestBuilder::new().login().user_detail.unwrap(); + for password in invalid_password_test_case() { + let request = UpdateUserRequest { + id: user_detail.id.clone(), + name: None, + email: None, + workspace: None, + password: Some(password), + }; + + assert_eq!( + UserTestBuilder::new() + .event(UpdateUser) + .request(request) + .sync_send() + .error() + .code, + UserErrorCode::PasswordInvalid + ); + } +} + +#[test] +#[serial] +fn user_update_with_invalid_name() { + let user_detail = UserTestBuilder::new().login().user_detail.unwrap(); + let request = UpdateUserRequest { + id: user_detail.id.clone(), + name: Some("".to_string()), + email: None, + workspace: None, + password: None, + }; + + assert_eq!( + UserTestBuilder::new() + .event(UpdateUser) + .request(request) + .sync_send() + .error() + .code, + UserErrorCode::UserNameInvalid + ); +} diff --git a/rust-lib/flowy-user/tests/event/user_status_test.rs b/rust-lib/flowy-user/tests/event/user_status_test.rs index b327a1cfa8..62655f37bc 100644 --- a/rust-lib/flowy-user/tests/event/user_status_test.rs +++ b/rust-lib/flowy-user/tests/event/user_status_test.rs @@ -5,7 +5,7 @@ use serial_test::*; #[test] #[should_panic] #[serial] -fn user_status_not_found_before_login() { +fn user_status_get_failed_before_login() { let _ = UserTestBuilder::new() .logout() .event(GetStatus) @@ -15,7 +15,7 @@ fn user_status_not_found_before_login() { #[test] #[serial] -fn user_status_did_found_after_login() { +fn user_status_get_success_after_login() { let request = SignInRequest { email: valid_email(), password: valid_password(), @@ -34,29 +34,3 @@ fn user_status_did_found_after_login() { .sync_send() .parse::(); } - -#[test] -#[serial] -fn user_update_with_invalid_email() { - let user_detail = UserTestBuilder::new().login().user_detail.unwrap(); - - for email in invalid_email_test_case() { - let request = UpdateUserRequest { - id: user_detail.id.clone(), - name: None, - email: Some(email), - workspace: None, - password: None, - }; - - assert_eq!( - UserTestBuilder::new() - .event(UpdateUser) - .request(request) - .sync_send() - .error() - .code, - UserErrorCode::EmailInvalid - ); - } -} diff --git a/rust-lib/flowy-workspace/src/services/workspace_controller.rs b/rust-lib/flowy-workspace/src/services/workspace_controller.rs index 05e8c73375..568cdc06bd 100644 --- a/rust-lib/flowy-workspace/src/services/workspace_controller.rs +++ b/rust-lib/flowy-workspace/src/services/workspace_controller.rs @@ -21,6 +21,8 @@ impl WorkspaceController { .values(workspace) .execute(&*conn)?; + self.user.set_current_workspace(&detail.id); + Ok(detail) }