2022-09-08 17:49:51 +05:30
|
|
|
/*
|
|
|
|
* Copyright (C) 2022 Aravinth Manivannan <realaravinth@batsense.net>
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* 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 std::collections::HashMap;
|
|
|
|
|
2022-12-29 01:24:48 +05:30
|
|
|
use actix_web::{web, HttpResponse, Responder};
|
2022-12-29 01:38:51 +05:30
|
|
|
use actix_web_httpauth::middleware::HttpAuthentication;
|
2022-09-08 17:49:51 +05:30
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
2022-12-29 01:24:48 +05:30
|
|
|
use crate::db::FormSubmission;
|
2022-09-09 17:15:24 +05:30
|
|
|
use crate::errors::*;
|
2022-09-08 17:49:51 +05:30
|
|
|
use crate::AppCtx;
|
2022-09-09 17:15:24 +05:30
|
|
|
use crate::*;
|
2022-09-08 17:49:51 +05:30
|
|
|
|
2022-12-29 01:38:51 +05:30
|
|
|
use super::bearerauth;
|
|
|
|
|
2022-09-08 17:49:51 +05:30
|
|
|
pub mod routes {
|
|
|
|
use super::*;
|
|
|
|
#[derive(Debug, Eq, PartialEq, Deserialize, Serialize)]
|
|
|
|
pub struct Forms {
|
|
|
|
pub submit: &'static str,
|
2022-12-29 01:24:48 +05:30
|
|
|
pub get_all: &'static str,
|
2022-09-08 17:49:51 +05:30
|
|
|
}
|
|
|
|
impl Forms {
|
|
|
|
pub const fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
submit: "/api/v1/forms/submit",
|
2022-12-29 01:24:48 +05:30
|
|
|
get_all: "/api/v1/forms/list",
|
2022-09-08 17:49:51 +05:30
|
|
|
}
|
|
|
|
}
|
2022-12-29 01:24:48 +05:30
|
|
|
|
|
|
|
pub fn get_submit(&self, host: &str, path: &str) -> String {
|
|
|
|
format!("{}?host={}&path={}", self.submit, host, path)
|
|
|
|
}
|
2022-09-08 17:49:51 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn services(cfg: &mut web::ServiceConfig) {
|
|
|
|
cfg.service(upload);
|
2022-12-29 01:24:48 +05:30
|
|
|
cfg.service(list_all);
|
2022-09-08 17:49:51 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize, Debug)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum FormDType {
|
|
|
|
Num(f64),
|
|
|
|
Str(String),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FormDType {
|
|
|
|
fn apply_types(&mut self) {
|
|
|
|
if let Self::Str(data) = self {
|
|
|
|
if let Ok(num) = data.parse::<f64>() {
|
|
|
|
*self = Self::Num(num);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub type FormValue = HashMap<String, FormDType>;
|
|
|
|
|
2022-12-29 01:24:48 +05:30
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
|
|
struct Page {
|
|
|
|
page: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug)]
|
|
|
|
pub struct Table {
|
|
|
|
pub host: String,
|
|
|
|
pub path: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Debug, Default)]
|
|
|
|
pub struct FormSubmissionResp {
|
|
|
|
pub value: Option<serde_json::Value>,
|
|
|
|
pub time: i64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<FormSubmission> for FormSubmissionResp {
|
|
|
|
fn from(f: FormSubmission) -> Self {
|
|
|
|
Self {
|
|
|
|
value: f.value,
|
|
|
|
time: f.time.unix_timestamp(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-29 01:38:51 +05:30
|
|
|
#[actix_web_codegen_const_routes::post(
|
|
|
|
path = "API_V1_ROUTES.forms.get_all",
|
|
|
|
wrap = "HttpAuthentication::bearer(bearerauth)"
|
|
|
|
)]
|
2022-12-29 01:45:47 +05:30
|
|
|
#[tracing::instrument(name = "Get form submissions", skip(ctx))]
|
2022-12-29 01:24:48 +05:30
|
|
|
async fn list_all(
|
|
|
|
ctx: AppCtx,
|
|
|
|
payload: web::Json<Table>,
|
|
|
|
page: web::Query<Page>,
|
|
|
|
) -> ServiceResult<impl Responder> {
|
|
|
|
let mut subs = ctx
|
|
|
|
.db
|
|
|
|
.get_form_submissions(page.page, &payload.host, &payload.path)
|
|
|
|
.await?;
|
|
|
|
let mut resp: Vec<FormSubmissionResp> = Vec::with_capacity(subs.len());
|
|
|
|
for sub in subs.drain(0..) {
|
|
|
|
resp.push(sub.into());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok().json(resp))
|
|
|
|
}
|
|
|
|
|
2022-09-08 17:49:51 +05:30
|
|
|
#[actix_web_codegen_const_routes::post(path = "API_V1_ROUTES.forms.submit")]
|
2022-12-29 01:45:47 +05:30
|
|
|
#[tracing::instrument(name = "Upload form", skip(ctx, payload))]
|
2022-09-08 17:49:51 +05:30
|
|
|
async fn upload(
|
|
|
|
ctx: AppCtx,
|
2022-12-29 01:24:48 +05:30
|
|
|
query: web::Query<Table>,
|
2022-09-08 17:49:51 +05:30
|
|
|
payload: web::Either<web::Json<serde_json::Value>, web::Form<FormValue>>,
|
2022-09-09 17:15:24 +05:30
|
|
|
) -> ServiceResult<impl Responder> {
|
2022-12-29 01:24:48 +05:30
|
|
|
let host = &query.host;
|
|
|
|
let path = &query.path;
|
2022-09-08 17:49:51 +05:30
|
|
|
|
|
|
|
let data = match payload {
|
|
|
|
web::Either::Left(json) => json.into_inner(),
|
|
|
|
web::Either::Right(form) => {
|
|
|
|
let mut form = form.into_inner();
|
|
|
|
for (_, v) in form.iter_mut() {
|
|
|
|
v.apply_types();
|
|
|
|
}
|
|
|
|
serde_json::to_value(&form).unwrap()
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-09-09 19:51:54 +05:30
|
|
|
ctx.db
|
2022-12-29 01:24:48 +05:30
|
|
|
.add_form_submission(&data, &host, path)
|
2022-09-09 19:51:54 +05:30
|
|
|
.await
|
|
|
|
.unwrap();
|
|
|
|
|
2022-09-08 17:49:51 +05:30
|
|
|
Ok(HttpResponse::Ok().json(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
pub mod tests {
|
|
|
|
use actix_web::{
|
|
|
|
http::{header, StatusCode},
|
|
|
|
test, App,
|
|
|
|
};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
2022-12-29 01:24:48 +05:30
|
|
|
#[derive(Serialize, Clone, Debug, Deserialize, PartialEq)]
|
2022-09-08 17:49:51 +05:30
|
|
|
struct Foo {
|
|
|
|
foo: String,
|
|
|
|
num: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[actix_rt::test]
|
|
|
|
async fn submit_works() {
|
|
|
|
// const USERNAME: &str = "index_works";
|
|
|
|
// const PASSWORD: &str = "23k4j;123k4j1;l23kj4";
|
|
|
|
let settings = Settings::new().unwrap();
|
|
|
|
// let settings = Settings::new().unwrap();
|
|
|
|
let ctx = AppCtx::new(crate::ctx::Ctx::new(&settings).await);
|
|
|
|
let app = test::init_service(
|
|
|
|
App::new()
|
|
|
|
.app_data(ctx.clone())
|
|
|
|
.configure(crate::routes::services),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
|
2022-12-29 01:24:48 +05:30
|
|
|
let host = "localhost:8008";
|
|
|
|
let path = "/foo";
|
|
|
|
let upload_path = API_V1_ROUTES.forms.get_submit(host, path);
|
|
|
|
println!("{upload_path}");
|
|
|
|
|
2022-12-29 01:38:51 +05:30
|
|
|
let _ = ctx.db.delete_site(host).await;
|
|
|
|
|
2022-09-08 17:49:51 +05:30
|
|
|
let foo = Foo {
|
|
|
|
foo: "Foo".into(),
|
|
|
|
num: 2.33,
|
|
|
|
};
|
|
|
|
|
|
|
|
// upload json
|
|
|
|
let upload_json = test::call_service(
|
|
|
|
&app,
|
|
|
|
test::TestRequest::post()
|
2022-12-29 01:24:48 +05:30
|
|
|
.uri(&upload_path)
|
2022-09-08 17:49:51 +05:30
|
|
|
.set_json(&foo)
|
|
|
|
.to_request(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if upload_json.status() != StatusCode::OK {
|
|
|
|
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;
|
|
|
|
|
|
|
|
// upload url encoded
|
|
|
|
let upload_form = test::call_service(
|
|
|
|
&app,
|
|
|
|
test::TestRequest::post()
|
2022-12-29 01:24:48 +05:30
|
|
|
.uri(&upload_path)
|
2022-09-08 17:49:51 +05:30
|
|
|
.set_form(&foo)
|
|
|
|
.to_request(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
if upload_form.status() != StatusCode::OK {
|
|
|
|
let resp_err: crate::errors::ErrorToResponse = test::read_body_json(upload_form).await;
|
|
|
|
panic!("{:?}", resp_err.error);
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_eq!(upload_form.status(), StatusCode::OK);
|
|
|
|
let form: serde_json::Value = test::read_body_json(upload_form).await;
|
|
|
|
assert_eq!(form, json);
|
2022-12-29 01:24:48 +05:30
|
|
|
|
|
|
|
let get_sub_route = format!("{}?page={}", API_V1_ROUTES.forms.get_all, 0);
|
|
|
|
let payload = Table {
|
|
|
|
host: host.into(),
|
|
|
|
path: path.to_owned(),
|
|
|
|
};
|
|
|
|
println!("{get_sub_route}");
|
|
|
|
let get_subs = test::call_service(
|
|
|
|
&app,
|
|
|
|
test::TestRequest::post()
|
|
|
|
.set_json(&payload)
|
|
|
|
.uri(&get_sub_route)
|
2022-12-29 01:38:51 +05:30
|
|
|
.insert_header((
|
|
|
|
header::AUTHORIZATION,
|
|
|
|
format!("Bearer {}", ctx.settings.dash.api_key),
|
|
|
|
))
|
2022-12-29 01:24:48 +05:30
|
|
|
.to_request(),
|
|
|
|
)
|
|
|
|
.await;
|
|
|
|
println!("{:?}", get_subs);
|
|
|
|
assert_eq!(get_subs.status(), StatusCode::OK);
|
|
|
|
let subs: Vec<FormSubmissionResp> = test::read_body_json(get_subs).await;
|
|
|
|
let foo_as_json_value = serde_json::to_value(&foo).unwrap();
|
|
|
|
assert_eq!(subs.len(), 2);
|
|
|
|
assert_eq!(subs[0].value.as_ref().unwrap(), &foo_as_json_value);
|
|
|
|
assert_eq!(subs[1].value.as_ref().unwrap(), &foo_as_json_value);
|
2022-09-08 17:49:51 +05:30
|
|
|
}
|
|
|
|
}
|