Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
476b25fecf
|
||
|
|
4cc42697cb
|
||
|
|
1a885274a8
|
||
|
|
53ed0060c8
|
||
|
|
9b4d0b7b32
|
||
|
|
dd877369e3
|
@@ -305,8 +305,7 @@ pub mod endpoint {
|
|||||||
),
|
),
|
||||||
responses(
|
responses(
|
||||||
(status = 200, description = "Password updated successfully", body = super::response::update_password::Response),
|
(status = 200, description = "Password updated successfully", body = super::response::update_password::Response),
|
||||||
(status = 400, description = "Invalid", body = super::response::update_password::Response),
|
(status = 400, description = "Failure", body = super::response::update_password::Response)
|
||||||
(status = 500, description = "Failure", body = super::response::update_password::Response)
|
|
||||||
)
|
)
|
||||||
)]
|
)]
|
||||||
pub async fn update_password(
|
pub async fn update_password(
|
||||||
@@ -320,11 +319,9 @@ pub mod endpoint {
|
|||||||
|
|
||||||
if !payload.is_valid() {
|
if !payload.is_valid() {
|
||||||
response.message = "Invalid request".to_string();
|
response.message = "Invalid request".to_string();
|
||||||
println!("Invalid request");
|
|
||||||
return (axum::http::StatusCode::BAD_REQUEST, axum::Json(response));
|
return (axum::http::StatusCode::BAD_REQUEST, axum::Json(response));
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("Get User");
|
|
||||||
match repo::user::get(&pool, &payload.username).await {
|
match repo::user::get(&pool, &payload.username).await {
|
||||||
Ok(user) => {
|
Ok(user) => {
|
||||||
if hashing::verify_password(&payload.current_password, user.password.clone())
|
if hashing::verify_password(&payload.current_password, user.password.clone())
|
||||||
@@ -333,10 +330,9 @@ pub mod endpoint {
|
|||||||
match repo::salt::get(&pool, &user.salt_id).await {
|
match repo::salt::get(&pool, &user.salt_id).await {
|
||||||
Ok(salt) => {
|
Ok(salt) => {
|
||||||
let updated_salt_string = hashing::generate_salt().unwrap();
|
let updated_salt_string = hashing::generate_salt().unwrap();
|
||||||
let updated_salt = simodels::user::salt::Salt {
|
let mut updated_salt = simodels::user::salt::Salt::default();
|
||||||
salt: updated_salt_string.to_string(),
|
updated_salt.salt = updated_salt_string.to_string();
|
||||||
id: salt.id,
|
updated_salt.id = salt.id.clone();
|
||||||
};
|
|
||||||
let updated_hashed_password = hashing::hash_password(
|
let updated_hashed_password = hashing::hash_password(
|
||||||
&payload.updated_password,
|
&payload.updated_password,
|
||||||
&updated_salt_string,
|
&updated_salt_string,
|
||||||
@@ -360,7 +356,7 @@ pub mod endpoint {
|
|||||||
Err(err) => {
|
Err(err) => {
|
||||||
response.message = err.to_string();
|
response.message = err.to_string();
|
||||||
(
|
(
|
||||||
axum::http::StatusCode::INTERNAL_SERVER_ERROR,
|
axum::http::StatusCode::BAD_REQUEST,
|
||||||
axum::Json(response),
|
axum::Json(response),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
+23
-159
@@ -21,7 +21,7 @@ async fn main() {
|
|||||||
mod init {
|
mod init {
|
||||||
use axum::{
|
use axum::{
|
||||||
Router,
|
Router,
|
||||||
routing::{get, patch, post},
|
routing::{get, post},
|
||||||
};
|
};
|
||||||
use utoipa::OpenApi;
|
use utoipa::OpenApi;
|
||||||
|
|
||||||
@@ -38,11 +38,11 @@ mod init {
|
|||||||
paths(
|
paths(
|
||||||
common_callers::endpoint::db_ping, common_callers::endpoint::root,
|
common_callers::endpoint::db_ping, common_callers::endpoint::root,
|
||||||
register_caller::register_user,
|
register_caller::register_user,
|
||||||
login_endpoints::login, login_endpoints::update_password, login_endpoints::service_login, login_endpoints::refresh_token
|
login_endpoints::login, login_endpoints::service_login, login_endpoints::refresh_token
|
||||||
),
|
),
|
||||||
components(schemas(common_callers::response::TestResult,
|
components(schemas(common_callers::response::TestResult,
|
||||||
register_responses::Response,
|
register_responses::Response,
|
||||||
login_responses::Response, login_responses::service_login::Response, login_responses::update_password::Response, login_responses::refresh_token::Response)),
|
login_responses::Response, login_responses::service_login::Response, login_responses::refresh_token::Response)),
|
||||||
tags(
|
tags(
|
||||||
(name = "soaricarus Auth API", description = "Auth API for soaricarus API")
|
(name = "soaricarus Auth API", description = "Auth API for soaricarus API")
|
||||||
)
|
)
|
||||||
@@ -51,20 +51,20 @@ mod init {
|
|||||||
|
|
||||||
mod cors {
|
mod cors {
|
||||||
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
|
pub async fn configure_cors() -> tower_http::cors::CorsLayer {
|
||||||
|
// Start building the CORS layer with common settings
|
||||||
let cors = tower_http::cors::CorsLayer::new()
|
let cors = tower_http::cors::CorsLayer::new()
|
||||||
.allow_methods([
|
.allow_methods([
|
||||||
axum::http::Method::GET,
|
axum::http::Method::GET,
|
||||||
axum::http::Method::PATCH,
|
|
||||||
axum::http::Method::POST,
|
axum::http::Method::POST,
|
||||||
axum::http::Method::PUT,
|
axum::http::Method::PUT,
|
||||||
axum::http::Method::DELETE,
|
axum::http::Method::DELETE,
|
||||||
])
|
]) // Specify allowed methods:cite[2]
|
||||||
.allow_headers([
|
.allow_headers([
|
||||||
axum::http::header::CONTENT_TYPE,
|
axum::http::header::CONTENT_TYPE,
|
||||||
axum::http::header::AUTHORIZATION,
|
axum::http::header::AUTHORIZATION,
|
||||||
])
|
]) // Specify allowed headers:cite[2]
|
||||||
.allow_credentials(true)
|
.allow_credentials(true) // If you need to send cookies or authentication headers:cite[2]
|
||||||
.max_age(std::time::Duration::from_secs(3600));
|
.max_age(std::time::Duration::from_secs(3600)); // Cache the preflight response for 1 hour:cite[2]
|
||||||
|
|
||||||
// Dynamically set the allowed origin based on the environment
|
// Dynamically set the allowed origin based on the environment
|
||||||
match std::env::var(sienvy::keys::APP_ENV).as_deref() {
|
match std::env::var(sienvy::keys::APP_ENV).as_deref() {
|
||||||
@@ -116,10 +116,6 @@ mod init {
|
|||||||
callers::endpoints::LOGIN,
|
callers::endpoints::LOGIN,
|
||||||
post(callers::login::endpoint::login),
|
post(callers::login::endpoint::login),
|
||||||
)
|
)
|
||||||
.route(
|
|
||||||
callers::endpoints::UPDATE_PASSWORD,
|
|
||||||
patch(callers::login::endpoint::update_password),
|
|
||||||
)
|
|
||||||
.route(
|
.route(
|
||||||
callers::endpoints::SERVICE_LOGIN,
|
callers::endpoints::SERVICE_LOGIN,
|
||||||
post(callers::login::endpoint::service_login),
|
post(callers::login::endpoint::service_login),
|
||||||
@@ -251,27 +247,6 @@ mod tests {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_test_login_payload(usr: &callers::login::request::Request) -> serde_json::Value {
|
|
||||||
json!({
|
|
||||||
"username": &usr.username,
|
|
||||||
"password": &usr.password,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod test_data {
|
|
||||||
use serde_json::json;
|
|
||||||
|
|
||||||
pub fn get_updated_password(user_id: &uuid::Uuid) -> serde_json::Value {
|
|
||||||
json!({
|
|
||||||
"username": "somethingsss",
|
|
||||||
"user_id": user_id,
|
|
||||||
"current_password": "Raindown!",
|
|
||||||
"updated_password": "SpeakWithUrChest22!",
|
|
||||||
"confirmed_password": "SpeakWithUrChest22!"
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod requests {
|
pub mod requests {
|
||||||
use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
|
use tower::ServiceExt; // for `call`, `oneshot`, and `ready`
|
||||||
|
|
||||||
@@ -289,36 +264,6 @@ mod tests {
|
|||||||
|
|
||||||
app.clone().oneshot(req).await
|
app.clone().oneshot(req).await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn login(
|
|
||||||
app: &axum::Router,
|
|
||||||
usr: &super::callers::login::request::Request,
|
|
||||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
|
||||||
let payload = super::get_test_login_payload(&usr);
|
|
||||||
let req = axum::http::Request::builder()
|
|
||||||
.method(axum::http::Method::POST)
|
|
||||||
.uri(crate::callers::endpoints::LOGIN)
|
|
||||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
|
||||||
.body(axum::body::Body::from(payload.to_string()))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
app.clone().oneshot(req).await
|
|
||||||
}
|
|
||||||
|
|
||||||
pub async fn update_password(
|
|
||||||
app: &axum::Router,
|
|
||||||
user_id: &uuid::Uuid,
|
|
||||||
) -> Result<axum::response::Response, std::convert::Infallible> {
|
|
||||||
let payload = super::test_data::get_updated_password(user_id);
|
|
||||||
let req = axum::http::Request::builder()
|
|
||||||
.method(axum::http::Method::PATCH)
|
|
||||||
.uri(crate::callers::endpoints::UPDATE_PASSWORD)
|
|
||||||
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
|
||||||
.body(axum::body::Body::from(payload.to_string()))
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
app.clone().oneshot(req).await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
@@ -449,11 +394,22 @@ mod tests {
|
|||||||
);
|
);
|
||||||
assert!(returned_usr.date_created.is_some(), "Date Created is empty");
|
assert!(returned_usr.date_created.is_some(), "Date Created is empty");
|
||||||
|
|
||||||
let mut n_usr = callers::login::request::Request::default();
|
let login_payload = json!({
|
||||||
n_usr.username = usr.username.clone();
|
"username": &usr.username,
|
||||||
n_usr.password = usr.password.clone();
|
"password": &usr.password,
|
||||||
|
});
|
||||||
|
|
||||||
match requests::login(&app, &n_usr).await {
|
match app
|
||||||
|
.oneshot(
|
||||||
|
Request::builder()
|
||||||
|
.method(axum::http::Method::POST)
|
||||||
|
.uri(callers::endpoints::LOGIN)
|
||||||
|
.header(axum::http::header::CONTENT_TYPE, "application/json")
|
||||||
|
.body(Body::from(login_payload.to_string()))
|
||||||
|
.unwrap(),
|
||||||
|
)
|
||||||
|
.await
|
||||||
|
{
|
||||||
Ok(resp) => {
|
Ok(resp) => {
|
||||||
assert_eq!(StatusCode::OK, resp.status(), "Status is not right");
|
assert_eq!(StatusCode::OK, resp.status(), "Status is not right");
|
||||||
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
|
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
|
||||||
@@ -597,96 +553,4 @@ mod tests {
|
|||||||
|
|
||||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn test_update_password() {
|
|
||||||
let tm_pool = db_mgr::get_pool().await.unwrap();
|
|
||||||
|
|
||||||
let db_name = db_mgr::generate_db_name().await;
|
|
||||||
|
|
||||||
match db_mgr::create_database(&tm_pool, &db_name).await {
|
|
||||||
Ok(_) => {
|
|
||||||
println!("Success");
|
|
||||||
}
|
|
||||||
Err(e) => {
|
|
||||||
assert!(false, "Error: {:?}", e.to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let pool = db_mgr::connect_to_db(&db_name).await.unwrap();
|
|
||||||
|
|
||||||
db::init::migrations(&pool).await;
|
|
||||||
|
|
||||||
let app = init::routes().await.layer(axum::Extension(pool));
|
|
||||||
|
|
||||||
let usr = get_test_register_request();
|
|
||||||
|
|
||||||
match requests::register(&app, &usr).await {
|
|
||||||
Ok(resp) => {
|
|
||||||
assert_eq!(
|
|
||||||
resp.status(),
|
|
||||||
StatusCode::CREATED,
|
|
||||||
"Message: {:?} {:?}",
|
|
||||||
resp,
|
|
||||||
usr.username
|
|
||||||
);
|
|
||||||
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let parsed_body: callers::register::response::Response =
|
|
||||||
serde_json::from_slice(&body).unwrap();
|
|
||||||
let returned_usr = &parsed_body.data[0];
|
|
||||||
|
|
||||||
assert_eq!(false, returned_usr.id.is_nil(), "Id is not populated");
|
|
||||||
|
|
||||||
assert_eq!(
|
|
||||||
usr.username, returned_usr.username,
|
|
||||||
"Usernames do not match"
|
|
||||||
);
|
|
||||||
assert!(returned_usr.date_created.is_some(), "Date Created is empty");
|
|
||||||
|
|
||||||
let mut n_usr = callers::login::request::Request::default();
|
|
||||||
n_usr.username = usr.username.clone();
|
|
||||||
n_usr.password = usr.password.clone();
|
|
||||||
|
|
||||||
match requests::login(&app, &n_usr).await {
|
|
||||||
Ok(resp) => {
|
|
||||||
assert_eq!(StatusCode::OK, resp.status(), "Status is not right");
|
|
||||||
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let parsed_body: callers::login::response::Response =
|
|
||||||
serde_json::from_slice(&body).unwrap();
|
|
||||||
let login_result = &parsed_body.data[0];
|
|
||||||
assert!(!login_result.id.is_nil(), "Id is nil");
|
|
||||||
|
|
||||||
match requests::update_password(&app, &login_result.id).await {
|
|
||||||
Ok(resp) => {
|
|
||||||
assert_eq!(StatusCode::OK, resp.status(), "Status is not right");
|
|
||||||
let body = axum::body::to_bytes(resp.into_body(), usize::MAX)
|
|
||||||
.await
|
|
||||||
.unwrap();
|
|
||||||
let parsed_body: callers::login::response::update_password::Response =
|
|
||||||
serde_json::from_slice(&body).unwrap();
|
|
||||||
let id = parsed_body.data[0];
|
|
||||||
|
|
||||||
assert_eq!(id, login_result.id, "Ids do not match");
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
assert!(false, "Error: {err:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
assert!(false, "Error: {:?}", err.to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Err(err) => {
|
|
||||||
assert!(false, "Error: {err:?}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let _ = db_mgr::drop_database(&tm_pool, &db_name).await;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-7
@@ -57,14 +57,15 @@ pub mod user {
|
|||||||
.bind(password)
|
.bind(password)
|
||||||
.bind(user.id)
|
.bind(user.id)
|
||||||
.execute(pool)
|
.execute(pool)
|
||||||
|
// .fetch_optional(pool)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(r) => {
|
Ok(r) => {
|
||||||
if r.rows_affected() > 0 {
|
return if r.rows_affected() > 0 {
|
||||||
Ok(true)
|
Ok(true)
|
||||||
} else {
|
} else {
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
@@ -239,11 +240,9 @@ pub mod salt {
|
|||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
Ok(row) => {
|
Ok(row) => {
|
||||||
if row.rows_affected() > 0 {
|
let result = if row.rows_affected() > 0 { true } else { false };
|
||||||
Ok(true)
|
|
||||||
} else {
|
Ok(result)
|
||||||
Ok(false)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Err(err) => Err(err),
|
Err(err) => Err(err),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user