Commit 2f4902f6 authored by Daniel Brüning's avatar Daniel Brüning
Browse files

Implemented the option to create a user in the start_server_and_fn-function....

Implemented the option to create a user in the start_server_and_fn-function. Deleted start_server_and_create_user_and_fn (not needed anymore).
Also removed all warnings in the test-cases.
parent 25f4dc60
Pipeline #61 passed with stage
in 4 minutes and 9 seconds
...@@ -220,34 +220,7 @@ mod tests { ...@@ -220,34 +220,7 @@ mod tests {
use super::*; use super::*;
use std::io::Read; use std::io::Read;
fn start_server_and_fn<F>(port: u16, f: F) where F: FnOnce() { fn start_server_and_fn<F>(port: u16, set_user: Option<(String, String)>, f: F) where F: FnOnce() {
use std::{thread, time};
use std::sync::mpsc::channel;
let (start_tx, start_rx) = channel();
let (stop_tx, stop_rx) = channel();
thread::spawn(move || {
let mut conn = Connection::open_in_memory().unwrap();
db_apply_migrations::test(&mut conn);
let mut config = read_config_from_file(Path::new("thisfileshoudnotexist"));
config.port = Some(port);
let srvr = start_server(conn, config);
start_tx.send(()).unwrap();
stop_rx.recv().unwrap();
srvr.unwrap().close().unwrap();
});
// wait for server to start:
start_rx.recv().unwrap();
thread::sleep(time::Duration::from_millis(100));
f();
stop_tx.send(()).unwrap();
}
fn start_server_and_create_user_and_fn<F>(port: u16, f: F) where F: FnOnce() {
use std::{thread, time}; use std::{thread, time};
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
let (start_tx, start_rx) = channel(); let (start_tx, start_rx) = channel();
...@@ -257,12 +230,12 @@ mod tests { ...@@ -257,12 +230,12 @@ mod tests {
let mut conn = Connection::open_in_memory().unwrap(); let mut conn = Connection::open_in_memory().unwrap();
db_apply_migrations::test(&mut conn); db_apply_migrations::test(&mut conn);
let mut test_user = conn.new_session(); if let Some(user) = set_user {
test_user.username = Some("testusr".into()); let mut test_user = conn.new_session();
match test_user.set_password("testpw") { test_user.username = Some(user.0.into());
None => panic!("Set Password did not work correctly.)"), match test_user.set_password(&user.1) {
_ => { None => panic!("Set Password did not work correctly.)"),
conn.save_session(test_user); _ => conn.save_session(test_user),
} }
} }
...@@ -286,7 +259,7 @@ mod tests { ...@@ -286,7 +259,7 @@ mod tests {
fn login_for_tests(port: u16, client: &reqwest::Client, username: &str, password: &str) -> reqwest::Response { fn login_for_tests(port: u16, client: &reqwest::Client, username: &str, password: &str) -> reqwest::Response {
let params = [("username", username), ("password", password)]; let params = [("username", username), ("password", password)];
let mut resp = client.post(&format!("http://localhost:{}/login", port)) let resp = client.post(&format!("http://localhost:{}/login", port))
.form(&params).send().unwrap(); .form(&params).send().unwrap();
return resp; return resp;
} }
...@@ -300,11 +273,11 @@ mod tests { ...@@ -300,11 +273,11 @@ mod tests {
#[test] #[test]
fn start_server_and_check_request() { fn start_server_and_check_request() {
start_server_and_fn(8080, ||{ start_server_and_fn(8080, None, ||{
let mut resp = reqwest::get("http://localhost:8080").unwrap(); let mut resp = reqwest::get("http://localhost:8080").unwrap();
check_status(&resp, reqwest::StatusCode::Ok); check_status(&resp, reqwest::StatusCode::Ok);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content); resp.read_to_string(&mut content).unwrap();
assert!(content.contains("<h1>Jugendwettbewerb Informatik</h1>")); assert!(content.contains("<h1>Jugendwettbewerb Informatik</h1>"));
assert!(!content.contains("Error")); assert!(!content.contains("Error"));
...@@ -312,7 +285,7 @@ mod tests { ...@@ -312,7 +285,7 @@ mod tests {
let mut resp = reqwest::get("http://localhost:8080/contest").unwrap(); let mut resp = reqwest::get("http://localhost:8080/contest").unwrap();
check_status(&resp, reqwest::StatusCode::Ok); check_status(&resp, reqwest::StatusCode::Ok);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content); resp.read_to_string(&mut content).unwrap();
assert!(content.contains("<h1>Wettbewerbe</h1>")); assert!(content.contains("<h1>Wettbewerbe</h1>"));
assert!(!content.contains("Error")); assert!(!content.contains("Error"));
}) })
...@@ -320,12 +293,12 @@ mod tests { ...@@ -320,12 +293,12 @@ mod tests {
#[test] #[test]
fn check_login_wrong_credentials() { fn check_login_wrong_credentials() {
start_server_and_fn(8081, ||{ start_server_and_fn(8081, None, ||{
let client = reqwest::Client::new().unwrap(); let client = reqwest::Client::new().unwrap();
let mut resp = login_for_tests(8081, &client, "nonexistingusername", "wrongpassword"); let mut resp = login_for_tests(8081, &client, "nonexistingusername", "wrongpassword");
check_status(&resp, reqwest::StatusCode::Ok); check_status(&resp, reqwest::StatusCode::Ok);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content); resp.read_to_string(&mut content).unwrap();
assert!(content.contains("<h1>Login</h1>")); assert!(content.contains("<h1>Login</h1>"));
assert!(content.contains("Login fehlgeschlagen.")); assert!(content.contains("Login fehlgeschlagen."));
assert!(!content.contains("Error")); assert!(!content.contains("Error"));
...@@ -334,31 +307,31 @@ mod tests { ...@@ -334,31 +307,31 @@ mod tests {
#[test] #[test]
fn start_server_and_check_login() { fn start_server_and_check_login() {
start_server_and_create_user_and_fn(8082, ||{ start_server_and_fn(8082, Some(("testusr".to_string(), "testpw".to_string())), ||{
let mut client = reqwest::Client::new().unwrap(); let mut client = reqwest::Client::new().unwrap();
client.redirect(reqwest::RedirectPolicy::custom(|attempt| {attempt.stop()})); client.redirect(reqwest::RedirectPolicy::custom(|attempt| {attempt.stop()}));
let mut resp = login_for_tests(8082, &client, "testusr", "testpw"); let mut resp = login_for_tests(8082, &client, "testusr", "testpw");
check_status(&resp, reqwest::StatusCode::Found); check_status(&resp, reqwest::StatusCode::Found);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content); resp.read_to_string(&mut content).unwrap();
assert!(!content.contains("Error")); assert!(!content.contains("Error"));
let header = resp.headers(); let header = resp.headers();
let setCookie = header.get::<reqwest::header::SetCookie>(); let set_cookie = header.get::<reqwest::header::SetCookie>();
match setCookie { match set_cookie {
None => panic!("No setCookie."), None => panic!("No setCookie."),
Some(cookie) => if cookie.len() == 1 { Some(cookie) => if cookie.len() == 1 {
let newCookie = reqwest::header::Cookie(cookie.to_vec()); let new_cookie = reqwest::header::Cookie(cookie.to_vec());
let mut newResp = client.get("http://localhost:8082") let mut new_resp = client.get("http://localhost:8082")
.header(newCookie).send().unwrap(); .header(new_cookie).send().unwrap();
check_status(&newResp, reqwest::StatusCode::Ok); check_status(&new_resp, reqwest::StatusCode::Ok);
let mut newContent = String::new(); let mut new_content = String::new();
newResp.read_to_string(&mut newContent); new_resp.read_to_string(&mut new_content).unwrap();
assert!(!content.contains("Error")); assert!(!content.contains("Error"));
assert!(newContent.contains("Eingeloggt als <em>testusr</em>")); assert!(new_content.contains("Eingeloggt als <em>testusr</em>"));
assert!(newContent.contains("<h1>Jugendwettbewerb Informatik</h1>")); assert!(new_content.contains("<h1>Jugendwettbewerb Informatik</h1>"));
} else { } else {
panic!("More than one setCookie."); panic!("More than one setCookie.");
}, },
...@@ -368,31 +341,31 @@ mod tests { ...@@ -368,31 +341,31 @@ mod tests {
#[test] #[test]
fn start_server_and_check_logout() { fn start_server_and_check_logout() {
start_server_and_create_user_and_fn(8083, ||{ start_server_and_fn(8083, Some(("testusr".to_string(), "testpw".to_string())), ||{
let mut client = reqwest::Client::new().unwrap(); let mut client = reqwest::Client::new().unwrap();
client.redirect(reqwest::RedirectPolicy::custom(|attempt| {attempt.stop()})); client.redirect(reqwest::RedirectPolicy::custom(|attempt| {attempt.stop()}));
let mut resp = login_for_tests(8083, &client, "testusr", "testpw"); let resp = login_for_tests(8083, &client, "testusr", "testpw");
check_status(&resp, reqwest::StatusCode::Found); check_status(&resp, reqwest::StatusCode::Found);
let header = resp.headers(); let header = resp.headers();
let setCookie = header.get::<reqwest::header::SetCookie>(); let set_cookie = header.get::<reqwest::header::SetCookie>();
match setCookie { match set_cookie {
None => panic!("No setCookie."), None => panic!("No setCookie."),
Some(cookie) => if cookie.len() == 1 { Some(cookie) => if cookie.len() == 1 {
let newCookie = reqwest::header::Cookie(cookie.to_vec()); let new_cookie = reqwest::header::Cookie(cookie.to_vec());
let mut newResp = client.get("http://localhost:8082/logout") let mut new_resp = client.get("http://localhost:8082/logout")
.header(newCookie.clone()).send().unwrap(); .header(new_cookie.clone()).send().unwrap();
check_status(&newResp, reqwest::StatusCode::Found); check_status(&new_resp, reqwest::StatusCode::Found);
newResp = client.get("http://localhost:8082") new_resp = client.get("http://localhost:8082")
.header(newCookie).send().unwrap(); .header(new_cookie).send().unwrap();
check_status(&newResp, reqwest::StatusCode::Ok); check_status(&new_resp, reqwest::StatusCode::Ok);
let mut newContent = String::new(); let mut new_content = String::new();
newResp.read_to_string(&mut newContent); new_resp.read_to_string(&mut new_content).unwrap();
assert!(newContent.contains("Benutzername:")); assert!(new_content.contains("Benutzername:"));
assert!(newContent.contains("Passwort:")); assert!(new_content.contains("Passwort:"));
assert!(newContent.contains("Gruppencode / Teilnahmecode:")); assert!(new_content.contains("Gruppencode / Teilnahmecode:"));
assert!(newContent.contains("<h1>Jugendwettbewerb Informatik</h1>")); assert!(new_content.contains("<h1>Jugendwettbewerb Informatik</h1>"));
} else { } else {
panic!("More than one setCookie."); panic!("More than one setCookie.");
}, },
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment