fixed channel creation/deletion test so it behaves properlu, all new tests passing atm
This commit is contained in:
parent
0280ae09ae
commit
ced0965060
@ -8,6 +8,7 @@ use mysql_async::prelude::{params, Queryable};
|
|||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
use crate::db_types::{UBigInt, VarChar, Integer};
|
use crate::db_types::{UBigInt, VarChar, Integer};
|
||||||
|
use crate::common;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum ChannelType {
|
pub enum ChannelType {
|
||||||
@ -164,9 +165,10 @@ pub async fn create_channel(pool: &Pool, response: &mut Response<Body>, params:
|
|||||||
match insert_channel(pool, name, desc, kind).await {
|
match insert_channel(pool, name, desc, kind).await {
|
||||||
// Server Errors are generally _ok_ to reveal in body I suppose
|
// Server Errors are generally _ok_ to reveal in body I suppose
|
||||||
Err(Error::Server(se)) => {
|
Err(Error::Server(se)) => {
|
||||||
*response.status_mut() = StatusCode::BAD_REQUEST;
|
common::db_err_response_body(response, se);
|
||||||
let b = format!("Server code: {}\nServer Message:{}", se.code, se.message);
|
//*response.status_mut() = StatusCode::BAD_REQUEST;
|
||||||
*response.body_mut() = Body::from(b);
|
//let b = format!("Server code: {}\nServer Message:{}", se.code, se.message);
|
||||||
|
//*response.body_mut() = Body::from(b);
|
||||||
},
|
},
|
||||||
// generic errors get a 500
|
// generic errors get a 500
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
@ -193,7 +195,7 @@ pub async fn delete_channel(pool: &Pool, response: &mut Response<Body>, params:
|
|||||||
match db_delete_channel(pool, name).await {
|
match db_delete_channel(pool, name).await {
|
||||||
Ok(_) => *response.status_mut() = StatusCode::OK,
|
Ok(_) => *response.status_mut() = StatusCode::OK,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
println!("delete_chanel sql error :\n{}", e);
|
*response.body_mut() = Body::from(format!("delete_chanel sql error :\n{}", e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -208,6 +210,7 @@ mod channels_tests {
|
|||||||
use crate::testing::{get_pool, hyper_resp};
|
use crate::testing::{get_pool, hyper_resp};
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
use hyper::StatusCode;
|
use hyper::StatusCode;
|
||||||
|
const DUMMY_TRANSIENT_CHANNEL: &'static str = "sample channel";
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn list_all_channels_good() {
|
async fn list_all_channels_good() {
|
||||||
@ -225,22 +228,37 @@ mod channels_tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn create_channel_good() {
|
async fn delete_and_create_channel_good() {
|
||||||
|
use chrono::Utc;
|
||||||
let p = get_pool();
|
let p = get_pool();
|
||||||
let mut resp = hyper_resp();
|
let mut resp = hyper_resp();
|
||||||
// @params: name + kind + [description]
|
// @params: name + kind + [description]
|
||||||
let params: Value = serde_json::from_str(r#"
|
let cname_id = Utc::now();
|
||||||
{
|
let params: Value = serde_json::from_str(&format!(r#"
|
||||||
"name": "sample channel",
|
{{
|
||||||
|
"name": "{}-{}",
|
||||||
"kind": 2,
|
"kind": 2,
|
||||||
"description": "some random bs"
|
"description": "some random bs"
|
||||||
}
|
}}
|
||||||
"#).unwrap();
|
"#, DUMMY_TRANSIENT_CHANNEL, cname_id)).unwrap();
|
||||||
|
|
||||||
super::create_channel(&p, &mut resp, params).await;
|
super::create_channel(&p, &mut resp, params).await;
|
||||||
|
|
||||||
// hopefully we 200
|
println!("CREATE CHANNEL: {:?}", resp.body());
|
||||||
assert_eq!(StatusCode::OK, resp.status());
|
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;
|
let _ = p.disconnect().await;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
38
server/src/common.rs
Normal file
38
server/src/common.rs
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
use mysql_async::error::ServerError;
|
||||||
|
use hyper::{Body, Response, StatusCode};
|
||||||
|
use serde::Serialize;
|
||||||
|
use serde_json::to_string as json;
|
||||||
|
|
||||||
|
#[derive(Serialize)]
|
||||||
|
struct GenericErrData {
|
||||||
|
status: u16,
|
||||||
|
message: &'static str,
|
||||||
|
note: &'static str,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
pub fn db_err_response_body(response: &mut Response<Body>, err: ServerError) {
|
||||||
|
*response.status_mut() = StatusCode::INTERNAL_SERVER_ERROR;
|
||||||
|
match err.code {
|
||||||
|
// Duplicate unique value was (tried to be) inserted
|
||||||
|
1062 => {
|
||||||
|
|
||||||
|
let s = json(&GenericErrData {
|
||||||
|
status: 1062,
|
||||||
|
message: "Duplicate key was inserted and failed",
|
||||||
|
note: "Check parameters given"
|
||||||
|
}).unwrap();
|
||||||
|
*response.body_mut() = Body::from(s);
|
||||||
|
},
|
||||||
|
// Generic errors
|
||||||
|
_ => {
|
||||||
|
let s = json(&GenericErrData{
|
||||||
|
status: 500,
|
||||||
|
message: "Generic Backend error",
|
||||||
|
note:""
|
||||||
|
}).unwrap();
|
||||||
|
*response.body_mut() = Body::from(s);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -22,21 +22,21 @@ use mysql_async::Pool;
|
|||||||
|
|
||||||
use dotenv::dotenv;
|
use dotenv::dotenv;
|
||||||
use clap::{Arg, App};
|
use clap::{Arg, App};
|
||||||
|
|
||||||
mod testing;
|
|
||||||
mod auth;
|
|
||||||
use auth::AuthReason;
|
use auth::AuthReason;
|
||||||
|
|
||||||
|
mod auth;
|
||||||
|
|
||||||
mod routes;
|
mod routes;
|
||||||
mod invites;
|
mod invites;
|
||||||
mod channels;
|
mod channels;
|
||||||
|
|
||||||
mod members;
|
mod members;
|
||||||
|
|
||||||
mod messages;
|
|
||||||
mod http_params;
|
|
||||||
mod perms;
|
mod perms;
|
||||||
|
mod messages;
|
||||||
|
|
||||||
|
mod http_params;
|
||||||
mod db_types;
|
mod db_types;
|
||||||
|
mod common;
|
||||||
|
mod testing;
|
||||||
|
|
||||||
const NO_ERR: u16 = 0;
|
const NO_ERR: u16 = 0;
|
||||||
const CONFIG_ERR: u16 = 1;
|
const CONFIG_ERR: u16 = 1;
|
||||||
|
Loading…
Reference in New Issue
Block a user