Verified that the join function is working as intended

This commit is contained in:
shockrah 2020-05-07 22:14:44 -07:00
parent cd41dc64c3
commit 4703116e32

View File

@ -112,18 +112,49 @@ pub fn leave(conn: DBConn, api_key: Form<AuthKey>) -> Status {
#[cfg(test)]
mod auth_tests {
use crate::invites::static_rocket_route_info_for_use_invite;
use crate::schema;
use crate::models::{Invite};
use super::*;
use rocket;
use rocket::{self, local::Client};
use diesel::mysql::MysqlConnection;
use chrono::{Duration, Utc};
use rand::random;
use std::env;
use dotenv::dotenv;
fn setup_dotenv() -> Result<(), i32> {
match dotenv() {
Ok(_) => Ok(()),
Err(e) => panic!("`.env` could not be loaded: {:?}", e)
}
}
#[test]
fn feed_n_leave() {
// Create an invite in our db manually
// Use that invite to join
// Then leave using our neato /auth/leave route
if let Err(_denv) = setup_dotenv() {
panic!("env failed fukc")
}
let app = rocket::ignite()
.mount("/auth", routes![crate::invites::use_invite, leave])
.attach(super::DBConn::fairing());
.mount("/invite", routes![use_invite])
.attach(DBConn::fairing());
let conn = MysqlConnection::establish(&env::var("DATABASE_URL").unwrap()).unwrap();
let dt = Utc::now() + Duration::minutes(30);
let invite = Invite {
id: random::<u64>(),
uses: 1,
expires: dt.timestamp() as u64,
};
let _ = diesel::insert_into(schema::invites::table)
.values(&invite)
.execute(&conn);
let conn = MysqlConnection::establish("mysql://freechat_dev:password@localhost:3306/freechat");
// use our new invite to "join" the server
let rocket_c = Client::new(app).expect("Invalid rocket instance");
let mut response = rocket_c.get(format!("/invite/join/{}/{}", invite.id, "billybob")).dispatch();
let body: String = response.body_string().unwrap();
println!("{}", body)
}
}