Commit cd45a3e2 authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Update to reqwest 0.9 to shorten test code

parent 9e378175
This diff is collapsed.
...@@ -34,7 +34,7 @@ handlebars-iron = "0.25.1" ...@@ -34,7 +34,7 @@ handlebars-iron = "0.25.1"
iron-sessionstorage = "*" iron-sessionstorage = "*"
serde_yaml = "0.8.9" serde_yaml = "0.8.9"
structopt = "0.2.18" structopt = "0.2.18"
reqwest = "0.6.2" reqwest = "0.9.19"
linked-hash-map = "0.5.1" linked-hash-map = "0.5.1"
bcrypt = "0.3" bcrypt = "0.3"
......
...@@ -27,13 +27,13 @@ extern crate urlencoded; ...@@ -27,13 +27,13 @@ extern crate urlencoded;
#[cfg(feature = "webbrowser")] #[cfg(feature = "webbrowser")]
extern crate webbrowser; extern crate webbrowser;
pub mod config;
pub mod contestreader_yaml;
mod db_apply_migrations; mod db_apply_migrations;
pub mod db_conn; pub mod db_conn;
mod db_conn_postgres; mod db_conn_postgres;
mod db_conn_sqlite; mod db_conn_sqlite;
mod db_objects; mod db_objects;
pub mod config;
pub mod contestreader_yaml;
pub mod functions; pub mod functions;
pub mod oauth_provider; pub mod oauth_provider;
mod webfw_iron; mod webfw_iron;
...@@ -241,8 +241,8 @@ fn main() { ...@@ -241,8 +241,8 @@ fn main() {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use std::io::Read;
use reqwest::StatusCode; use reqwest::StatusCode;
use std::io::Read;
fn start_server_and_fn<F>(port: u16, set_user: Option<(String, String, bool)>, f: F) fn start_server_and_fn<F>(port: u16, set_user: Option<(String, String, bool)>, f: F)
where F: Fn() { where F: Fn() {
...@@ -291,7 +291,7 @@ mod tests { ...@@ -291,7 +291,7 @@ mod tests {
fn start_server_and_check_requests() { fn start_server_and_check_requests() {
start_server_and_fn(8080, None, || { start_server_and_fn(8080, None, || {
let mut resp = reqwest::get("http://localhost:8080").unwrap(); let mut resp = reqwest::get("http://localhost:8080").unwrap();
assert_eq!(resp.status(), &StatusCode::Ok); assert_eq!(resp.status(), StatusCode::OK);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content).unwrap(); resp.read_to_string(&mut content).unwrap();
assert!(content.contains("Jugendwettbewerb Informatik</h1>")); assert!(content.contains("Jugendwettbewerb Informatik</h1>"));
...@@ -299,7 +299,7 @@ mod tests { ...@@ -299,7 +299,7 @@ mod tests {
assert!(!content.contains("Gruppenverwaltung")); assert!(!content.contains("Gruppenverwaltung"));
let mut resp = reqwest::get("http://localhost:8080/contest").unwrap(); let mut resp = reqwest::get("http://localhost:8080/contest").unwrap();
assert_eq!(resp.status(), &StatusCode::Ok); assert_eq!(resp.status(), StatusCode::OK);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content).unwrap(); resp.read_to_string(&mut content).unwrap();
assert!(content.contains("<h1>Wettbewerbe</h1>")); assert!(content.contains("<h1>Wettbewerbe</h1>"));
...@@ -315,9 +315,9 @@ mod tests { ...@@ -315,9 +315,9 @@ mod tests {
#[test] #[test]
fn check_login_wrong_credentials() { fn check_login_wrong_credentials() {
start_server_and_fn(8081, None, || { start_server_and_fn(8081, None, || {
let client = reqwest::Client::new().unwrap(); let client = reqwest::Client::new();
let mut resp = login_for_tests(8081, &client, "nonexistingusername", "wrongpassword"); let mut resp = login_for_tests(8081, &client, "nonexistingusername", "wrongpassword");
assert_eq!(resp.status(), &StatusCode::Ok); assert_eq!(resp.status(), StatusCode::OK);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content).unwrap(); resp.read_to_string(&mut content).unwrap();
assert!(content.contains("<h1>Login</h1>")); assert!(content.contains("<h1>Login</h1>"));
...@@ -329,24 +329,25 @@ mod tests { ...@@ -329,24 +329,25 @@ mod tests {
#[test] #[test]
fn start_server_and_check_login() { fn start_server_and_check_login() {
start_server_and_fn(8082, Some(("testusr".to_string(), "testpw".to_string(), false)), || { start_server_and_fn(8082, Some(("testusr".to_string(), "testpw".to_string(), false)), || {
let mut client = reqwest::Client::new().unwrap(); let client = reqwest::Client::builder().cookie_store(true)
client.redirect(reqwest::RedirectPolicy::custom(|attempt| attempt.stop())); .redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let mut resp = login_for_tests(8082, &client, "testusr", "testpw"); let mut resp = login_for_tests(8082, &client, "testusr", "testpw");
assert_eq!(resp.status(), &StatusCode::Found); assert_eq!(resp.status(), StatusCode::FOUND);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content).unwrap(); resp.read_to_string(&mut content).unwrap();
assert!(!content.contains("Error")); assert!(!content.contains("Error"));
let header = resp.headers(); let mut set_cookie = resp.headers().get_all("Set-Cookie").iter();
let set_cookie = header.get::<reqwest::header::SetCookie>().expect("No coockies transmitted"); assert!(set_cookie.next().is_some());
assert_eq!(set_cookie.len(), 1); assert!(set_cookie.next().is_none());
let cookie = reqwest::header::Cookie(set_cookie.to_vec()); let mut resp = client.get("http://localhost:8082").send().unwrap();
let mut resp = client.get("http://localhost:8082").header(cookie).send().unwrap(); assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.status(), &StatusCode::Ok);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content).unwrap(); resp.read_to_string(&mut content).unwrap();
assert!(!content.contains("Error")); assert!(!content.contains("Error"));
...@@ -359,25 +360,26 @@ mod tests { ...@@ -359,25 +360,26 @@ mod tests {
#[test] #[test]
fn start_server_and_check_logout() { fn start_server_and_check_logout() {
start_server_and_fn(8083, Some(("testusr".to_string(), "testpw".to_string(), false)), || { start_server_and_fn(8083, Some(("testusr".to_string(), "testpw".to_string(), false)), || {
let mut client = reqwest::Client::new().unwrap(); let client = reqwest::Client::builder().cookie_store(true)
client.redirect(reqwest::RedirectPolicy::custom(|attempt| attempt.stop())); .redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let resp = login_for_tests(8083, &client, "testusr", "testpw"); let resp = login_for_tests(8083, &client, "testusr", "testpw");
assert_eq!(resp.status(), &StatusCode::Found); assert_eq!(resp.status(), StatusCode::FOUND);
let header = resp.headers(); let mut set_cookie = resp.headers().get_all("Set-Cookie").iter();
let set_cookie = header.get::<reqwest::header::SetCookie>().expect("No coockies transmitted"); assert!(set_cookie.next().is_some());
assert_eq!(set_cookie.len(), 1); assert!(set_cookie.next().is_none());
let cookie = reqwest::header::Cookie(set_cookie.to_vec());
let resp = client.get("http://localhost:8083/logout").send().unwrap();
let resp = client.get("http://localhost:8083/logout").header(cookie.clone()).send().unwrap(); assert_eq!(resp.status(), StatusCode::FOUND);
assert_eq!(resp.status(), &StatusCode::Found);
let mut resp = client.get("http://localhost:8083").send().unwrap();
let mut resp = client.get("http://localhost:8083").header(cookie).send().unwrap(); assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.status(), &StatusCode::Ok);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content).unwrap(); resp.read_to_string(&mut content).unwrap();
assert_eq!(resp.status(), &StatusCode::Ok); assert_eq!(resp.status(), StatusCode::OK);
assert!(content.contains("Benutzername")); assert!(content.contains("Benutzername"));
assert!(content.contains("Passwort")); assert!(content.contains("Passwort"));
assert!(content.contains("Gruppencode / Teilnahmecode")); assert!(content.contains("Gruppencode / Teilnahmecode"));
...@@ -388,42 +390,41 @@ mod tests { ...@@ -388,42 +390,41 @@ mod tests {
#[test] #[test]
fn check_group_creation_and_group_code_login() { fn check_group_creation_and_group_code_login() {
start_server_and_fn(8084, Some(("testusr".to_string(), "testpw".to_string(), true)), || { start_server_and_fn(8084, Some(("testusr".to_string(), "testpw".to_string(), true)), || {
let mut client = reqwest::Client::new().unwrap(); let client = reqwest::Client::builder().cookie_store(true)
client.redirect(reqwest::RedirectPolicy::custom(|attempt| attempt.stop())); .redirect(reqwest::RedirectPolicy::none())
.build()
.unwrap();
let resp = login_for_tests(8084, &client, "testusr", "testpw"); let resp = login_for_tests(8084, &client, "testusr", "testpw");
assert_eq!(resp.status(), &StatusCode::Found); assert_eq!(resp.status(), StatusCode::FOUND);
let header = resp.headers(); let mut set_cookie = resp.headers().get_all("Set-Cookie").iter();
let set_cookie = header.get::<reqwest::header::SetCookie>().expect("No coockies transmitted"); assert!(set_cookie.next().is_some());
assert_eq!(set_cookie.len(), 1); assert!(set_cookie.next().is_none());
let cookie = reqwest::header::Cookie(set_cookie.to_vec());
let mut resp = client.get("http://localhost:8084").header(cookie.clone()).send().unwrap(); let mut resp = client.get("http://localhost:8084").send().unwrap();
assert_eq!(resp.status(), &StatusCode::Ok); assert_eq!(resp.status(), StatusCode::OK);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content).unwrap(); resp.read_to_string(&mut content).unwrap();
assert!(content.contains("[Lehrer]")); assert!(content.contains("[Lehrer]"));
assert!(content.contains("Gruppenverwaltung")); assert!(content.contains("Gruppenverwaltung"));
let mut resp = client.get("http://localhost:8084/group/").send().unwrap();
let mut resp = client.get("http://localhost:8084/group/").header(cookie.clone()).send().unwrap(); assert_eq!(resp.status(), StatusCode::OK);
assert_eq!(resp.status(), &StatusCode::Ok);
let mut content = String::new(); let mut content = String::new();
resp.read_to_string(&mut content).unwrap(); resp.read_to_string(&mut content).unwrap();
assert!(content.contains("Gruppe anlegen")); assert!(content.contains("Gruppe anlegen"));
let params = [("name", "groupname"), ("tag", "marker"), ("csrf", "76CfTPJaoz")]; let params = [("name", "groupname"), ("tag", "marker"), ("csrf", "76CfTPJaoz")];
let resp = client.post("http://localhost:8084/group/").form(&params).header(cookie.clone()).send().unwrap(); let resp = client.post("http://localhost:8084/group/").form(&params).send().unwrap();
assert_eq!(resp.status(), &StatusCode::Forbidden); assert_eq!(resp.status(), StatusCode::FORBIDDEN);
let pos = content.find("type=\"hidden\" name=\"csrf\" value=\"").expect("CSRF-Token not found"); let pos = content.find("type=\"hidden\" name=\"csrf\" value=\"").expect("CSRF-Token not found");
let csrf = &content[pos+33..pos+43]; let csrf = &content[pos + 33..pos + 43];
let params = [("name", "groupname"), ("tag", "marker"), ("csrf", csrf)]; let params = [("name", "groupname"), ("tag", "marker"), ("csrf", csrf)];
let resp = client.post("http://localhost:8084/group/").form(&params).header(cookie).send().unwrap(); let resp = client.post("http://localhost:8084/group/").form(&params).send().unwrap();
assert_eq!(resp.status(), &StatusCode::Found); assert_eq!(resp.status(), StatusCode::FOUND);
}) })
} }
} }
...@@ -682,7 +682,6 @@ pub struct OAuthUserData { ...@@ -682,7 +682,6 @@ pub struct OAuthUserData {
fn oauth<C>(req: &mut Request) -> IronResult<Response> fn oauth<C>(req: &mut Request) -> IronResult<Response>
where C: MedalConnection + std::marker::Send + 'static { where C: MedalConnection + std::marker::Send + 'static {
use params::{Params, Value}; use params::{Params, Value};
use reqwest::header;
let oauth_id = req.expect_str("oauthid")?; let oauth_id = req.expect_str("oauthid")?;
...@@ -726,18 +725,12 @@ fn oauth<C>(req: &mut Request) -> IronResult<Response> ...@@ -726,18 +725,12 @@ fn oauth<C>(req: &mut Request) -> IronResult<Response>
} }
}; };
let client = reqwest::Client::new().unwrap(); let client = reqwest::Client::new();
let params = [("code", code), ("grant_type", "authorization_code".to_string())]; let params = [("code", code), ("grant_type", "authorization_code".to_string())];
let res = client.post(&access_token_url) let res = client.post(&access_token_url).basic_auth(client_id, Some(client_secret)).form(&params).send();
.header(header::Authorization(header::Basic { username: client_id, password: Some(client_secret) }))
.form(&params)
.send();
let access: OAuthAccess = res.expect("network error").json().expect("malformed json"); let access: OAuthAccess = res.expect("network error").json().expect("malformed json");
let res = client.post(&user_data_url) let res = client.post(&user_data_url).bearer_auth(access.access_token).form(&params).send();
.header(header::Authorization(header::Bearer { token: access.access_token }))
.form(&params)
.send();
let mut user_data: OAuthUserData = res.expect("network error").json().expect("malformed json"); let mut user_data: OAuthUserData = res.expect("network error").json().expect("malformed json");
if let Some(id) = user_data.userID { if let Some(id) = user_data.userID {
......
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