Basic new user route with a sample test

Creating a payloads module as well
This commit is contained in:
shockrah
2020-02-07 14:57:44 -08:00
parent 6dd002171c
commit dd3acb5abe
4 changed files with 86 additions and 3 deletions

View File

@@ -7,6 +7,7 @@
extern crate chrono;
extern crate dotenv;
extern crate serde;
extern crate rand;
use rocket_contrib::serve::StaticFiles;
@@ -15,9 +16,11 @@ use dotenv::dotenv;
use std::env::var;
mod website;
mod users;
mod schema;
mod invites;
mod models;
mod invites;
mod payload;
use website::*;
use invites::*;
@@ -25,7 +28,7 @@ use invites::*;
#[database("freechat_sample_db")]
pub struct DBConn(diesel::MysqlConnection);
fn rocket() -> rocket::Rocket {
pub fn rocket() -> rocket::Rocket {
rocket::ignite()
.mount("/static", StaticFiles::from("/static"))
.mount("/", routes![
@@ -35,6 +38,9 @@ fn rocket() -> rocket::Rocket {
.mount("/invite", routes![
generate_invite, use_invite
])
.mount("/user", routes![
users::create_user
])
.attach(Template::fairing())
.attach(DBConn::fairing())
}

14
server/src/payload.rs Normal file
View File

@@ -0,0 +1,14 @@
/* Module containg various structure which we use to pass back
* and forth from client/server as auth tokens
*/
// This structure allows us to provide some critical data for the client to reconnect to
// the server without having to go through a sign in process everytime
#[derive(Serialize)]
pub struct NewUserResponse {
pub userid: u64,
pub token: String,
pub username: String,
pub email: Option<String>,
}

55
server/src/users.rs Normal file
View File

@@ -0,0 +1,55 @@
use rocket::request::Form;
use rocket_contrib::json::Json;
use crate::payload;
// TODO: authentication needs to get applied basically everywhere but right now its missing
#[derive(FromForm)]
pub struct NewUserForm {
pub username: String,
pub display: Option<String>,
pub email: Option<String>,
pub password: String // this part is generated for the user if an invite is used
}
#[post("/create", data = "<user_sign_up>")]
pub fn create_user(user_sign_up: Form<NewUserForm>) -> Json<payload::NewUserResponse> {
let email = match user_sign_up.email.clone() {
Some(val) => {
val
}
None => {
"None".to_string()
}
};
// Constructing the response to the user with stuff they'll need to reconnect to the server
let user_auth = payload::NewUserResponse {
userid: 1,
token: "random token".to_string(),
username: user_sign_up.username.clone(),
email: Some(email),
};
Json(user_auth)
}
#[cfg(test)]
mod user_tests {
use super::*;
use rocket;
use rocket::local::Client;
use rocket::http::{Status, ContentType};
#[test]
fn new_user() {
let rocket = rocket::ignite()
.mount("/user", routes![create_user]);
let client = Client::new(rocket).expect("Invalid rocket instance");
let response = client.post("/user/create")
.header(ContentType::Form)
.body("username=testuser&password=testpass")
.dispatch();
assert_eq!(response.status(), Status::Ok);
}
}