From 751b947befee2e13de532e1bb3248e91bc168ccf Mon Sep 17 00:00:00 2001 From: shockrah Date: Tue, 17 Nov 2020 00:08:13 -0800 Subject: [PATCH] db/src/channels.rs/ : Clerical error fix in Response::Other message src/channels.rs/ : simple log of sql error to stderr main.rs : swapped secret and name parameters as they were backwards somehow (tfw cant type) src/messages.sr : more clerical shit(mispelled parameter name) and logging sql to stderr --- server-api/db/src/channels.rs | 3 +- server-api/src/channels.rs | 89 ++--------------------------------- server-api/src/main.rs | 2 +- server-api/src/messages.rs | 6 +-- 4 files changed, 10 insertions(+), 90 deletions(-) diff --git a/server-api/db/src/channels.rs b/server-api/db/src/channels.rs index fe17f8c..e2f8de6 100644 --- a/server-api/db/src/channels.rs +++ b/server-api/db/src/channels.rs @@ -186,7 +186,8 @@ impl Channel { Err(_) => Response::Empty }; } - return Response::Other("Could fetch new channel".into()); + // TODO: change this to return Result<> which should help with ambiguity + return Response::Other("Could not fetch new channel".into()); } return Response::Other(no_conn!("db::channels::add")) } diff --git a/server-api/src/channels.rs b/server-api/src/channels.rs index f72d536..d5be622 100644 --- a/server-api/src/channels.rs +++ b/server-api/src/channels.rs @@ -59,7 +59,10 @@ pub async fn create_channel(pool: &Pool, response: &mut Response, params: }, db::Response::Empty => *response.status_mut() = StatusCode::NOT_FOUND, // TODO: loggin - db::Response::Other(_msg) => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR, + db::Response::Other(msg) => { + *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; + eprintln!("\t{}", msg); + } _ => *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR } }, @@ -88,87 +91,3 @@ pub async fn delete_channel(pool: &Pool, response: &mut Response, params: } } - -#[cfg(test)] -mod channels_tests { - use crate::testing::{get_pool, hyper_resp}; - use serde_json::Value; - use hyper::StatusCode; - const DUMMY_TRANSIENT_CHANNEL: &'static str = "sample channel"; - - #[tokio::test] - async fn list_all_channels_good() { - // Generation of data - let p = get_pool(); - let mut resp = hyper_resp(); - // @params: none - // Collection of data - super::list_channels(&p, &mut resp).await; - - // Analysis - assert_eq!(StatusCode::OK, resp.status()); - println!("list_all_channels_good : \t{:?}", resp.body()); - let _ = p.disconnect().await; - } - - #[tokio::test] - async fn delete_and_create_channel_good() { - use chrono::Utc; - let p = get_pool(); - let mut resp = hyper_resp(); - // @params: name + kind + [description] - let cname_id = Utc::now(); - let params: Value = serde_json::from_str(&format!(r#" - {{ - "name": "{}-{}", - "kind": 2, - "description": "some random bs" - }} - "#, DUMMY_TRANSIENT_CHANNEL, cname_id)).unwrap(); - - super::create_channel(&p, &mut resp, params).await; - - println!("CREATE CHANNEL: {:?}", resp.body()); - assert_eq!(StatusCode::OK, resp.status()); - - - // clean up and hopefully delete the channel properly - let mut resp_delete = hyper_resp(); - let params_delete: Value = serde_json::from_str(&format!(r#" - {{ - "name": "{}-{}" - }} - "#, DUMMY_TRANSIENT_CHANNEL, cname_id)).unwrap(); - println!("Parameters: {}", params_delete); - super::delete_channel(&p, &mut resp_delete, params_delete).await; - - println!("Body: {:?}", resp.body()); - let _ = p.disconnect().await; - } - - #[tokio::test] - async fn delete_channel_missing_name() { - let p = get_pool(); - let mut resp = hyper_resp(); - // this endpoint is super lenient for some reason btw - let param: Value = serde_json::from_str("{}").expect("JSON is not written correctly"); - - super::delete_channel(&p, &mut resp, param).await; - - assert_eq!(StatusCode::BAD_REQUEST, resp.status()); - } - - #[tokio::test] - async fn delet_channel_non_real_channel() { - let p = get_pool(); - let mut resp = hyper_resp(); - // this endpoint is super lenient for some reason btw - let param: Value = serde_json::from_str(r#"{ - "name": "this channel doesn't exist" - }"#).expect("JSON is not written correctly"); - - super::delete_channel(&p, &mut resp, param).await; - - assert_eq!(StatusCode::OK, resp.status()); - } -} diff --git a/server-api/src/main.rs b/server-api/src/main.rs index d87ea2b..ae08c46 100644 --- a/server-api/src/main.rs +++ b/server-api/src/main.rs @@ -210,7 +210,7 @@ OPTIONS: let owner_secret = auth::generate_secret(); // creation of owner should just dump straight to stdout since this fires // from a commandline parameter anyway - match db::member::Member::add(&p, &owner_secret, "owner sama uwu", perms::OWNER).await { + match db::member::Member::add(&p, "owner sama uwu", &owner_secret, perms::OWNER).await { Ok(response) => { match response { db::Response::Row(owner) => diff --git a/server-api/src/messages.rs b/server-api/src/messages.rs index 55410d6..d66bc90 100644 --- a/server-api/src/messages.rs +++ b/server-api/src/messages.rs @@ -12,16 +12,16 @@ pub async fn send_message(pool: &Pool, response: &mut Response, params: Va let author = params.get("id") .unwrap().as_u64().unwrap(); - match (params.get("conetnt") , params.get("channel")) { + match (params.get("content") , params.get("channel")) { (Some(content_v), Some(channel_id_v)) => { let (content, channel) = (content_v.as_str(), channel_id_v.as_u64()); if let (Some(message), Some(cid)) = (content, channel) { // call returns empty on sucess so we don't need to do anything // TODO: loggin - if let Err(_issue) = db::messages::Message::send(pool, message, cid, author).await { + if let Err(issue) = db::messages::Message::send(pool, message, cid, author).await { *response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR; - // log(send) + eprintln!("\t{}", issue); } // if there's no issue then we don't need to do anything at all }