test::auth::mysql_conn helper function for mysql connections
test::auth::feed_n_leave added form header and cleaned up post parameters test::auth::dummy_leave standalone test for route => /auth/leave
This commit is contained in:
parent
f12048b49b
commit
ab4fe70081
@ -179,16 +179,21 @@ pub fn leave(conn: DBConn, api_key: Form<AuthKey>) -> Status {
|
|||||||
.execute(&conn.0).unwrap();
|
.execute(&conn.0).unwrap();
|
||||||
|
|
||||||
|
|
||||||
Status::Accepted
|
Status::Ok
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod auth_tests {
|
mod auth_tests {
|
||||||
use crate::invites::static_rocket_route_info_for_use_invite;
|
use crate::{
|
||||||
use crate::schema;
|
invites::static_rocket_route_info_for_use_invite,
|
||||||
use crate::models::{Invite};
|
schema,
|
||||||
|
models::Invite,
|
||||||
|
utils::encode_param
|
||||||
|
};
|
||||||
use super::*;
|
use super::*;
|
||||||
use rocket::{self, local::Client};
|
use rocket::{
|
||||||
|
self, local::Client, http::ContentType
|
||||||
|
};
|
||||||
use diesel::mysql::MysqlConnection;
|
use diesel::mysql::MysqlConnection;
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
use rand::random;
|
use rand::random;
|
||||||
@ -202,6 +207,10 @@ mod auth_tests {
|
|||||||
Err(e) => panic!("`.env` could not be loaded: {:?}", e)
|
Err(e) => panic!("`.env` could not be loaded: {:?}", e)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
fn mysql_conn() -> MysqlConnection {
|
||||||
|
MysqlConnection::establish(&env::var("DATABASE_URL").unwrap())
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn feed_n_leave() {
|
fn feed_n_leave() {
|
||||||
@ -213,9 +222,11 @@ mod auth_tests {
|
|||||||
}
|
}
|
||||||
let app = rocket::ignite()
|
let app = rocket::ignite()
|
||||||
.mount("/invite", routes![use_invite])
|
.mount("/invite", routes![use_invite])
|
||||||
|
.mount("/auth", routes![leave])
|
||||||
.attach(DBConn::fairing());
|
.attach(DBConn::fairing());
|
||||||
|
|
||||||
// First we create a new invite
|
// First we create a new invite
|
||||||
let conn = MysqlConnection::establish(&env::var("DATABASE_URL").unwrap()).unwrap();
|
let conn = mysql_conn();
|
||||||
let dt = Utc::now() + Duration::minutes(30);
|
let dt = Utc::now() + Duration::minutes(30);
|
||||||
let invite = Invite {
|
let invite = Invite {
|
||||||
id: random::<u64>(),
|
id: random::<u64>(),
|
||||||
@ -232,13 +243,40 @@ mod auth_tests {
|
|||||||
let body: String = response.body_string().unwrap();
|
let body: String = response.body_string().unwrap();
|
||||||
let api_key: Value = serde_json::from_str(&body).unwrap();
|
let api_key: Value = serde_json::from_str(&body).unwrap();
|
||||||
|
|
||||||
let body_params = format!("id={}&secret={}", api_key["id"], api_key["secret"]);
|
// Go about leaving the server
|
||||||
|
let secret_str = encode_param(&format!("{}", api_key["secret"]));
|
||||||
|
let body_params = format!("id={}&secret={}", api_key["id"], secret_str);
|
||||||
println!("Parameters being sent {}", body_params);
|
println!("Parameters being sent {}", body_params);
|
||||||
let leave_response = rocket_c.get("/auth/leave")
|
let leave_response = rocket_c.post("/auth/leave")
|
||||||
.body(body_params)
|
.body(body_params)
|
||||||
|
.header(ContentType::Form)
|
||||||
.dispatch();
|
.dispatch();
|
||||||
|
|
||||||
assert_eq!(leave_response.status(), Status::Ok);
|
assert_eq!(leave_response.status(), Status::Ok);
|
||||||
println!("{}", body);
|
println!("{}", body);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn dummy_leave() {
|
||||||
|
/*
|
||||||
|
* Naive test for the /auth/leave route
|
||||||
|
*/
|
||||||
|
setup_dotenv().unwrap();
|
||||||
|
let app = rocket::ignite()
|
||||||
|
.mount("/auth", routes![leave])
|
||||||
|
.attach(DBConn::fairing());
|
||||||
|
|
||||||
|
let rocket_client = Client::new(app).expect("asdf");
|
||||||
|
// Some dummy parameters as the /auth/leave route only has one type of response
|
||||||
|
let id = 12345;
|
||||||
|
let secret = encode_param("raw: &str");
|
||||||
|
let params = format!("id={}&secret={}", id, secret);
|
||||||
|
println!("Parameters posted to /auth/leave: {}", params);
|
||||||
|
let response = rocket_client.post("/auth/leave")
|
||||||
|
.body(params)
|
||||||
|
.header(ContentType::Form)
|
||||||
|
.dispatch();
|
||||||
|
|
||||||
|
assert_eq!(response.status(), Status::Ok);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user