user test with server

This commit is contained in:
appflowy 2021-09-01 17:57:06 +08:00
parent 06a1c96a28
commit 99ef3c1112
11 changed files with 198 additions and 173 deletions

View File

@ -48,8 +48,16 @@ where
}
}
pub fn login(mut self) -> Self {
let user_detail = self.tester.login();
pub fn sign_up(self) -> SignUpContext {
let (user_detail, password) = self.tester.sign_up();
SignUpContext {
user_detail,
password,
}
}
pub fn sign_in(mut self) -> Self {
let user_detail = self.tester.sign_in();
self.user_detail = Some(user_detail);
self
}
@ -160,3 +168,8 @@ where
fn context(&self) -> &TesterContext { &self.context }
}
pub struct SignUpContext {
pub user_detail: UserDetail,
pub password: String,
}

View File

@ -10,6 +10,7 @@ use flowy_user::{
prelude::*,
};
use flowy_user::event::UserEvent::SignIn;
use std::{
convert::TryFrom,
fmt::{Debug, Display},
@ -105,12 +106,13 @@ pub trait TesterTrait {
.into_inner()
}
fn login(&self) -> UserDetail {
fn sign_up(&self) -> (UserDetail, String) {
init_test_sdk();
let password = valid_password();
let payload = SignUpRequest {
email: self.context().user_email.clone(),
name: "app flowy".to_string(),
password: valid_password(),
password: password.clone(),
}
.into_bytes()
.unwrap();
@ -121,6 +123,24 @@ pub trait TesterTrait {
.unwrap()
.unwrap();
(user_detail, password)
}
fn sign_in(&self) -> UserDetail {
init_test_sdk();
let payload = SignInRequest {
email: self.context().user_email.clone(),
password: valid_password(),
}
.into_bytes()
.unwrap();
let request = ModuleRequest::new(SignIn).payload(payload);
let user_detail = EventDispatch::sync_send(request)
.parse::<UserDetail, UserError>()
.unwrap()
.unwrap();
user_detail
}
@ -131,7 +151,7 @@ pub trait TesterTrait {
.unwrap()
{
Ok(user_detail) => user_detail,
Err(_e) => self.login(),
Err(_e) => self.sign_in(),
}
}

View File

@ -17,7 +17,7 @@ impl std::default::Default for UserStatus {
fn default() -> Self { UserStatus::Unknown }
}
#[derive(ProtoBuf, Default, Debug)]
#[derive(ProtoBuf, Default, Debug, PartialEq, Eq)]
pub struct UserDetail {
#[pb(index = 1)]
pub id: String,
@ -85,7 +85,7 @@ impl UpdateUserRequest {
}
}
#[derive(ProtoBuf, Default)]
#[derive(ProtoBuf, Default, Clone)]
pub struct UpdateUserParams {
// TODO: remove user id
#[pb(index = 1)]

View File

@ -6,7 +6,7 @@ use crate::{
use crate::entities::UpdateUserParams;
use flowy_infra::future::ResultFuture;
use flowy_infra::{future::ResultFuture, uuid};
pub struct UserServerMock {}
@ -14,7 +14,7 @@ impl UserServerMock {}
impl UserServerAPI for UserServerMock {
fn sign_up(&self, params: SignUpParams) -> ResultFuture<SignUpResponse, UserError> {
let uid = params.email.clone();
let uid = uuid();
ResultFuture::new(async move {
Ok(SignUpResponse {
uid,
@ -28,7 +28,7 @@ impl UserServerAPI for UserServerMock {
fn sign_in(&self, params: SignInParams) -> ResultFuture<SignInResponse, UserError> {
ResultFuture::new(async {
Ok(SignInResponse {
uid: "fake id".to_owned(),
uid: uuid(),
name: "fake name".to_owned(),
email: params.email,
token: "fake token".to_string(),

View File

@ -105,6 +105,19 @@ impl UserSession {
}
pub async fn update_user(&self, params: UpdateUserParams) -> Result<(), UserError> {
let session = self.get_session()?;
match self
.server
.update_user(&session.token, params.clone())
.await
{
Ok(_) => {},
Err(e) => {
// TODO: retry?
log::error!("update user profile failed: {:?}", e);
},
}
let changeset = UserTableChangeset::new(params);
let conn = self.get_db_connection()?;
diesel_update_table!(user_table, changeset, conn);
@ -126,8 +139,7 @@ impl UserSession {
log::info!("{:?}", e);
},
}
})
.await;
});
let user = dsl::user_table
.filter(user_table::id.eq(&session.user_id))

View File

@ -0,0 +1,107 @@
use crate::helper::*;
use flowy_user::{errors::ErrorCode, event::UserEvent::*, prelude::*};
use serial_test::*;
#[test]
#[serial]
fn sign_up_success() {
let user_detail = TestBuilder::new().sign_up().user_detail;
log::info!("{:?}", user_detail);
}
#[test]
#[serial]
fn sign_up_with_invalid_email() {
for email in invalid_email_test_case() {
let request = SignUpRequest {
email: email.to_string(),
name: valid_name(),
password: valid_password(),
};
assert_eq!(
TestBuilder::new()
.event(SignUp)
.request(request)
.sync_send()
.error()
.code,
ErrorCode::EmailFormatInvalid
);
}
}
#[test]
#[serial]
fn sign_up_with_invalid_password() {
for password in invalid_password_test_case() {
let request = SignUpRequest {
email: random_email(),
name: valid_name(),
password,
};
TestBuilder::new()
.event(SignUp)
.request(request)
.sync_send()
.assert_error();
}
}
#[test]
#[serial]
fn sign_in_success() {
let context = TestBuilder::new().sign_up();
let _ = TestBuilder::new().event(SignOut).sync_send();
let request = SignInRequest {
email: context.user_detail.email,
password: context.password,
};
let response = TestBuilder::new()
.event(SignIn)
.request(request)
.sync_send()
.parse::<UserDetail>();
dbg!(&response);
}
#[test]
#[serial]
fn sign_in_with_invalid_email() {
for email in invalid_email_test_case() {
let request = SignInRequest {
email: email.to_string(),
password: valid_password(),
};
assert_eq!(
TestBuilder::new()
.event(SignIn)
.request(request)
.sync_send()
.error()
.code,
ErrorCode::EmailFormatInvalid
);
}
}
#[test]
#[serial]
fn sign_in_with_invalid_password() {
for password in invalid_password_test_case() {
let request = SignInRequest {
email: random_email(),
password,
};
TestBuilder::new()
.event(SignIn)
.request(request)
.sync_send()
.assert_error();
}
}

View File

@ -1,5 +1,3 @@
mod auth_test;
mod helper;
mod sign_in_test;
mod sign_up_test;
mod user_detail_test;
mod user_update_test;
mod user_profile_test;

View File

@ -1,58 +0,0 @@
use crate::helper::*;
use flowy_user::{errors::ErrorCode, event::UserEvent::*, prelude::*};
use serial_test::*;
#[test]
#[serial]
fn sign_in_success() {
let request = SignInRequest {
email: random_email(),
password: valid_password(),
};
let response = TestBuilder::new()
.logout()
.event(SignIn)
.request(request)
.sync_send()
.parse::<UserDetail>();
dbg!(&response);
}
#[test]
#[serial]
fn sign_in_with_invalid_email() {
for email in invalid_email_test_case() {
let request = SignInRequest {
email: email.to_string(),
password: valid_password(),
};
assert_eq!(
TestBuilder::new()
.event(SignIn)
.request(request)
.sync_send()
.error()
.code,
ErrorCode::EmailFormatInvalid
);
}
}
#[test]
#[serial]
fn sign_in_with_invalid_password() {
for password in invalid_password_test_case() {
let request = SignInRequest {
email: random_email(),
password,
};
TestBuilder::new()
.event(SignIn)
.request(request)
.sync_send()
.assert_error();
}
}

View File

@ -1,58 +0,0 @@
use crate::helper::*;
use flowy_user::{errors::*, event::UserEvent::*, prelude::*};
use serial_test::*;
#[test]
#[serial]
fn sign_up_success() {
let request = SignUpRequest {
email: random_email(),
name: valid_name(),
password: valid_password(),
};
let _response = TestBuilder::new()
.logout()
.event(SignUp)
.request(request)
.sync_send();
}
#[test]
#[serial]
fn sign_up_with_invalid_email() {
for email in invalid_email_test_case() {
let request = SignUpRequest {
email: email.to_string(),
name: valid_name(),
password: valid_password(),
};
assert_eq!(
TestBuilder::new()
.event(SignUp)
.request(request)
.sync_send()
.error()
.code,
ErrorCode::EmailFormatInvalid
);
}
}
#[test]
#[serial]
fn sign_up_with_invalid_password() {
for password in invalid_password_test_case() {
let request = SignUpRequest {
email: random_email(),
name: valid_name(),
password,
};
TestBuilder::new()
.event(SignUp)
.request(request)
.sync_send()
.assert_error();
}
}

View File

@ -1,34 +0,0 @@
use crate::helper::*;
use flowy_user::{event::UserEvent::*, prelude::*};
use serial_test::*;
#[test]
#[serial]
fn user_status_get_failed() {
let _ = TestBuilder::new()
.logout()
.event(GetUserProfile)
.assert_error()
.sync_send();
}
#[test]
#[serial]
fn user_detail_get() {
let request = SignInRequest {
email: random_email(),
password: valid_password(),
};
let response = TestBuilder::new()
.event(SignIn)
.request(request)
.sync_send()
.parse::<UserDetail>();
dbg!(&response);
let _ = TestBuilder::new()
.event(GetUserProfile)
.sync_send()
.parse::<UserDetail>();
}

View File

@ -1,11 +1,36 @@
use crate::helper::*;
use flowy_infra::uuid;
use flowy_user::{errors::ErrorCode, event::UserEvent::*, prelude::*};
use serial_test::*;
#[test]
#[serial]
fn user_status_get_failed() {
let user_detail = TestBuilder::new()
.event(GetUserProfile)
.assert_error()
.sync_send()
.user_detail;
assert!(user_detail.is_none())
}
#[test]
#[serial]
fn user_detail_get() {
let user_detail = TestBuilder::new().sign_up().user_detail;
let user_detail2 = TestBuilder::new()
.event(GetUserProfile)
.sync_send()
.parse::<UserDetail>();
assert_eq!(user_detail, user_detail2);
}
#[test]
#[serial]
fn user_update_with_name() {
let user_detail = TestBuilder::new().login().user_detail.unwrap();
let user_detail = TestBuilder::new().sign_up().user_detail;
let new_name = "hello_world".to_owned();
let request = UpdateUserRequest::new(&user_detail.id).name(&new_name);
let _ = TestBuilder::new()
@ -25,8 +50,8 @@ fn user_update_with_name() {
#[test]
#[serial]
fn user_update_with_email() {
let user_detail = TestBuilder::new().login().user_detail.unwrap();
let new_email = "123@gmai.com".to_owned();
let user_detail = TestBuilder::new().sign_up().user_detail;
let new_email = format!("{}@gmai.com", uuid());
let request = UpdateUserRequest::new(&user_detail.id).email(&new_email);
let _ = TestBuilder::new()
@ -46,7 +71,7 @@ fn user_update_with_email() {
#[test]
#[serial]
fn user_update_with_password() {
let user_detail = TestBuilder::new().login().user_detail.unwrap();
let user_detail = TestBuilder::new().sign_up().user_detail;
let new_password = "H123world!".to_owned();
let request = UpdateUserRequest::new(&user_detail.id).password(&new_password);
@ -60,7 +85,7 @@ fn user_update_with_password() {
#[test]
#[serial]
fn user_update_with_invalid_email() {
let user_detail = TestBuilder::new().login().user_detail.unwrap();
let user_detail = TestBuilder::new().sign_up().user_detail;
for email in invalid_email_test_case() {
let request = UpdateUserRequest::new(&user_detail.id).email(&email);
assert_eq!(
@ -78,7 +103,7 @@ fn user_update_with_invalid_email() {
#[test]
#[serial]
fn user_update_with_invalid_password() {
let user_detail = TestBuilder::new().login().user_detail.unwrap();
let user_detail = TestBuilder::new().sign_up().user_detail;
for password in invalid_password_test_case() {
let request = UpdateUserRequest::new(&user_detail.id).password(&password);
@ -93,7 +118,7 @@ fn user_update_with_invalid_password() {
#[test]
#[serial]
fn user_update_with_invalid_name() {
let user_detail = TestBuilder::new().login().user_detail.unwrap();
let user_detail = TestBuilder::new().sign_up().user_detail;
let request = UpdateUserRequest::new(&user_detail.id).name("");
TestBuilder::new()