ldap0r/src/tests.rs

79 lines
2.5 KiB
Rust

use rocket::{self, http::Status, local::Client, routes};
use rocket_contrib::templates::Template;
#[test]
fn index() {
let rocket = rocket::ignite().mount("/", routes![super::index, super::reset]);
let client = Client::new(rocket).unwrap();
let mut response = client.get("/").dispatch();
assert_eq!(response.status().code, 303);
assert!(response.body().is_none());
for h in response.headers().iter() {
match h.name.as_str() {
"Location" => assert_eq!(h.value, "/reset"),
"Content-Length" => assert_eq!(h.value.parse::<i32>().unwrap(), 0),
_ => { /* let these through */ }
}
}
}
#[test]
fn 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();
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::Mutex;
let mut context = HashMap::new();
context.insert(
"key",
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
);
let rocket = rocket::ignite()
.mount("/", routes![super::reset_key])
.attach(Template::fairing())
.manage(Keys::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::Mutex;
let rocket = rocket::ignite()
.mount("/", routes![super::reset, super::reset_key])
.manage(Keys::new(Mutex::new(HashMap::new())));
let client = Client::new(rocket).unwrap();
let response = client
.get("/reset/0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz")
.dispatch();
assert_eq!(response.status(), Status::NotFound);
}