AppFlowy/frontend/rust-lib/flowy-test/src/helper.rs

143 lines
4.5 KiB
Rust
Raw Normal View History

2021-08-29 14:00:42 +00:00
use bytes::Bytes;
2021-12-04 03:26:17 +00:00
use lib_dispatch::prelude::{EventDispatcher, ModuleRequest, ToBytes};
use lib_infra::{kv::KV, uuid};
2021-09-04 07:12:53 +00:00
use flowy_user::{
2021-09-04 08:53:58 +00:00
entities::{SignInRequest, SignUpRequest, UserProfile},
2021-09-16 04:35:55 +00:00
errors::UserError,
event::UserEvent::{SignIn, SignOut, SignUp},
};
2021-08-29 14:00:42 +00:00
use flowy_workspace::{
entities::workspace::{CreateWorkspaceRequest, QueryWorkspaceRequest, Workspace},
errors::WorkspaceError,
2021-08-29 14:00:42 +00:00
event::WorkspaceEvent::{CreateWorkspace, OpenWorkspace},
};
2021-09-04 07:12:53 +00:00
use std::{fs, path::PathBuf, sync::Arc};
2021-07-16 15:18:12 +00:00
pub fn root_dir() -> String {
// https://doc.rust-lang.org/cargo/reference/environment-variables.html
2021-11-27 11:19:41 +00:00
let manifest_dir = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_else(|_| "./".to_owned());
2021-07-16 15:18:12 +00:00
let mut path_buf = fs::canonicalize(&PathBuf::from(&manifest_dir)).unwrap();
path_buf.pop(); // rust-lib
path_buf.push("flowy-test");
path_buf.push("temp");
path_buf.push("flowy");
let root_dir = path_buf.to_str().unwrap().to_string();
if !std::path::Path::new(&root_dir).exists() {
std::fs::create_dir_all(&root_dir).unwrap();
}
root_dir
}
2021-08-31 15:01:46 +00:00
pub fn random_email() -> String { format!("{}@appflowy.io", uuid()) }
2021-07-16 15:18:12 +00:00
2021-09-04 01:00:15 +00:00
pub fn login_email() -> String { "annie2@appflowy.io".to_string() }
2021-07-17 02:26:05 +00:00
pub fn login_password() -> String { "HelloWorld!123".to_string() }
2021-08-29 14:00:42 +00:00
2021-11-27 11:19:41 +00:00
const DEFAULT_WORKSPACE_NAME: &str = "My workspace";
const DEFAULT_WORKSPACE_DESC: &str = "This is your first workspace";
const DEFAULT_WORKSPACE: &str = "Default_Workspace";
2021-08-29 14:00:42 +00:00
2021-09-11 06:26:30 +00:00
#[allow(dead_code)]
2021-12-04 03:26:17 +00:00
pub(crate) fn create_default_workspace_if_need(dispatch: Arc<EventDispatcher>, user_id: &str) -> Result<(), UserError> {
2021-08-29 14:00:42 +00:00
let key = format!("{}{}", user_id, DEFAULT_WORKSPACE);
2021-09-03 04:44:48 +00:00
if KV::get_bool(&key).unwrap_or(false) {
2021-09-16 04:35:55 +00:00
return Err(UserError::internal());
2021-08-29 14:00:42 +00:00
}
2021-09-03 04:44:48 +00:00
KV::set_bool(&key, true);
2021-08-29 14:00:42 +00:00
let payload: Bytes = CreateWorkspaceRequest {
name: DEFAULT_WORKSPACE_NAME.to_string(),
desc: DEFAULT_WORKSPACE_DESC.to_string(),
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(CreateWorkspace).payload(payload);
2021-12-04 03:26:17 +00:00
let result = EventDispatcher::sync_send(dispatch.clone(), request)
.parse::<Workspace, WorkspaceError>()
2021-09-16 04:35:55 +00:00
.map_err(|e| UserError::internal().context(e))?;
2021-08-29 14:00:42 +00:00
2021-09-16 04:35:55 +00:00
let workspace = result.map_err(|e| UserError::internal().context(e))?;
2021-08-29 14:00:42 +00:00
let query: Bytes = QueryWorkspaceRequest {
2021-11-27 11:19:41 +00:00
workspace_id: Some(workspace.id),
2021-08-29 14:00:42 +00:00
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(OpenWorkspace).payload(query);
2021-12-04 03:26:17 +00:00
let _result = EventDispatcher::sync_send(dispatch, request)
.parse::<Workspace, WorkspaceError>()
2021-08-29 14:00:42 +00:00
.unwrap()
.unwrap();
Ok(())
}
pub struct SignUpContext {
2021-09-04 08:53:58 +00:00
pub user_profile: UserProfile,
pub password: String,
}
2021-12-04 03:26:17 +00:00
pub fn sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
let password = login_password();
let payload = SignUpRequest {
email: random_email(),
name: "app flowy".to_string(),
password: password.clone(),
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(SignUp).payload(payload);
2021-12-04 03:26:17 +00:00
let user_profile = EventDispatcher::sync_send(dispatch, request)
2021-09-04 08:53:58 +00:00
.parse::<UserProfile, UserError>()
.unwrap()
.unwrap();
SignUpContext { user_profile, password }
}
2021-12-04 03:26:17 +00:00
pub async fn async_sign_up(dispatch: Arc<EventDispatcher>) -> SignUpContext {
let password = login_password();
let payload = SignUpRequest {
email: random_email(),
name: "app flowy".to_string(),
password: password.clone(),
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(SignUp).payload(payload);
2021-12-04 03:26:17 +00:00
let user_profile = EventDispatcher::async_send(dispatch.clone(), request)
.await
.parse::<UserProfile, UserError>()
.unwrap()
.unwrap();
2021-09-11 06:26:30 +00:00
// let _ = create_default_workspace_if_need(dispatch.clone(), &user_profile.id);
2021-09-04 08:53:58 +00:00
SignUpContext { user_profile, password }
}
#[allow(dead_code)]
2021-12-04 03:26:17 +00:00
fn sign_in(dispatch: Arc<EventDispatcher>) -> UserProfile {
let payload = SignInRequest {
email: login_email(),
password: login_password(),
2021-11-07 08:58:06 +00:00
name: "rust".to_owned(),
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(SignIn).payload(payload);
2021-12-04 03:26:17 +00:00
EventDispatcher::sync_send(dispatch, request)
2021-09-04 08:53:58 +00:00
.parse::<UserProfile, UserError>()
.unwrap()
2021-11-27 11:19:41 +00:00
.unwrap()
}
#[allow(dead_code)]
2021-12-04 03:26:17 +00:00
fn logout(dispatch: Arc<EventDispatcher>) { let _ = EventDispatcher::sync_send(dispatch, ModuleRequest::new(SignOut)); }