diff --git a/src/tests.rs b/src/tests.rs index 50039d7..f5d0b0c 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1,4 +1,5 @@ -use rocket::{self, local::Client, routes}; +use rocket::{self, http::Status, local::Client, routes}; +use rocket_contrib::templates::Template; #[test] fn index() { @@ -18,8 +19,64 @@ fn index() { #[test] fn reset() { - let rocket = rocket::ignite().mount("/", routes![super::reset]); + let rocket = rocket::ignite() + .mount("/", routes![super::reset]) + .attach(Template::fairing()); let client = Client::new(rocket).unwrap(); let mut response = client.get("/reset").dispatch(); - assert_eq!(response.body_string(), Some("Reset ldap0r!".into())); + let expected = Template::show(client.rocket(), "reset", {}).unwrap(); + assert_eq!(response.status(), Status::Ok); + assert_eq!(response.body_string(), Some(expected)); +} + +#[test] +fn reset_valid_url() { + use super::Keys; + use std::collections::HashMap; + use std::sync::{Arc, Mutex}; + + let mut context = HashMap::new(); + context.insert( + "key", + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", + ); + + let rocket = rocket::ignite() + .mount("/", routes![super::reset_key]) + .attach(Template::fairing()) + .manage(Keys { + keys: Arc::new(Mutex::new( + Some(( + "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".to_string(), + "foobar".to_string(), + )) + .into_iter() + .collect(), + )), + }); + let client = Client::new(rocket).unwrap(); + let mut response = client + .get("/reset/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") + .dispatch(); + let expected = Template::show(client.rocket(), "reset_key", &context).unwrap(); + assert_eq!(response.status(), Status::Ok); + assert_eq!(response.body_string(), Some(expected)); +} + +#[test] +fn reset_invalid_url() { + use super::Keys; + use std::collections::HashMap; + use std::sync::{Arc, Mutex}; + + let rocket = rocket::ignite() + .mount("/", routes![super::reset, super::reset_key]) + .manage(Keys { + keys: Arc::new(Mutex::new(HashMap::new())), + }); + let client = Client::new(rocket).unwrap(); + let response = client + .get("/reset/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") + .dispatch(); + assert_eq!(response.status(), Status::NotFound); }