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

Fix warnings, tests, and clippy lints

parent b652144b
...@@ -34,8 +34,8 @@ impl MedalConnection for Connection { ...@@ -34,8 +34,8 @@ impl MedalConnection for Connection {
let create_string = "CREATE TABLE IF NOT EXISTS migrations (name TEXT PRIMARY KEY);"; let create_string = "CREATE TABLE IF NOT EXISTS migrations (name TEXT PRIMARY KEY);";
self.execute(create_string, &[]).unwrap(); self.execute(create_string, &[]).unwrap();
let mut stmt = self.prepare("SELECT name FROM migrations WHERE name = $1").unwrap(); let stmt = self.prepare("SELECT name FROM migrations WHERE name = $1").unwrap();
stmt.query(&[&name]).unwrap().len() > 0 !stmt.query(&[&name]).unwrap().is_empty()
} }
fn apply_migration(&mut self, name: &str, contents: &str) { fn apply_migration(&mut self, name: &str, contents: &str) {
...@@ -53,60 +53,53 @@ impl MedalConnection for Connection { ...@@ -53,60 +53,53 @@ impl MedalConnection for Connection {
// fn get_session<T: ToSql>(&self, key: T, keyname: &str) -> Option<SessionUser> { // fn get_session<T: ToSql>(&self, key: T, keyname: &str) -> Option<SessionUser> {
fn get_session(&self, key: &str) -> Option<SessionUser> { fn get_session(&self, key: &str) -> Option<SessionUser> {
let res = self.query("SELECT id, csrf_token, last_login, last_activity, permanent_login, username, password, logincode, email, email_unconfirmed, email_confirmationcode, firstname, lastname, street, zip, city, nation, grade, is_teacher, managed_by, oauth_provider, oauth_foreign_id, salt FROM session WHERE session_token = $1", &[&key]); let session = self.query("SELECT id, csrf_token, last_login, last_activity, permanent_login, username, password, logincode, email, email_unconfirmed, email_confirmationcode, firstname, lastname, street, zip, city, nation, grade, is_teacher, managed_by, oauth_provider, oauth_foreign_id, salt FROM session WHERE session_token = $1", &[&key]).ok()?.iter().next().map(|row| {
let rows = match res { SessionUser { id: row.get(0),
Ok(rows) => rows, session_token: Some(key.to_string()),
Err(err) => return None, csrf_token: row.get(1),
}; last_login: row.get(2),
last_activity: row.get(3),
permanent_login: row.get(4),
username: row.get(5),
password: row.get(6),
salt: row.get(22),
logincode: row.get(7),
email: row.get(8),
email_unconfirmed: row.get(9),
email_confirmationcode: row.get(10),
firstname: row.get(11),
lastname: row.get(12),
street: row.get(13),
zip: row.get(14),
city: row.get(15),
nation: row.get(16),
grade: row.get(17),
is_teacher: row.get(18),
managed_by: row.get(19),
oauth_provider: row.get(20),
oauth_foreign_id: row.get(21) }
})?;
let duration = if session.permanent_login { Duration::days(90) } else { Duration::minutes(90) };
let now = time::get_time();
for row in &rows { if let Some(last_activity) = session.last_activity {
let session = SessionUser { id: row.get(0), if now - last_activity < duration {
session_token: Some(key.to_string()), self.execute("UPDATE session SET last_activity = $1 WHERE id = $2", &[&now, &session.id]).unwrap();
csrf_token: row.get(1), return Some(session);
last_login: row.get(2),
last_activity: row.get(3),
permanent_login: row.get(4),
username: row.get(5),
password: row.get(6),
salt: row.get(22),
logincode: row.get(7),
email: row.get(8),
email_unconfirmed: row.get(9),
email_confirmationcode: row.get(10),
firstname: row.get(11),
lastname: row.get(12),
street: row.get(13),
zip: row.get(14),
city: row.get(15),
nation: row.get(16),
grade: row.get(17),
is_teacher: row.get(18),
managed_by: row.get(19),
oauth_provider: row.get(20),
oauth_foreign_id: row.get(21) };
let duration = if session.permanent_login { Duration::days(90) } else { Duration::minutes(90) };
let now = time::get_time();
return if let Some(last_activity) = session.last_activity {
if now - last_activity < duration {
self.execute("UPDATE session SET last_activity = $1 WHERE id = $2", &[&now, &session.id]).unwrap();
Some(session)
} else {
// Session timed out
// Should remove session token from session
None
}
} else { } else {
// last_activity undefined // Session timed out
// TODO: What should happen here? // Should remove session token from session
None return None;
}; }
} }
None // no rows fetched // last_activity undefined
// TODO: What should happen here?
None
} }
fn save_session(&self, session: SessionUser) { fn save_session(&self, session: SessionUser) {
self.execute("UPDATE session SET self.execute("UPDATE session SET
...@@ -142,49 +135,53 @@ impl MedalConnection for Connection { ...@@ -142,49 +135,53 @@ impl MedalConnection for Connection {
&[&session_token, &csrf_token, &now]) &[&session_token, &csrf_token, &now])
.unwrap(); .unwrap();
for row in &self.query("SELECT lastval()", &[]).unwrap() { let id = self.query("SELECT lastval()", &[])
let id: i64 = row.get(0); .unwrap()
return SessionUser::minimal(id as i32, session_token.to_owned(), csrf_token); .iter()
} .next()
panic!("Expected to get last row id") .map(|row| -> i64 { row.get(0) })
.expect("Expected to get last row id");
SessionUser::minimal(id as i32, session_token.to_owned(), csrf_token)
} }
fn get_session_or_new(&self, key: &str) -> SessionUser { fn get_session_or_new(&self, key: &str) -> SessionUser {
self.get_session(&key).unwrap_or_else(|| self.new_session(&key)) self.get_session(&key).unwrap_or_else(|| self.new_session(&key))
} }
fn get_user_by_id(&self, user_id: i32) -> Option<SessionUser> { fn get_user_by_id(&self, user_id: i32) -> Option<SessionUser> {
let rows = self.query("SELECT session_token, csrf_token, last_login, last_activity, permanent_login, username, password, logincode, email, email_unconfirmed, email_confirmationcode, firstname, lastname, street, zip, city, nation, grade, is_teacher, managed_by, oauth_provider, oauth_foreign_id, salt FROM session WHERE id = $1", &[&user_id]).ok()?; self.query("SELECT session_token, csrf_token, last_login, last_activity, permanent_login, username, password, logincode, email, email_unconfirmed, email_confirmationcode, firstname, lastname, street, zip, city, nation, grade, is_teacher, managed_by, oauth_provider, oauth_foreign_id, salt FROM session WHERE id = $1", &[&user_id])
for row in &rows { .ok()?
return Some(SessionUser { id: user_id, .iter()
session_token: row.get(0), .next()
csrf_token: row.get(1), .map(|row| {
last_login: row.get(2), SessionUser { id: user_id,
last_activity: row.get(3), session_token: row.get(0),
permanent_login: row.get(4), csrf_token: row.get(1),
last_login: row.get(2),
username: row.get(5), last_activity: row.get(3),
password: row.get(6), permanent_login: row.get(4),
salt: row.get(22),
logincode: row.get(7), username: row.get(5),
email: row.get(8), password: row.get(6),
email_unconfirmed: row.get(9), salt: row.get(22),
email_confirmationcode: row.get(10), logincode: row.get(7),
email: row.get(8),
firstname: row.get(11), email_unconfirmed: row.get(9),
lastname: row.get(12), email_confirmationcode: row.get(10),
street: row.get(13),
zip: row.get(14), firstname: row.get(11),
city: row.get(15), lastname: row.get(12),
nation: row.get(16), street: row.get(13),
grade: row.get(17), zip: row.get(14),
city: row.get(15),
is_teacher: row.get(18), nation: row.get(16),
managed_by: row.get(19), grade: row.get(17),
oauth_provider: row.get(20), is_teacher: row.get(18),
oauth_foreign_id: row.get(21) }); managed_by: row.get(19),
}
return None; oauth_provider: row.get(20),
oauth_foreign_id: row.get(21) }
})
} }
fn get_user_and_group_by_id(&self, user_id: i32) -> Option<(SessionUser, Option<Group>)> { fn get_user_and_group_by_id(&self, user_id: i32) -> Option<(SessionUser, Option<Group>)> {
...@@ -388,7 +385,7 @@ impl MedalConnection for Connection { ...@@ -388,7 +385,7 @@ impl MedalConnection for Connection {
fn get_contest_groups_grades(&self, session_id: i32, contest_id: i32) fn get_contest_groups_grades(&self, session_id: i32, contest_id: i32)
-> (Vec<String>, Vec<(Group, Vec<(UserInfo, Vec<Grade>)>)>) { -> (Vec<String>, Vec<(Group, Vec<(UserInfo, Vec<Grade>)>)>) {
let mut stmt = self.prepare("SELECT id, name FROM taskgroup WHERE contest = $1 ORDER BY id ASC").unwrap(); let stmt = self.prepare("SELECT id, name FROM taskgroup WHERE contest = $1 ORDER BY id ASC").unwrap();
let res = stmt.query(&[&contest_id]).unwrap(); let res = stmt.query(&[&contest_id]).unwrap();
let tasknames_iter = res.iter().map(|row| { let tasknames_iter = res.iter().map(|row| {
let x: (i32, String) = (row.get(0), row.get(1)); let x: (i32, String) = (row.get(0), row.get(1));
...@@ -403,7 +400,7 @@ impl MedalConnection for Connection { ...@@ -403,7 +400,7 @@ impl MedalConnection for Connection {
taskindex.insert(*i, index); taskindex.insert(*i, index);
} }
let mut stmt = self.prepare("SELECT grade.taskgroup, grade.session, grade.grade, grade.validated, usergroup.id, usergroup.name, usergroup.groupcode, usergroup.tag, student.id, student.username, student.logincode, student.firstname, student.lastname let stmt = self.prepare("SELECT grade.taskgroup, grade.session, grade.grade, grade.validated, usergroup.id, usergroup.name, usergroup.groupcode, usergroup.tag, student.id, student.username, student.logincode, student.firstname, student.lastname
FROM grade FROM grade
JOIN taskgroup ON grade.taskgroup = taskgroup.id JOIN taskgroup ON grade.taskgroup = taskgroup.id
JOIN session AS student ON grade.session = student.id JOIN session AS student ON grade.session = student.id
...@@ -442,20 +439,19 @@ impl MedalConnection for Connection { ...@@ -442,20 +439,19 @@ impl MedalConnection for Connection {
// help to spare all these clones? // help to spare all these clones?
for ggu in gradeinfo_iter { for ggu in gradeinfo_iter {
if let (g, gr, ui) = ggu { let (g, gr, ui) = ggu;
if gr.id != group.id { if gr.id != group.id {
users.push((userinfo.clone(), grades)); users.push((userinfo.clone(), grades));
grades = vec![Default::default(); n_tasks]; grades = vec![Default::default(); n_tasks];
groups.push((group.clone(), users)); groups.push((group.clone(), users));
users = Vec::new(); users = Vec::new();
} else if ui.id != userinfo.id { } else if ui.id != userinfo.id {
users.push((userinfo.clone(), grades)); users.push((userinfo.clone(), grades));
grades = vec![Default::default(); n_tasks]; grades = vec![Default::default(); n_tasks];
}
let index = g.taskgroup;
grades[taskindex[&index]] = g;
} }
let index = g.taskgroup;
grades[taskindex[&index]] = g;
} }
users.push((userinfo, grades)); users.push((userinfo, grades));
groups.push((group, users)); groups.push((group, users));
...@@ -578,13 +574,12 @@ impl MedalConnection for Connection { ...@@ -578,13 +574,12 @@ impl MedalConnection for Connection {
let (mut contest, mut taskgroup, task) = taskgroupcontest_iter.next().unwrap(); let (mut contest, mut taskgroup, task) = taskgroupcontest_iter.next().unwrap();
taskgroup.tasks.push(task); taskgroup.tasks.push(task);
for tgc in taskgroupcontest_iter { for tgc in taskgroupcontest_iter {
if let (_, tg, t) = tgc { let (_, tg, t) = tgc;
if tg.id != taskgroup.id { if tg.id != taskgroup.id {
contest.taskgroups.push(taskgroup); contest.taskgroups.push(taskgroup);
taskgroup = tg; taskgroup = tg;
}
taskgroup.tasks.push(t);
} }
taskgroup.tasks.push(t);
} }
contest.taskgroups.push(taskgroup); contest.taskgroups.push(taskgroup);
contest contest
...@@ -611,9 +606,8 @@ impl MedalConnection for Connection { ...@@ -611,9 +606,8 @@ impl MedalConnection for Connection {
let (mut contest, taskgroup) = taskgroupcontest_iter.next().unwrap(); let (mut contest, taskgroup) = taskgroupcontest_iter.next().unwrap();
contest.taskgroups.push(taskgroup); contest.taskgroups.push(taskgroup);
for tgc in taskgroupcontest_iter { for tgc in taskgroupcontest_iter {
if let (_, tg) = tgc { let (_, tg) = tgc;
contest.taskgroups.push(tg); contest.taskgroups.push(tg);
}
} }
contest contest
} }
......
...@@ -23,8 +23,6 @@ extern crate structopt; ...@@ -23,8 +23,6 @@ extern crate structopt;
extern crate time; extern crate time;
extern crate urlencoded; extern crate urlencoded;
use rusqlite::Connection;
mod db_apply_migrations; mod db_apply_migrations;
mod db_conn; mod db_conn;
mod db_conn_postgres; mod db_conn_postgres;
...@@ -305,7 +303,7 @@ mod tests { ...@@ -305,7 +303,7 @@ mod tests {
let (stop_tx, stop_rx) = channel(); let (stop_tx, stop_rx) = channel();
thread::spawn(move || { thread::spawn(move || {
let mut conn = Connection::open_in_memory().unwrap(); let mut conn = rusqlite::Connection::open_in_memory().unwrap();
db_apply_migrations::test(&mut conn); db_apply_migrations::test(&mut conn);
if let Some(user) = set_user { if let Some(user) = set_user {
......
...@@ -16,7 +16,6 @@ use staticfile::Static; ...@@ -16,7 +16,6 @@ use staticfile::Static;
use iron_sessionstorage::backends::SignedCookieBackend; use iron_sessionstorage::backends::SignedCookieBackend;
use iron_sessionstorage::SessionStorage; use iron_sessionstorage::SessionStorage;
use persistent::Write; use persistent::Write;
use rusqlite::Connection;
use urlencoded::{UrlEncodedBody, UrlEncodedQuery}; use urlencoded::{UrlEncodedBody, UrlEncodedQuery};
pub use handlebars_iron::handlebars::to_json; pub use handlebars_iron::handlebars::to_json;
...@@ -25,7 +24,6 @@ use handlebars_iron::{DirectorySource, HandlebarsEngine, Template}; ...@@ -25,7 +24,6 @@ use handlebars_iron::{DirectorySource, HandlebarsEngine, Template};
use iron; use iron;
use iron_sessionstorage; use iron_sessionstorage;
use reqwest; use reqwest;
use rusqlite;
use db_conn::MedalConnection; use db_conn::MedalConnection;
......
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