From 8af290ab80c6941adeada8b05500585f46253c98 Mon Sep 17 00:00:00 2001 From: appflowy Date: Sun, 12 Mar 2023 20:58:40 +0800 Subject: [PATCH] chore: update database config & fix test --- .env | 2 +- build/docker_env.sh | 2 +- build/init_database.sh | 4 ++-- configuration/base.yaml | 2 +- src/component/auth/error.rs | 2 +- src/component/auth/user.rs | 9 ++++----- tests/api/login.rs | 17 +++++++++++++++++ tests/api/main.rs | 1 + tests/api/register.rs | 14 +++++++++----- tests/api/test_server.rs | 25 ++++++++++++++++++++++--- 10 files changed, 59 insertions(+), 19 deletions(-) create mode 100644 tests/api/login.rs diff --git a/.env b/.env index 8ee6c6b1..bc1ad643 100644 --- a/.env +++ b/.env @@ -1 +1 @@ -DATABASE_URL="postgres://postgres:password@localhost:5433/flowy" \ No newline at end of file +DATABASE_URL="postgres://postgres:password@localhost:5433/appflowy_pg" \ No newline at end of file diff --git a/build/docker_env.sh b/build/docker_env.sh index 4bd66874..73843beb 100644 --- a/build/docker_env.sh +++ b/build/docker_env.sh @@ -5,6 +5,6 @@ export POSTGRES_USER=postgres export POSTGRES_PASSWORD=password export POSTGRES_PORT=5432 export POSTGRES_HOST=db -export POSTGRES_DB=flowy +export POSTGRES_DB=appflowy_pg export DATABASE_URL=postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB} \ No newline at end of file diff --git a/build/init_database.sh b/build/init_database.sh index 5c75018f..612a86b3 100755 --- a/build/init_database.sh +++ b/build/init_database.sh @@ -22,7 +22,7 @@ DB_USER="${POSTGRES_USER:=postgres}" DB_PASSWORD="${POSTGRES_PASSWORD:=password}" DB_PORT="${POSTGRES_PORT:=5432}" DB_HOST="${POSTGRES_HOST:=localhost}" -DB_NAME="${POSTGRES_DB:=flowy}" +DB_NAME="${POSTGRES_DB:=appflowy_pg}" if [[ -z "${SKIP_DOCKER}" ]] then @@ -39,7 +39,7 @@ then -e POSTGRES_DB="${DB_NAME}" \ -p "${DB_PORT}":5432 \ -d \ - --name "flowy_postgres_$(date '+%s')" \ + --name "af_postgres_$(date '+%s')" \ postgres -N 1000 fi diff --git a/configuration/base.yaml b/configuration/base.yaml index 4b78eb1b..4c2a1507 100644 --- a/configuration/base.yaml +++ b/configuration/base.yaml @@ -6,4 +6,4 @@ database: port: 5433 username: "postgres" password: "password" - database_name: "flowy" + database_name: "appflowy_pg" diff --git a/src/component/auth/error.rs b/src/component/auth/error.rs index 7f33a2db..278b405e 100644 --- a/src/component/auth/error.rs +++ b/src/component/auth/error.rs @@ -18,7 +18,7 @@ pub enum AuthError { Unauthorized, #[error("User internal error")] - InternalError(#[source] anyhow::Error), + InternalError(#[from] anyhow::Error), #[error("Parser uuid failed: {}", err)] InvalidUuid { err: String }, diff --git a/src/component/auth/user.rs b/src/component/auth/user.rs index 01727342..2b4cec18 100644 --- a/src/component/auth/user.rs +++ b/src/component/auth/user.rs @@ -29,16 +29,16 @@ pub async fn login( password: Secret::new(password), }; - match validate_credentials(credentials, &pg_pool).await? { + match validate_credentials(credentials, &pg_pool).await { Ok(uid) => { let uid = uid.to_string(); let token = Token::create_token(&uid)?.into(); - let logged_user = LoggedUser::new(uid); + let logged_user = LoggedUser::new(uid.clone()); cache.write().await.authorized(logged_user); Ok(LoginResponse { token, - user_id: uid, + uid, }) } Err(err) => Err(err), @@ -120,13 +120,12 @@ async fn is_email_exist( pub struct LoginRequest { pub email: String, pub password: String, - pub name: String, } #[derive(Default, Serialize, Deserialize, Debug)] pub struct LoginResponse { pub token: String, - pub user_id: String, + pub uid: String, } #[derive(Default, Serialize, Deserialize, Debug)] diff --git a/tests/api/login.rs b/tests/api/login.rs new file mode 100644 index 00000000..d1887015 --- /dev/null +++ b/tests/api/login.rs @@ -0,0 +1,17 @@ +use actix_web::http::StatusCode; +use appflowy_server::component::auth::LoginResponse; +use crate::test_server::{spawn_server, TestUser}; + +#[tokio::test] +async fn login_success() { + let server = spawn_server().await; + let test_user = TestUser::generate(); + test_user.register(&server).await; + + let http_resp = server.login(&test_user.email, &test_user.password).await; + assert_eq!(http_resp.status(), StatusCode::OK); + + let bytes = http_resp.bytes().await.unwrap(); + let response: LoginResponse = serde_json::from_slice(&bytes).unwrap(); + assert!(!response.token.is_empty()) +} diff --git a/tests/api/main.rs b/tests/api/main.rs index c58a6756..e3b28e70 100644 --- a/tests/api/main.rs +++ b/tests/api/main.rs @@ -1,2 +1,3 @@ +mod login; mod register; mod test_server; diff --git a/tests/api/register.rs b/tests/api/register.rs index 14c89668..9d043b1f 100644 --- a/tests/api/register.rs +++ b/tests/api/register.rs @@ -1,5 +1,5 @@ -use crate::test_server::spawn_server; -use appflowy_server::component::auth::RegisterResponse; +use crate::test_server::{error_msg_from_resp, spawn_server}; +use appflowy_server::component::auth::{InputParamsError, RegisterResponse}; use reqwest::StatusCode; #[tokio::test] @@ -12,7 +12,6 @@ async fn register_success() { let bytes = http_resp.bytes().await.unwrap(); let response: RegisterResponse = serde_json::from_slice(&bytes).unwrap(); - println!("{:?}", response); } @@ -21,22 +20,27 @@ async fn register_with_invalid_password() { let server = spawn_server().await; let http_resp = server.register("user 1", "fake@appflowy.io", "123").await; assert_eq!(http_resp.status(), StatusCode::BAD_REQUEST); + assert_eq!(error_msg_from_resp(http_resp).await, InputParamsError::InvalidPassword.to_string()); } #[tokio::test] async fn register_with_invalid_name() { let server = spawn_server().await; + let name = "".to_string(); let http_resp = server - .register("", "fake@appflowy.io", "FakePassword!123") + .register(&name, "fake@appflowy.io", "FakePassword!123") .await; assert_eq!(http_resp.status(), StatusCode::BAD_REQUEST); + assert_eq!(error_msg_from_resp(http_resp).await, InputParamsError::InvalidName(name).to_string()); } #[tokio::test] async fn register_with_invalid_email() { let server = spawn_server().await; + let email = "appflowy.io".to_string(); let http_resp = server - .register("me", "appflowy.io", "FakePassword!123") + .register("me", &email, "FakePassword!123") .await; assert_eq!(http_resp.status(), StatusCode::BAD_REQUEST); + assert_eq!(error_msg_from_resp(http_resp).await, InputParamsError::InvalidEmail(email).to_string()); } diff --git a/tests/api/test_server.rs b/tests/api/test_server.rs index c9fa1ac6..12284dd9 100644 --- a/tests/api/test_server.rs +++ b/tests/api/test_server.rs @@ -40,7 +40,21 @@ impl TestServer { .json(&payload) .send() .await - .expect("Fail to register user") + .expect("Register failed") + } + + pub async fn login(&self, email: &str, password: &str) -> reqwest::Response { + let payload = serde_json::json!({ + "password": password, + "email": email + }); + let url = format!("{}/api/user/login", self.address); + self.api_client + .post(&url) + .json(&payload) + .send() + .await + .expect("Login failed") } } @@ -116,7 +130,7 @@ impl TestUser { Self { name: "Me".to_string(), email: "me@appflowy.io".to_string(), - password: "HelloAppFlowy123".to_string(), + password: "Hello@AppFlowy123".to_string(), } } @@ -125,9 +139,14 @@ impl TestUser { test_server .api_client .post(&url) - .json(&self) + .json(self) .send() .await .expect("Fail to register user"); } } + +pub async fn error_msg_from_resp(resp: reqwest::Response) -> String { + let bytes = resp.bytes().await.unwrap(); + String::from_utf8(bytes.to_vec()).unwrap() +} \ No newline at end of file