writing code in airport, continue after onboard

This commit is contained in:
appflowy 2021-07-15 08:46:16 +08:00
parent e767cc3a01
commit 4354ff0c59
9 changed files with 213 additions and 41 deletions

View File

@ -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");
}

View File

@ -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<T, E>(self) -> Result<Result<T, E>, DispatchError>
where
T: FromBytes,
E: FromBytes,
{
if self.status_code == StatusCode::Err {
match <Data<E>>::try_from(self.payload) {
Ok(err) => Ok(Err(err.into_inner())),
Err(e) => Err(InternalError::new(e).into()),
}
} else {
match <Data<T>>::try_from(self.payload) {
Ok(a) => Ok(Ok(a.into_inner())),
Err(e) => Err(InternalError::new(e).into()),
}
}
}
}
impl std::fmt::Display for EventResponse {

View File

@ -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<Error> {
@ -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 = <Data<Error>>::try_from(response.payload)
.unwrap()
.into_inner();
dbg!(&error);
panic!("")
} else {
<Data<R>>::try_from(response.payload).unwrap().into_inner()
match response.parse::<R, Error>() {
Ok(Ok(data)) => data,
Ok(Err(e)) => panic!("parse failed: {:?}", e),
Err(e) => panic!("Internal error: {:?}", e),
}
}

View File

@ -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,

View File

@ -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::<UserError>::new(SignIn)
// .request(request)
// .sync_send()
// .parse::<UserDetail>();
//
// user_detail
}
#[allow(dead_code)]
pub fn get_current_workspace(&self) -> Result<String, UserError> {
// let response = EventDispatch::sync_send(ModuleRequest::new(GetStatus));
// let user_detail =
// <Data<UserDetail>>::try_from(response.payload).unwrap().into_inner();
let user_id = get_current_user_id()?;
let conn = self.get_db_connection()?;

View File

@ -1,4 +1,5 @@
mod helper;
mod sign_in_test;
mod sign_up_test;
mod user_detail_test;
mod user_status_test;

View File

@ -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::<UserDetail>();
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::<UserDetail>();
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
);
}

View File

@ -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::<UserDetail>();
}
#[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
);
}
}

View File

@ -21,6 +21,8 @@ impl WorkspaceController {
.values(workspace)
.execute(&*conn)?;
self.user.set_current_workspace(&detail.id);
Ok(detail)
}