From 1a34b598db2b0cc5b62aa24a2a0880b61805fd6a Mon Sep 17 00:00:00 2001 From: finga Date: Wed, 8 Jul 2020 23:16:20 +0200 Subject: [PATCH] Fix and add rudimentary tests - Fix test of initial reset page - Add test for valid password reset URL - Add test for invalid password reset URL --- src/tests.rs | 63 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 60 insertions(+), 3 deletions(-) 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); }