invite generation cleaned and working
added a basic test for the fixed invite generation
This commit is contained in:
parent
58fcb99e03
commit
62fd63d3b5
@ -1,10 +1,11 @@
|
|||||||
// Module handles creating invites for potentially new users
|
// Module handles creating invites for potentially new users
|
||||||
use diesel::{self, prelude::*};
|
use diesel::{self, prelude::*};
|
||||||
|
use rocket_contrib::json::Json;
|
||||||
use rand::random;
|
use rand::random;
|
||||||
use chrono::{Duration, Utc};
|
use chrono::{Duration, Utc};
|
||||||
use crate::DBConn;
|
use crate::DBConn;
|
||||||
use crate::models::Invite;
|
use crate::models::Invite;
|
||||||
use crate::{schema, payload};
|
use crate::schema;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
TODO: both the generation and usage endpoints for invites need the following
|
TODO: both the generation and usage endpoints for invites need the following
|
||||||
@ -13,24 +14,29 @@ TODO: both the generation and usage endpoints for invites need the following
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#[get("/generate")]
|
#[get("/generate")]
|
||||||
pub fn generate_invite(conn: DBConn) -> Result<String, String> {
|
pub fn generate_invite(conn: DBConn) -> Json<Invite> {
|
||||||
let dt = Utc::now() + Duration::minutes(30);
|
let dt = Utc::now() + Duration::minutes(30);
|
||||||
let invite = Invite {
|
let mut new_invite = Invite {
|
||||||
id: random::<u64>(),
|
id: random::<u64>(), // hopefully there won't ever be collision with this size of pool
|
||||||
uses: Some(1),
|
uses: Some(1), // default/hardcorded for now
|
||||||
expires: dt.timestamp() as u64
|
expires: dt.timestamp() as u64
|
||||||
};
|
};
|
||||||
let id = invite.id;
|
// Next we cache this invite
|
||||||
// Insert our newly made invite into the invites table
|
let result = diesel::insert_into(schema::invites::table)
|
||||||
let _new_invite = diesel::insert_into(schema::invites::table)
|
.values(&new_invite)
|
||||||
.values(invite)
|
.execute(&conn.0);
|
||||||
.execute(&conn.0)
|
|
||||||
.map_err(|err| -> String {
|
match result {
|
||||||
println!("Could not insert new invite into table so it was not created {:?}", err);
|
Ok(val) => {
|
||||||
"Failure to create new invite".into()
|
Json(new_invite)
|
||||||
})?;
|
}
|
||||||
// Insert the new invite into our database
|
Err(e) => {
|
||||||
Ok(format!("freechat.io/invite/{}", id))
|
new_invite.id = 0;
|
||||||
|
new_invite.uses = Some(0);
|
||||||
|
new_invite.expires = 0;
|
||||||
|
Json(new_invite)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/<hash>")]
|
#[get("/<hash>")]
|
||||||
@ -61,3 +67,23 @@ pub fn use_invite(hash: u64, conn: DBConn) -> Result<String, String> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod invite_tests {
|
||||||
|
use super::*;
|
||||||
|
use rocket;
|
||||||
|
use rocket::local::Client;
|
||||||
|
use rocket::http::{Status, ContentType};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn make_invite() {
|
||||||
|
let rocket = rocket::ignite()
|
||||||
|
.mount("/invite", routes![generate_invite])
|
||||||
|
.attach(DBConn::fairing());
|
||||||
|
|
||||||
|
let client = Client::new(rocket).expect("Invalid rocket instance");
|
||||||
|
let mut response = client.get("/invite/generate").dispatch();
|
||||||
|
|
||||||
|
println!("Response looks like: {}", response.body_string().unwrap());
|
||||||
|
assert_eq!(response.status(), Status::Ok);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -16,25 +16,19 @@ use dotenv::dotenv;
|
|||||||
use std::env::var;
|
use std::env::var;
|
||||||
|
|
||||||
mod website;
|
mod website;
|
||||||
mod users;
|
|
||||||
mod schema;
|
mod schema;
|
||||||
mod models;
|
mod models;
|
||||||
mod invites;
|
mod invites;
|
||||||
mod payload;
|
mod payload;
|
||||||
|
|
||||||
use website::*;
|
|
||||||
use invites::*;
|
use invites::*;
|
||||||
|
|
||||||
#[database("freechat_sample_db")]
|
#[database("freechat")]
|
||||||
pub struct DBConn(diesel::MysqlConnection);
|
pub struct DBConn(diesel::MysqlConnection);
|
||||||
|
|
||||||
pub fn rocket() -> rocket::Rocket {
|
pub fn rocket() -> rocket::Rocket {
|
||||||
rocket::ignite()
|
rocket::ignite()
|
||||||
.mount("/static", StaticFiles::from("/static"))
|
.mount("/static", StaticFiles::from("/static"))
|
||||||
.mount("/", routes![
|
|
||||||
homepage, about_page, server_info,
|
|
||||||
static_css, static_js, static_media
|
|
||||||
])
|
|
||||||
.mount("/invite", routes![
|
.mount("/invite", routes![
|
||||||
generate_invite, use_invite
|
generate_invite, use_invite
|
||||||
])
|
])
|
||||||
@ -54,49 +48,3 @@ fn main() -> Result<(), i32> {
|
|||||||
}
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Integrating some basic tests just for this module
|
|
||||||
#[cfg(test)]
|
|
||||||
mod test {
|
|
||||||
use super::rocket;
|
|
||||||
use rocket::local::Client;
|
|
||||||
use rocket::http::Status;
|
|
||||||
|
|
||||||
macro_rules! check_get {
|
|
||||||
($client:expr, $file:expr) => {
|
|
||||||
let response = $client.get($file).dispatch();
|
|
||||||
assert_eq!(response.status(), Status::Ok);
|
|
||||||
};
|
|
||||||
($client:expr, $file:expr, $code:expr) => {
|
|
||||||
let response = $client.get($file).dispatch();
|
|
||||||
assert_eq!(response.status(), $code);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
#[test]
|
|
||||||
fn pageroutes_get() {
|
|
||||||
// Just make sure that when request the home page we actually get the html back
|
|
||||||
let client = Client::new(rocket()).expect("Valid rocket instance");
|
|
||||||
check_get!(client, "/");
|
|
||||||
check_get!(client, "/about");
|
|
||||||
}
|
|
||||||
// Next we test the static resource routes
|
|
||||||
#[test]
|
|
||||||
fn static_css_get() {
|
|
||||||
let client = Client::new(rocket()).expect("Valid rocket instance");
|
|
||||||
check_get!(client, "/static/css/index.css");
|
|
||||||
check_get!(client, "/static/css/about.css");
|
|
||||||
}
|
|
||||||
|
|
||||||
// this one is meant to fail for now because we don't have any js to serve
|
|
||||||
#[test]
|
|
||||||
fn static_media_get() {
|
|
||||||
let client = Client::new(rocket()).expect("Valid rocket instance");
|
|
||||||
check_get!(client, "/static/media/favicon.png");
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn static_js_get() {
|
|
||||||
let client = Client::new(rocket()).expect("Valid rocket instance");
|
|
||||||
check_get!(client, "/static/js/jquery.js", Status::NotFound);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
@ -7,7 +7,9 @@
|
|||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
pub struct NewUserResponse {
|
pub struct NewUserResponse {
|
||||||
|
// passkey-value
|
||||||
|
// userid-value
|
||||||
pub userid: u64,
|
pub userid: u64,
|
||||||
pub username: String,
|
pub username: String,
|
||||||
pub email: String,
|
pub email: String,
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user