Added basic setup for testing
parent
7aee0282b2
commit
9a058110a7
|
@ -16,16 +16,16 @@ pub struct Section
|
||||||
pub has_titles: bool,
|
pub has_titles: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Insertable)]
|
#[derive(Serialize, Deserialize, Insertable)]
|
||||||
#[table_name = "sections"]
|
#[table_name = "sections"]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct NewSection
|
pub struct NewSection
|
||||||
{
|
{
|
||||||
title: String,
|
pub title: String,
|
||||||
pub shortname: String,
|
pub shortname: String,
|
||||||
description: Option<String>,
|
pub description: Option<String>,
|
||||||
is_default: Option<bool>,
|
pub is_default: Option<bool>,
|
||||||
has_titles: Option<bool>,
|
pub has_titles: Option<bool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, AsChangeset)]
|
#[derive(Deserialize, AsChangeset)]
|
||||||
|
|
|
@ -19,6 +19,14 @@ use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
pub mod v1;
|
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")]
|
#[database("postgres_rb")]
|
||||||
pub struct RbDbConn(diesel::PgConnection);
|
pub struct RbDbConn(diesel::PgConnection);
|
||||||
|
|
||||||
|
|
|
@ -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?)
|
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
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
|
@ -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?)
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
4
test.py
4
test.py
|
@ -12,8 +12,8 @@ data = {
|
||||||
"shortname": "short",
|
"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.content)
|
||||||
print(r.status_code)
|
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())
|
print(r.json())
|
||||||
|
|
Reference in New Issue