AppFlowy/backend/tests/api/auth.rs

152 lines
4.1 KiB
Rust
Raw Normal View History

2021-08-24 05:10:53 +00:00
use crate::helper::{spawn_app, TestApp};
2021-09-01 08:08:32 +00:00
use flowy_user::{
entities::{
SignInParams,
SignInResponse,
SignUpParams,
SignUpResponse,
UpdateUserParams,
UserToken,
},
errors::UserError,
};
#[actix_rt::test]
async fn user_register() {
let app = spawn_app().await;
2021-09-01 08:08:32 +00:00
let response = register_user(&app, "annie@appflowy.io", "HelloWorld123!").await;
log::info!("{:?}", response);
}
#[actix_rt::test]
2021-08-24 05:10:53 +00:00
#[should_panic]
async fn user_sign_in_with_invalid_password() {
let app = spawn_app().await;
let email = "annie@appflowy.io";
let password = "123";
2021-08-24 05:10:53 +00:00
let _ = register_user(&app, email, password).await;
}
2021-08-24 05:10:53 +00:00
#[actix_rt::test]
#[should_panic]
async fn user_sign_in_with_invalid_email() {
let app = spawn_app().await;
let email = "annie@gmail@";
2021-09-01 08:08:32 +00:00
let password = "HelloWorld123!";
2021-08-24 05:10:53 +00:00
let _ = register_user(&app, email, password).await;
}
2021-08-24 05:10:53 +00:00
#[actix_rt::test]
async fn user_sign_in() {
let app = spawn_app().await;
let email = "annie@appflowy.io";
2021-09-01 08:08:32 +00:00
let password = "HelloWorld123!";
2021-08-24 05:10:53 +00:00
let _ = register_user(&app, email, password).await;
2021-09-01 08:08:32 +00:00
let params = SignInParams {
email: email.to_string(),
password: password.to_string(),
};
let _ = app.sign_in(params).await.unwrap();
}
2021-08-24 05:10:53 +00:00
2021-08-31 09:25:08 +00:00
#[actix_rt::test]
2021-09-01 08:08:32 +00:00
#[should_panic]
2021-08-31 09:25:08 +00:00
async fn user_sign_out() {
let app = spawn_app().await;
let email = "annie@appflowy.io";
2021-09-01 08:08:32 +00:00
let password = "HelloWorld123!";
2021-08-31 09:25:08 +00:00
let _ = register_user(&app, email, password).await;
2021-09-01 08:08:32 +00:00
let params = SignInParams {
email: email.to_string(),
password: password.to_string(),
};
let sign_in_resp = app.sign_in(params).await.unwrap();
2021-08-31 09:56:38 +00:00
let token = sign_in_resp.token.clone();
2021-08-31 15:01:46 +00:00
let user_token = UserToken {
2021-08-31 09:56:38 +00:00
token: token.clone(),
2021-08-31 09:25:08 +00:00
};
2021-08-31 15:01:46 +00:00
app.sign_out(user_token).await;
2021-08-31 09:56:38 +00:00
2021-09-01 08:08:32 +00:00
// user_detail will be empty because use was sign out.
app.get_user_detail(&token).await;
2021-08-31 09:56:38 +00:00
}
#[actix_rt::test]
async fn user_get_detail() {
2021-09-01 08:08:32 +00:00
let app = spawn_app().await;
let sign_up_resp = sign_up_user(&app).await;
log::info!("{:?}", app.get_user_detail(&sign_up_resp.token).await);
}
#[actix_rt::test]
async fn user_update_password() {
2021-08-31 09:56:38 +00:00
let app = spawn_app().await;
let email = "annie@appflowy.io";
2021-09-01 08:08:32 +00:00
let password = "HelloWorld123!";
2021-08-31 09:56:38 +00:00
let sign_up_resp = register_user(&app, email, password).await;
2021-09-01 08:08:32 +00:00
let params = UpdateUserParams::new(&sign_up_resp.uid).password("Hello123!");
app.update_user_detail(&sign_up_resp.token, params)
.await
.unwrap();
let sign_in_params = SignInParams {
email: email.to_string(),
password: password.to_string(),
2021-08-31 09:56:38 +00:00
};
2021-09-01 08:08:32 +00:00
match app.sign_in(sign_in_params).await {
Ok(_) => {},
Err(e) => {
assert_eq!(e.code, flowy_user::errors::ErrorCode::PasswordNotMatch);
},
}
}
#[actix_rt::test]
async fn user_update_name() {
let app = spawn_app().await;
let sign_up_resp = sign_up_user(&app).await;
let name = "tom".to_string();
let params = UpdateUserParams::new(&sign_up_resp.uid).name(&name);
app.update_user_detail(&sign_up_resp.token, params)
.await
.unwrap();
let user = app.get_user_detail(&sign_up_resp.token).await;
assert_eq!(user.name, name);
}
#[actix_rt::test]
async fn user_update_email() {
let app = spawn_app().await;
let sign_up_resp = sign_up_user(&app).await;
let email = "123@gmail.com".to_string();
let params = UpdateUserParams::new(&sign_up_resp.uid).email(&email);
app.update_user_detail(&sign_up_resp.token, params)
.await
.unwrap();
let user = app.get_user_detail(&sign_up_resp.token).await;
assert_eq!(user.email, email);
}
async fn sign_up_user(app: &TestApp) -> SignUpResponse {
let email = "annie@appflowy.io";
let password = "HelloWorld123!";
let response = register_user(&app, email, password).await;
response
2021-08-31 09:25:08 +00:00
}
2021-08-24 05:10:53 +00:00
async fn register_user(app: &TestApp, email: &str, password: &str) -> SignUpResponse {
let params = SignUpParams {
email: email.to_string(),
name: "annie".to_string(),
password: password.to_string(),
};
let response = app.register_user(params).await;
response
}