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
This commit is contained in:
finga 2020-07-08 23:16:20 +02:00
parent f737bc589d
commit 1a34b598db

View file

@ -1,4 +1,5 @@
use rocket::{self, local::Client, routes}; use rocket::{self, http::Status, local::Client, routes};
use rocket_contrib::templates::Template;
#[test] #[test]
fn index() { fn index() {
@ -18,8 +19,64 @@ fn index() {
#[test] #[test]
fn reset() { 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 client = Client::new(rocket).unwrap();
let mut response = client.get("/reset").dispatch(); 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);
} }