chore: update database config & fix test
This commit is contained in:
parent
2429a41b2f
commit
8af290ab80
2
.env
2
.env
|
|
@ -1 +1 @@
|
|||
DATABASE_URL="postgres://postgres:password@localhost:5433/flowy"
|
||||
DATABASE_URL="postgres://postgres:password@localhost:5433/appflowy_pg"
|
||||
|
|
@ -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}
|
||||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -6,4 +6,4 @@ database:
|
|||
port: 5433
|
||||
username: "postgres"
|
||||
password: "password"
|
||||
database_name: "flowy"
|
||||
database_name: "appflowy_pg"
|
||||
|
|
|
|||
|
|
@ -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 },
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
|
|
|
|||
|
|
@ -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())
|
||||
}
|
||||
|
|
@ -1,2 +1,3 @@
|
|||
mod login;
|
||||
mod register;
|
||||
mod test_server;
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
}
|
||||
Loading…
Reference in New Issue