2016-10-17 22:14:57 +00:00
|
|
|
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
|
|
|
|
2017-02-02 10:16:21 +00:00
|
|
|
use super::rocket;
|
|
|
|
|
2016-12-16 04:53:54 +00:00
|
|
|
fn test_login(username: &str, password: &str, age: isize, status: Status,
|
|
|
|
body: Option<&'static str>) {
|
2017-02-02 10:16:21 +00:00
|
|
|
let rocket = rocket();
|
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);
|
2017-04-14 08:59:28 +00:00
|
|
|
let body_str = response.body_string();
|
2016-12-16 04:53:54 +00:00
|
|
|
|
2016-12-30 05:57:37 +00:00
|
|
|
println!("Checking: {:?}/{:?}/{:?}/{:?}", username, password, age, body_str);
|
2016-12-16 04:53:54 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-02-02 02:35:54 +00:00
|
|
|
fn check_bad_form(form_str: &str, status: Status) {
|
2017-02-02 10:16:21 +00:00
|
|
|
let rocket = rocket();
|
2017-02-02 02:35:54 +00:00
|
|
|
let mut req = MockRequest::new(Post, "/login")
|
|
|
|
.header(ContentType::Form)
|
|
|
|
.body(form_str);
|
|
|
|
|
2017-02-02 10:16:21 +00:00
|
|
|
let response = req.dispatch_with(&rocket);
|
2017-02-02 02:35:54 +00:00
|
|
|
assert_eq!(response.status(), status);
|
|
|
|
}
|
|
|
|
|
2016-10-17 22:14:57 +00:00
|
|
|
#[test]
|
|
|
|
fn test_bad_form() {
|
2017-02-02 02:35:54 +00:00
|
|
|
check_bad_form("&", Status::BadRequest);
|
|
|
|
check_bad_form("=", Status::BadRequest);
|
|
|
|
check_bad_form("&&&===&", Status::BadRequest);
|
|
|
|
|
|
|
|
check_bad_form("username=Sergio", Status::UnprocessableEntity);
|
|
|
|
check_bad_form("username=Sergio&", Status::UnprocessableEntity);
|
|
|
|
check_bad_form("username=Sergio&pass=something", Status::UnprocessableEntity);
|
|
|
|
check_bad_form("user=Sergio&password=something", Status::UnprocessableEntity);
|
|
|
|
check_bad_form("password=something", Status::UnprocessableEntity);
|
2016-10-17 22:14:57 +00:00
|
|
|
}
|