2016-10-17 22:14:57 +00:00
|
|
|
use super::rocket;
|
|
|
|
use rocket::testing::MockRequest;
|
|
|
|
use rocket::http::Method::*;
|
2016-12-16 04:53:54 +00:00
|
|
|
use rocket::http::{ContentType, Status};
|
2016-10-17 22:14:57 +00:00
|
|
|
|
2016-12-16 04:53:54 +00:00
|
|
|
fn test_login(username: &str, password: &str, age: isize, status: Status,
|
|
|
|
body: Option<&'static str>) {
|
2016-10-17 22:14:57 +00:00
|
|
|
let rocket = rocket::ignite().mount("/", routes![super::user_page, super::login]);
|
2016-12-16 04:53:54 +00:00
|
|
|
let mut req = MockRequest::new(Post, "/login")
|
2016-12-16 00:34:19 +00:00
|
|
|
.header(ContentType::Form)
|
2016-12-16 04:53:54 +00:00
|
|
|
.body(&format!("username={}&password={}&age={}", username, password, age));
|
|
|
|
|
|
|
|
let mut response = req.dispatch_with(&rocket);
|
|
|
|
let body_str = response.body().and_then(|body| body.to_string());
|
|
|
|
|
|
|
|
println!("Checking: {:?}/{:?}", username, password);
|
|
|
|
assert_eq!(response.status(), status);
|
|
|
|
|
|
|
|
if let Some(string) = body {
|
|
|
|
assert!(body_str.map_or(true, |s| s.contains(string)));
|
|
|
|
}
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_good_login() {
|
2016-12-16 04:53:54 +00:00
|
|
|
test_login("Sergio", "password", 30, Status::SeeOther, None);
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
|
2016-12-16 04:53:54 +00:00
|
|
|
const OK: Status = self::Status::Ok;
|
|
|
|
|
2016-10-17 22:14:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_bad_login() {
|
2016-12-16 04:53:54 +00:00
|
|
|
test_login("Sergio", "password", 20, OK, Some("Sorry, 20 is too young!"));
|
|
|
|
test_login("Sergio", "password", 200, OK, Some("Are you sure you're 200?"));
|
|
|
|
test_login("Sergio", "jk", -100, OK, Some("'-100' is not a valid integer."));
|
|
|
|
test_login("Sergio", "ok", 30, OK, Some("Wrong password!"));
|
|
|
|
test_login("Mike", "password", 30, OK, Some("Unrecognized user, 'Mike'."));
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_bad_form() {
|
2016-12-16 04:53:54 +00:00
|
|
|
// Mess with the form formatting.
|
|
|
|
test_login("Sergio&other=blah&", "password", 0, Status::BadRequest, None);
|
|
|
|
test_login("&&&===&", "password", 0, Status::BadRequest, None);
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|