tests: Reorganize the directory

zj
Oct 12, 2021, 8:05 PM
NO2FEJLRL533XIWZA5ZY67GVDM42K4L5SCO5WI3PX3UTC52WN46QC

Dependencies

  • [2] Z63HIZPS testing: Move tests to specific directory Tests I didn't really write until today, as I mostly didn't really know how to set it up. This is now partially mitigated, just by forcing myself to do it. There's a few problems still in the code; the database is shared with the dev application for instance. Though as a start I'll take it.

Change contents

  • file addition: user_model_tests.rs (----------)
    [2.17]
    mod common;
    pub use common::*;
    use nidobyte::database::Database;
    use nidobyte::models::users::User;
    #[tokio::test]
    async fn user_from_basic_auth_test() {
    let client = common::testing_client().await;
    let mut req = client.get("/");
    let db: &rocket::State<Database> = req.guard().await.unwrap();
    let created_user = common::user(db).await;
    let credentials =
    http_auth_basic::Credentials::new(&created_user.name.clone(), common::TEST_USER_PASSWORD);
    req.add_header(rocket::http::Header::new(
    "Authorization",
    credentials.as_http_header(),
    ));
    use rocket::request::FromRequest;
    let user = User::from_request(&req).await;
    assert_eq!(created_user.id, user.unwrap().id);
    }
  • file addition: common (d--r------)
    [2.17]
  • file addition: mod.rs (----------)
    [0.799]
    pub async fn testing_client() -> rocket::local::asynchronous::Client {
    use rocket::local::asynchronous::Client;
    Client::tracked(nidobyte::rocket())
    .await
    .expect("cannot create rocket testing instance")
    }
    pub static TEST_USER_PASSWORD: &str = "password";
    use nidobyte::{database::Database, models::users::User};
    pub async fn user(db: &rocket::State<Database>) -> nidobyte::models::users::User {
    use rand::Rng;
    const CHARSET: &[u8] = b"abcdefghijklmnopqrstuvwxyz0123456789";
    let mut rng = rand::thread_rng();
    let username: String = (0..12)
    .map(|_| {
    let idx = rng.gen_range(0..CHARSET.len());
    CHARSET[idx] as char
    })
    .collect();
    User::create(
    db,
    username.clone(),
    TEST_USER_PASSWORD.to_string(),
    format!("{}@nidobyte.test", username.clone()),
    )
    .await
    .unwrap()
    }