diff --git a/src/db/sections.rs b/src/db/sections.rs index 11b594f..fc5b7a0 100644 --- a/src/db/sections.rs +++ b/src/db/sections.rs @@ -16,16 +16,16 @@ pub struct Section pub has_titles: bool, } -#[derive(Deserialize, Insertable)] +#[derive(Serialize, Deserialize, Insertable)] #[table_name = "sections"] #[serde(rename_all = "camelCase")] pub struct NewSection { - title: String, + pub title: String, pub shortname: String, - description: Option, - is_default: Option, - has_titles: Option, + pub description: Option, + pub is_default: Option, + pub has_titles: Option, } #[derive(Deserialize, AsChangeset)] diff --git a/src/main.rs b/src/main.rs index 683c0b0..0a5696d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,6 +19,14 @@ use serde::{Deserialize, Serialize}; pub mod v1; +/// This function just returns a pre-built rocket header containing a valid JWT for the secret +/// "secret", valid until 2034 or something. It's easy for testing the API. +#[cfg(test)] +pub fn auth_header() -> rocket::http::Header<'static> +{ + return rocket::http::Header::new("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJpZCI6IjVjMjM2OTI0NjY4ZDQzZWFiNGNmNDczYjk1YWZiNzgzIiwidXNlcm5hbWUiOiJKb2huIERvZSIsImFkbWluIjp0cnVlLCJleHAiOjE1MTYyMzkwMjIwfQ.if939L9le8LP-dtXnQs-mHPkb-VieRAvAfSu20755jY"); +} + #[database("postgres_rb")] pub struct RbDbConn(diesel::PgConnection); diff --git a/src/v1/posts.rs b/src/v1/posts.rs index c5810ed..6c273db 100644 --- a/src/v1/posts.rs +++ b/src/v1/posts.rs @@ -56,3 +56,22 @@ pub async fn delete(_admin: Admin, conn: RbDbConn, id: uuid::Uuid) -> RbResult<( { Ok(conn.run(move |c| db::posts::delete(c, &id)).await?) } + +// #[cfg(test)] +// mod tests { +// use crate::rocket; +// use rocket::local::blocking::Client; +// use rocket::http::Status; + +// #[test] +// fn test_create_get() { +// let client = Client::tracked(rocket()).expect("valid rocket instance"); + +// let data = db::NewPost { +// id: Uuid::parse_str("936DA01F9ABD4d9d80C702AF85C822A8").unwrap(), + +// }; + +// let mut res = cli +// } +// } diff --git a/src/v1/sections.rs b/src/v1/sections.rs index 86c60a9..4e9d107 100644 --- a/src/v1/sections.rs +++ b/src/v1/sections.rs @@ -61,3 +61,34 @@ pub async fn delete(_admin: Admin, conn: RbDbConn, id: uuid::Uuid) -> RbResult<( { Ok(conn.run(move |c| db::posts::delete(c, &id)).await?) } + +#[cfg(test)] +mod tests +{ + use rocket::{http::Status, local::blocking::Client}; + + use super::*; + use crate::rocket; + + #[test] + fn test_create_get() + { + let client = Client::tracked(rocket()).expect("valid rocket instance"); + + let data = db::NewSection { + title: String::from("Some Cool Title"), + shortname: String::from("test"), + description: None, + is_default: None, + has_titles: None, + }; + + let res = client + .post("/v1/sections") + .json(&data) + .header(crate::auth_header()) + .dispatch(); + + assert_eq!(res.status(), Status::Ok); + } +} diff --git a/test.py b/test.py index 2493a41..eff2639 100644 --- a/test.py +++ b/test.py @@ -12,8 +12,8 @@ data = { "shortname": "short", } -r = requests.post("http://localhost:8000/sections", headers=headers, json=data) +r = requests.post("http://localhost:8000/v1/sections", headers=headers, json=data) print(r.content) print(r.status_code) -r = requests.get("http://localhost:8000/sections?offset=0&limit=100") +r = requests.get("http://localhost:8000/v1/sections?offset=0&limit=100") print(r.json())