(port: u16, set_user: Option<(String, String, bool)>, f: F)
where F: Fn() {
use std::sync::mpsc::channel;
use std::{thread, time};
let (start_tx, start_rx) = channel();
let (stop_tx, stop_rx) = channel();
thread::spawn(move || {
let mut conn = rusqlite::Connection::open_in_memory().unwrap();
db_apply_migrations::test(&mut conn);
if let Some(user) = set_user {
let mut test_user = conn.new_session("");
test_user.username = Some(user.0);
test_user.is_teacher = user.2;
test_user.set_password(&user.1).expect("Set Password did not work correctly.");
conn.save_session(test_user);
}
// ID: 1, gets renamed
let mut contest = Contest::new("directory".to_string(),
"public.yaml".to_string(),
"RenamedContestName".to_string(),
1,
true,
None,
None,
None,
None,
None,
None,
None);
contest.save(&conn);
// ID: 1
let mut contest = Contest::new("directory".to_string(),
"public.yaml".to_string(),
"PublicContestName".to_string(),
1,
true,
None,
None,
None,
None,
None,
None,
None);
let mut taskgroup = Taskgroup::new("TaskgroupName".to_string(), None);
let task = Task::new("taskdir1".to_string(), 3); // ID: 1
taskgroup.tasks.push(task);
let task = Task::new("taskdir2".to_string(), 4); // ID: 2
taskgroup.tasks.push(task);
contest.taskgroups.push(taskgroup);
contest.save(&conn);
// ID: 2
let mut contest = Contest::new("directory".to_string(),
"private.yaml".to_string(),
"PrivateContestName".to_string(),
1,
false,
None,
None,
None,
None,
None,
None,
None);
let mut taskgroup = Taskgroup::new("TaskgroupName".to_string(), None);
let task = Task::new("taskdir1".to_string(), 3); // ID: 3
taskgroup.tasks.push(task);
let task = Task::new("taskdir2".to_string(), 4); // ID: 4
taskgroup.tasks.push(task);
contest.taskgroups.push(taskgroup);
contest.save(&conn);
// ID: 3
let mut contest = Contest::new("directory".to_string(),
"infinte.yaml".to_string(),
"InfiniteContestName".to_string(),
0,
true,
None,
None,
None,
None,
None,
None,
None);
let mut taskgroup = Taskgroup::new("TaskgroupRenameName".to_string(), None);
let task = Task::new("taskdir1".to_string(), 3); // ID: 5
taskgroup.tasks.push(task);
let task = Task::new("taskdir2".to_string(), 4); // ID: 6
taskgroup.tasks.push(task);
contest.taskgroups.push(taskgroup);
contest.save(&conn);
let mut taskgroup = Taskgroup::new("TaskgroupNewName".to_string(), None);
let task = Task::new("taskdir1".to_string(), 3); // ID: 5
taskgroup.tasks.push(task);
let task = Task::new("taskdir2".to_string(), 4); // ID: 6
taskgroup.tasks.push(task);
contest.taskgroups.push(taskgroup);
contest.save(&conn);
let mut config = config::read_config_from_file(Path::new("thisfileshoudnotexist"));
config.port = Some(port);
config.cookie_signing_secret = Some("testtesttesttesttesttesttesttest".to_string());
let mut srvr = start_server(conn, config).expect(&format!("Could not start server on port {}", port));
// Message server started
start_tx.send(()).unwrap();
// Wait for test to finish
stop_rx.recv().unwrap();
srvr.close().unwrap();
});
// Wait for server to start
start_rx.recv().unwrap();
thread::sleep(time::Duration::from_millis(100));
// Run test code
f();
// Message test finished
stop_tx.send(()).unwrap();
}
fn login(port: u16, client: &reqwest::Client, username: &str, password: &str) -> reqwest::Response {
let params = [("username", username), ("password", password)];
let resp = client.post(&format!("http://localhost:{}/login", port)).form(¶ms).send().unwrap();
resp
}
fn login_code(port: u16, client: &reqwest::Client, code: &str) -> reqwest::Response {
let params = [("code", code)];
let resp = client.post(&format!("http://localhost:{}/clogin", port)).form(¶ms).send().unwrap();
resp
}
#[test]
fn start_server_and_check_requests() {
start_server_and_fn(8080, None, || {
let mut resp = reqwest::get("http://localhost:8080").unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("Jugendwettbewerb Informatik"));
assert!(!content.contains("Error"));
assert!(!content.contains("Gruppenverwaltung"));
let mut resp = reqwest::get("http://localhost:8080/contest").unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("Wettbewerbe
"));
assert!(!content.contains("Error"));
let mut resp = reqwest::get("http://localhost:8080/group").unwrap();
let content = resp.text().unwrap();
assert!(content.contains("Login
"));
})
}
#[test]
fn check_login_wrong_credentials() {
start_server_and_fn(8081, None, || {
let client = reqwest::Client::new();
let mut resp = login(8081, &client, "nonexistingusername", "wrongpassword");
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("Login
"));
assert!(content.contains("Login fehlgeschlagen."));
assert!(!content.contains("Error"));
let mut resp = login_code(8081, &client, "g23AgaV");
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("Login
"));
assert!(content.contains("Kein gültiger Code."));
assert!(!content.contains("Error"));
let mut resp = login_code(8081, &client, "u9XuAbH7p");
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("Login
"));
assert!(content.contains("Kein gültiger Code."));
assert!(!content.contains("Error"));
})
}
#[test]
fn check_login() {
start_server_and_fn(8082, Some(("testusr".to_string(), "testpw".to_string(), false)), || {
let client = reqwest::Client::builder().cookie_store(true)
.redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let mut resp = login(8082, &client, "testusr", "testpw");
assert_eq!(resp.status(), StatusCode::FOUND);
let content = resp.text().unwrap();
assert!(!content.contains("Error"));
let mut set_cookie = resp.headers().get_all("Set-Cookie").iter();
assert!(set_cookie.next().is_some());
assert!(set_cookie.next().is_none());
let location = resp.headers().get(reqwest::header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(location, "http://localhost:8082/");
let mut resp = client.get(location).send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(!content.contains("Error"));
assert!(!content.contains("Gruppenverwaltung"));
assert!(content.contains("Eingeloggt als testusr"));
assert!(content.contains("Jugendwettbewerb Informatik"));
})
}
#[test]
fn check_logout() {
start_server_and_fn(8083, Some(("testusr".to_string(), "testpw".to_string(), false)), || {
let client = reqwest::Client::builder().cookie_store(true)
.redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let resp = login(8083, &client, "testusr", "testpw");
assert_eq!(resp.status(), StatusCode::FOUND);
let resp = client.get("http://localhost:8083/logout").send().unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let mut resp = client.get("http://localhost:8083").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("Benutzername"));
assert!(content.contains("Passwort"));
assert!(content.contains("Gruppencode / Teilnahmecode"));
assert!(content.contains("Jugendwettbewerb Informatik"));
})
}
#[test]
fn check_group_creation_and_group_code_login() {
start_server_and_fn(8084, Some(("testusr".to_string(), "testpw".to_string(), true)), || {
let client = reqwest::Client::builder().cookie_store(true)
.redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let resp = login(8084, &client, "testusr", "testpw");
assert_eq!(resp.status(), StatusCode::FOUND);
let mut resp = client.get("http://localhost:8084").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("[Lehrer]"));
assert!(content.contains("Gruppenverwaltung"));
let mut resp = client.get("http://localhost:8084/group/").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("Gruppe anlegen"));
let params = [("name", "WrongGroupname"), ("tag", "WrongMarker"), ("csrf_token", "76CfTPJaoz")];
let resp = client.post("http://localhost:8084/group/").form(¶ms).send().unwrap();
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
let pos = content.find("type=\"hidden\" name=\"csrf_token\" value=\"").expect("CSRF-Token not found");
let csrf = &content[pos + 39..pos + 49];
let params = [("name", "Groupname"), ("tag", "Marker"), ("csrf_token", csrf)];
let resp = client.post("http://localhost:8084/group/").form(¶ms).send().unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let mut resp = client.get("http://localhost:8084/group/").send().unwrap();
let content = resp.text().unwrap();
assert!(!content.contains("WrongGroupname"));
let pos = content.find("Groupname | ").expect("Group not found");
let groupcode = &content[pos + 58..pos + 65];
// New client to test group code login
let client = reqwest::Client::builder().cookie_store(true)
.redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let resp = login_code(8084, &client, groupcode);
assert_eq!(resp.status(), StatusCode::FOUND);
let mut set_cookie = resp.headers().get_all("Set-Cookie").iter();
assert!(set_cookie.next().is_some());
assert!(set_cookie.next().is_none());
let location = resp.headers().get(reqwest::header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(location, "http://localhost:8084/profile?status=firstlogin");
let mut resp = client.get(location).send().unwrap();
let content = resp.text().unwrap();
let pos = content.find("Login-Code: ").expect("Logincode not found");
let logincode = &content[pos + 15..pos + 24];
// New client to test login code login
let client = reqwest::Client::builder().cookie_store(true)
.redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let resp = login_code(8084, &client, logincode);
assert_eq!(resp.status(), StatusCode::FOUND);
let location = resp.headers().get(reqwest::header::LOCATION).unwrap().to_str().unwrap();
assert_eq!(location, "http://localhost:8084/");
let mut resp = client.get(location).send().unwrap();
let content = resp.text().unwrap();
assert!(content.contains("Eingeloggt als "));
})
}
#[test]
fn check_contest_start() {
start_server_and_fn(8085, Some(("testusr".to_string(), "testpw".to_string(), false)), || {
let client = reqwest::Client::builder().cookie_store(true)
.redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let resp = login(8085, &client, "testusr", "testpw");
assert_eq!(resp.status(), StatusCode::FOUND);
let mut resp = client.get("http://localhost:8085/contest/").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("PublicContestName"));
assert!(content.contains("InfiniteContestName"));
//assert!(content.contains("PrivateContestName"));
assert!(!content.contains("WrongContestName"));
assert!(!content.contains("RenamedContestName"));
assert!(content.contains("PublicContestName"));
let mut resp = client.get("http://localhost:8085/contest/1").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("PublicContestName"));
assert!(!content.contains("InfiniteContestName"));
assert!(!content.contains("PrivateContestName"));
assert!(!content.contains("WrongContestName"));
assert!(!content.contains("RenamedContestName"));
let params = [("csrf_token", "76CfTPJaoz")];
let resp = client.post("http://localhost:8085/contest/1").form(¶ms).send().unwrap();
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
let pos = content.find("type=\"hidden\" name=\"csrf_token\" value=\"").expect("CSRF-Token not found");
let csrf = &content[pos + 39..pos + 49];
let params = [("csrf_token", csrf)];
let resp = client.post("http://localhost:8085/contest/1").form(¶ms).send().unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let mut resp = client.get("http://localhost:8085/contest/1").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("☆☆☆"));
assert!(content.contains("☆☆☆☆"));
})
}
#[test]
fn check_task_load_save() {
start_server_and_fn(8086, None, || {
let client = reqwest::Client::builder().cookie_store(true)
.redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let resp = client.get("http://localhost:8086/contest/3").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let mut resp = client.get("http://localhost:8086/task/5").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
let pos = content.find("#taskid=5&csrftoken=").expect("CSRF-Token not found");
let csrf = &content[pos + 20..pos + 30];
let mut resp = client.get("http://localhost:8086/load/5").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert_eq!(content, "{}");
let params = [("data", "WrongData"), ("grade", "1"), ("csrf_token", "FNQU4QsEMY")];
let resp = client.post("http://localhost:8086/save/5").form(¶ms).send().unwrap();
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
// Check that the illegitimate request did not actually change anything
let mut resp = client.get("http://localhost:8086/load/5").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert_eq!(content, "{}");
let mut resp = client.get("http://localhost:8086/contest/3").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("☆☆☆"));
assert!(content.contains("☆☆☆☆"));
let params = [("data", "SomeData"), ("grade", "2"), ("csrf_token", csrf)];
let mut resp = client.post("http://localhost:8086/save/5").form(¶ms).send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert_eq!(content, "{}");
let mut resp = client.get("http://localhost:8086/load/5").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert_eq!(content, "SomeData");
let mut resp = client.get("http://localhost:8086/contest/3").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("★★☆"));
assert!(content.contains("☆☆☆☆"));
})
}
#[test]
fn check_task_load_save_logged_in() {
start_server_and_fn(8087, Some(("testusr".to_string(), "testpw".to_string(), false)), || {
let client = reqwest::Client::builder().cookie_store(true)
.redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let resp = login(8087, &client, "testusr", "testpw");
assert_eq!(resp.status(), StatusCode::FOUND);
let mut resp = client.get("http://localhost:8087/contest/1").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
let pos = content.find("type=\"hidden\" name=\"csrf_token\" value=\"").expect("CSRF-Token not found");
let csrf = &content[pos + 39..pos + 49];
let params = [("csrf_token", csrf)];
let resp = client.post("http://localhost:8087/contest/1").form(¶ms).send().unwrap();
assert_eq!(resp.status(), StatusCode::FOUND);
let mut resp = client.get("http://localhost:8087/task/1").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
let pos = content.find("#taskid=1&csrftoken=").expect("CSRF-Token not found");
let csrf = &content[pos + 20..pos + 30];
let mut resp = client.get("http://localhost:8087/load/1").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert_eq!(content, "{}");
let params = [("data", "WrongData"), ("grade", "1"), ("csrf_token", "FNQU4QsEMY")];
let resp = client.post("http://localhost:8087/save/1").form(¶ms).send().unwrap();
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
// Check that the illigal request did not actually change anything
let mut resp = client.get("http://localhost:8087/load/1").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert_eq!(content, "{}");
let mut resp = client.get("http://localhost:8087/contest/1").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("☆☆☆"));
assert!(content.contains("☆☆☆☆"));
let params = [("data", "SomeData"), ("grade", "2"), ("csrf_token", csrf)];
let mut resp = client.post("http://localhost:8087/save/1").form(¶ms).send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert_eq!(content, "{}");
let mut resp = client.get("http://localhost:8087/load/1").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert_eq!(content, "SomeData");
let mut resp = client.get("http://localhost:8087/contest/1").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("★★☆"));
assert!(content.contains("☆☆☆☆"));
})
}
#[test]
fn check_taskgroup_rename() {
start_server_and_fn(8088, None, || {
let client = reqwest::Client::builder().cookie_store(true)
.redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let mut resp = client.get("http://localhost:8088/contest/3").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
println!("{}", content);
assert!(content.contains("TaskgroupNewName"));
assert!(!content.contains("TaskgroupRenameName"));
let mut resp = client.get("http://localhost:8088/task/5").send().unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let content = resp.text().unwrap();
assert!(content.contains("TaskgroupNewName"));
assert!(!content.contains("TaskgroupRenameName"));
})
}
}