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();
|
||||
|
||||
|
||||
Status::Accepted
|
||||
Status::Ok
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod auth_tests {
|
||||
use crate::invites::static_rocket_route_info_for_use_invite;
|
||||
use crate::schema;
|
||||
use crate::models::{Invite};
|
||||
use crate::{
|
||||
invites::static_rocket_route_info_for_use_invite,
|
||||
schema,
|
||||
models::Invite,
|
||||
utils::encode_param
|
||||
};
|
||||
use super::*;
|
||||
use rocket::{self, local::Client};
|
||||
use rocket::{
|
||||
self, local::Client, http::ContentType
|
||||
};
|
||||
use diesel::mysql::MysqlConnection;
|
||||
use chrono::{Duration, Utc};
|
||||
use rand::random;
|
||||
@ -202,6 +207,10 @@ mod auth_tests {
|
||||
Err(e) => panic!("`.env` could not be loaded: {:?}", e)
|
||||
}
|
||||
}
|
||||
fn mysql_conn() -> MysqlConnection {
|
||||
MysqlConnection::establish(&env::var("DATABASE_URL").unwrap())
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn feed_n_leave() {
|
||||
@ -213,15 +222,17 @@ mod auth_tests {
|
||||
}
|
||||
let app = rocket::ignite()
|
||||
.mount("/invite", routes![use_invite])
|
||||
.mount("/auth", routes![leave])
|
||||
.attach(DBConn::fairing());
|
||||
|
||||
// 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 invite = Invite {
|
||||
id: random::<u64>(),
|
||||
uses: 1,
|
||||
expires: dt.timestamp() as u64,
|
||||
};
|
||||
id: random::<u64>(),
|
||||
uses: 1,
|
||||
expires: dt.timestamp() as u64,
|
||||
};
|
||||
let _ = diesel::insert_into(schema::invites::table)
|
||||
.values(&invite)
|
||||
.execute(&conn);
|
||||
@ -232,13 +243,40 @@ mod auth_tests {
|
||||
let body: String = response.body_string().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);
|
||||
let leave_response = rocket_c.get("/auth/leave")
|
||||
let leave_response = rocket_c.post("/auth/leave")
|
||||
.body(body_params)
|
||||
.header(ContentType::Form)
|
||||
.dispatch();
|
||||
|
||||
assert_eq!(leave_response.status(), Status::Ok);
|
||||
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