From 936c4babdc3584b4aabe0acbcf9328864a01cbf8 Mon Sep 17 00:00:00 2001 From: Aravinth Manivannan Date: Fri, 30 Dec 2022 19:08:29 +0530 Subject: [PATCH] feat: rest api to list forms --- src/api/v1/forms.rs | 39 ++++++++++++++++++++++++++++++++++++++- src/db.rs | 16 ++++++++-------- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/api/v1/forms.rs b/src/api/v1/forms.rs index 2f70461..4e4d2cf 100644 --- a/src/api/v1/forms.rs +++ b/src/api/v1/forms.rs @@ -32,16 +32,22 @@ pub mod routes { pub submit: &'static str, pub get_all: &'static str, pub delete: &'static str, + pub get_forms_for_host: &'static str, } impl Forms { pub const fn new() -> Self { Self { submit: "/api/v1/forms/submit", get_all: "/api/v1/forms/list", + get_forms_for_host: "/api/v1/forms/host", delete: "/api/v1/forms/delete/{id}", } } + pub fn get_forms_for_host_route(&self, host: &str) -> String { + format!("{}?host={}", self.get_forms_for_host, host) + } + pub fn get_submit(&self, host: &str, path: &str) -> String { format!("{}?host={}&path={}", self.submit, host, path) } @@ -57,6 +63,7 @@ pub fn services(cfg: &mut web::ServiceConfig) { cfg.service(upload); cfg.service(list_all); cfg.service(delete_submission); + cfg.service(list_all_forms); } #[actix_web_codegen_const_routes::post( @@ -131,6 +138,21 @@ async fn upload( Ok(HttpResponse::Ok().json(data)) } +#[derive(Serialize, Deserialize, Clone, Debug)] +pub struct Host { + host: String, +} + +#[actix_web_codegen_const_routes::get( + path = "API_V1_ROUTES.forms.get_forms_for_host", + wrap = "HttpAuthentication::bearer(bearerauth)" +)] +#[tracing::instrument(name = "Get forms belonging to hostname", skip(ctx))] +async fn list_all_forms(ctx: AppCtx, query: web::Query) -> ServiceResult { + let forms = ctx.db.list_forms(&query.host).await?; + Ok(HttpResponse::Ok().json(forms)) +} + #[cfg(test)] pub mod tests { use actix_web::{ @@ -185,10 +207,25 @@ pub mod tests { let resp_err: crate::errors::ErrorToResponse = test::read_body_json(upload_json).await; panic!("{:?}", resp_err.error); } - assert_eq!(upload_json.status(), StatusCode::OK); let json: serde_json::Value = test::read_body_json(upload_json).await; + // get all forms for host + let list_forms_resp = test::call_service( + &app, + test::TestRequest::get() + .uri(&API_V1_ROUTES.forms.get_forms_for_host_route(host)) + .insert_header(( + header::AUTHORIZATION, + format!("Bearer {}", ctx.settings.dash.api_key), + )) + .to_request(), + ) + .await; + assert_eq!(list_forms_resp.status(), StatusCode::OK); + let forms: Vec = test::read_body_json(list_forms_resp).await; + assert_eq!(forms, vec![path.to_string()]); + // upload url encoded let upload_form = test::call_service( &app, diff --git a/src/db.rs b/src/db.rs index e6b0832..c5bece8 100644 --- a/src/db.rs +++ b/src/db.rs @@ -116,9 +116,9 @@ impl Database { Ok(()) } - /// Get forms belonging to a host + /// Get forms belonging to a host pub async fn list_forms(&self, host: &str) -> ServiceResult> { - struct S{ + struct S { website_path: String, } let mut forms = sqlx::query_as!( @@ -128,11 +128,12 @@ impl Database { FROM forms_submissions WHERE - website_id = (SELECT ID FROM forms_websites WHERE hostname = $1)", - host) - .fetch_all(&self.pool) - .await - .unwrap(); + website_id = (SELECT ID FROM forms_websites WHERE hostname = $1)", + host + ) + .fetch_all(&self.pool) + .await + .unwrap(); let mut resp = Vec::with_capacity(forms.len()); for f in forms.drain(0..) { resp.push(f.website_path); @@ -288,7 +289,6 @@ mod tests { .await .unwrap(); - assert_eq!(db.list_forms(url).await.unwrap().len(), 1); let subs = db.get_form_submissions(0, url, path).await.unwrap();