Commit 61f612f5 authored by Robert Czechowski's avatar Robert Czechowski
Browse files

Fix error handling in save task

parent f1d0ae52
Pipeline #199 failed with stage
in 9 minutes and 26 seconds
......@@ -379,7 +379,7 @@ pub fn save_submission<T: MedalConnection>(conn: &T, task_id: i32, session_token
.ok_or(MedalError::AccessDenied)?; // TODO SessionTimeout
if session.csrf_token != csrf_token {
return Err(MedalError::CsrfError);
return Err(MedalError::CsrfCheckFailed);
}
let submission = Submission { id: None,
......
......@@ -602,7 +602,7 @@ mod tests {
assert_eq!(content, "{}");
let params = [("data","WrongData"),("grade","1"),("csrf_token", "FNQU4QsEMY")];
let mut resp = client.post("http://localhost:8085/save/1").form(&params).send().unwrap();
let resp = client.post("http://localhost:8085/save/1").form(&params).send().unwrap();
assert_eq!(resp.status(), StatusCode::FORBIDDEN);
let mut resp = client.get("http://localhost:8085/load/1").send().unwrap();
......@@ -610,6 +610,13 @@ mod tests {
let content = resp.text().unwrap();
assert_eq!(content, "{}");
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("<a href=\"/task/1\">☆☆☆</a></li>"));
assert!(content.contains("<a href=\"/task/2\">☆☆☆☆</a></li>"));
let params = [("data","SomeData"),("grade","2"),("csrf_token", csrf)];
let mut resp = client.post("http://localhost:8085/save/1").form(&params).send().unwrap();
......@@ -623,6 +630,13 @@ mod tests {
let content = resp.text().unwrap();
assert_eq!(content, "SomeData");
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("<a href=\"/task/1\">★★☆</a></li>"));
assert!(content.contains("<a href=\"/task/2\">☆☆☆☆</a></li>"));
})
}
}
......@@ -202,7 +202,7 @@ impl<'c, 'a, 'b> From<AugMedalError<'c, 'a, 'b>> for IronError {
}
functions::MedalError::AccessDenied => {
IronError { error: Box::new(SessionError { message: "Access denied".to_string() }),
response: Response::with(status::Forbidden) }
response: Response::with(status::Unauthorized) }
}
functions::MedalError::CsrfCheckFailed => {
IronError { error: Box::new(SessionError { message: "CSRF Error".to_string() }),
......@@ -214,11 +214,11 @@ impl<'c, 'a, 'b> From<AugMedalError<'c, 'a, 'b>> for IronError {
}
functions::MedalError::DatabaseError => {
IronError { error: Box::new(SessionError { message: "Database Error".to_string() }),
response: Response::with(status::Forbidden) }
response: Response::with(status::InternalServerError) }
}
functions::MedalError::PasswordHashingError => {
IronError { error: Box::new(SessionError { message: "Error hashing the passwords".to_string() }),
response: Response::with(status::Forbidden) }
response: Response::with(status::InternalServerError) }
}
functions::MedalError::UnmatchedPasswords => {
IronError { error: Box::new(SessionError { message:
......@@ -482,12 +482,9 @@ fn submission_post<C>(req: &mut Request) -> IronResult<Response>
*/
let result =
with_conn![functions::save_submission, C, req, task_id, &session_token, &csrf_token, data, grade, subtask];
with_conn![functions::save_submission, C, req, task_id, &session_token, &csrf_token, data, grade, subtask].aug(req)?;
match result {
Ok(_) => Ok(Response::with((status::Ok, mime!(Application / Json), format!("{{}}")))),
Err(_) => Ok(Response::with((status::BadRequest, mime!(Application / Json), format!("{{}}")))),
}
Ok(Response::with((status::Ok, mime!(Application / Json), result)))
}
fn task<C>(req: &mut Request) -> IronResult<Response>
......
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