chore: lint, use actix_web_codegen_const_routes and serde all routes

This commit is contained in:
Aravinth Manivannan 2023-01-24 19:02:29 +05:30
parent 17307074c3
commit 73cfe1b95d
Signed by: realaravinth
GPG key ID: AD9F0F08E855ED88
15 changed files with 56 additions and 38 deletions

View file

@ -22,7 +22,7 @@ use super::auth::runners::Password;
use crate::errors::*;
use crate::AppData;
#[my_codegen::post(
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.admin.account.delete",
wrap = "crate::api::v1::admin::get_admin_check_login()"
)]

View file

@ -29,7 +29,9 @@ pub struct Email {
pub email: String,
}
#[my_codegen::post(path = "crate::V1_API_ROUTES.admin.account.email_exists")]
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.admin.account.email_exists"
)]
pub async fn email_exists(
payload: web::Json<AccountCheckPayload>,
data: AppData,
@ -53,7 +55,7 @@ pub async fn email_exists(
}
/// update email
#[my_codegen::post(
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.admin.account.update_email",
wrap = "crate::api::v1::admin::get_admin_check_login()"
)]

View file

@ -28,7 +28,9 @@ pub mod username;
pub use super::auth;
pub mod routes {
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Account {
pub delete: &'static str,
pub email_exists: &'static str,

View file

@ -68,7 +68,7 @@ async fn update_password_runner(
Ok(())
}
#[my_codegen::post(
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.admin.account.update_password",
wrap = "crate::api::v1::admin::get_admin_check_login()"
)]
@ -118,7 +118,6 @@ mod tests {
use actix_web::test;
use crate::api::v1::ROUTES;
use crate::data::Data;
use crate::tests::*;
#[actix_rt::test]
@ -128,7 +127,7 @@ mod tests {
const EMAIL: &str = "updatepassuser@a.com";
{
let data = Data::new().await;
let data = get_test_data().await;
delete_user(NAME, &data).await;
}

View file

@ -29,7 +29,7 @@ pub struct Secret {
pub secret: String,
}
#[my_codegen::get(
#[actix_web_codegen_const_routes::get(
path = "crate::V1_API_ROUTES.admin.account.get_secret",
wrap = "crate::api::v1::admin::get_admin_check_login()"
)]
@ -47,7 +47,7 @@ async fn get_secret(id: Identity, data: AppData) -> ServiceResult<impl Responder
Ok(HttpResponse::Ok().json(secret))
}
#[my_codegen::post(
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.admin.account.update_secret",
wrap = "crate::api::v1::admin::get_admin_check_login()"
)]

View file

@ -23,7 +23,6 @@ use super::username::Username;
use super::*;
use crate::api::v1::admin::auth::runners::Password;
use crate::api::v1::ROUTES;
use crate::data::Data;
use crate::*;
use crate::errors::*;
@ -36,7 +35,7 @@ async fn uname_email_exists_works() {
const EMAIL: &str = "testuserexists@a.com2";
{
let data = Data::new().await;
let data = get_test_data().await;
delete_user(NAME, &data).await;
}
@ -126,7 +125,7 @@ async fn email_udpate_password_validation_del_userworks() {
const EMAIL2: &str = "eupdauser@a.com";
{
let data = Data::new().await;
let data = get_test_data().await;
delete_user(NAME, &data).await;
delete_user(NAME2, &data).await;
}
@ -209,7 +208,7 @@ async fn username_update_works() {
const NAME_CHANGE: &str = "terstusrtdsxx";
{
let data = Data::new().await;
let data = get_test_data().await;
futures::join!(
delete_user(NAME, &data),

View file

@ -24,7 +24,9 @@ use super::{AccountCheckPayload, AccountCheckResp};
use crate::errors::*;
use crate::AppData;
#[my_codegen::post(path = "crate::V1_API_ROUTES.admin.account.username_exists")]
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.admin.account.username_exists"
)]
async fn username_exists(
payload: web::Json<AccountCheckPayload>,
data: AppData,
@ -65,7 +67,7 @@ pub struct Username {
}
/// update username
#[my_codegen::post(
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.admin.account.update_username",
wrap = "crate::api::v1::admin::get_admin_check_login()"
)]

View file

@ -26,7 +26,9 @@ use crate::AppData;
pub mod routes {
use actix_auth_middleware::GetLoginRoute;
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Auth {
pub logout: &'static str,
pub login: &'static str,
@ -153,7 +155,7 @@ pub mod runners {
payload: &Register,
data: &AppData,
) -> ServiceResult<()> {
if !crate::SETTINGS.allow_registration {
if !data.settings.allow_registration {
return Err(ServiceError::ClosedForRegistration);
}
@ -222,7 +224,9 @@ pub fn services(cfg: &mut web::ServiceConfig) {
cfg.service(login);
cfg.service(signout);
}
#[my_codegen::post(path = "crate::V1_API_ROUTES.admin.auth.register")]
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.admin.auth.register"
)]
async fn register(
payload: web::Json<runners::Register>,
data: AppData,
@ -231,7 +235,7 @@ async fn register(
Ok(HttpResponse::Ok())
}
#[my_codegen::post(path = "crate::V1_API_ROUTES.admin.auth.login")]
#[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.admin.auth.login")]
async fn login(
id: Identity,
payload: web::Json<runners::Login>,
@ -250,7 +254,7 @@ async fn login(
Ok(HttpResponse::Ok().into())
}
}
#[my_codegen::get(
#[actix_web_codegen_const_routes::get(
path = "crate::V1_API_ROUTES.admin.auth.logout",
wrap = "crate::api::v1::admin::get_admin_check_login()"
)]

View file

@ -27,6 +27,9 @@ use crate::errors::*;
use crate::AppData;
pub mod routes {
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq, Clone)]
pub struct Campaign {
pub add: &'static str,
pub delete: &'static str,
@ -250,7 +253,7 @@ pub mod runners {
}
}
#[my_codegen::post(
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.admin.campaign.delete",
wrap = "get_admin_check_login()"
)]
@ -279,7 +282,7 @@ pub async fn delete(
// pub feedbacks: Vec<Feedback>,
//}
//
//#[my_codegen::post(
//#[actix_web_codegen_const_routes::post(
// path = "crate::V1_API_ROUTES.campaign.get_feedback",
// wrap = "crate::CheckLogin"
//)]
@ -300,7 +303,7 @@ pub struct ListCampaignResp {
pub uuid: String,
}
#[my_codegen::post(
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.admin.campaign.list",
wrap = "get_admin_check_login()"
)]
@ -332,7 +335,7 @@ pub fn services(cfg: &mut web::ServiceConfig) {
//cfg.service(get_feedback);
}
#[my_codegen::post(path = "crate::V1_API_ROUTES.admin.campaign.add")]
#[actix_web_codegen_const_routes::post(path = "crate::V1_API_ROUTES.admin.campaign.add")]
async fn add(
payload: web::Json<AddCapmaign>,
data: AppData,
@ -350,7 +353,6 @@ async fn add(
#[cfg(test)]
mod tests {
use crate::api::v1::bench::Submission;
use crate::data::Data;
use crate::errors::*;
use crate::tests::*;
use crate::*;
@ -360,7 +362,7 @@ mod tests {
#[actix_rt::test]
async fn test_bench_register_works() {
let data = Data::new().await;
let data = get_test_data().await;
let app = get_app!(data).await;
let signin_resp = test::call_service(
&app,
@ -400,7 +402,7 @@ mod tests {
const THREADS: i32 = 4;
{
let data = Data::new().await;
let data = get_test_data().await;
delete_user(NAME, &data).await;
}

View file

@ -24,7 +24,6 @@ pub mod campaigns;
mod tests;
pub use super::{get_random, get_uuid, RedirectQuery};
use crate::api::v1::bench::SURVEY_USER_ID;
pub fn services(cfg: &mut ServiceConfig) {
auth::services(cfg);
@ -40,7 +39,9 @@ pub mod routes {
use super::account::routes::Account;
use super::auth::routes::Auth;
use super::campaigns::routes::Campaign;
use serde::Serialize;
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct Admin {
pub auth: Auth,
pub account: Account,

View file

@ -20,7 +20,6 @@ use actix_web::test;
use crate::api::v1::admin::auth::runners::{Login, Register};
use crate::api::v1::ROUTES;
use crate::data::Data;
use crate::errors::*;
use crate::*;
@ -28,7 +27,7 @@ use crate::tests::*;
#[actix_rt::test]
async fn auth_works() {
let data = Data::new().await;
let data = get_test_data().await;
const NAME: &str = "testuser";
const PASSWORD: &str = "longpassword";
const EMAIL: &str = "testuser1@a.com";
@ -146,7 +145,7 @@ async fn serverside_password_validation_works() {
const NAME: &str = "testuser542";
const PASSWORD: &str = "longpassword2";
let data = Data::new().await;
let data = get_test_data().await;
delete_user(NAME, &data).await;
let app = get_app!(data).await;

View file

@ -18,7 +18,6 @@
use actix_web::http::StatusCode;
use actix_web::test;
use crate::data::Data;
use crate::*;
use crate::tests::*;
@ -32,7 +31,7 @@ async fn protected_routes_work() {
let get_protected_urls = [V1_API_ROUTES.admin.auth.logout];
{
let data = Data::new().await;
let data = get_test_data().await;
delete_user(NAME, &data).await;
}

View file

@ -33,9 +33,11 @@ use crate::AppData;
pub const SURVEY_USER_ID: &str = "survey_user_id";
pub mod routes {
use serde::{Deserialize, Serialize};
use actix_auth_middleware::GetLoginRoute;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Benches {
pub submit: &'static str,
pub register: &'static str,
@ -125,7 +127,7 @@ pub mod runners {
}
}
#[my_codegen::get(path = "crate::V1_API_ROUTES.benches.register")]
#[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.benches.register")]
async fn register(
data: AppData,
session: Session,
@ -178,7 +180,7 @@ pub struct SubmissionProof {
fn is_session_authenticated(r: &HttpRequest, mut pl: &mut Payload) -> bool {
use actix_web::FromRequest;
matches!(
Session::from_request(&r, &mut pl).into_inner().map(|x| {
Session::from_request(r, pl).into_inner().map(|x| {
let val = x.get::<String>(SURVEY_USER_ID);
println!("{:#?}", val);
val
@ -196,7 +198,7 @@ pub fn get_check_login() -> Authentication<routes::Benches> {
// }
//}
#[my_codegen::post(
#[actix_web_codegen_const_routes::post(
path = "crate::V1_API_ROUTES.benches.submit",
wrap = "get_check_login()"
)]
@ -307,7 +309,7 @@ pub struct BenchConfig {
pub difficulties: Vec<i32>,
}
#[my_codegen::get(
#[actix_web_codegen_const_routes::get(
path = "crate::V1_API_ROUTES.benches.fetch",
wrap = "get_check_login()"
)]

View file

@ -29,6 +29,9 @@ pub struct BuildDetails {
}
pub mod routes {
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct Meta {
pub build_details: &'static str,
pub health: &'static str,
@ -45,7 +48,7 @@ pub mod routes {
}
/// emmits build details of the bninary
#[my_codegen::get(path = "crate::V1_API_ROUTES.meta.build_details")]
#[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.meta.build_details")]
async fn build_details() -> impl Responder {
let build = BuildDetails {
version: VERSION,
@ -61,7 +64,7 @@ pub struct Health {
}
/// checks all components of the system
#[my_codegen::get(path = "crate::V1_API_ROUTES.meta.health")]
#[actix_web_codegen_const_routes::get(path = "crate::V1_API_ROUTES.meta.health")]
async fn health(data: AppData) -> impl Responder {
use sqlx::Connection;
@ -87,6 +90,7 @@ mod tests {
use super::*;
use crate::api::v1::services;
use crate::tests::get_test_data;
use crate::*;
#[actix_rt::test]
@ -106,7 +110,7 @@ mod tests {
#[actix_rt::test]
async fn health_works() {
println!("{}", V1_API_ROUTES.meta.health);
let data = Data::new().await;
let data = get_test_data().await;
let app = get_app!(data).await;
let resp = test::call_service(

View file

@ -14,12 +14,15 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use serde::Serialize;
use super::admin::routes::Admin;
use super::bench::routes::Benches;
use super::meta::routes::Meta;
pub const ROUTES: Routes = Routes::new();
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct Routes {
pub admin: Admin,
pub meta: Meta,