testing user list endpoint

new macro to shorten test code
This commit is contained in:
shockrah 2020-03-17 23:23:52 -07:00
parent 3b3059e6d2
commit 960233b7bc
2 changed files with 39 additions and 6 deletions

View File

@ -18,7 +18,7 @@ pub struct User {
}
pub const USER_OFFLINE: i32 = 1;
pub const _USER_ONLINE: i32 = 2;
pub const USER_ONLINE: i32 = 2;
pub const _USER_AWAY: i32 = 3;
pub const _USER_DO_NOT_DISTRUB: i32 = 4;

View File

@ -1,7 +1,7 @@
use chrono::Utc;
use rocket_contrib::json::{Json, JsonValue};
use crate::rand_utils::{new_user_id, new_key};
use crate::models::{User, USER_OFFLINE};
use crate::models::{User, USER_ONLINE, USER_OFFLINE};
use crate::payload::NewUserResponse;
use crate::{DBConn, schema};
use diesel::{self, prelude::*};
@ -16,7 +16,7 @@ pub fn create_new_user() -> User {
username: uname,
key: rstring,
date: Utc::now().timestamp() as u64,
status: USER_OFFLINE,
status: USER_ONLINE,
}
}
@ -53,6 +53,8 @@ pub fn remove_user(hash: u64, conn: DBConn) -> JsonValue {
}
// TODO: optional paramter where we can specify getting the offline users as well
// TODO: More optionally specify what kind of users we want online/offline/away etc.
#[get("/list")]
pub fn get_user_list(conn: DBConn) -> Json<Vec<(u64, String, i32)>> {
use schema::users::dsl::*;
@ -75,6 +77,39 @@ mod user_tests {
use rocket::local::Client;
use rocket::http::Status;
macro_rules! rocket_inst {
($func:expr) => {
rocket::ignite()
.mount("/users", routes![$func])
.attach(DBConn::fairing())
}
}
#[test]
fn generate_bs_users() {
use diesel::mysql::MysqlConnection;
// please don't ask me why this is here it just is
let conn = MysqlConnection::establish("mysql://freechat_dev:password@localhost:3306/freechat")
.unwrap();
for _i in 0..100 {
let user = create_new_user();
let _bs = diesel::insert_into(schema::users::table)
.values(&user)
.execute(&conn);
}
}
#[test]
fn get_user_list() {
let app = rocket_inst!(get_user_list);
let client = Client::new(app).expect("Bad rocket instance");
let mut response = client.get("/users/list").dispatch();
let body: String = response.body_string().unwrap();
assert_eq!(response.status(), Status::Ok);
println!("{}", body);
}
#[test]
fn user_struct() {
@ -86,9 +121,7 @@ mod user_tests {
let user = create_new_user();
println!("Client to remove: {:?}", user);
let app = rocket::ignite()
.mount("/users", routes![remove_user])
.attach(DBConn::fairing());
let app = rocket_inst!(remove_user);
let client = Client::new(app).expect("Bad rocket instance");
let mut response = client.delete(format!("/users/remove/{}", user.userid)).dispatch();